Skip to content

Commit fc04401

Browse files
refactor: the modules.context option was renamed to the modules.localIdentContext option (#1119)
BREAKING CHANGE: the `modules.context` option was renamed to the `modules.localIdentContext` option, default `modules.localIdentContext` value is `compiler.context` for the `module.getLocalIdent` option
1 parent 3a96a3d commit fc04401

6 files changed

+49
-50
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ module.exports = {
726726
Type: `String`
727727
Default: `'[hash:base64]'`
728728

729-
You can configure the generated ident with the `localIdentName` query parameter.
729+
You can configure the generated ident with the `localIdentName` option.
730730
See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
731731

732732
Recommendations:
@@ -807,13 +807,12 @@ module.exports = {
807807
};
808808
```
809809

810-
##### `context`
810+
##### `localIdentContext`
811811

812812
Type: `String`
813-
Default: `undefined`
813+
Default: `compiler.context`
814814

815815
Allow to redefine basic loader context for local ident name.
816-
By default we use `rootContext` of loader.
817816

818817
**webpack.config.js**
819818

src/options.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@
6363
"type": "boolean"
6464
},
6565
"localIdentName": {
66-
"type": "string"
66+
"type": "string",
67+
"minLength": 1
6768
},
68-
"context": {
69-
"type": "string"
69+
"localIdentContext": {
70+
"type": "string",
71+
"minLength": 1
7072
},
7173
"localIdentHashPrefix": {
72-
"type": "string"
74+
"type": "string",
75+
"minLength": 1
7376
},
7477
"localIdentRegExp": {
7578
"anyOf": [
7679
{
77-
"type": "string"
80+
"type": "string",
81+
"minLength": 1
7882
},
7983
{
8084
"instanceof": "RegExp"

src/utils.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@ const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g;
5252
const reRelativePath = /^\.+/;
5353

5454
function getLocalIdent(loaderContext, localIdentName, localName, options) {
55-
if (!options.context) {
56-
// eslint-disable-next-line no-param-reassign
57-
options.context = loaderContext.rootContext;
58-
}
59-
6055
const request = normalizePath(
61-
path.relative(options.context || '', loaderContext.resourcePath)
56+
path.relative(options.context, loaderContext.resourcePath)
6257
);
6358

6459
// eslint-disable-next-line no-param-reassign
@@ -125,13 +120,14 @@ function getModulesOptions(rawOptions, loaderContext) {
125120
let modulesOptions = {
126121
auto: true,
127122
mode: 'local',
123+
exportGlobals: false,
128124
localIdentName: '[hash:base64]',
125+
localIdentContext: loaderContext.rootContext,
129126
localIdentHashPrefix: '',
130127
// eslint-disable-next-line no-undefined
131128
localIdentRegExp: undefined,
132-
localsConvention: 'asIs',
133129
getLocalIdent,
134-
exportGlobals: false,
130+
localsConvention: 'asIs',
135131
namedExport: false,
136132
exportOnlyLocals: false,
137133
};
@@ -245,7 +241,7 @@ function getModulesPlugins(options, loaderContext) {
245241
options.modules.localIdentName,
246242
exportName,
247243
{
248-
context: options.modules.context,
244+
context: options.modules.localIdentContext,
249245
hashPrefix: options.modules.localIdentHashPrefix,
250246
regExp: options.modules.localIdentRegExp,
251247
}
@@ -258,7 +254,7 @@ function getModulesPlugins(options, loaderContext) {
258254
options.modules.localIdentName,
259255
exportName,
260256
{
261-
context: options.modules.context,
257+
context: options.modules.localIdentContext,
262258
hashPrefix: options.modules.localIdentHashPrefix,
263259
regExp: options.modules.localIdentRegExp,
264260
}

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

+24-24
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ exports[`validate options should throw an error on the "importLoaders" option wi
3939
exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = `
4040
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
4141
- options.modules should be one of these:
42-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
42+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
4343
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
4444
Details:
4545
* options.modules.auto should be one of these:
@@ -50,11 +50,6 @@ exports[`validate options should throw an error on the "modules" option with "{"
5050
* options.modules.auto should be a boolean."
5151
`;
5252
53-
exports[`validate options should throw an error on the "modules" option with "{"context":true}" value 1`] = `
54-
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
55-
- options.modules.context should be a string."
56-
`;
57-
5853
exports[`validate options should throw an error on the "modules" option with "{"exportGlobals":"invalid"}" value 1`] = `
5954
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
6055
- options.modules.exportGlobals should be a boolean."
@@ -69,7 +64,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
6964
exports[`validate options should throw an error on the "modules" option with "{"getLocalIdent":[]}" value 1`] = `
7065
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
7166
- options.modules should be one of these:
72-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
67+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
7368
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
7469
Details:
7570
* options.modules.getLocalIdent should be one of these:
@@ -79,26 +74,31 @@ exports[`validate options should throw an error on the "modules" option with "{"
7974
* options.modules.getLocalIdent should be an instance of function."
8075
`;
8176
77+
exports[`validate options should throw an error on the "modules" option with "{"localIdentContext":true}" value 1`] = `
78+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
79+
- options.modules.localIdentContext should be a non-empty string."
80+
`;
81+
8282
exports[`validate options should throw an error on the "modules" option with "{"localIdentHashPrefix":true}" value 1`] = `
8383
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
84-
- options.modules.localIdentHashPrefix should be a string."
84+
- options.modules.localIdentHashPrefix should be a non-empty string."
8585
`;
8686
8787
exports[`validate options should throw an error on the "modules" option with "{"localIdentName":true}" value 1`] = `
8888
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
89-
- options.modules.localIdentName should be a string."
89+
- options.modules.localIdentName should be a non-empty string."
9090
`;
9191
9292
exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = `
9393
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
9494
- options.modules should be one of these:
95-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
95+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
9696
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
9797
Details:
9898
* options.modules.localIdentRegExp should be one of these:
99-
string | RegExp
99+
non-empty string | RegExp
100100
Details:
101-
* options.modules.localIdentRegExp should be a string.
101+
* options.modules.localIdentRegExp should be a non-empty string.
102102
* options.modules.localIdentRegExp should be an instance of RegExp."
103103
`;
104104
@@ -112,7 +112,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
112112
exports[`validate options should throw an error on the "modules" option with "{"mode":"globals"}" value 1`] = `
113113
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
114114
- options.modules should be one of these:
115-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
115+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
116116
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
117117
Details:
118118
* options.modules.mode should be one of these:
@@ -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 "{"mode":"locals"}" 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\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
129+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
130130
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
131131
Details:
132132
* options.modules.mode 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":"pures"}" 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\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
143+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, 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:
@@ -154,7 +154,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
154154
exports[`validate options should throw an error on the "modules" option with "{"mode":true}" value 1`] = `
155155
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
156156
- options.modules should be one of these:
157-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
157+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
158158
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
159159
Details:
160160
* options.modules.mode should be one of these:
@@ -174,53 +174,53 @@ exports[`validate options should throw an error on the "modules" option with "{"
174174
exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = `
175175
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
176176
- options.modules should be one of these:
177-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
177+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
178178
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
179179
Details:
180180
* options.modules should be a boolean.
181181
* options.modules should be one of these:
182182
\\"local\\" | \\"global\\" | \\"pure\\"
183183
* options.modules should be an object:
184-
object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
184+
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
185185
`;
186186
187187
exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = `
188188
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
189189
- options.modules should be one of these:
190-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
190+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
191191
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
192192
Details:
193193
* options.modules should be a boolean.
194194
* options.modules should be one of these:
195195
\\"local\\" | \\"global\\" | \\"pure\\"
196196
* options.modules should be an object:
197-
object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
197+
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
198198
`;
199199
200200
exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = `
201201
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
202202
- options.modules should be one of these:
203-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
203+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
204204
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
205205
Details:
206206
* options.modules should be a boolean.
207207
* options.modules should be one of these:
208208
\\"local\\" | \\"global\\" | \\"pure\\"
209209
* options.modules should be an object:
210-
object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
210+
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
211211
`;
212212
213213
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
214214
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
215215
- options.modules should be one of these:
216-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
216+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }
217217
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
218218
Details:
219219
* options.modules should be a boolean.
220220
* options.modules should be one of these:
221221
\\"local\\" | \\"global\\" | \\"pure\\"
222222
* options.modules should be an object:
223-
object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
223+
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }"
224224
`;
225225
226226
exports[`validate options should throw an error on the "sourceMap" option with "true" value 1`] = `

0 commit comments

Comments
 (0)