Skip to content

Commit 1d7a464

Browse files
feat: modules options now accepts object config (#937)
BREAKING CHANGE: - `localIdentName` option was removed in favor `modules.localIdentName` option - `context` option was remove in favor `modules.context` option - `hashPrefix` option was removed in favor `modules.hashPrefix` option - `getLocalIdent` option was removed in favor `modules.getLocalIdent` option - `localIdentRegExp` option was removed in favor `modules.localIdentRegExp` option
1 parent 38ff645 commit 1d7a464

12 files changed

+5633
-2677
lines changed

README.md

+178-59
Large diffs are not rendered by default.

src/options.json

+37-27
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,45 @@
2929
{
3030
"type": "string",
3131
"enum": ["local", "global"]
32-
}
33-
]
34-
},
35-
"localIdentName": {
36-
"type": "string"
37-
},
38-
"localIdentRegExp": {
39-
"anyOf": [
40-
{
41-
"type": "string"
4232
},
4333
{
44-
"instanceof": "RegExp"
45-
}
46-
]
47-
},
48-
"context": {
49-
"type": "string"
50-
},
51-
"hashPrefix": {
52-
"type": "string"
53-
},
54-
"getLocalIdent": {
55-
"anyOf": [
56-
{
57-
"type": "boolean"
58-
},
59-
{
60-
"instanceof": "Function"
34+
"type": "object",
35+
"additionalProperties": false,
36+
"properties": {
37+
"mode": {
38+
"type": "string",
39+
"enum": ["local", "global"]
40+
},
41+
"localIdentName": {
42+
"type": "string"
43+
},
44+
"localIdentRegExp": {
45+
"anyOf": [
46+
{
47+
"type": "string"
48+
},
49+
{
50+
"instanceof": "RegExp"
51+
}
52+
]
53+
},
54+
"context": {
55+
"type": "string"
56+
},
57+
"hashPrefix": {
58+
"type": "string"
59+
},
60+
"getLocalIdent": {
61+
"anyOf": [
62+
{
63+
"type": "boolean"
64+
},
65+
{
66+
"instanceof": "Function"
67+
}
68+
]
69+
}
70+
}
6171
}
6272
]
6373
},

src/utils.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,41 @@ function getImports(messages, importUrlPrefix, loaderContext, callback) {
267267
}
268268

269269
function getModulesPlugins(options, loaderContext) {
270-
const mode = typeof options.modules === 'boolean' ? 'local' : options.modules;
270+
let modulesOptions = {
271+
mode: 'local',
272+
localIdentName: '[hash:base64]',
273+
getLocalIdent,
274+
context: null,
275+
hashPrefix: '',
276+
localIdentRegExp: null,
277+
};
278+
279+
if (
280+
typeof options.modules === 'boolean' ||
281+
typeof options.modules === 'string'
282+
) {
283+
modulesOptions.mode =
284+
typeof options.modules === 'string' ? options.modules : 'local';
285+
} else {
286+
modulesOptions = Object.assign({}, modulesOptions, options.modules);
287+
}
271288

272289
return [
273290
modulesValues,
274-
localByDefault({ mode }),
291+
localByDefault({ mode: modulesOptions.mode }),
275292
extractImports(),
276293
modulesScope({
277294
generateScopedName: function generateScopedName(exportName) {
278-
const localIdentName = options.localIdentName || '[hash:base64]';
279-
const customGetLocalIdent = options.getLocalIdent || getLocalIdent;
280-
281-
return customGetLocalIdent(loaderContext, localIdentName, exportName, {
282-
regExp: options.localIdentRegExp,
283-
hashPrefix: options.hashPrefix || '',
284-
context: options.context,
285-
});
295+
return modulesOptions.getLocalIdent(
296+
loaderContext,
297+
modulesOptions.localIdentName,
298+
exportName,
299+
{
300+
context: modulesOptions.context,
301+
hashPrefix: modulesOptions.hashPrefix,
302+
regExp: modulesOptions.localIdentRegExp,
303+
}
304+
);
286305
},
287306
}),
288307
];

test/__snapshots__/errors.test.js.snap

-117
This file was deleted.

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

-150
This file was deleted.

0 commit comments

Comments
 (0)