Skip to content

Commit ec87b40

Browse files
refactor: rename localIdentHashPrefix option in 'localIdentHashSalt'
1 parent 1d5761c commit ec87b40

7 files changed

+38
-38
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ module.exports = {
583583
exportGlobals: true,
584584
localIdentName: "[path][name]__[local]--[hash:base64:5]",
585585
localIdentContext: path.resolve(__dirname, "src"),
586-
localIdentHashPrefix: "my-custom-hash",
586+
localIdentHashSalt: "my-custom-hash",
587587
namedExport: true,
588588
exportLocalsConvention: "camelCase",
589589
exportOnlyLocals: false,
@@ -833,7 +833,7 @@ module.exports = {
833833
};
834834
```
835835

836-
##### `localIdentHashPrefix`
836+
##### `localIdentHashSalt
837837

838838
Type: `String`
839839
Default: `undefined`
@@ -851,7 +851,7 @@ module.exports = {
851851
loader: "css-loader",
852852
options: {
853853
modules: {
854-
localIdentHashPrefix: "hash",
854+
localIdentHashSalt: "hash",
855855
},
856856
},
857857
},

src/options.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@
9898
"type": "string",
9999
"minLength": 1
100100
},
101-
"localIdentHashPrefix": {
102-
"description": "Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashprefix).",
101+
"localIdentHashSalt": {
102+
"description": "Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashsalt).",
103103
"type": "string",
104104
"minLength": 1
105105
},

src/utils.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ function defaultGetLocalIdent(
356356
);
357357

358358
// eslint-disable-next-line no-param-reassign
359-
options.content = `${options.hashPrefix}${relativeMatchResource}${relativeResourcePath}\x00${localName}`;
359+
options.content = `${relativeMatchResource}${relativeResourcePath}\x00${localName}`;
360360

361361
// eslint-disable-next-line no-underscore-dangle
362362
const { outputOptions } = loaderContext._compilation;
363-
const { hashSalt } = outputOptions;
363+
const { hashSalt = options.hashSalt } = outputOptions;
364364
let { hashFunction, hashDigest, hashDigestLength } = outputOptions;
365365

366366
const mathes = localIdentName.match(
@@ -529,7 +529,7 @@ function getModulesOptions(rawOptions, loaderContext) {
529529
exportGlobals: false,
530530
localIdentName: "[hash:base64]",
531531
localIdentContext: loaderContext.rootContext,
532-
localIdentHashPrefix: "",
532+
localIdentHashSalt: "",
533533
// eslint-disable-next-line no-undefined
534534
localIdentRegExp: undefined,
535535
// eslint-disable-next-line no-undefined
@@ -673,7 +673,7 @@ function getModulesPlugins(options, loaderContext) {
673673
getLocalIdent,
674674
localIdentName,
675675
localIdentContext,
676-
localIdentHashPrefix,
676+
localIdentHashSalt,
677677
localIdentRegExp,
678678
} = options.modules;
679679

@@ -695,7 +695,7 @@ function getModulesPlugins(options, loaderContext) {
695695
unescape(exportName),
696696
{
697697
context: localIdentContext,
698-
hashPrefix: localIdentHashPrefix,
698+
hashSalt: localIdentHashSalt,
699699
regExp: localIdentRegExp,
700700
}
701701
);
@@ -710,7 +710,7 @@ function getModulesPlugins(options, loaderContext) {
710710
unescape(exportName),
711711
{
712712
context: localIdentContext,
713-
hashPrefix: localIdentHashPrefix,
713+
hashSalt: localIdentHashSalt,
714714
regExp: localIdentRegExp,
715715
}
716716
);

test/__snapshots__/modules-option.test.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -2817,9 +2817,9 @@ Array [
28172817

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

2820-
exports[`"modules" option should work and respect the "hashPrefix" option: errors 1`] = `Array []`;
2820+
exports[`"modules" option should work and respect the "hashSalt" option: errors 1`] = `Array []`;
28212821

2822-
exports[`"modules" option should work and respect the "hashPrefix" option: module 1`] = `
2822+
exports[`"modules" option should work and respect the "hashSalt" option: module 1`] = `
28232823
"// Imports
28242824
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
28252825
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
@@ -2879,7 +2879,7 @@ export default ___CSS_LOADER_EXPORT___;
28792879
"
28802880
`;
28812881

2882-
exports[`"modules" option should work and respect the "hashPrefix" option: result 1`] = `
2882+
exports[`"modules" option should work and respect the "hashSalt" option: result 1`] = `
28832883
Array [
28842884
Array [
28852885
"./modules/localIdentName/localIdentName.css",
@@ -3002,7 +3002,7 @@ Array [
30023002
]
30033003
`;
30043004

3005-
exports[`"modules" option should work and respect the "hashPrefix" option: warnings 1`] = `Array []`;
3005+
exports[`"modules" option should work and respect the "hashSalt" option: warnings 1`] = `Array []`;
30063006

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

test/__snapshots__/validate-options.test.js.snap

+17-17
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ exports[`validate options should throw an error on the "import" option with "tru
6868
exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = `
6969
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
7070
- options.modules should be one of these:
71-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
71+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
7272
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
7373
Details:
7474
* options.modules.auto should be one of these:
@@ -111,10 +111,10 @@ exports[`validate options should throw an error on the "modules" option with "{"
111111
-> Allows to redefine basic loader context for local ident name (https://github.com/webpack-contrib/css-loader#localidentcontext)."
112112
`;
113113

114-
exports[`validate options should throw an error on the "modules" option with "{"localIdentHashPrefix":true}" value 1`] = `
114+
exports[`validate options should throw an error on the "modules" option with "{"localIdentHashSalt":true}" value 1`] = `
115115
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
116-
- options.modules.localIdentHashPrefix should be a non-empty string.
117-
-> Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashprefix)."
116+
- options.modules.localIdentHashSalt should be a non-empty string.
117+
-> Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashsalt)."
118118
`;
119119

120120
exports[`validate options should throw an error on the "modules" option with "{"localIdentName":true}" value 1`] = `
@@ -126,7 +126,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
126126
exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = `
127127
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
128128
- options.modules should be one of these:
129-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
129+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
130130
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
131131
Details:
132132
* options.modules.localIdentRegExp should be one of these:
@@ -140,7 +140,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
140140
exports[`validate options should throw an error on the "modules" option with "{"mode":"globals"}" value 1`] = `
141141
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
142142
- options.modules should be one of these:
143-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
143+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
144144
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
145145
Details:
146146
* options.modules.mode should be one of these:
@@ -155,7 +155,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
155155
exports[`validate options should throw an error on the "modules" option with "{"mode":"locals"}" value 1`] = `
156156
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
157157
- options.modules should be one of these:
158-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
158+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
159159
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
160160
Details:
161161
* options.modules.mode should be one of these:
@@ -170,7 +170,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
170170
exports[`validate options should throw an error on the "modules" option with "{"mode":"pures"}" value 1`] = `
171171
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
172172
- options.modules should be one of these:
173-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
173+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
174174
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
175175
Details:
176176
* options.modules.mode should be one of these:
@@ -185,7 +185,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
185185
exports[`validate options should throw an error on the "modules" option with "{"mode":true}" value 1`] = `
186186
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
187187
- options.modules should be one of these:
188-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
188+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
189189
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
190190
Details:
191191
* options.modules.mode should be one of these:
@@ -206,53 +206,53 @@ exports[`validate options should throw an error on the "modules" option with "{"
206206
exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = `
207207
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
208208
- options.modules should be one of these:
209-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
209+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
210210
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
211211
Details:
212212
* options.modules should be a boolean.
213213
* options.modules should be one of these:
214214
\\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\"
215215
* options.modules should be an object:
216-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
216+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
217217
`;
218218

219219
exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = `
220220
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
221221
- options.modules should be one of these:
222-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
222+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
223223
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
224224
Details:
225225
* options.modules should be a boolean.
226226
* options.modules should be one of these:
227227
\\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\"
228228
* options.modules should be an object:
229-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
229+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
230230
`;
231231

232232
exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = `
233233
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
234234
- options.modules should be one of these:
235-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
235+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
236236
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
237237
Details:
238238
* options.modules should be a boolean.
239239
* options.modules should be one of these:
240240
\\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\"
241241
* options.modules should be an object:
242-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
242+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
243243
`;
244244

245245
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
246246
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
247247
- options.modules should be one of these:
248-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
248+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
249249
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
250250
Details:
251251
* options.modules should be a boolean.
252252
* options.modules should be one of these:
253253
\\"local\\" | \\"global\\" | \\"pure\\" | \\"icss\\"
254254
* options.modules should be an object:
255-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
255+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashSalt?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
256256
`;
257257

258258
exports[`validate options should throw an error on the "sourceMap" option with "true" value 1`] = `

test/modules-option.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ describe('"modules" option', () => {
188188
expect(getErrors(stats)).toMatchSnapshot("errors");
189189
});
190190

191-
it('should work and respect the "hashPrefix" option', async () => {
191+
it('should work and respect the "hashSalt" option', async () => {
192192
const compiler = getCompiler("./modules/localIdentName/localIdentName.js", {
193193
modules: {
194194
localIdentName: "[local]--[hash]",
195-
localIdentHashPrefix: "x",
195+
localIdentHashSalt: "x",
196196
},
197197
});
198198
const stats = await compile(compiler);
@@ -278,7 +278,7 @@ describe('"modules" option', () => {
278278
modules: {
279279
localIdentRegExp: "regExp",
280280
localIdentContext: "context",
281-
localIdentHashPrefix: "hash",
281+
localIdentHashSalt: "hash",
282282
getLocalIdent(loaderContext, localIdentName, localName, options) {
283283
expect(loaderContext).toBeDefined();
284284
expect(typeof localIdentName).toBe("string");
@@ -287,7 +287,7 @@ describe('"modules" option', () => {
287287

288288
expect(options.regExp).toBe("regExp");
289289
expect(options.context).toBe("context");
290-
expect(options.hashPrefix).toBe("hash");
290+
expect(options.hashSalt).toBe("hash");
291291

292292
return "foo";
293293
},

0 commit comments

Comments
 (0)