Skip to content

Commit 5781220

Browse files
refactor: code (#1352)
1 parent 5a17c77 commit 5781220

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

Diff for: src/utils.js

+32-49
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,9 @@ function getFilter(filter, resourcePath) {
485485
}
486486

487487
function getValidLocalName(localName, exportLocalsConvention) {
488-
if (typeof exportLocalsConvention === "function") {
489-
const result = exportLocalsConvention(localName);
488+
const result = exportLocalsConvention(localName);
490489

491-
return Array.isArray(result) ? result[0] : result;
492-
}
493-
494-
if (exportLocalsConvention === "dashesOnly") {
495-
return dashesCamelCase(localName);
496-
}
497-
498-
return camelCase(localName);
490+
return Array.isArray(result) ? result[0] : result;
499491
}
500492

501493
const IS_MODULES = /\.module(s)?\.\w+$/i;
@@ -552,6 +544,32 @@ function getModulesOptions(rawOptions, loaderContext) {
552544
...rawModulesOptions,
553545
};
554546

547+
let exportLocalsConventionType;
548+
549+
if (typeof modulesOptions.exportLocalsConvention === "string") {
550+
exportLocalsConventionType = modulesOptions.exportLocalsConvention;
551+
552+
modulesOptions.exportLocalsConvention = (name) => {
553+
switch (exportLocalsConventionType) {
554+
case "camelCase": {
555+
return [name, camelCase(name)];
556+
}
557+
case "camelCaseOnly": {
558+
return camelCase(name);
559+
}
560+
case "dashes": {
561+
return [name, dashesCamelCase(name)];
562+
}
563+
case "dashesOnly": {
564+
return dashesCamelCase(name);
565+
}
566+
case "asIs":
567+
default:
568+
return name;
569+
}
570+
};
571+
}
572+
555573
if (typeof modulesOptions.auto === "boolean") {
556574
const isModules = modulesOptions.auto && IS_MODULES.test(resourcePath);
557575

@@ -594,9 +612,9 @@ function getModulesOptions(rawOptions, loaderContext) {
594612
}
595613

596614
if (
597-
typeof modulesOptions.exportLocalsConvention === "string" &&
598-
modulesOptions.exportLocalsConvention !== "camelCaseOnly" &&
599-
modulesOptions.exportLocalsConvention !== "dashesOnly"
615+
typeof exportLocalsConventionType === "string" &&
616+
exportLocalsConventionType !== "camelCaseOnly" &&
617+
exportLocalsConventionType !== "dashesOnly"
600618
) {
601619
throw new Error(
602620
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"'
@@ -983,42 +1001,7 @@ function getExportCode(exports, replacements, needToUseIcssPlugin, options) {
9831001
};
9841002

9851003
for (const { name, value } of exports) {
986-
if (typeof options.modules.exportLocalsConvention === "function") {
987-
addExportToLocalsCode(
988-
options.modules.exportLocalsConvention(name),
989-
value
990-
);
991-
992-
// eslint-disable-next-line no-continue
993-
continue;
994-
}
995-
996-
switch (options.modules.exportLocalsConvention) {
997-
case "camelCase": {
998-
const modifiedName = camelCase(name);
999-
1000-
addExportToLocalsCode([name, modifiedName], value);
1001-
break;
1002-
}
1003-
case "camelCaseOnly": {
1004-
addExportToLocalsCode(camelCase(name), value);
1005-
break;
1006-
}
1007-
case "dashes": {
1008-
const modifiedName = dashesCamelCase(name);
1009-
1010-
addExportToLocalsCode([name, modifiedName], value);
1011-
break;
1012-
}
1013-
case "dashesOnly": {
1014-
addExportToLocalsCode(dashesCamelCase(name), value);
1015-
break;
1016-
}
1017-
case "asIs":
1018-
default:
1019-
addExportToLocalsCode(name, value);
1020-
break;
1021-
}
1004+
addExportToLocalsCode(options.modules.exportLocalsConvention(name), value);
10221005
}
10231006

10241007
for (const item of replacements) {

0 commit comments

Comments
 (0)