diff --git a/src/utils.js b/src/utils.js index 10a3cada..4b9f0b9c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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, @@ -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; }, }), ]; diff --git a/test/__snapshots__/modules-option.test.js.snap b/test/__snapshots__/modules-option.test.js.snap index 6fdf6f44..b9063875 100644 --- a/test/__snapshots__/modules-option.test.js.snap +++ b/test/__snapshots__/modules-option.test.js.snap @@ -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`] = ` diff --git a/test/modules-option.test.js b/test/modules-option.test.js index 75d89fe4..73630a77 100644 --- a/test/modules-option.test.js +++ b/test/modules-option.test.js @@ -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 } },