Skip to content

Commit 88130b6

Browse files
committed
feat: do not hash localName if the localName is inlined:
Adds a new setting .modules.hashStrategy, default is backward-compat
1 parent cbe3898 commit 88130b6

File tree

6 files changed

+910
-17
lines changed

6 files changed

+910
-17
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,37 @@ module.exports = {
872872
};
873873
```
874874

875+
##### `hashStrategy`
876+
877+
Type: `'use-local-name' | 'omit-local-name' | 'auto'`
878+
Default: `'use-local-name'`
879+
880+
Should local name be used when computing the hash.
881+
882+
- `'auto'` Auto detect based on [localIdentName](#localidentname). Use this value to get the output that is better compressable by GZIP or Brotli.
883+
- `'use-local-name'` Each identifier in a module gets its own hash digest.
884+
- `'omit-local-name'` All identifiers from the same module shares the same hash digest. Handle with care!
885+
886+
**webpack.config.js**
887+
888+
```js
889+
module.exports = {
890+
module: {
891+
rules: [
892+
{
893+
test: /\.css$/i,
894+
loader: "css-loader",
895+
options: {
896+
modules: {
897+
hashStrategy: "auto",
898+
},
899+
},
900+
},
901+
],
902+
},
903+
};
904+
```
905+
875906
##### `localIdentRegExp`
876907

877908
Type: `String|RegExp`

src/options.json

Lines changed: 5 additions & 0 deletions
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": ["use-local-name", "omit-local-name", "auto"]
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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,28 @@ 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

339+
let useLocalNameInHash;
340+
switch (hashStrategy) {
341+
case "omit-local-name":
342+
useLocalNameInHash = false;
343+
break;
344+
case "auto":
345+
useLocalNameInHash = !/\[local\]/.test(localIdentName);
346+
break;
347+
default:
348+
// undefined or "use-local-name"
349+
useLocalNameInHash = true;
350+
}
339351
// eslint-disable-next-line no-param-reassign
340-
options.content = `${relativeResourcePath}\x00${localName}`;
352+
options.content = useLocalNameInHash
353+
? `${relativeResourcePath}\x00${localName}`
354+
: relativeResourcePath;
341355

342356
let { hashFunction, hashDigest, hashDigestLength } = options;
343357
const matches = localIdentName.match(
@@ -756,6 +770,7 @@ function getModulesPlugins(options, loaderContext) {
756770
localIdentHashDigest,
757771
localIdentHashDigestLength,
758772
localIdentRegExp,
773+
hashStrategy,
759774
} = options.modules;
760775

761776
let plugins = [];
@@ -780,6 +795,7 @@ function getModulesPlugins(options, loaderContext) {
780795
hashFunction: localIdentHashFunction,
781796
hashDigest: localIdentHashDigest,
782797
hashDigestLength: localIdentHashDigestLength,
798+
hashStrategy,
783799
regExp: localIdentRegExp,
784800
}
785801
);
@@ -798,6 +814,7 @@ function getModulesPlugins(options, loaderContext) {
798814
hashFunction: localIdentHashFunction,
799815
hashDigest: localIdentHashDigest,
800816
hashDigestLength: localIdentHashDigestLength,
817+
hashStrategy,
801818
regExp: localIdentRegExp,
802819
}
803820
);

0 commit comments

Comments
 (0)