Skip to content

Commit 023d85c

Browse files
committed
fix: improve esMododule exports
1 parent 1787dab commit 023d85c

File tree

18 files changed

+37
-229
lines changed

18 files changed

+37
-229
lines changed

README.md

-42
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ module.exports = {
407407
- **[`publicPath`](#publicPath)**
408408
- **[`emit`](#emit)**
409409
- **[`esModule`](#esModule)**
410-
- **[`defaultExport`](#defaultExport)**
411410

412411
#### `publicPath`
413412

@@ -550,47 +549,6 @@ module.exports = {
550549
};
551550
```
552551

553-
#### `defaultExport`
554-
555-
Type:
556-
557-
```ts
558-
type defaultExport = boolean;
559-
```
560-
561-
Default: `false`
562-
563-
By default, `mini-css-extract-plugin` generates JS modules with a default export.
564-
However for name exports, each local is exported as a named export.
565-
566-
In case you need both default and named exports, you can enable this option:
567-
568-
**webpack.config.js**
569-
570-
```js
571-
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
572-
573-
module.exports = {
574-
plugins: [new MiniCssExtractPlugin()],
575-
module: {
576-
rules: [
577-
{
578-
test: /\.css$/i,
579-
use: [
580-
{
581-
loader: MiniCssExtractPlugin.loader,
582-
options: {
583-
defaultExport: true,
584-
},
585-
},
586-
"css-loader",
587-
],
588-
},
589-
],
590-
},
591-
};
592-
```
593-
594552
## Examples
595553

596554
### Recommended

src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const {
3636
* @property {string | ((resourcePath: string, rootContext: string) => string)} [publicPath]
3737
* @property {boolean} [emit]
3838
* @property {boolean} [esModule]
39-
* @property {boolean} [defaultExport]
4039
* @property {string} [layer]
4140
*/
4241

src/loader-options.json

-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
"description": "Generates JS modules that use the ES modules syntax.",
2626
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#esmodule"
2727
},
28-
"defaultExport": {
29-
"type": "boolean",
30-
"description": "Generates JS modules with the default export syntax even for named exports.",
31-
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#defaultexports"
32-
},
3328
"layer": {
3429
"type": "string"
3530
}

src/loader.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ function pitch(request) {
124124
const esModule =
125125
typeof options.esModule !== "undefined" ? options.esModule : true;
126126

127-
const defaultExport =
128-
typeof options.defaultExport !== "undefined"
129-
? options.defaultExport
130-
: false;
131-
132127
/**
133128
* @param {Dependency[] | [null, object][]} dependencies
134129
*/
@@ -276,10 +271,10 @@ function pitch(request) {
276271
const exportsString = `export { ${identifiers
277272
.map(([id, key]) => `${id} as ${JSON.stringify(key)}`)
278273
.join(", ")} }`;
279-
const exportDefaultString = defaultExport
280-
? `export default ${JSON.stringify(locals)}\n`
281-
: "";
282-
return `${localsString}\n${exportsString}\n${exportDefaultString}`;
274+
const exportDefaultString = `export default ${JSON.stringify(
275+
locals
276+
)}`;
277+
return `${localsString}\n${exportsString}\n${exportDefaultString}\n`;
283278
}
284279

285280
return `\n${

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,47 @@ exports[`validate options should throw an error on the "publicPath" option with
2121
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
2222
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
2323
- options has an unknown property 'unknown'. These properties are valid:
24-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
24+
object { publicPath?, emit?, esModule?, layer? }"
2525
`;
2626
2727
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
2828
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
2929
- options has an unknown property 'unknown'. These properties are valid:
30-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
30+
object { publicPath?, emit?, esModule?, layer? }"
3131
`;
3232
3333
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
3434
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
3535
- options has an unknown property 'unknown'. These properties are valid:
36-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
36+
object { publicPath?, emit?, esModule?, layer? }"
3737
`;
3838
3939
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
4040
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
4141
- options has an unknown property 'unknown'. These properties are valid:
42-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
42+
object { publicPath?, emit?, esModule?, layer? }"
4343
`;
4444
4545
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
4646
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
4747
- options has an unknown property 'unknown'. These properties are valid:
48-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
48+
object { publicPath?, emit?, esModule?, layer? }"
4949
`;
5050
5151
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
5252
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
5353
- options has an unknown property 'unknown'. These properties are valid:
54-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
54+
object { publicPath?, emit?, esModule?, layer? }"
5555
`;
5656
5757
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
5858
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
5959
- options has an unknown property 'unknown'. These properties are valid:
60-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
60+
object { publicPath?, emit?, esModule?, layer? }"
6161
`;
6262
6363
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
6464
"Invalid options object. Mini CSS Extract Plugin Loader has been initialized using an options object that does not match the API schema.
6565
- options has an unknown property 'unknown'. These properties are valid:
66-
object { publicPath?, emit?, esModule?, defaultExport?, layer? }"
66+
object { publicPath?, emit?, esModule?, layer? }"
6767
`;

test/cases/custom-loader-with-functional-exports/expected/main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
__webpack_require__.r(__webpack_exports__);
99
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
1010
/* harmony export */ cnA: () => (/* binding */ _1),
11-
/* harmony export */ cnB: () => (/* binding */ _2)
11+
/* harmony export */ cnB: () => (/* binding */ _2),
12+
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
1213
/* harmony export */ });
1314
// extracted by mini-css-extract-plugin
1415
var _1 = () => "class-name-a";
1516
var _2 = () => "class-name-b";
1617

18+
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({});
1719

1820

1921
/***/ })

test/cases/es-module-concatenation-modules/expected/main.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ __webpack_require__.d(__webpack_exports__, {
4848
var a_namespaceObject = {};
4949
__webpack_require__.r(a_namespaceObject);
5050
__webpack_require__.d(a_namespaceObject, {
51-
a: () => (_1)
51+
a: () => (_1),
52+
"default": () => (a)
5253
});
5354

5455
// NAMESPACE OBJECT: ./b.css
5556
var b_namespaceObject = {};
5657
__webpack_require__.r(b_namespaceObject);
5758
__webpack_require__.d(b_namespaceObject, {
58-
b: () => (b_1)
59+
b: () => (b_1),
60+
"default": () => (b)
5961
});
6062

6163
// NAMESPACE OBJECT: ./index.js
@@ -71,16 +73,19 @@ __webpack_require__.d(index_namespaceObject, {
7173
// extracted by mini-css-extract-plugin
7274
var _1 = "foo__a";
7375

76+
/* harmony default export */ const a = ({"a":"foo__a"});
7477

7578
;// CONCATENATED MODULE: ./b.css
7679
// extracted by mini-css-extract-plugin
7780
var b_1 = "foo__b";
7881

82+
/* harmony default export */ const b = ({"b":"foo__b"});
7983

8084
;// CONCATENATED MODULE: ./c.css
8185
// extracted by mini-css-extract-plugin
8286
var c_1 = "foo__c";
8387

88+
/* harmony default export */ const c = ({"c":"foo__c"});
8489

8590
;// CONCATENATED MODULE: ./index.js
8691
/* eslint-disable import/no-namespace */

test/cases/es-named-export-as-is-output-module/expected/main.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ __webpack_require__.r(__webpack_exports__);
77
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
88
/* harmony export */ "a-class": () => (/* binding */ _1),
99
/* harmony export */ b__class: () => (/* binding */ _2),
10-
/* harmony export */ cClass: () => (/* binding */ _3)
10+
/* harmony export */ cClass: () => (/* binding */ _3),
11+
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
1112
/* harmony export */ });
1213
// extracted by mini-css-extract-plugin
1314
var _1 = "Xh041yLR4iCP4RGjge50";
1415
var _2 = "NMuRsxoDwvW8BhSXhFAY";
1516
var _3 = "ayWIv09rPsAqE2JznIsI";
1617

18+
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({"a-class":"Xh041yLR4iCP4RGjge50","b__class":"NMuRsxoDwvW8BhSXhFAY","cClass":"ayWIv09rPsAqE2JznIsI"});
1719

1820

1921
/***/ })

test/cases/es-named-export-as-is/expected/main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ __webpack_require__.r(__webpack_exports__);
99
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
1010
/* harmony export */ "a-class": () => (/* binding */ _1),
1111
/* harmony export */ b__class: () => (/* binding */ _2),
12-
/* harmony export */ cClass: () => (/* binding */ _3)
12+
/* harmony export */ cClass: () => (/* binding */ _3),
13+
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
1314
/* harmony export */ });
1415
// extracted by mini-css-extract-plugin
1516
var _1 = "Xh041yLR4iCP4RGjge50";
1617
var _2 = "NMuRsxoDwvW8BhSXhFAY";
1718
var _3 = "ayWIv09rPsAqE2JznIsI";
1819

20+
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({"a-class":"Xh041yLR4iCP4RGjge50","b__class":"NMuRsxoDwvW8BhSXhFAY","cClass":"ayWIv09rPsAqE2JznIsI"});
1921

2022

2123
/***/ })

test/cases/es-named-export-output-module/expected/main.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ __webpack_require__.r(__webpack_exports__);
77
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
88
/* harmony export */ aClass: () => (/* binding */ _1),
99
/* harmony export */ bClass: () => (/* binding */ _2),
10-
/* harmony export */ cClass: () => (/* binding */ _3)
10+
/* harmony export */ cClass: () => (/* binding */ _3),
11+
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
1112
/* harmony export */ });
1213
// extracted by mini-css-extract-plugin
1314
var _1 = "foo__style__a-class";
1415
var _2 = "foo__style__b__class";
1516
var _3 = "foo__style__cClass";
1617

18+
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({"aClass":"foo__style__a-class","bClass":"foo__style__b__class","cClass":"foo__style__cClass"});
1719

1820

1921
/***/ })

test/cases/es-named-export-with-default/expected/main.css

-12
This file was deleted.

test/cases/es-named-export-with-default/expected/main.js

-91
This file was deleted.

test/cases/es-named-export-with-default/index.js

-4
This file was deleted.

test/cases/es-named-export-with-default/style.css

-11
This file was deleted.

0 commit comments

Comments
 (0)