Skip to content

Commit

Permalink
feat: allow modules.getLocalIdent to return a falsy value
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim committed Jun 28, 2019
1 parent c4b7f71 commit bca9163
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function getModulesPlugins(options, loaderContext) {
extractImports(),
modulesScope({
generateScopedName: function generateScopedName(exportName) {
return modulesOptions.getLocalIdent(
let localIdent = modulesOptions.getLocalIdent(
loaderContext,
modulesOptions.localIdentName,
exportName,
Expand All @@ -151,6 +151,21 @@ function getModulesPlugins(options, loaderContext) {
regExp: modulesOptions.localIdentRegExp,
}
);

if (!localIdent) {
localIdent = getLocalIdent(
loaderContext,
modulesOptions.localIdentName,
exportName,
{
context: modulesOptions.context,
hashPrefix: modulesOptions.hashPrefix,
regExp: modulesOptions.localIdentRegExp,
}
);
}

return localIdent;
},
}),
];
Expand Down
30 changes: 30 additions & 0 deletions test/__snapshots__/modules-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6201,6 +6201,36 @@ exports.locals = {
exports[`modules composes should supports resolving: warnings 1`] = `Array []`;
exports[`modules getLocalIdent should be allowed to return false: errors 1`] = `Array []`;
exports[`modules getLocalIdent should be allowed to return false: locals 1`] = `
Object {
"abc": "abc",
"def": "def",
"ghi": "ghi",
"jkl": "jkl",
}
`;
exports[`modules getLocalIdent should be allowed to return false: module (evaluated) 1`] = `
Array [
Array [
1,
".abc .def {
color: red;
}
.ghi .jkl {
color: blue;
}
",
"",
],
]
`;
exports[`modules getLocalIdent should be allowed to return false: warnings 1`] = `Array []`;
exports[`modules issue #286: errors 1`] = `Array []`;
exports[`modules issue #286: module (evaluated) 1`] = `
Expand Down
23 changes: 23 additions & 0 deletions test/modules-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ describe('modules', () => {
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('getLocalIdent should be allowed to return false', async () => {
const config = {
loader: {
options: {
modules: {
localIdentName: '[local]',
getLocalIdent: () => false,
},
},
},
};
const testId = './modules/getLocalIdent.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);
const evaluatedModule = evaluated(module.source);

expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
expect(evaluatedModule.locals).toMatchSnapshot('locals');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('composes should supports resolving', async () => {
const config = {
loader: { options: { import: true, modules: true } },
Expand Down

0 comments on commit bca9163

Please sign in to comment.