Skip to content

Commit 596e47a

Browse files
feat: new esModule option (#475)
1 parent 78e1613 commit 596e47a

File tree

11 files changed

+159
-2
lines changed

11 files changed

+159
-2
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,49 @@ Default: the `publicPath` in `webpackOptions.output`
4141

4242
Specifies a custom public path for the target file(s).
4343

44+
#### `esModule`
45+
46+
Type: `Boolean`
47+
Default: `false`
48+
49+
By default, `mini-css-extract-plugin` generates JS modules that use the CommonJS modules syntax.
50+
There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
51+
52+
You can enable a ES module syntax using:
53+
54+
**webpack.config.js**
55+
56+
```js
57+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
58+
59+
module.exports = {
60+
plugins: [new MiniCssExtractPlugin()],
61+
module: {
62+
rules: [
63+
{
64+
test: /\.css$/i,
65+
use: [
66+
{
67+
loader: MiniCssExtractPlugin.loader,
68+
options: {
69+
esModule: true,
70+
},
71+
},
72+
'css-loader',
73+
],
74+
},
75+
],
76+
},
77+
};
78+
```
79+
4480
#### Minimal example
4581

4682
**webpack.config.js**
4783

4884
```js
4985
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
86+
5087
module.exports = {
5188
plugins: [
5289
new MiniCssExtractPlugin({

src/loader.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,16 @@ export function pitch(request) {
203203
return callback(e);
204204
}
205205

206-
let resultSource = `// extracted by ${pluginName}`;
206+
const esModule =
207+
typeof options.esModule !== 'undefined' ? options.esModule : false;
207208
const result = locals
208-
? `\nmodule.exports = ${JSON.stringify(locals)};`
209+
? `\n${esModule ? 'export default' : 'module.exports ='} ${JSON.stringify(
210+
locals
211+
)};`
209212
: '';
210213

214+
let resultSource = `// extracted by ${pluginName}`;
215+
211216
resultSource += options.hmr
212217
? hotLoader(result, { context: this.context, options, locals })
213218
: result;

src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"instanceof": "Function"
1111
}
1212
]
13+
},
14+
"esModule": {
15+
"type": "boolean"
1316
}
1417
},
1518
"errorMessages": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo__style__a {
2+
background: red;
3+
}
4+
5+
.foo__style__b {
6+
color: green;
7+
}
8+
9+
.c {
10+
color: blue;
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a {
2+
background: red;
3+
}
4+
5+
:local(.b) {
6+
color: green;
7+
}
8+
9+
:global(.c) {
10+
color: blue;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
{
11+
loader: Self.loader,
12+
options: { esModule: false },
13+
},
14+
{
15+
loader: 'css-loader',
16+
options: {
17+
modules: {
18+
mode: 'local',
19+
localIdentName: 'foo__[name]__[local]',
20+
},
21+
},
22+
},
23+
],
24+
},
25+
],
26+
},
27+
plugins: [
28+
new Self({
29+
filename: '[name].css',
30+
}),
31+
],
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo__style__a {
2+
background: red;
3+
}
4+
5+
.foo__style__b {
6+
color: green;
7+
}
8+
9+
.c {
10+
color: blue;
11+
}
12+

test/cases/es-module-syntax/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';

test/cases/es-module-syntax/style.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a {
2+
background: red;
3+
}
4+
5+
:local(.b) {
6+
color: green;
7+
}
8+
9+
:global(.c) {
10+
color: blue;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
{
11+
loader: Self.loader,
12+
options: { esModule: true },
13+
},
14+
{
15+
loader: 'css-loader',
16+
options: {
17+
modules: {
18+
mode: 'local',
19+
localIdentName: 'foo__[name]__[local]',
20+
},
21+
},
22+
},
23+
],
24+
},
25+
],
26+
},
27+
plugins: [
28+
new Self({
29+
filename: '[name].css',
30+
}),
31+
],
32+
};

0 commit comments

Comments
 (0)