Skip to content

Commit 992633b

Browse files
committed
Revert breaking changes
- Sets default of `exportLocalsConvention` back to "camelCase" when using named exports - Use previous output format for "camelCase" - Tests for "asIs" option
1 parent d34643d commit 992633b

File tree

9 files changed

+305
-424
lines changed

9 files changed

+305
-424
lines changed

CHANGELOG.md

-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5-
### **7.0.0**
6-
7-
### ⚠ BREAKING CHANGES
8-
9-
* minimum supported `Node.js` version is `16.0.0`
10-
* default value of `exportLocalsConvention` is now `"asIs"`, even when the `namedExport` option is set
11-
125
### [6.8.1](https://github.com/webpack-contrib/css-loader/compare/v6.8.0...v6.8.1) (2023-05-28)
136

147

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,18 @@ Default: `false`
11171117

11181118
Enables/disables ES modules named export for locals.
11191119

1120+
> **Warning**
1121+
>
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.
1126+
1127+
> **Warning**
1128+
>
1129+
> It is not allowed to use JavaScript reserved words in css class names unless
1130+
> `exportLocalsConvention` is `"asIs"`.
1131+
11201132
**styles.css**
11211133

11221134
```css
@@ -1131,9 +1143,11 @@ Enables/disables ES modules named export for locals.
11311143
**index.js**
11321144

11331145
```js
1134-
import { "foo-baz" as fooBaz, bar } from "./styles.css";
1146+
import * as styles from "./styles.css";
11351147

1136-
console.log(fooBaz, bar);
1148+
console.log(styles.fooBaz, styles.bar);
1149+
// or if using `exportLocalsConvention: "asIs"`:
1150+
console.log(styles["foo-baz"], styles.bar);
11371151
```
11381152

11391153
You can enable a ES module named export using:

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-loader",
3-
"version": "7.0.0-pre",
3+
"version": "6.8.1",
44
"description": "css loader module for webpack",
55
"license": "MIT",
66
"repository": "webpack-contrib/css-loader",
@@ -13,7 +13,7 @@
1313
},
1414
"main": "dist/cjs.js",
1515
"engines": {
16-
"node": ">= 16.0.0"
16+
"node": ">= 12.13.0"
1717
},
1818
"scripts": {
1919
"start": "npm run build -- -w",

src/utils.js

+29-14
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,14 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
587587
// eslint-disable-next-line no-undefined
588588
getLocalIdent: undefined,
589589
namedExport: needNamedExport || false,
590-
exportLocalsConvention: "asIs",
590+
exportLocalsConvention:
591+
(rawModulesOptions.namedExport === true || needNamedExport) &&
592+
typeof rawModulesOptions.exportLocalsConvention === "undefined"
593+
? "camelCaseOnly"
594+
: "asIs",
591595
exportOnlyLocals: false,
592596
...rawModulesOptions,
597+
useExportsAs: rawModulesOptions.exportLocalsConvention === "asIs",
593598
};
594599

595600
let exportLocalsConventionType;
@@ -672,6 +677,17 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
672677
"The 'modules.namedExport' option requires the 'esModules' option to be enabled"
673678
);
674679
}
680+
681+
if (
682+
typeof exportLocalsConventionType === "string" &&
683+
exportLocalsConventionType !== "asIs" &&
684+
exportLocalsConventionType !== "camelCaseOnly" &&
685+
exportLocalsConventionType !== "dashesOnly"
686+
) {
687+
throw new Error(
688+
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"'
689+
);
690+
}
675691
}
676692

677693
return modulesOptions;
@@ -1150,25 +1166,24 @@ function getExportCode(
11501166
: new Set([names]);
11511167

11521168
for (const name of normalizedNames) {
1169+
const serializedValue = isTemplateLiteralSupported
1170+
? convertToTemplateLiteral(value)
1171+
: JSON.stringify(value);
11531172
if (options.modules.namedExport) {
1154-
identifierId += 1;
1155-
const id = `_${identifierId.toString(16)}`;
1156-
localsCode += `var ${id} = ${
1157-
isTemplateLiteralSupported
1158-
? convertToTemplateLiteral(value)
1159-
: JSON.stringify(value)
1160-
};\n`;
1161-
localsCode += `export { ${id} as ${JSON.stringify(name)} };\n`;
1173+
if (options.modules.useExportsAs) {
1174+
identifierId += 1;
1175+
const id = `_${identifierId.toString(16)}`;
1176+
localsCode += `var ${id} = ${serializedValue};\n`;
1177+
localsCode += `export { ${id} as ${JSON.stringify(name)} };\n`;
1178+
} else {
1179+
localsCode += `export var ${name} = ${serializedValue};\n`;
1180+
}
11621181
} else {
11631182
if (localsCode) {
11641183
localsCode += `,\n`;
11651184
}
11661185

1167-
localsCode += `\t${JSON.stringify(name)}: ${
1168-
isTemplateLiteralSupported
1169-
? convertToTemplateLiteral(value)
1170-
: JSON.stringify(value)
1171-
}`;
1186+
localsCode += `\t${JSON.stringify(name)}: ${serializedValue}`;
11721187
}
11731188
}
11741189
};

0 commit comments

Comments
 (0)