Skip to content

Commit 0080f88

Browse files
authoredJul 21, 2020
refactor: default values modules and module.auto are true (#1117)
BREAKING CHANGE: the `modules` option is `true` by default for all files matching `/\.module\.\w+$/i.test(filename)` regular expression, `module.auto` is `true` by default
1 parent e1c55e4 commit 0080f88

File tree

4 files changed

+140
-85
lines changed

4 files changed

+140
-85
lines changed
 

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ module.exports = {
284284
### `modules`
285285

286286
Type: `Boolean|String|Object`
287-
Default: `false`
287+
Default: based on filename, `true` for all files matching `/\.module\.\w+$/i.test(filename)` regular expression, more information you can read [here](https://github.com/webpack-contrib/css-loader#auto)
288288

289289
Enables/Disables CSS Modules and their configuration.
290290

@@ -546,7 +546,7 @@ module.exports = {
546546
##### `auto`
547547

548548
Type: `Boolean|RegExp|Function`
549-
Default: `'undefined'`
549+
Default: `'true'`
550550

551551
Allows auto enable css modules based on filename.
552552

@@ -968,7 +968,7 @@ module.exports = {
968968
};
969969
```
970970

971-
### `exportOnlyLocals`
971+
##### `exportOnlyLocals`
972972

973973
Type: `Boolean`
974974
Default: `false`

‎src/utils.js

+31-21
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,26 @@ function getFilter(filter, resourcePath) {
104104
};
105105
}
106106

107+
const moduleRegExp = /\.module\.\w+$/i;
108+
107109
function getModulesOptions(rawOptions, loaderContext) {
110+
const { resourcePath } = loaderContext;
111+
108112
if (typeof rawOptions.modules === 'undefined') {
109-
return false;
110-
}
113+
const isModules = moduleRegExp.test(resourcePath);
111114

112-
if (typeof rawOptions.modules === 'boolean' && rawOptions.modules === false) {
115+
if (!isModules) {
116+
return false;
117+
}
118+
} else if (
119+
typeof rawOptions.modules === 'boolean' &&
120+
rawOptions.modules === false
121+
) {
113122
return false;
114123
}
115124

116125
let modulesOptions = {
126+
auto: true,
117127
mode: 'local',
118128
localIdentName: '[hash:base64]',
119129
// eslint-disable-next-line no-undefined
@@ -133,30 +143,30 @@ function getModulesOptions(rawOptions, loaderContext) {
133143
modulesOptions.mode =
134144
typeof rawOptions.modules === 'string' ? rawOptions.modules : 'local';
135145
} else {
136-
const { resourcePath } = loaderContext;
146+
if (rawOptions.modules) {
147+
if (typeof rawOptions.modules.auto === 'boolean') {
148+
const isModules =
149+
rawOptions.modules.auto && moduleRegExp.test(resourcePath);
137150

138-
if (typeof rawOptions.modules.auto === 'boolean') {
139-
const isModules =
140-
rawOptions.modules.auto && /\.module\.\w+$/i.test(resourcePath);
141-
142-
if (!isModules) {
143-
return false;
144-
}
145-
} else if (rawOptions.modules.auto instanceof RegExp) {
146-
const isModules = rawOptions.modules.auto.test(resourcePath);
151+
if (!isModules) {
152+
return false;
153+
}
154+
} else if (rawOptions.modules.auto instanceof RegExp) {
155+
const isModules = rawOptions.modules.auto.test(resourcePath);
147156

148-
if (!isModules) {
149-
return false;
150-
}
151-
} else if (typeof rawOptions.modules.auto === 'function') {
152-
const isModule = rawOptions.modules.auto(resourcePath);
157+
if (!isModules) {
158+
return false;
159+
}
160+
} else if (typeof rawOptions.modules.auto === 'function') {
161+
const isModule = rawOptions.modules.auto(resourcePath);
153162

154-
if (!isModule) {
155-
return false;
163+
if (!isModule) {
164+
return false;
165+
}
156166
}
157167
}
158168

159-
modulesOptions = { ...modulesOptions, ...rawOptions.modules };
169+
modulesOptions = { ...modulesOptions, ...(rawOptions.modules || {}) };
160170
}
161171

162172
if (typeof modulesOptions.mode === 'function') {

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

+90-59
Original file line numberDiff line numberDiff line change
@@ -3790,65 +3790,6 @@ Array [
37903790
37913791
exports[`"modules" option should work when the "getLocalIdent" option returns "false": warnings 1`] = `Array []`;
37923792
3793-
exports[`"modules" option should work with a modules.auto Boolean that is "false": errors 1`] = `Array []`;
3794-
3795-
exports[`"modules" option should work with a modules.auto Boolean that is "false": module 1`] = `
3796-
"// Imports
3797-
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
3798-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
3799-
// Module
3800-
___CSS_LOADER_EXPORT___.push([module.id, \\".relative {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
3801-
// Exports
3802-
export default ___CSS_LOADER_EXPORT___;
3803-
"
3804-
`;
3805-
3806-
exports[`"modules" option should work with a modules.auto Boolean that is "false": result 1`] = `
3807-
Array [
3808-
Array [
3809-
"./modules/mode/relative.module.css",
3810-
".relative {
3811-
color: red;
3812-
}
3813-
",
3814-
"",
3815-
],
3816-
]
3817-
`;
3818-
3819-
exports[`"modules" option should work with a modules.auto Boolean that is "false": warnings 1`] = `Array []`;
3820-
3821-
exports[`"modules" option should work with a modules.auto Boolean that is "true": errors 1`] = `Array []`;
3822-
3823-
exports[`"modules" option should work with a modules.auto Boolean that is "true": module 1`] = `
3824-
"// Imports
3825-
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
3826-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
3827-
// Module
3828-
___CSS_LOADER_EXPORT___.push([module.id, \\"._1HULUYtV_Lb49H2tf3WnVr {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
3829-
// Exports
3830-
___CSS_LOADER_EXPORT___.locals = {
3831-
\\"relative\\": \\"_1HULUYtV_Lb49H2tf3WnVr\\"
3832-
};
3833-
export default ___CSS_LOADER_EXPORT___;
3834-
"
3835-
`;
3836-
3837-
exports[`"modules" option should work with a modules.auto Boolean that is "true": result 1`] = `
3838-
Array [
3839-
Array [
3840-
"./modules/mode/relative.module.css",
3841-
"._1HULUYtV_Lb49H2tf3WnVr {
3842-
color: red;
3843-
}
3844-
",
3845-
"",
3846-
],
3847-
]
3848-
`;
3849-
3850-
exports[`"modules" option should work with a modules.auto Boolean that is "true": warnings 1`] = `Array []`;
3851-
38523793
exports[`"modules" option should work with a modules.auto Function that returns "false": errors 1`] = `Array []`;
38533794
38543795
exports[`"modules" option should work with a modules.auto Function that returns "false": module 1`] = `
@@ -11941,6 +11882,96 @@ Array [
1194111882
1194211883
exports[`"modules" option should work with the "[local]" placeholder for the "localIdentName" option: warnings 1`] = `Array []`;
1194311884
11885+
exports[`"modules" option should work with the "auto" by default: errors 1`] = `Array []`;
11886+
11887+
exports[`"modules" option should work with the "auto" by default: module 1`] = `
11888+
"// Imports
11889+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
11890+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
11891+
// Module
11892+
___CSS_LOADER_EXPORT___.push([module.id, \\"._1HULUYtV_Lb49H2tf3WnVr {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
11893+
// Exports
11894+
___CSS_LOADER_EXPORT___.locals = {
11895+
\\"relative\\": \\"_1HULUYtV_Lb49H2tf3WnVr\\"
11896+
};
11897+
export default ___CSS_LOADER_EXPORT___;
11898+
"
11899+
`;
11900+
11901+
exports[`"modules" option should work with the "auto" by default: result 1`] = `
11902+
Array [
11903+
Array [
11904+
"./modules/mode/relative.module.css",
11905+
"._1HULUYtV_Lb49H2tf3WnVr {
11906+
color: red;
11907+
}
11908+
",
11909+
"",
11910+
],
11911+
]
11912+
`;
11913+
11914+
exports[`"modules" option should work with the "auto" by default: warnings 1`] = `Array []`;
11915+
11916+
exports[`"modules" option should work with the "auto" when it is "false": errors 1`] = `Array []`;
11917+
11918+
exports[`"modules" option should work with the "auto" when it is "false": module 1`] = `
11919+
"// Imports
11920+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
11921+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
11922+
// Module
11923+
___CSS_LOADER_EXPORT___.push([module.id, \\".relative {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
11924+
// Exports
11925+
export default ___CSS_LOADER_EXPORT___;
11926+
"
11927+
`;
11928+
11929+
exports[`"modules" option should work with the "auto" when it is "false": result 1`] = `
11930+
Array [
11931+
Array [
11932+
"./modules/mode/relative.module.css",
11933+
".relative {
11934+
color: red;
11935+
}
11936+
",
11937+
"",
11938+
],
11939+
]
11940+
`;
11941+
11942+
exports[`"modules" option should work with the "auto" when it is "false": warnings 1`] = `Array []`;
11943+
11944+
exports[`"modules" option should work with the "auto" when it is "true": errors 1`] = `Array []`;
11945+
11946+
exports[`"modules" option should work with the "auto" when it is "true": module 1`] = `
11947+
"// Imports
11948+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
11949+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
11950+
// Module
11951+
___CSS_LOADER_EXPORT___.push([module.id, \\"._1HULUYtV_Lb49H2tf3WnVr {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
11952+
// Exports
11953+
___CSS_LOADER_EXPORT___.locals = {
11954+
\\"relative\\": \\"_1HULUYtV_Lb49H2tf3WnVr\\"
11955+
};
11956+
export default ___CSS_LOADER_EXPORT___;
11957+
"
11958+
`;
11959+
11960+
exports[`"modules" option should work with the "auto" when it is "true": result 1`] = `
11961+
Array [
11962+
Array [
11963+
"./modules/mode/relative.module.css",
11964+
"._1HULUYtV_Lb49H2tf3WnVr {
11965+
color: red;
11966+
}
11967+
",
11968+
"",
11969+
],
11970+
]
11971+
`;
11972+
11973+
exports[`"modules" option should work with the "auto" when it is "true": warnings 1`] = `Array []`;
11974+
1194411975
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`global\`): errors 1`] = `Array []`;
1194511976
1194611977
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`global\`): module 1`] = `

‎test/modules-option.test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,21 @@ describe('"modules" option', () => {
711711
expect(getErrors(stats)).toMatchSnapshot('errors');
712712
});
713713

714-
it('should work with a modules.auto Boolean that is "false"', async () => {
714+
it('should work with the "auto" by default', async () => {
715+
const compiler = getCompiler('./modules/mode/modules.js');
716+
const stats = await compile(compiler);
717+
718+
expect(
719+
getModuleSource('./modules/mode/relative.module.css', stats)
720+
).toMatchSnapshot('module');
721+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
722+
'result'
723+
);
724+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
725+
expect(getErrors(stats)).toMatchSnapshot('errors');
726+
});
727+
728+
it('should work with the "auto" when it is "false"', async () => {
715729
const compiler = getCompiler('./modules/mode/modules.js', {
716730
modules: {
717731
auto: false,
@@ -729,7 +743,7 @@ describe('"modules" option', () => {
729743
expect(getErrors(stats)).toMatchSnapshot('errors');
730744
});
731745

732-
it('should work with a modules.auto Boolean that is "true"', async () => {
746+
it('should work with the "auto" when it is "true"', async () => {
733747
const compiler = getCompiler('./modules/mode/modules.js', {
734748
modules: {
735749
auto: true,

0 commit comments

Comments
 (0)