Skip to content

Commit

Permalink
Add explanation of stringifyRequest to README
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Feb 16, 2017
1 parent 01d86ef commit aa644a5
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,41 @@ null -> {}

### `stringifyRequest`

Makes a request pretty and stringifies it. Absolute paths are replaced with relative ones.
Turns a request into a string that can be used inside a `require()` or `import()` call or an `import` statement.
Use it instead of `JSON.stringify(...)` if you're generating code inside a loader.

Use it instead of `JSON.stringify(...)` to build code of a `require(...)` call in a loader.
This function:

``` javascript
loaderUtils.stringifyRequest(this, require.resolve("./test"));
// = "../node_modules/some-loader/lib/test.js"
- resolves module requests into relative requests
- resolves absolute requests into relative requests if the request and the module are on the same hard drive
- replaces `\` with `/` if the request and the module are on the same hard drive
- won't change the path at all if the request and the module are on different hard drives
- applies `JSON.stringify` to the result

```javascript
loaderUtils.stringifyRequest(this, "./test.js");
// "\"./test.js\""

loaderUtils.stringifyRequest(this, "test");
// "\"test\""

loaderUtils.stringifyRequest(this, "test/lib/index.js");
// "\"test/lib/index.js\""

loaderUtils.stringifyRequest(this, "otherLoader?andConfig!test?someConfig");
// "\"otherLoader?andConfig!test?someConfig\""

loaderUtils.stringifyRequest(this, require.resolve("test"));
// "\"../node_modules/some-loader/lib/test.js\""

loaderUtils.stringifyRequest(this, "C:\\module\\test.js");
// "\"../../test.js\"" (on Windows, in case the module and the request are on the same drive)

loaderUtils.stringifyRequest(this, "C:\\module\\test.js");
// "\"C:\\module\\test.js\"" (on Windows, in case the module and the request are on different drives)

loaderUtils.stringifyRequest(this, "\\network-drive\\test.js");
// "\"\\\\network-drive\\\\test.js\"" (on Windows, in case the module and the request are on different drives)
```

### `urlToRequest`
Expand Down

0 comments on commit aa644a5

Please sign in to comment.