Skip to content

Commit 0902353

Browse files
authored
feat: allow modules.auto to be a filter function (#1086)
1 parent 505d2e6 commit 0902353

7 files changed

+167
-22
lines changed

README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ module.exports = {
542542

543543
##### `auto`
544544

545-
Type: `Boolean|RegExp`
545+
Type: `Boolean|RegExp|Function`
546546
Default: `'undefined'`
547547

548548
Allows auto enable css modules based on filename.
@@ -576,7 +576,7 @@ module.exports = {
576576

577577
###### `RegExp`
578578

579-
Enable css modules for files based on a filename and satisfying your regex.
579+
Enable css modules for files based on the filename satisfying your regex check.
580580

581581
**webpack.config.js**
582582

@@ -598,6 +598,30 @@ module.exports = {
598598
};
599599
```
600600

601+
###### `Function`
602+
603+
Enable css modules for files based on the filename satisfying your filter function check.
604+
605+
**webpack.config.js**
606+
607+
```js
608+
module.exports = {
609+
module: {
610+
rules: [
611+
{
612+
test: /\.css$/i,
613+
loader: 'css-loader',
614+
options: {
615+
modules: {
616+
auto: (resourcePath) => resourcePath.endsWith('.custom-module.css'),
617+
},
618+
},
619+
},
620+
],
621+
},
622+
};
623+
```
624+
601625
##### `mode`
602626

603627
Type: `String|Function`

src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
{
4242
"instanceof": "RegExp"
4343
},
44+
{
45+
"instanceof": "Function"
46+
},
4447
{
4548
"type": "boolean"
4649
}

src/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function shouldUseModulesPlugins(modules, resourcePath) {
117117
return modules.auto.test(resourcePath);
118118
}
119119

120+
if (typeof modules.auto === 'function') {
121+
return modules.auto(resourcePath);
122+
}
123+
120124
return true;
121125
}
122126

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

+75-16
Original file line numberDiff line numberDiff line change
@@ -3148,9 +3148,9 @@ Array [
31483148
31493149
exports[`"modules" option should work when the "getLocalIdent" option returns "false": warnings 1`] = `Array []`;
31503150
3151-
exports[`"modules" option should work with a modules.auto equal "false": errors 1`] = `Array []`;
3151+
exports[`"modules" option should work with a modules.auto Boolean that is "false": errors 1`] = `Array []`;
31523152
3153-
exports[`"modules" option should work with a modules.auto equal "false": module 1`] = `
3153+
exports[`"modules" option should work with a modules.auto Boolean that is "false": module 1`] = `
31543154
"// Imports
31553155
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
31563156
exports = ___CSS_LOADER_API_IMPORT___(false);
@@ -3161,7 +3161,7 @@ module.exports = exports;
31613161
"
31623162
`;
31633163
3164-
exports[`"modules" option should work with a modules.auto equal "false": result 1`] = `
3164+
exports[`"modules" option should work with a modules.auto Boolean that is "false": result 1`] = `
31653165
Array [
31663166
Array [
31673167
"./modules/mode/relative.module.css",
@@ -3174,11 +3174,11 @@ Array [
31743174
]
31753175
`;
31763176
3177-
exports[`"modules" option should work with a modules.auto equal "false": warnings 1`] = `Array []`;
3177+
exports[`"modules" option should work with a modules.auto Boolean that is "false": warnings 1`] = `Array []`;
31783178
3179-
exports[`"modules" option should work with a modules.auto equal "true": errors 1`] = `Array []`;
3179+
exports[`"modules" option should work with a modules.auto Boolean that is "true": errors 1`] = `Array []`;
31803180
3181-
exports[`"modules" option should work with a modules.auto equal "true": module 1`] = `
3181+
exports[`"modules" option should work with a modules.auto Boolean that is "true": module 1`] = `
31823182
"// Imports
31833183
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
31843184
exports = ___CSS_LOADER_API_IMPORT___(false);
@@ -3192,7 +3192,7 @@ module.exports = exports;
31923192
"
31933193
`;
31943194
3195-
exports[`"modules" option should work with a modules.auto equal "true": result 1`] = `
3195+
exports[`"modules" option should work with a modules.auto Boolean that is "true": result 1`] = `
31963196
Array [
31973197
Array [
31983198
"./modules/mode/relative.module.css",
@@ -3205,11 +3205,11 @@ Array [
32053205
]
32063206
`;
32073207
3208-
exports[`"modules" option should work with a modules.auto equal "true": warnings 1`] = `Array []`;
3208+
exports[`"modules" option should work with a modules.auto Boolean that is "true": warnings 1`] = `Array []`;
32093209
3210-
exports[`"modules" option should work with a modules.auto returns "false": errors 1`] = `Array []`;
3210+
exports[`"modules" option should work with a modules.auto Function that returns "false": errors 1`] = `Array []`;
32113211
3212-
exports[`"modules" option should work with a modules.auto returns "false": module 1`] = `
3212+
exports[`"modules" option should work with a modules.auto Function that returns "false": module 1`] = `
32133213
"// Imports
32143214
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
32153215
exports = ___CSS_LOADER_API_IMPORT___(false);
@@ -3220,7 +3220,7 @@ module.exports = exports;
32203220
"
32213221
`;
32223222
3223-
exports[`"modules" option should work with a modules.auto returns "false": result 1`] = `
3223+
exports[`"modules" option should work with a modules.auto Function that returns "false": result 1`] = `
32243224
Array [
32253225
Array [
32263226
"./modules/mode/relative.module.css",
@@ -3233,11 +3233,11 @@ Array [
32333233
]
32343234
`;
32353235
3236-
exports[`"modules" option should work with a modules.auto returns "false": warnings 1`] = `Array []`;
3236+
exports[`"modules" option should work with a modules.auto Function that returns "false": warnings 1`] = `Array []`;
32373237
3238-
exports[`"modules" option should work with a modules.auto returns "true": errors 1`] = `Array []`;
3238+
exports[`"modules" option should work with a modules.auto Function that returns "true": errors 1`] = `Array []`;
32393239
3240-
exports[`"modules" option should work with a modules.auto returns "true": module 1`] = `
3240+
exports[`"modules" option should work with a modules.auto Function that returns "true": module 1`] = `
32413241
"// Imports
32423242
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
32433243
exports = ___CSS_LOADER_API_IMPORT___(false);
@@ -3251,7 +3251,7 @@ module.exports = exports;
32513251
"
32523252
`;
32533253
3254-
exports[`"modules" option should work with a modules.auto returns "true": result 1`] = `
3254+
exports[`"modules" option should work with a modules.auto Function that returns "true": result 1`] = `
32553255
Array [
32563256
Array [
32573257
"./modules/mode/relative.module.css",
@@ -3264,7 +3264,66 @@ Array [
32643264
]
32653265
`;
32663266
3267-
exports[`"modules" option should work with a modules.auto returns "true": warnings 1`] = `Array []`;
3267+
exports[`"modules" option should work with a modules.auto Function that returns "true": warnings 1`] = `Array []`;
3268+
3269+
exports[`"modules" option should work with a modules.auto RegExp that returns "false": errors 1`] = `Array []`;
3270+
3271+
exports[`"modules" option should work with a modules.auto RegExp that returns "false": module 1`] = `
3272+
"// Imports
3273+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
3274+
exports = ___CSS_LOADER_API_IMPORT___(false);
3275+
// Module
3276+
exports.push([module.id, \\".relative {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
3277+
// Exports
3278+
module.exports = exports;
3279+
"
3280+
`;
3281+
3282+
exports[`"modules" option should work with a modules.auto RegExp that returns "false": result 1`] = `
3283+
Array [
3284+
Array [
3285+
"./modules/mode/relative.module.css",
3286+
".relative {
3287+
color: red;
3288+
}
3289+
",
3290+
"",
3291+
],
3292+
]
3293+
`;
3294+
3295+
exports[`"modules" option should work with a modules.auto RegExp that returns "false": warnings 1`] = `Array []`;
3296+
3297+
exports[`"modules" option should work with a modules.auto RegExp that returns "true": errors 1`] = `Array []`;
3298+
3299+
exports[`"modules" option should work with a modules.auto RegExp that returns "true": module 1`] = `
3300+
"// Imports
3301+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
3302+
exports = ___CSS_LOADER_API_IMPORT___(false);
3303+
// Module
3304+
exports.push([module.id, \\".y35Nud52-ZFXmqL6AWueX {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
3305+
// Exports
3306+
exports.locals = {
3307+
\\"relative\\": \\"y35Nud52-ZFXmqL6AWueX\\"
3308+
};
3309+
module.exports = exports;
3310+
"
3311+
`;
3312+
3313+
exports[`"modules" option should work with a modules.auto RegExp that returns "true": result 1`] = `
3314+
Array [
3315+
Array [
3316+
"./modules/mode/relative.module.css",
3317+
".y35Nud52-ZFXmqL6AWueX {
3318+
color: red;
3319+
}
3320+
",
3321+
"",
3322+
],
3323+
]
3324+
`;
3325+
3326+
exports[`"modules" option should work with a modules.auto RegExp that returns "true": warnings 1`] = `Array []`;
32683327
32693328
exports[`"modules" option should work with case \`animation\` (\`modules\` value is \`false)\`: errors 1`] = `Array []`;
32703329

test/__snapshots__/validate-options.test.js.snap

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ exports[`validate options should throw an error on the "localsConvention" option
4343
-> Style of exported classnames (https://github.com/webpack-contrib/css-loader#localsconvention)."
4444
`;
4545

46+
exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = `
47+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
48+
- options.modules should be one of these:
49+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
50+
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
51+
Details:
52+
* options.modules.auto should be one of these:
53+
RegExp | function | boolean
54+
Details:
55+
* options.modules.auto should be an instance of RegExp.
56+
* options.modules.auto should be an instance of function.
57+
* options.modules.auto should be a boolean."
58+
`;
59+
4660
exports[`validate options should throw an error on the "modules" option with "{"context":true}" value 1`] = `
4761
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
4862
- options.modules.context should be a string."

test/modules-option.test.js

+40-4
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ describe('"modules" option', () => {
689689
expect(getErrors(stats)).toMatchSnapshot('errors');
690690
});
691691

692-
it('should work with a modules.auto equal "false"', async () => {
692+
it('should work with a modules.auto Boolean that is "false"', async () => {
693693
const compiler = getCompiler('./modules/mode/modules.js', {
694694
modules: {
695695
auto: false,
@@ -707,7 +707,7 @@ describe('"modules" option', () => {
707707
expect(getErrors(stats)).toMatchSnapshot('errors');
708708
});
709709

710-
it('should work with a modules.auto equal "true"', async () => {
710+
it('should work with a modules.auto Boolean that is "true"', async () => {
711711
const compiler = getCompiler('./modules/mode/modules.js', {
712712
modules: {
713713
auto: true,
@@ -725,7 +725,7 @@ describe('"modules" option', () => {
725725
expect(getErrors(stats)).toMatchSnapshot('errors');
726726
});
727727

728-
it('should work with a modules.auto returns "true"', async () => {
728+
it('should work with a modules.auto RegExp that returns "true"', async () => {
729729
const compiler = getCompiler('./modules/mode/modules.js', {
730730
modules: {
731731
auto: /relative.module.css$/,
@@ -743,7 +743,7 @@ describe('"modules" option', () => {
743743
expect(getErrors(stats)).toMatchSnapshot('errors');
744744
});
745745

746-
it('should work with a modules.auto returns "false"', async () => {
746+
it('should work with a modules.auto RegExp that returns "false"', async () => {
747747
const compiler = getCompiler('./modules/mode/modules.js', {
748748
modules: {
749749
auto: /will no pass/,
@@ -760,4 +760,40 @@ describe('"modules" option', () => {
760760
expect(getWarnings(stats)).toMatchSnapshot('warnings');
761761
expect(getErrors(stats)).toMatchSnapshot('errors');
762762
});
763+
764+
it('should work with a modules.auto Function that returns "true"', async () => {
765+
const compiler = getCompiler('./modules/mode/modules.js', {
766+
modules: {
767+
auto: (relativePath) => relativePath.endsWith('module.css'),
768+
},
769+
});
770+
const stats = await compile(compiler);
771+
772+
expect(
773+
getModuleSource('./modules/mode/relative.module.css', stats)
774+
).toMatchSnapshot('module');
775+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
776+
'result'
777+
);
778+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
779+
expect(getErrors(stats)).toMatchSnapshot('errors');
780+
});
781+
782+
it('should work with a modules.auto Function that returns "false"', async () => {
783+
const compiler = getCompiler('./modules/mode/modules.js', {
784+
modules: {
785+
auto: (relativePath) => relativePath.endsWith('will no pass'),
786+
},
787+
});
788+
const stats = await compile(compiler);
789+
790+
expect(
791+
getModuleSource('./modules/mode/relative.module.css', stats)
792+
).toMatchSnapshot('module');
793+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
794+
'result'
795+
);
796+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
797+
expect(getErrors(stats)).toMatchSnapshot('errors');
798+
});
763799
});

test/validate-options.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ describe('validate options', () => {
2828
{ localIdentRegExp: 'page-(.*)\\.js' },
2929
{ localIdentRegExp: /page-(.*)\.js/ },
3030
{ exportGlobals: true },
31+
{ auto: true },
32+
{ auto: false },
33+
{ auto: /custom-regex/ },
34+
{ auto: () => true },
3135
],
3236
failure: [
3337
'true',
@@ -44,6 +48,7 @@ describe('validate options', () => {
4448
{ getLocalIdent: [] },
4549
{ localIdentRegExp: true },
4650
{ exportGlobals: 'invalid' },
51+
{ auto: 'invalid' },
4752
],
4853
},
4954
sourceMap: {

0 commit comments

Comments
 (0)