From 2b1e089bddd34941145353667f3e1b8666d90141 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 21 Jul 2020 20:37:12 +0300 Subject: [PATCH] refactor: the `modules.context` option was renamed to the `modules.localIdentContext` option 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 --- README.md | 7 ++- src/options.json | 14 ++++-- src/utils.js | 16 +++---- .../validate-options.test.js.snap | 48 +++++++++---------- test/modules-option.test.js | 10 ++-- test/validate-options.test.js | 4 +- 6 files changed, 49 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 19cb17a6..4465737a 100644 --- a/README.md +++ b/README.md @@ -726,7 +726,7 @@ module.exports = { Type: `String` Default: `'[hash:base64]'` -You can configure the generated ident with the `localIdentName` query parameter. +You can configure the generated ident with the `localIdentName` option. See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options. Recommendations: @@ -807,13 +807,12 @@ module.exports = { }; ``` -##### `context` +##### `localIdentContext` Type: `String` -Default: `undefined` +Default: `compiler.context` Allow to redefine basic loader context for local ident name. -By default we use `rootContext` of loader. **webpack.config.js** diff --git a/src/options.json b/src/options.json index 6c226e37..4ec5b263 100644 --- a/src/options.json +++ b/src/options.json @@ -63,18 +63,22 @@ "type": "boolean" }, "localIdentName": { - "type": "string" + "type": "string", + "minLength": 1 }, - "context": { - "type": "string" + "localIdentContext": { + "type": "string", + "minLength": 1 }, "localIdentHashPrefix": { - "type": "string" + "type": "string", + "minLength": 1 }, "localIdentRegExp": { "anyOf": [ { - "type": "string" + "type": "string", + "minLength": 1 }, { "instanceof": "RegExp" diff --git a/src/utils.js b/src/utils.js index ecaee9a2..9afb971a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -52,13 +52,8 @@ const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; const reRelativePath = /^\.+/; function getLocalIdent(loaderContext, localIdentName, localName, options) { - if (!options.context) { - // eslint-disable-next-line no-param-reassign - options.context = loaderContext.rootContext; - } - const request = normalizePath( - path.relative(options.context || '', loaderContext.resourcePath) + path.relative(options.context, loaderContext.resourcePath) ); // eslint-disable-next-line no-param-reassign @@ -125,13 +120,14 @@ function getModulesOptions(rawOptions, loaderContext) { let modulesOptions = { auto: true, mode: 'local', + exportGlobals: false, localIdentName: '[hash:base64]', + localIdentContext: loaderContext.rootContext, localIdentHashPrefix: '', // eslint-disable-next-line no-undefined localIdentRegExp: undefined, - localsConvention: 'asIs', getLocalIdent, - exportGlobals: false, + localsConvention: 'asIs', namedExport: false, exportOnlyLocals: false, }; @@ -245,7 +241,7 @@ function getModulesPlugins(options, loaderContext) { options.modules.localIdentName, exportName, { - context: options.modules.context, + context: options.modules.localIdentContext, hashPrefix: options.modules.localIdentHashPrefix, regExp: options.modules.localIdentRegExp, } @@ -258,7 +254,7 @@ function getModulesPlugins(options, loaderContext) { options.modules.localIdentName, exportName, { - context: options.modules.context, + context: options.modules.localIdentContext, hashPrefix: options.modules.localIdentHashPrefix, regExp: options.modules.localIdentRegExp, } diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index f829b813..776b3f31 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -39,7 +39,7 @@ exports[`validate options should throw an error on the "importLoaders" option wi exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.auto should be one of these: @@ -50,11 +50,6 @@ exports[`validate options should throw an error on the "modules" option with "{" * options.modules.auto should be a boolean." `; -exports[`validate options should throw an error on the "modules" option with "{"context":true}" value 1`] = ` -"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - - options.modules.context should be a string." -`; - exports[`validate options should throw an error on the "modules" option with "{"exportGlobals":"invalid"}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules.exportGlobals should be a boolean." @@ -69,7 +64,7 @@ exports[`validate options should throw an error on the "modules" option with "{" exports[`validate options should throw an error on the "modules" option with "{"getLocalIdent":[]}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.getLocalIdent should be one of these: @@ -79,26 +74,31 @@ exports[`validate options should throw an error on the "modules" option with "{" * options.modules.getLocalIdent should be an instance of function." `; +exports[`validate options should throw an error on the "modules" option with "{"localIdentContext":true}" value 1`] = ` +"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. + - options.modules.localIdentContext should be a non-empty string." +`; + exports[`validate options should throw an error on the "modules" option with "{"localIdentHashPrefix":true}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - - options.modules.localIdentHashPrefix should be a string." + - options.modules.localIdentHashPrefix should be a non-empty string." `; exports[`validate options should throw an error on the "modules" option with "{"localIdentName":true}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - - options.modules.localIdentName should be a string." + - options.modules.localIdentName should be a non-empty string." `; exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.localIdentRegExp should be one of these: - string | RegExp + non-empty string | RegExp Details: - * options.modules.localIdentRegExp should be a string. + * options.modules.localIdentRegExp should be a non-empty string. * options.modules.localIdentRegExp should be an instance of RegExp." `; @@ -112,7 +112,7 @@ exports[`validate options should throw an error on the "modules" option with "{" exports[`validate options should throw an error on the "modules" option with "{"mode":"globals"}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.mode should be one of these: @@ -126,7 +126,7 @@ exports[`validate options should throw an error on the "modules" option with "{" exports[`validate options should throw an error on the "modules" option with "{"mode":"locals"}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.mode should be one of these: @@ -140,7 +140,7 @@ exports[`validate options should throw an error on the "modules" option with "{" exports[`validate options should throw an error on the "modules" option with "{"mode":"pures"}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.mode should be one of these: @@ -154,7 +154,7 @@ exports[`validate options should throw an error on the "modules" option with "{" exports[`validate options should throw an error on the "modules" option with "{"mode":true}" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules.mode should be one of these: @@ -174,53 +174,53 @@ exports[`validate options should throw an error on the "modules" option with "{" exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules should be a boolean. * options.modules should be one of these: \\"local\\" | \\"global\\" | \\"pure\\" * options.modules should be an object: - object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" + object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" `; exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules should be a boolean. * options.modules should be one of these: \\"local\\" | \\"global\\" | \\"pure\\" * options.modules should be an object: - object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" + object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" `; exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules should be a boolean. * options.modules should be one of these: \\"local\\" | \\"global\\" | \\"pure\\" * options.modules should be an object: - object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" + object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" `; exports[`validate options should throw an error on the "modules" option with "true" value 1`] = ` "Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema. - options.modules should be one of these: - boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } + boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? } -> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules). Details: * options.modules should be a boolean. * options.modules should be one of these: \\"local\\" | \\"global\\" | \\"pure\\" * options.modules should be an object: - object { auto?, mode?, exportGlobals?, localIdentName?, context?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" + object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, localsConvention?, namedExport?, exportOnlyLocals? }" `; exports[`validate options should throw an error on the "sourceMap" option with "true" value 1`] = ` diff --git a/test/modules-option.test.js b/test/modules-option.test.js index b05c23d1..810e960b 100644 --- a/test/modules-option.test.js +++ b/test/modules-option.test.js @@ -99,7 +99,7 @@ describe('"modules" option', () => { const compiler = getCompiler('./modules/localIdentName/localIdentName.js', { modules: { localIdentName: '[name]--[local]--[hash:base64:5]', - context: path.resolve(__dirname), + localIdentContext: path.resolve(__dirname), }, }); const stats = await compile(compiler); @@ -118,7 +118,7 @@ describe('"modules" option', () => { const compiler = getCompiler('./modules/localIdentName/localIdentName.js', { modules: { localIdentName: '[hash:base64:8]', - context: path.resolve(__dirname), + localIdentContext: path.resolve(__dirname), }, }); const stats = await compile(compiler); @@ -137,7 +137,7 @@ describe('"modules" option', () => { const compiler = getCompiler('./modules/localIdentName/localIdentName.js', { modules: { localIdentName: '[path][name]__[local]', - context: path.resolve(__dirname), + localIdentContext: path.resolve(__dirname), }, }); const stats = await compile(compiler); @@ -241,7 +241,7 @@ describe('"modules" option', () => { const compiler = getCompiler('./modules/localIdentName/localIdentName.js', { modules: { localIdentRegExp: 'regExp', - context: 'context', + localIdentContext: 'context', localIdentHashPrefix: 'hash', getLocalIdent(loaderContext, localIdentName, localName, options) { expect(loaderContext).toBeDefined(); @@ -275,7 +275,7 @@ describe('"modules" option', () => { const compiler = getCompiler('./modules/localIdentName/localIdentName.js', { modules: { getLocalIdent(loaderContext, localIdentName, localName, options) { - expect(options.context).toBeUndefined(); + expect(options.context).toBeDefined(); return 'foo'; }, diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 0ef49812..10d4dead 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -22,7 +22,7 @@ describe('validate options', () => { { mode: 'pure' }, { mode: () => 'local' }, { localIdentName: '[path][name]__[local]--[hash:base64:5]' }, - { context: 'context' }, + { localIdentContext: 'context' }, { localIdentHashPrefix: 'hash' }, { getLocalIdent: () => {} }, { localIdentRegExp: 'page-(.*)\\.js' }, @@ -52,7 +52,7 @@ describe('validate options', () => { { mode: 'locals' }, { mode: 'pures' }, { localIdentName: true }, - { context: true }, + { localIdentContext: true }, { localIdentHashPrefix: true }, { getLocalIdent: [] }, { localIdentRegExp: true },