Skip to content

Commit 4aa23d5

Browse files
refactor: code
1 parent 0116433 commit 4aa23d5

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ Style of exported classnames.
766766

767767
By default, the exported JSON keys mirror the class names (i.e `asIs` value).
768768

769+
When `namedExport` is enabled, the `localsConvention` option is ignored.
770+
Do not use these options together.
771+
769772
| Name | Type | Description |
770773
| :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
771774
| **`'asIs'`** | `{String}` | Class names will be exported as is. |
@@ -924,6 +927,9 @@ Default: `false`
924927
Enable/disable ES modules named export for css classes.
925928
Names of exported classes are converted to camelCase.
926929

930+
When `namedExport` is enabled, the `localsConvention` option is ignored.
931+
Do not use these options together.
932+
927933
> i It is not allowed to use JavaScript reserved words in css class names
928934
929935
**styles.css**

src/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export default function loader(content, map, meta) {
4242
const urlHandler = (url) =>
4343
stringifyRequest(this, preRequester(options.importLoaders) + url);
4444

45+
const callback = this.async();
46+
4547
const esModule =
4648
typeof options.esModule !== 'undefined' ? options.esModule : true;
4749

@@ -51,11 +53,27 @@ export default function loader(content, map, meta) {
5153
modulesOptions = getModulesOptions(options, this);
5254

5355
if (modulesOptions.namedExport === true && esModule === false) {
54-
this.emitError(
56+
callback(
5557
new Error(
5658
'`Options.module.namedExport` cannot be used without `options.esModule`'
5759
)
5860
);
61+
62+
return;
63+
}
64+
65+
if (
66+
modulesOptions.namedExport === true &&
67+
modulesOptions.localsConvention !== 'asIs' &&
68+
modulesOptions.localsConvention !== 'camelCaseOnly'
69+
) {
70+
callback(
71+
new Error(
72+
'When `namedExport` is enabled class names are always converted to camelCase. Remove the `localsConvention` option from the webpack.config or set it to `asIs` / `camelCaseOnly`'
73+
)
74+
);
75+
76+
return;
5977
}
6078

6179
plugins.push(...getModulesPlugins(modulesOptions, this));
@@ -128,8 +146,6 @@ export default function loader(content, map, meta) {
128146
}
129147
}
130148

131-
const callback = this.async();
132-
133149
postcss(plugins)
134150
.process(content, {
135151
from: this.resourcePath,

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ exports[`"esModule" option should emit error when class has unsupported name: wa
1111

1212
exports[`"esModule" option should emit error when namedExport true && esModule false: errors 1`] = `
1313
Array [
14-
"ModuleError: Module Error (from \`replaced original path\`):
15-
\`Options.module.namedExport\` cannot be used without \`options.esModule\`",
14+
"ModuleBuildError: Module build failed (from \`replaced original path\`):
15+
Error: \`Options.module.namedExport\` cannot be used without \`options.esModule\`
16+
at Object.loader (/src/index.js:57:9)",
1617
]
1718
`;
1819

20+
exports[`"esModule" option should emit error when namedExport true && localsConvention invalid: errors 1`] = `
21+
Array [
22+
"ModuleBuildError: Module build failed (from \`replaced original path\`):
23+
Error: When \`namedExport\` is enabled class names are always converted to camelCase. Remove the \`localsConvention\` option from the webpack.config or set it to \`asIs\` / \`camelCaseOnly\`",
24+
]
25+
`;
26+
27+
exports[`"esModule" option should emit error when namedExport true && localsConvention invalid: warnings 1`] = `Array []`;
28+
1929
exports[`"esModule" option should work js template with "namedExport" option: errors 1`] = `Array []`;
2030

2131
exports[`"esModule" option should work js template with "namedExport" option: module 1`] = `

test/esModule-option.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,18 @@ describe('"esModule" option', () => {
321321

322322
expect(getErrors(stats)).toMatchSnapshot('errors');
323323
});
324+
325+
it('should emit error when namedExport true && localsConvention invalid', async () => {
326+
const compiler = getCompiler('./es-module/named/broken/index.js', {
327+
esModule: true,
328+
modules: {
329+
namedExport: true,
330+
localsConvention: 'dashes',
331+
},
332+
});
333+
const stats = await compile(compiler);
334+
335+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
336+
expect(getErrors(stats, true)).toMatchSnapshot('errors');
337+
});
324338
});

0 commit comments

Comments
 (0)