Skip to content

Commit

Permalink
feat: do not hash localName if the localName is inlined: rename optio…
Browse files Browse the repository at this point in the history
…n values
  • Loading branch information
subzey committed Feb 2, 2022
1 parent 4c07771 commit 381d470
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,13 @@ module.exports = {

##### `hashStrategy`

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

Should local name be used when computing the hash.

- `'use-local-name'` Each identifier in a module gets its own hash digest.
- `'auto'` Identifiers from the same module shares the same hash digest if it's possible. Use this value to optimize the output for better GZIP or Brotli compression.
- `'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**

Expand All @@ -893,7 +893,7 @@ module.exports = {
loader: "css-loader",
options: {
modules: {
hashStrategy: "auto",
hashStrategy: "minimal-subset",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"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", "auto"]
"enum": ["resource-path-and-local-name", "minimal-subset"]
},
"localIdentRegExp": {
"description": "Allows to specify custom RegExp for local ident name.",
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function defaultGetLocalIdent(

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

Expand Down
24 changes: 12 additions & 12 deletions test/__snapshots__/modules-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3397,9 +3397,9 @@ Array [

exports[`"modules" option should work and respect the "hashSalt" option: warnings 1`] = `Array []`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and [local]: errors 1`] = `Array []`;
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and [local]: errors 1`] = `Array []`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and [local]: module 1`] = `
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and [local]: module 1`] = `
"// Imports
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../../src/runtime/noSourceMaps.js\\";
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
Expand Down Expand Up @@ -3461,7 +3461,7 @@ export default ___CSS_LOADER_EXPORT___;
"
`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and [local]: result 1`] = `
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and [local]: result 1`] = `
Array [
Array [
"./modules/localIdentName/localIdentName.css",
Expand Down Expand Up @@ -3585,11 +3585,11 @@ Array [
]
`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and [local]: warnings 1`] = `Array []`;
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and [local]: warnings 1`] = `Array []`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and no [local]: errors 1`] = `Array []`;
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and no [local]: errors 1`] = `Array []`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and no [local]: module 1`] = `
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and no [local]: module 1`] = `
"// Imports
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../../src/runtime/noSourceMaps.js\\";
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
Expand Down Expand Up @@ -3651,7 +3651,7 @@ export default ___CSS_LOADER_EXPORT___;
"
`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and no [local]: result 1`] = `
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and no [local]: result 1`] = `
Array [
Array [
"./modules/localIdentName/localIdentName.css",
Expand Down Expand Up @@ -3775,11 +3775,11 @@ Array [
]
`;

exports[`"modules" option should work and respect the "hashStrategy" = "auto" and no [local]: warnings 1`] = `Array []`;
exports[`"modules" option should work and respect the "hashStrategy" = "minimal-subset" and no [local]: warnings 1`] = `Array []`;

exports[`"modules" option should work and respect the "hashStrategy" = "use-local-name": errors 1`] = `Array []`;
exports[`"modules" option should work and respect the "hashStrategy" = "resource-path-and-local-name": errors 1`] = `Array []`;

exports[`"modules" option should work and respect the "hashStrategy" = "use-local-name": module 1`] = `
exports[`"modules" option should work and respect the "hashStrategy" = "resource-path-and-local-name": module 1`] = `
"// Imports
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../../src/runtime/noSourceMaps.js\\";
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
Expand Down Expand Up @@ -3841,7 +3841,7 @@ export default ___CSS_LOADER_EXPORT___;
"
`;

exports[`"modules" option should work and respect the "hashStrategy" = "use-local-name": result 1`] = `
exports[`"modules" option should work and respect the "hashStrategy" = "resource-path-and-local-name": result 1`] = `
Array [
Array [
"./modules/localIdentName/localIdentName.css",
Expand Down Expand Up @@ -3965,7 +3965,7 @@ Array [
]
`;

exports[`"modules" option should work and respect the "hashStrategy" = "use-local-name": warnings 1`] = `Array []`;
exports[`"modules" option should work and respect the "hashStrategy" = "resource-path-and-local-name": warnings 1`] = `Array []`;

exports[`"modules" option should work and respect the "localConvention" option with the "asIs" value: errors 1`] = `Array []`;

Expand Down
12 changes: 6 additions & 6 deletions test/modules-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,12 @@ describe('"modules" option', () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should work and respect the "hashStrategy" = "use-local-name"', async () => {
it('should work and respect the "hashStrategy" = "resource-path-and-local-name"', async () => {
const compiler = getCompiler("./modules/localIdentName/localIdentName.js", {
modules: {
localIdentName: "[local]__[hash:base64:8]",
// localName should be used even if [local] is contained in the localIdentName template
hashStrategy: "use-local-name",
hashStrategy: "resource-path-and-local-name",
},
});
const stats = await compile(compiler);
Expand All @@ -401,12 +401,12 @@ describe('"modules" option', () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should work and respect the "hashStrategy" = "auto" and [local]', async () => {
it('should work and respect the "hashStrategy" = "minimal-subset" and [local]', async () => {
const compiler = getCompiler("./modules/localIdentName/localIdentName.js", {
modules: {
localIdentName: "[local]__[hash:base64:8]",
// localName should not be used: [local] is used
hashStrategy: "auto",
hashStrategy: "minimal-subset",
},
});
const stats = await compile(compiler);
Expand All @@ -421,12 +421,12 @@ describe('"modules" option', () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should work and respect the "hashStrategy" = "auto" and no [local]', async () => {
it('should work and respect the "hashStrategy" = "minimal-subset" and no [local]', async () => {
const compiler = getCompiler("./modules/localIdentName/localIdentName.js", {
modules: {
localIdentName: "[hash:base64:8]",
// localName should be used: [local] is not used
hashStrategy: "auto",
hashStrategy: "minimal-subset",
},
});
const stats = await compile(compiler);
Expand Down

0 comments on commit 381d470

Please sign in to comment.