Skip to content

Commit 18b4fec

Browse files
refactor: code for auto (webpack-contrib#1332)
1 parent 3275ef0 commit 18b4fec

File tree

6 files changed

+307
-73
lines changed

6 files changed

+307
-73
lines changed

src/utils.js

+47-53
Original file line numberDiff line numberDiff line change
@@ -504,40 +504,39 @@ function getValidLocalName(localName, exportLocalsConvention) {
504504
return camelCase(localName);
505505
}
506506

507-
const moduleRegExp = /\.module(s)?\.\w+$/i;
508-
const icssRegExp = /\.icss\.\w+$/i;
507+
const IS_MODULES = /\.module(s)?\.\w+$/i;
508+
const IS_ICSS = /\.icss\.\w+$/i;
509509

510510
function getModulesOptions(rawOptions, loaderContext) {
511+
if (typeof rawOptions.modules === "boolean" && rawOptions.modules === false) {
512+
return false;
513+
}
514+
511515
const resourcePath =
512516
// eslint-disable-next-line no-underscore-dangle
513517
(loaderContext._module && loaderContext._module.matchResource) ||
514518
loaderContext.resourcePath;
515519

516-
let isIcss;
520+
let auto;
521+
let rawModulesOptions;
517522

518523
if (typeof rawOptions.modules === "undefined") {
519-
const isModules = moduleRegExp.test(resourcePath);
520-
521-
if (!isModules) {
522-
isIcss = icssRegExp.test(resourcePath);
523-
}
524-
525-
if (!isModules && !isIcss) {
526-
return false;
527-
}
528-
} else if (
529-
typeof rawOptions.modules === "boolean" &&
530-
rawOptions.modules === false
531-
) {
532-
return false;
524+
rawModulesOptions = {};
525+
auto = true;
526+
} else if (typeof rawOptions.modules === "boolean") {
527+
rawModulesOptions = {};
528+
} else if (typeof rawOptions.modules === "string") {
529+
rawModulesOptions = { mode: rawOptions.modules };
530+
} else {
531+
rawModulesOptions = rawOptions.modules;
532+
({ auto } = rawModulesOptions);
533533
}
534534

535535
// eslint-disable-next-line no-underscore-dangle
536536
const { outputOptions } = loaderContext._compilation;
537-
538-
let modulesOptions = {
539-
auto: true,
540-
mode: isIcss ? "icss" : "local",
537+
const modulesOptions = {
538+
auto,
539+
mode: "local",
541540
exportGlobals: false,
542541
localIdentName: "[hash:base64]",
543542
localIdentContext: loaderContext.rootContext,
@@ -550,48 +549,43 @@ function getModulesOptions(rawOptions, loaderContext) {
550549
// eslint-disable-next-line no-undefined
551550
getLocalIdent: undefined,
552551
namedExport: false,
553-
exportLocalsConvention: "asIs",
552+
exportLocalsConvention:
553+
rawModulesOptions.namedExport === true &&
554+
typeof rawModulesOptions.exportLocalsConvention === "undefined"
555+
? "camelCaseOnly"
556+
: "asIs",
554557
exportOnlyLocals: false,
558+
...rawModulesOptions,
555559
};
556560

557-
if (
558-
typeof rawOptions.modules === "boolean" ||
559-
typeof rawOptions.modules === "string"
560-
) {
561-
modulesOptions.mode =
562-
typeof rawOptions.modules === "string" ? rawOptions.modules : "local";
563-
} else {
564-
if (rawOptions.modules) {
565-
if (typeof rawOptions.modules.auto === "boolean") {
566-
const isModules =
567-
rawOptions.modules.auto && moduleRegExp.test(resourcePath);
561+
if (typeof modulesOptions.auto === "boolean") {
562+
const isModules = modulesOptions.auto && IS_MODULES.test(resourcePath);
568563

569-
if (!isModules) {
570-
return false;
571-
}
572-
} else if (rawOptions.modules.auto instanceof RegExp) {
573-
const isModules = rawOptions.modules.auto.test(resourcePath);
564+
let isIcss;
574565

575-
if (!isModules) {
576-
return false;
577-
}
578-
} else if (typeof rawOptions.modules.auto === "function") {
579-
const isModule = rawOptions.modules.auto(resourcePath);
566+
if (!isModules) {
567+
isIcss = IS_ICSS.test(resourcePath);
580568

581-
if (!isModule) {
582-
return false;
583-
}
569+
if (isIcss) {
570+
modulesOptions.mode = "icss";
584571
}
572+
}
585573

586-
if (
587-
rawOptions.modules.namedExport === true &&
588-
typeof rawOptions.modules.exportLocalsConvention === "undefined"
589-
) {
590-
modulesOptions.exportLocalsConvention = "camelCaseOnly";
591-
}
574+
if (!isModules && !isIcss) {
575+
return false;
592576
}
577+
} else if (modulesOptions.auto instanceof RegExp) {
578+
const isModules = modulesOptions.auto.test(resourcePath);
593579

594-
modulesOptions = { ...modulesOptions, ...(rawOptions.modules || {}) };
580+
if (!isModules) {
581+
return false;
582+
}
583+
} else if (typeof modulesOptions.auto === "function") {
584+
const isModule = modulesOptions.auto(resourcePath);
585+
586+
if (!isModule) {
587+
return false;
588+
}
595589
}
596590

597591
if (typeof modulesOptions.mode === "function") {

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

+167-8
Original file line numberDiff line numberDiff line change
@@ -4414,6 +4414,126 @@ Object {
44144414

44154415
exports[`"modules" option should work js template with "namedExport" option: warnings 1`] = `Array []`;
44164416

4417+
exports[`"modules" option should work when the "auto" is not specified, but specified other modules options: errors 1`] = `Array []`;
4418+
4419+
exports[`"modules" option should work when the "auto" is not specified, but specified other modules options: modules-module 1`] = `
4420+
"// Imports
4421+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
4422+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4423+
// Module
4424+
___CSS_LOADER_EXPORT___.push([module.id, \\".modules-mode-style-modules__class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
4425+
// Exports
4426+
___CSS_LOADER_EXPORT___.locals = {
4427+
\\"class\\": \\"modules-mode-style-modules__class\\"
4428+
};
4429+
export default ___CSS_LOADER_EXPORT___;
4430+
"
4431+
`;
4432+
4433+
exports[`"modules" option should work when the "auto" is not specified, but specified other modules options: not-modules-module 1`] = `
4434+
"// Imports
4435+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
4436+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4437+
// Module
4438+
___CSS_LOADER_EXPORT___.push([module.id, \\".modules-mode-no-modules__class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
4439+
// Exports
4440+
___CSS_LOADER_EXPORT___.locals = {
4441+
\\"class\\": \\"modules-mode-no-modules__class\\"
4442+
};
4443+
export default ___CSS_LOADER_EXPORT___;
4444+
"
4445+
`;
4446+
4447+
exports[`"modules" option should work when the "auto" is not specified, but specified other modules options: result 1`] = `
4448+
".modules-mode-style-modules__class {
4449+
color: red;
4450+
}
4451+
.modules-mode-no-modules__class {
4452+
color: red;
4453+
}
4454+
"
4455+
`;
4456+
4457+
exports[`"modules" option should work when the "auto" is not specified, but specified other modules options: warnings 1`] = `Array []`;
4458+
4459+
exports[`"modules" option should work when the "auto" is not specified: errors 1`] = `Array []`;
4460+
4461+
exports[`"modules" option should work when the "auto" is not specified: modules-module 1`] = `
4462+
"// Imports
4463+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
4464+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4465+
// Module
4466+
___CSS_LOADER_EXPORT___.push([module.id, \\"._BNmWUIwputjT_WFqpoZ {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
4467+
// Exports
4468+
___CSS_LOADER_EXPORT___.locals = {
4469+
\\"class\\": \\"_BNmWUIwputjT_WFqpoZ\\"
4470+
};
4471+
export default ___CSS_LOADER_EXPORT___;
4472+
"
4473+
`;
4474+
4475+
exports[`"modules" option should work when the "auto" is not specified: not-modules-module 1`] = `
4476+
"// Imports
4477+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
4478+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4479+
// Module
4480+
___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
4481+
// Exports
4482+
export default ___CSS_LOADER_EXPORT___;
4483+
"
4484+
`;
4485+
4486+
exports[`"modules" option should work when the "auto" is not specified: result 1`] = `
4487+
"._BNmWUIwputjT_WFqpoZ {
4488+
color: red;
4489+
}
4490+
.class {
4491+
color: red;
4492+
}
4493+
"
4494+
`;
4495+
4496+
exports[`"modules" option should work when the "auto" is not specified: warnings 1`] = `Array []`;
4497+
4498+
exports[`"modules" option should work when the "auto" option is "true" with other options: errors 1`] = `Array []`;
4499+
4500+
exports[`"modules" option should work when the "auto" option is "true" with other options: modules-module 1`] = `
4501+
"// Imports
4502+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
4503+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4504+
// Module
4505+
___CSS_LOADER_EXPORT___.push([module.id, \\".modules-mode-style-modules__class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
4506+
// Exports
4507+
___CSS_LOADER_EXPORT___.locals = {
4508+
\\"class\\": \\"modules-mode-style-modules__class\\"
4509+
};
4510+
export default ___CSS_LOADER_EXPORT___;
4511+
"
4512+
`;
4513+
4514+
exports[`"modules" option should work when the "auto" option is "true" with other options: not-modules-module 1`] = `
4515+
"// Imports
4516+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
4517+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4518+
// Module
4519+
___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
4520+
// Exports
4521+
export default ___CSS_LOADER_EXPORT___;
4522+
"
4523+
`;
4524+
4525+
exports[`"modules" option should work when the "auto" option is "true" with other options: result 1`] = `
4526+
".modules-mode-style-modules__class {
4527+
color: red;
4528+
}
4529+
.class {
4530+
color: red;
4531+
}
4532+
"
4533+
`;
4534+
4535+
exports[`"modules" option should work when the "auto" option is "true" with other options: warnings 1`] = `Array []`;
4536+
44174537
exports[`"modules" option should work when the "namedExport" is enabled and the "exportLocalsConvention" options has "dashesOnly" value: errors 1`] = `Array []`;
44184538

44194539
exports[`"modules" option should work when the "namedExport" is enabled and the "exportLocalsConvention" options has "dashesOnly" value: module 1`] = `
@@ -13743,9 +13863,48 @@ Array [
1374313863

1374413864
exports[`"modules" option should work with the "auto" by default: warnings 1`] = `Array []`;
1374513865

13746-
exports[`"modules" option should work with the "auto" when it is "false": errors 1`] = `Array []`;
13866+
exports[`"modules" option should work with the "auto" option in the "modules" option for icss: errors 1`] = `Array []`;
13867+
13868+
exports[`"modules" option should work with the "auto" option in the "modules" option for icss: module 1`] = `
13869+
"// Imports
13870+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../../src/runtime/api.js\\";
13871+
import ___CSS_LOADER_ICSS_IMPORT_0___ from \\"-!../../../../../src/index.js??ruleSet[1].rules[0].use[0]!./vars.icss.css\\";
13872+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
13873+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_ICSS_IMPORT_0___, \\"\\", true);
13874+
// Module
13875+
___CSS_LOADER_EXPORT___.push([module.id, \\".className {\\\\n color: \\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\";\\\\n}\\\\n\\", \\"\\"]);
13876+
// Exports
13877+
___CSS_LOADER_EXPORT___.locals = {
13878+
\\"primary-color\\": \\"\\" + ___CSS_LOADER_ICSS_IMPORT_0___.locals[\\"primary-color\\"] + \\"\\"
13879+
};
13880+
export default ___CSS_LOADER_EXPORT___;
13881+
"
13882+
`;
13883+
13884+
exports[`"modules" option should work with the "auto" option in the "modules" option for icss: result 1`] = `
13885+
Array [
13886+
Array [
13887+
"../../src/index.js??ruleSet[1].rules[0].use[0]!./modules/mode/icss/vars.icss.css",
13888+
"
13889+
",
13890+
"",
13891+
],
13892+
Array [
13893+
"./modules/mode/icss/relative.icss.css",
13894+
".className {
13895+
color: red;
13896+
}
13897+
",
13898+
"",
13899+
],
13900+
]
13901+
`;
13902+
13903+
exports[`"modules" option should work with the "auto" option in the "modules" option for icss: warnings 1`] = `Array []`;
13904+
13905+
exports[`"modules" option should work with the "auto" option is "false": errors 1`] = `Array []`;
1374713906

13748-
exports[`"modules" option should work with the "auto" when it is "false": module 1`] = `
13907+
exports[`"modules" option should work with the "auto" option is "false": module 1`] = `
1374913908
"// Imports
1375013909
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
1375113910
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
@@ -13756,7 +13915,7 @@ export default ___CSS_LOADER_EXPORT___;
1375613915
"
1375713916
`;
1375813917

13759-
exports[`"modules" option should work with the "auto" when it is "false": result 1`] = `
13918+
exports[`"modules" option should work with the "auto" option is "false": result 1`] = `
1376013919
Array [
1376113920
Array [
1376213921
"./modules/mode/relative.module.css",
@@ -13769,11 +13928,11 @@ Array [
1376913928
]
1377013929
`;
1377113930

13772-
exports[`"modules" option should work with the "auto" when it is "false": warnings 1`] = `Array []`;
13931+
exports[`"modules" option should work with the "auto" option is "false": warnings 1`] = `Array []`;
1377313932

13774-
exports[`"modules" option should work with the "auto" when it is "true": errors 1`] = `Array []`;
13933+
exports[`"modules" option should work with the "auto" option is "true": errors 1`] = `Array []`;
1377513934

13776-
exports[`"modules" option should work with the "auto" when it is "true": module 1`] = `
13935+
exports[`"modules" option should work with the "auto" option is "true": module 1`] = `
1377713936
"// Imports
1377813937
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../src/runtime/api.js\\";
1377913938
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
@@ -13787,7 +13946,7 @@ export default ___CSS_LOADER_EXPORT___;
1378713946
"
1378813947
`;
1378913948

13790-
exports[`"modules" option should work with the "auto" when it is "true": result 1`] = `
13949+
exports[`"modules" option should work with the "auto" option is "true": result 1`] = `
1379113950
Array [
1379213951
Array [
1379313952
"./modules/mode/relative.module.css",
@@ -13800,7 +13959,7 @@ Array [
1380013959
]
1380113960
`;
1380213961

13803-
exports[`"modules" option should work with the "auto" when it is "true": warnings 1`] = `Array []`;
13962+
exports[`"modules" option should work with the "auto" option is "true": warnings 1`] = `Array []`;
1380413963

1380513964
exports[`"modules" option should work with the "namedExport" option with nested import: errors 1`] = `Array []`;
1380613965

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
color: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import modules from './style.modules.css';
2+
import noModules from './no-modules.css';
3+
4+
__export__ = modules + noModules;
5+
6+
export default [modules, noModules];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
color: red;
3+
}

0 commit comments

Comments
 (0)