Skip to content

Commit ca4abce

Browse files
authored
feat: added the hashStrategy option
1 parent 3240394 commit ca4abce

6 files changed

+688
-17
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,36 @@ module.exports = {
872872
};
873873
```
874874

875+
##### `hashStrategy`
876+
877+
Type: `'resource-path-and-local-name' | 'minimal-subset'`
878+
Default: `'resource-path-and-local-name'`
879+
880+
Should local name be used when computing the hash.
881+
882+
- `'resource-path-and-local-name'` Both resource path and local name are used when hashing. Each identifier in a module gets its own hash digest, always.
883+
- `'minimal-subset'` Auto detect if identifier names can be omitted from hashing. Use this value to optimize the output for better GZIP or Brotli compression.
884+
885+
**webpack.config.js**
886+
887+
```js
888+
module.exports = {
889+
module: {
890+
rules: [
891+
{
892+
test: /\.css$/i,
893+
loader: "css-loader",
894+
options: {
895+
modules: {
896+
hashStrategy: "minimal-subset",
897+
},
898+
},
899+
},
900+
],
901+
},
902+
};
903+
```
904+
875905
##### `localIdentRegExp`
876906

877907
Type: `String|RegExp`

src/options.json

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
"link": "https://github.com/webpack-contrib/css-loader#localidenthashdigestlength",
115115
"type": "number"
116116
},
117+
"hashStrategy": {
118+
"description": "Allows to specify should localName be used when computing the hash.",
119+
"link": "https://github.com/webpack-contrib/css-loader#hashstrategy",
120+
"enum": ["resource-path-and-local-name", "minimal-subset"]
121+
},
117122
"localIdentRegExp": {
118123
"description": "Allows to specify custom RegExp for local ident name.",
119124
"link": "https://github.com/webpack-contrib/css-loader#localidentregexp",

src/utils.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,17 @@ function defaultGetLocalIdent(
330330
localName,
331331
options
332332
) {
333-
const { context, hashSalt } = options;
333+
const { context, hashSalt, hashStrategy } = options;
334334
const { resourcePath } = loaderContext;
335335
const relativeResourcePath = normalizePath(
336336
path.relative(context, resourcePath)
337337
);
338338

339339
// eslint-disable-next-line no-param-reassign
340-
options.content = `${relativeResourcePath}\x00${localName}`;
340+
options.content =
341+
hashStrategy === "minimal-subset" && /\[local\]/.test(localIdentName)
342+
? relativeResourcePath
343+
: `${relativeResourcePath}\x00${localName}`;
341344

342345
let { hashFunction, hashDigest, hashDigestLength } = options;
343346
const matches = localIdentName.match(
@@ -756,6 +759,7 @@ function getModulesPlugins(options, loaderContext) {
756759
localIdentHashDigest,
757760
localIdentHashDigestLength,
758761
localIdentRegExp,
762+
hashStrategy,
759763
} = options.modules;
760764

761765
let plugins = [];
@@ -780,6 +784,7 @@ function getModulesPlugins(options, loaderContext) {
780784
hashFunction: localIdentHashFunction,
781785
hashDigest: localIdentHashDigest,
782786
hashDigestLength: localIdentHashDigestLength,
787+
hashStrategy,
783788
regExp: localIdentRegExp,
784789
}
785790
);
@@ -798,6 +803,7 @@ function getModulesPlugins(options, loaderContext) {
798803
hashFunction: localIdentHashFunction,
799804
hashDigest: localIdentHashDigest,
800805
hashDigestLength: localIdentHashDigestLength,
806+
hashStrategy,
801807
regExp: localIdentRegExp,
802808
}
803809
);

0 commit comments

Comments
 (0)