Skip to content

Commit

Permalink
refactor!: stringifyRequest was removed in favor `JSON.stringify(lo…
Browse files Browse the repository at this point in the history
…aderContext.utils.contextify(this.context, request))` where `loaderContext` is `this` inside loader function, also it will be cachable and faster
  • Loading branch information
alexander-akait committed Oct 20, 2021
1 parent 973c82a commit 0d89522
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 288 deletions.
45 changes: 1 addition & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Parses a passed string (e.g. `loaderContext.resourceQuery`) as a query string, a

```javascript
const params = loaderUtils.parseQuery(this.resourceQuery); // resource: `file?param1=foo`

if (params.param1 === "foo") {
// do something
}
Expand All @@ -35,50 +36,6 @@ The string is parsed like this:
?{data:{a:1},isJSON5:true} -> { data: { a: 1 }, isJSON5: true }
```

### `stringifyRequest`

Turns a request into a string that can be used inside `require()` or `import` while avoiding absolute paths.
Use it instead of `JSON.stringify(...)` if you're generating code inside a loader.

**Why is this necessary?** Since webpack calculates the hash before module paths are translated into module ids, we must avoid absolute paths to ensure
consistent hashes across different compilations.

This function:

- 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.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`

Converts some resource URL to a webpack module request.
Expand Down
2 changes: 0 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"use strict";

const parseQuery = require("./parseQuery");
const stringifyRequest = require("./stringifyRequest");
const urlToRequest = require("./urlToRequest");
const parseString = require("./parseString");
const getHashDigest = require("./getHashDigest");
const interpolateName = require("./interpolateName");

exports.parseQuery = parseQuery;
exports.stringifyRequest = stringifyRequest;
exports.urlToRequest = urlToRequest;
exports.parseString = parseString;
exports.getHashDigest = getHashDigest;
Expand Down
51 changes: 0 additions & 51 deletions lib/stringifyRequest.js

This file was deleted.

191 changes: 0 additions & 191 deletions test/stringifyRequest.test.js

This file was deleted.

0 comments on commit 0d89522

Please sign in to comment.