Skip to content

Commit 02059cb

Browse files
committed
feat: support named exports with any characters
The limitation of only allowing `camelCase` named exports fails to take into account the full capabilities of module exports. In fact, you can export any name you want with `export { local as "string literal" }`. This removes the limitation and allows you to set `exportLocalsConvention` back to `asIs` when using the `namedExports` option. Relevant syntax documentation can be found here: https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export#syntax
1 parent e27ab5e commit 02059cb

File tree

5 files changed

+454
-252
lines changed

5 files changed

+454
-252
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,10 @@ Enables/disables ES modules named export for locals.
11191119

11201120
> **Warning**
11211121
>
1122-
> Names of locals are converted to camelcase, i.e. the `exportLocalsConvention` option has `camelCaseOnly` value by default.
1122+
> Names of locals are converted to camelcase, i.e. the `exportLocalsConvention` option has
1123+
> `camelCaseOnly` value by default. You can set this back to any other valid option but selectors
1124+
> which are not valid JavaScript identifiers may run into problems which do not implement the entire
1125+
> modules specification.
11231126
11241127
> **Warning**
11251128
>
@@ -1739,7 +1742,7 @@ With the help of the `/* webpackIgnore: true */`comment, it is possible to disab
17391742
.class {
17401743
/* Disabled url handling for the first url in the 'background' declaration */
17411744
color: red;
1742-
background:
1745+
background:
17431746
/* webpackIgnore: true */ url("./url/img.png"), url("./url/img.png");
17441747
}
17451748

@@ -1755,7 +1758,7 @@ With the help of the `/* webpackIgnore: true */`comment, it is possible to disab
17551758
/* Disabled url handling for the second url in the 'background' declaration */
17561759
color: red;
17571760
background: url("./url/img.png"),
1758-
/* webpackIgnore: true */
1761+
/* webpackIgnore: true */
17591762
url("./url/img.png");
17601763
}
17611764

package-lock.json

Lines changed: 44 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -676,16 +676,6 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
676676
"The 'modules.namedExport' option requires the 'esModules' option to be enabled"
677677
);
678678
}
679-
680-
if (
681-
typeof exportLocalsConventionType === "string" &&
682-
exportLocalsConventionType !== "camelCaseOnly" &&
683-
exportLocalsConventionType !== "dashesOnly"
684-
) {
685-
throw new Error(
686-
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"'
687-
);
688-
}
689679
}
690680

691681
return modulesOptions;
@@ -1156,6 +1146,7 @@ function getExportCode(
11561146

11571147
if (icssPluginUsed) {
11581148
let localsCode = "";
1149+
let identifierId = 0;
11591150

11601151
const addExportToLocalsCode = (names, value) => {
11611152
const normalizedNames = Array.isArray(names)
@@ -1164,11 +1155,14 @@ function getExportCode(
11641155

11651156
for (const name of normalizedNames) {
11661157
if (options.modules.namedExport) {
1167-
localsCode += `export var ${name} = ${
1158+
identifierId += 1;
1159+
const id = `_${identifierId.toString(16)}`;
1160+
localsCode += `var ${id} = ${
11681161
isTemplateLiteralSupported
11691162
? convertToTemplateLiteral(value)
11701163
: JSON.stringify(value)
11711164
};\n`;
1165+
localsCode += `export { ${id} as ${JSON.stringify(name)} };\n`;
11721166
} else {
11731167
if (localsCode) {
11741168
localsCode += `,\n`;

0 commit comments

Comments
 (0)