Skip to content

Commit d829319

Browse files
refactor: code
1 parent 5a17c77 commit d829319

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

src/utils.js

+33-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

@@ -593,10 +611,11 @@ function getModulesOptions(rawOptions, loaderContext) {
593611
);
594612
}
595613

614+
// Todo replace this to js identifier validation function
596615
if (
597-
typeof modulesOptions.exportLocalsConvention === "string" &&
598-
modulesOptions.exportLocalsConvention !== "camelCaseOnly" &&
599-
modulesOptions.exportLocalsConvention !== "dashesOnly"
616+
typeof exportLocalsConventionType === "string" &&
617+
exportLocalsConventionType !== "camelCaseOnly" &&
618+
exportLocalsConventionType !== "dashesOnly"
600619
) {
601620
throw new Error(
602621
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"'
@@ -983,42 +1002,7 @@ function getExportCode(exports, replacements, needToUseIcssPlugin, options) {
9831002
};
9841003

9851004
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-
}
1005+
addExportToLocalsCode(options.modules.exportLocalsConvention(name), value);
10221006
}
10231007

10241008
for (const item of replacements) {

0 commit comments

Comments
 (0)