Skip to content

Commit 954cdb2

Browse files
cap-Bernarditoalexander-akait
authored andcommitted
feat: removed modules.namedexport option (#768)
BREAKING CHANGE: the `modules.namedExport` option was removed, you don't need it anymore, because we respect the `modules.namedExport` option from `css-loader`, just remove it from loader options
1 parent 684ad36 commit 954cdb2

File tree

9 files changed

+82
-152
lines changed

9 files changed

+82
-152
lines changed

README.md

Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ module.exports = {
296296
| **[`publicPath`](#publicPath)** | `{String\|Function}` | `webpackOptions.output.publicPath` | Specifies a custom public path for the external resources like images, files, etc |
297297
| **[`emit`](#emit)** | `{Boolean}` | `true` | If false, the plugin will extract the CSS but **will not** emit the file |
298298
| **[`esModule`](#esModule)** | `{Boolean}` | `true` | Use ES modules syntax |
299-
| **[`modules`](#modules)** | `{Object}` | `undefined` | Configuration CSS Modules |
300299

301300
#### `publicPath`
302301

@@ -422,85 +421,6 @@ module.exports = {
422421
};
423422
```
424423

425-
#### `modules`
426-
427-
Type: `Object`
428-
Default: `undefined`
429-
430-
Configuration CSS Modules.
431-
432-
##### `namedExport`
433-
434-
Type: `Boolean`
435-
Default: `false`
436-
437-
Enables/disables ES modules named export for locals.
438-
439-
> ⚠ Names of locals are converted to `camelCase`.
440-
441-
> ⚠ It is not allowed to use JavaScript reserved words in css class names.
442-
443-
> ⚠ Options `esModule` and `modules.namedExport` in `css-loader` and `MiniCssExtractPlugin.loader` should be enabled.
444-
445-
**styles.css**
446-
447-
```css
448-
.foo-baz {
449-
color: red;
450-
}
451-
.bar {
452-
color: blue;
453-
}
454-
```
455-
456-
**index.js**
457-
458-
```js
459-
import { fooBaz, bar } from './styles.css';
460-
461-
console.log(fooBaz, bar);
462-
```
463-
464-
You can enable a ES module named export using:
465-
466-
**webpack.config.js**
467-
468-
```js
469-
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
470-
471-
module.exports = {
472-
plugins: [new MiniCssExtractPlugin()],
473-
module: {
474-
rules: [
475-
{
476-
test: /\.css$/,
477-
use: [
478-
{
479-
loader: MiniCssExtractPlugin.loader,
480-
options: {
481-
esModule: true,
482-
modules: {
483-
namedExport: true,
484-
},
485-
},
486-
},
487-
{
488-
loader: 'css-loader',
489-
options: {
490-
esModule: true,
491-
modules: {
492-
namedExport: true,
493-
localIdentName: 'foo__[name]__[local]',
494-
},
495-
},
496-
},
497-
],
498-
},
499-
],
500-
},
501-
};
502-
```
503-
504424
## Examples
505425

506426
### Recommend
@@ -573,6 +493,67 @@ module.exports = {
573493
};
574494
```
575495

496+
### Named export for CSS Modules
497+
498+
> ⚠ Names of locals are converted to `camelCase`.
499+
500+
> ⚠ It is not allowed to use JavaScript reserved words in css class names.
501+
502+
> ⚠ Options `esModule` and `modules.namedExport` in `css-loader` should be enabled.
503+
504+
**styles.css**
505+
506+
```css
507+
.foo-baz {
508+
color: red;
509+
}
510+
.bar {
511+
color: blue;
512+
}
513+
```
514+
515+
**index.js**
516+
517+
```js
518+
import { fooBaz, bar } from './styles.css';
519+
520+
console.log(fooBaz, bar);
521+
```
522+
523+
You can enable a ES module named export using:
524+
525+
**webpack.config.js**
526+
527+
```js
528+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
529+
530+
module.exports = {
531+
plugins: [new MiniCssExtractPlugin()],
532+
module: {
533+
rules: [
534+
{
535+
test: /\.css$/,
536+
use: [
537+
{
538+
loader: MiniCssExtractPlugin.loader,
539+
},
540+
{
541+
loader: 'css-loader',
542+
options: {
543+
esModule: true,
544+
modules: {
545+
namedExport: true,
546+
localIdentName: 'foo__[name]__[local]',
547+
},
548+
},
549+
},
550+
],
551+
},
552+
],
553+
},
554+
};
555+
```
556+
576557
### The `publicPath` option as function
577558

578559
**webpack.config.js**

src/loader-options.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@
2020
},
2121
"layer": {
2222
"type": "string"
23-
},
24-
"modules": {
25-
"type": "object",
26-
"additionalProperties": false,
27-
"properties": {
28-
"namedExport": {
29-
"description": "Enables/disables ES modules named export for locals (https://webpack.js.org/plugins/mini-css-extract-plugin/#namedexport).",
30-
"type": "boolean"
31-
}
32-
}
3323
}
3424
}
3525
}

src/loader.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ export function pitch(request) {
5656

5757
const handleExports = (originalExports, compilation, assets, assetsInfo) => {
5858
let locals;
59+
let namedExport;
5960

6061
const esModule =
6162
typeof options.esModule !== 'undefined' ? options.esModule : true;
62-
const namedExport =
63-
esModule && options.modules && options.modules.namedExport;
64-
6563
const addDependencies = (dependencies) => {
6664
if (!Array.isArray(dependencies) && dependencies != null) {
6765
throw new Error(
@@ -102,6 +100,10 @@ export function pitch(request) {
102100
? originalExports.default
103101
: originalExports;
104102

103+
namedExport =
104+
// eslint-disable-next-line no-underscore-dangle
105+
originalExports.__esModule && !('locals' in originalExports.default);
106+
105107
if (namedExport) {
106108
Object.keys(originalExports).forEach((key) => {
107109
if (key !== 'default') {

test/__snapshots__/validate-loader-options.test.js.snap.webpack4

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ exports[`validate options should throw an error on the "esModule" option with "1
55
- options.esModule should be a boolean."
66
`;
77

8-
exports[`validate options should throw an error on the "modules" option with "{"namedExport":"false"}" value 1`] = `
9-
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
10-
- options.modules.namedExport should be a boolean.
11-
-> Enables/disables ES modules named export for locals (https://webpack.js.org/plugins/mini-css-extract-plugin/#namedexport)."
12-
`;
13-
14-
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
15-
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
16-
- options.modules should be an object:
17-
object { namedExport? }"
18-
`;
19-
208
exports[`validate options should throw an error on the "publicPath" option with "true" value 1`] = `
219
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
2210
- options.publicPath should be one of these:
@@ -29,47 +17,47 @@ exports[`validate options should throw an error on the "publicPath" option with
2917
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
3018
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
3119
- options has an unknown property 'unknown'. These properties are valid:
32-
object { publicPath?, emit?, esModule?, layer?, modules? }"
20+
object { publicPath?, emit?, esModule?, layer? }"
3321
`;
3422

3523
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
3624
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
3725
- options has an unknown property 'unknown'. These properties are valid:
38-
object { publicPath?, emit?, esModule?, layer?, modules? }"
26+
object { publicPath?, emit?, esModule?, layer? }"
3927
`;
4028

4129
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
4230
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
4331
- options has an unknown property 'unknown'. These properties are valid:
44-
object { publicPath?, emit?, esModule?, layer?, modules? }"
32+
object { publicPath?, emit?, esModule?, layer? }"
4533
`;
4634

4735
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
4836
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
4937
- options has an unknown property 'unknown'. These properties are valid:
50-
object { publicPath?, emit?, esModule?, layer?, modules? }"
38+
object { publicPath?, emit?, esModule?, layer? }"
5139
`;
5240

5341
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
5442
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
5543
- options has an unknown property 'unknown'. These properties are valid:
56-
object { publicPath?, emit?, esModule?, layer?, modules? }"
44+
object { publicPath?, emit?, esModule?, layer? }"
5745
`;
5846

5947
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
6048
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
6149
- options has an unknown property 'unknown'. These properties are valid:
62-
object { publicPath?, emit?, esModule?, layer?, modules? }"
50+
object { publicPath?, emit?, esModule?, layer? }"
6351
`;
6452

6553
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
6654
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
6755
- options has an unknown property 'unknown'. These properties are valid:
68-
object { publicPath?, emit?, esModule?, layer?, modules? }"
56+
object { publicPath?, emit?, esModule?, layer? }"
6957
`;
7058

7159
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
7260
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
7361
- options has an unknown property 'unknown'. These properties are valid:
74-
object { publicPath?, emit?, esModule?, layer?, modules? }"
62+
object { publicPath?, emit?, esModule?, layer? }"
7563
`;

test/__snapshots__/validate-loader-options.test.js.snap.webpack5

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ exports[`validate options should throw an error on the "esModule" option with "1
55
- options.esModule should be a boolean."
66
`;
77

8-
exports[`validate options should throw an error on the "modules" option with "{"namedExport":"false"}" value 1`] = `
9-
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
10-
- options.modules.namedExport should be a boolean.
11-
-> Enables/disables ES modules named export for locals (https://webpack.js.org/plugins/mini-css-extract-plugin/#namedexport)."
12-
`;
13-
14-
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
15-
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
16-
- options.modules should be an object:
17-
object { namedExport? }"
18-
`;
19-
208
exports[`validate options should throw an error on the "publicPath" option with "true" value 1`] = `
219
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
2210
- options.publicPath should be one of these:
@@ -29,47 +17,47 @@ exports[`validate options should throw an error on the "publicPath" option with
2917
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
3018
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
3119
- options has an unknown property 'unknown'. These properties are valid:
32-
object { publicPath?, emit?, esModule?, layer?, modules? }"
20+
object { publicPath?, emit?, esModule?, layer? }"
3321
`;
3422

3523
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
3624
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
3725
- options has an unknown property 'unknown'. These properties are valid:
38-
object { publicPath?, emit?, esModule?, layer?, modules? }"
26+
object { publicPath?, emit?, esModule?, layer? }"
3927
`;
4028

4129
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
4230
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
4331
- options has an unknown property 'unknown'. These properties are valid:
44-
object { publicPath?, emit?, esModule?, layer?, modules? }"
32+
object { publicPath?, emit?, esModule?, layer? }"
4533
`;
4634

4735
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
4836
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
4937
- options has an unknown property 'unknown'. These properties are valid:
50-
object { publicPath?, emit?, esModule?, layer?, modules? }"
38+
object { publicPath?, emit?, esModule?, layer? }"
5139
`;
5240

5341
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
5442
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
5543
- options has an unknown property 'unknown'. These properties are valid:
56-
object { publicPath?, emit?, esModule?, layer?, modules? }"
44+
object { publicPath?, emit?, esModule?, layer? }"
5745
`;
5846

5947
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
6048
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
6149
- options has an unknown property 'unknown'. These properties are valid:
62-
object { publicPath?, emit?, esModule?, layer?, modules? }"
50+
object { publicPath?, emit?, esModule?, layer? }"
6351
`;
6452

6553
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
6654
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
6755
- options has an unknown property 'unknown'. These properties are valid:
68-
object { publicPath?, emit?, esModule?, layer?, modules? }"
56+
object { publicPath?, emit?, esModule?, layer? }"
6957
`;
7058

7159
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
7260
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
7361
- options has an unknown property 'unknown'. These properties are valid:
74-
object { publicPath?, emit?, esModule?, layer?, modules? }"
62+
object { publicPath?, emit?, esModule?, layer? }"
7563
`;

test/cases/es-module-concatenation-modules/webpack.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ module.exports = {
1414
loader: Self.loader,
1515
options: {
1616
esModule: true,
17-
modules: {
18-
namedExport: true,
19-
},
2017
},
2118
},
2219
{

test/cases/es-named-export-output-module/webpack.config.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ module.exports = {
99
use: [
1010
{
1111
loader: Self.loader,
12-
options: {
13-
esModule: true,
14-
modules: {
15-
namedExport: true,
16-
},
17-
},
1812
},
1913
{
2014
loader: 'css-loader',

test/cases/es-named-export/webpack.config.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ module.exports = {
99
use: [
1010
{
1111
loader: Self.loader,
12-
options: {
13-
esModule: true,
14-
modules: {
15-
namedExport: true,
16-
},
17-
},
1812
},
1913
{
2014
loader: 'css-loader',

0 commit comments

Comments
 (0)