Skip to content

Commit fe1faa3

Browse files
refactor: code
1 parent 9e1c005 commit fe1faa3

File tree

7 files changed

+86
-12
lines changed

7 files changed

+86
-12
lines changed

README.md

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

411412
#### `publicPath`
412413

@@ -549,6 +550,60 @@ module.exports = {
549550
};
550551
```
551552

553+
#### `defaultExport`
554+
555+
Type:
556+
557+
```ts
558+
type defaultExport = boolean;
559+
```
560+
561+
Default: `false`
562+
563+
> **Note**
564+
>
565+
> This options will work only when you set `namedExport` to `true` in `css-loader`
566+
567+
By default, `mini-css-extract-plugin` generates JS modules based on the `esModule` and `namedExport` options in `css-loader`.
568+
Using the `esModule` and `namedExport` options will allow you to better optimize your code.
569+
If you set `esModule: true` and `namedExport: true` for `css-loader` `mini-css-extract-plugin` will generate **only** a named export.
570+
Our official recommendation is to use only named export for better future compatibility.
571+
But for some applications, it is not easy to quickly rewrite the code from the default export to a named export.
572+
573+
In case you need both default and named exports, you can enable this option:
574+
575+
**webpack.config.js**
576+
577+
```js
578+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
579+
580+
module.exports = {
581+
plugins: [new MiniCssExtractPlugin()],
582+
module: {
583+
rules: [
584+
{
585+
test: /\.css$/i,
586+
use: [
587+
{
588+
loader: MiniCssExtractPlugin.loader,
589+
options: {
590+
defaultExport: true,
591+
},
592+
},
593+
{
594+
loader: "css-loader",
595+
esModule: true,
596+
modules: {
597+
namedExport: true,
598+
},
599+
},
600+
],
601+
},
602+
],
603+
},
604+
};
605+
```
606+
552607
## Examples
553608

554609
### Recommended

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
* @property {boolean} [emit]
3838
* @property {boolean} [esModule]
3939
* @property {string} [layer]
40+
* @property {boolean} [defaultExport]
4041
*/
4142

4243
/**

src/loader-options.json

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
},
2828
"layer": {
2929
"type": "string"
30+
},
31+
"defaultExport": {
32+
"type": "boolean",
33+
"description": "Duplicate the named export with CSS modules locals to the default export (only when `esModules: true` for css-loader).",
34+
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#defaultexports"
3035
}
3136
}
3237
}

src/loader.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,17 @@ function pitch(request) {
271271
const exportsString = `export { ${identifiers
272272
.map(([id, key]) => `${id} as ${JSON.stringify(key)}`)
273273
.join(", ")} }`;
274-
const exportDefaultString = `export default { ${identifiers
275-
.map(([id, key]) => `${JSON.stringify(key)}: ${id}`)
276-
.join(", ")} }`;
277-
return `${localsString}\n${exportsString}\n${exportDefaultString}\n`;
274+
275+
const defaultExport =
276+
typeof options.defaultExport !== "undefined"
277+
? options.defaultExport
278+
: false;
279+
280+
return defaultExport
281+
? `${localsString}\n${exportsString}\nexport default { ${identifiers
282+
.map(([id, key]) => `${JSON.stringify(key)}: ${id}`)
283+
.join(", ")} }\n`
284+
: `${localsString}\n${exportsString}\n`;
278285
}
279286

280287
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?, layer? }"
24+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
30+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
36+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
42+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
48+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
54+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
60+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
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?, layer? }"
66+
object { publicPath?, emit?, esModule?, layer?, defaultExport? }"
6767
`;

test/validate-loader-options.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ describe("validate options", () => {
1010
success: [true, false],
1111
failure: [1],
1212
},
13+
defaultExport: {
14+
success: [true, false],
15+
failure: [1],
16+
},
1317
unknown: {
1418
success: [],
1519
failure: [1, true, false, "test", /test/, [], {}, { foo: "bar" }],

types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ type PluginOptions = {
148148
* @property {boolean} [emit]
149149
* @property {boolean} [esModule]
150150
* @property {string} [layer]
151+
* @property {boolean} [defaultExport]
151152
*/
152153
/**
153154
* @typedef {Object} PluginOptions
@@ -200,6 +201,7 @@ type LoaderOptions = {
200201
emit?: boolean | undefined;
201202
esModule?: boolean | undefined;
202203
layer?: string | undefined;
204+
defaultExport?: boolean | undefined;
203205
};
204206
type NormalizedPluginOptions = {
205207
filename: Required<Configuration>["output"]["filename"];

0 commit comments

Comments
 (0)