Skip to content

Commit

Permalink
feat: added the hashStrategy option
Browse files Browse the repository at this point in the history
  • Loading branch information
subzey authored Feb 2, 2022
1 parent 3240394 commit ca4abce
Show file tree
Hide file tree
Showing 6 changed files with 688 additions and 17 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,36 @@ module.exports = {
};
```

##### `hashStrategy`

Type: `'resource-path-and-local-name' | 'minimal-subset'`
Default: `'resource-path-and-local-name'`

Should local name be used when computing the hash.

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

**webpack.config.js**

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

##### `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": ["resource-path-and-local-name", "minimal-subset"]
},
"localIdentRegExp": {
"description": "Allows to specify custom RegExp for local ident name.",
"link": "https://github.com/webpack-contrib/css-loader#localidentregexp",
Expand Down
10 changes: 8 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,17 @@ function defaultGetLocalIdent(
localName,
options
) {
const { context, hashSalt } = options;
const { context, hashSalt, hashStrategy } = options;
const { resourcePath } = loaderContext;
const relativeResourcePath = normalizePath(
path.relative(context, resourcePath)
);

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

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

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

0 comments on commit ca4abce

Please sign in to comment.