Skip to content

Commit

Permalink
feat: do not hash localName if the localName is inlined:
Browse files Browse the repository at this point in the history
Adds a new setting .modules.hashStrategy, default is backward-compat
  • Loading branch information
subzey committed Jan 26, 2022
1 parent cbe3898 commit 8e93b75
Show file tree
Hide file tree
Showing 6 changed files with 910 additions and 17 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,37 @@ module.exports = {
};
```

##### `hashStrategy`

Type: `'use-local-name' | 'omit-local-name' | 'auto'`
Default: `'use-local-name'`

Should local name be used when computing the hash.

- `'auto'` Auto detect based on [localIdentName](#localidentname). Use this value to get the output that is better compressable by GZIP or Brotli.
- `'use-local-name'` Each identifier in a module gets its own hash digest.
- `'omit-local-name'` All identifiers from the same module shares the same hash digest. Handle with care!

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: "css-loader",
options: {
modules: {
hashStrategy: "auto",
},
},
},
],
},
};
```

##### `localIdentRegExp`

Type: `String|RegExp`
Expand Down
5 changes: 5 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
"link": "https://github.com/webpack-contrib/css-loader#localidenthashdigestlength",
"type": "number"
},
"hashStrategy": {
"description": "Allows to specify should localName be used when computing the hash.",
"link": "https://github.com/webpack-contrib/css-loader#hashstrategy",
"enum": ["use-local-name", "omit-local-name", "auto"]
},
"localIdentRegExp": {
"description": "Allows to specify custom RegExp for local ident name.",
"link": "https://github.com/webpack-contrib/css-loader#localidentregexp",
Expand Down
21 changes: 19 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,28 @@ function defaultGetLocalIdent(
localName,
options
) {
const { context, hashSalt } = options;
const { context, hashSalt, hashStrategy } = options;
const { resourcePath } = loaderContext;
const relativeResourcePath = normalizePath(
path.relative(context, resourcePath)
);

let useLocalNameInHash;
switch (hashStrategy) {
case "omit-local-name":
useLocalNameInHash = false;
break;
case "auto":
useLocalNameInHash = !/\[local\]/.test(localIdentName);
break;
default:
// undefined or "use-local-name"
useLocalNameInHash = true;
}
// eslint-disable-next-line no-param-reassign
options.content = `${relativeResourcePath}\x00${localName}`;
options.content = useLocalNameInHash
? `${relativeResourcePath}\x00${localName}`
: relativeResourcePath;

let { hashFunction, hashDigest, hashDigestLength } = options;
const matches = localIdentName.match(
Expand Down Expand Up @@ -756,6 +770,7 @@ function getModulesPlugins(options, loaderContext) {
localIdentHashDigest,
localIdentHashDigestLength,
localIdentRegExp,
hashStrategy,
} = options.modules;

let plugins = [];
Expand All @@ -780,6 +795,7 @@ function getModulesPlugins(options, loaderContext) {
hashFunction: localIdentHashFunction,
hashDigest: localIdentHashDigest,
hashDigestLength: localIdentHashDigestLength,
hashStrategy,
regExp: localIdentRegExp,
}
);
Expand All @@ -798,6 +814,7 @@ function getModulesPlugins(options, loaderContext) {
hashFunction: localIdentHashFunction,
hashDigest: localIdentHashDigest,
hashDigestLength: localIdentHashDigestLength,
hashStrategy,
regExp: localIdentRegExp,
}
);
Expand Down

0 comments on commit 8e93b75

Please sign in to comment.