Skip to content

Commit bca9163

Browse files
author
Tim
committed
feat: allow modules.getLocalIdent to return a falsy value
1 parent c4b7f71 commit bca9163

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/utils.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function getModulesPlugins(options, loaderContext) {
141141
extractImports(),
142142
modulesScope({
143143
generateScopedName: function generateScopedName(exportName) {
144-
return modulesOptions.getLocalIdent(
144+
let localIdent = modulesOptions.getLocalIdent(
145145
loaderContext,
146146
modulesOptions.localIdentName,
147147
exportName,
@@ -151,6 +151,21 @@ function getModulesPlugins(options, loaderContext) {
151151
regExp: modulesOptions.localIdentRegExp,
152152
}
153153
);
154+
155+
if (!localIdent) {
156+
localIdent = getLocalIdent(
157+
loaderContext,
158+
modulesOptions.localIdentName,
159+
exportName,
160+
{
161+
context: modulesOptions.context,
162+
hashPrefix: modulesOptions.hashPrefix,
163+
regExp: modulesOptions.localIdentRegExp,
164+
}
165+
);
166+
}
167+
168+
return localIdent;
154169
},
155170
}),
156171
];

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

+30
Original file line numberDiff line numberDiff line change
@@ -6201,6 +6201,36 @@ exports.locals = {
62016201
62026202
exports[`modules composes should supports resolving: warnings 1`] = `Array []`;
62036203
6204+
exports[`modules getLocalIdent should be allowed to return false: errors 1`] = `Array []`;
6205+
6206+
exports[`modules getLocalIdent should be allowed to return false: locals 1`] = `
6207+
Object {
6208+
"abc": "abc",
6209+
"def": "def",
6210+
"ghi": "ghi",
6211+
"jkl": "jkl",
6212+
}
6213+
`;
6214+
6215+
exports[`modules getLocalIdent should be allowed to return false: module (evaluated) 1`] = `
6216+
Array [
6217+
Array [
6218+
1,
6219+
".abc .def {
6220+
color: red;
6221+
}
6222+
6223+
.ghi .jkl {
6224+
color: blue;
6225+
}
6226+
",
6227+
"",
6228+
],
6229+
]
6230+
`;
6231+
6232+
exports[`modules getLocalIdent should be allowed to return false: warnings 1`] = `Array []`;
6233+
62046234
exports[`modules issue #286: errors 1`] = `Array []`;
62056235
62066236
exports[`modules issue #286: module (evaluated) 1`] = `

test/modules-option.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ describe('modules', () => {
312312
expect(stats.compilation.errors).toMatchSnapshot('errors');
313313
});
314314

315+
it('getLocalIdent should be allowed to return false', async () => {
316+
const config = {
317+
loader: {
318+
options: {
319+
modules: {
320+
localIdentName: '[local]',
321+
getLocalIdent: () => false,
322+
},
323+
},
324+
},
325+
};
326+
const testId = './modules/getLocalIdent.css';
327+
const stats = await webpack(testId, config);
328+
const { modules } = stats.toJson();
329+
const module = modules.find((m) => m.id === testId);
330+
const evaluatedModule = evaluated(module.source);
331+
332+
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
333+
expect(evaluatedModule.locals).toMatchSnapshot('locals');
334+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
335+
expect(stats.compilation.errors).toMatchSnapshot('errors');
336+
});
337+
315338
it('composes should supports resolving', async () => {
316339
const config = {
317340
loader: { options: { import: true, modules: true } },

0 commit comments

Comments
 (0)