Skip to content

Commit 5cfb0ab

Browse files
refactor: code of the runtime option (webpack-contrib#832)
1 parent 5cc73e6 commit 5cfb0ab

14 files changed

+358
-46
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ module.exports = {
8787
| **[`insert`](#insert)** | `{String\|Function}` | `document.head.appendChild(linkTag);` | Inserts the `link` tag at the given position for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) CSS chunks |
8888
| **[`attributes`](#attributes)** | `{Object}` | `{}` | Adds custom attributes to the `link` tag for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) CSS chunks |
8989
| **[`linkType`](#linkType)** | `{String\|Boolean}` | `text/css` | Allows loading asynchronous chunks with a custom link type |
90-
| **[`noRuntime`](#linkType)** | `{Boolean}` | `false` | Skips the runtime generation |
90+
| **[`runtime`](#runtime)** | `{Boolean}` | `true` | Allows to enable/disable the runtime generation |
9191
| **[`experimentalUseImportModule`](#experimentalUseImportModule)** | `{Boolean}` | `false` | Use an experimental webpack API to execute modules instead of child compilers |
9292

9393
#### `filename`
@@ -266,22 +266,26 @@ module.exports = {
266266
};
267267
```
268268

269-
#### `noRuntime`
269+
#### `runtime`
270270

271-
##### `Boolean`
271+
Type: `Boolean`
272+
Default: `true`
272273

273-
An option to avoid the runtime generation. Assets are still extracted and can be used for a custom loading methods. For example, you can use [assets-webpack-plugin](https://github.com/ztoben/assets-webpack-plugin) to retreive them then use your own runtime code to download assets when needed.
274+
Allows to enable/disable the runtime generation.
275+
CSS will be still extracted and can be used for a custom loading methods.
276+
For example, you can use [assets-webpack-plugin](https://github.com/ztoben/assets-webpack-plugin) to retreive them then use your own runtime code to download assets when needed.
274277

275278
`true` to skip.
276279

277280
**webpack.config.js**
278281

279282
```js
280283
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
284+
281285
module.exports = {
282286
plugins: [
283287
new MiniCssExtractPlugin({
284-
skipRuntimeLoading: true,
288+
runtime: false,
285289
}),
286290
],
287291
module: {

src/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class MiniCssExtractPlugin {
337337
filename: DEFAULT_FILENAME,
338338
ignoreOrder: false,
339339
experimentalUseImportModule: false,
340-
noRuntime: false,
340+
runtime: true,
341341
},
342342
options
343343
);
@@ -521,13 +521,12 @@ class MiniCssExtractPlugin {
521521
}
522522
});
523523

524-
// All the code below is dedicated to the runtime and
525-
// can be skipped in a noRuntime context
526-
if (this.options.noRuntime) return;
527-
528-
const { Template } = webpack;
524+
// All the code below is dedicated to the runtime and can be skipped when the `runtime` option is `false`
525+
if (!this.options.runtime) {
526+
return;
527+
}
529528

530-
const { RuntimeGlobals, runtime } = webpack;
529+
const { Template, RuntimeGlobals, RuntimeModule, runtime } = webpack;
531530

532531
// eslint-disable-next-line no-shadow
533532
const getCssChunkObject = (mainChunk, compilation) => {
@@ -550,8 +549,6 @@ class MiniCssExtractPlugin {
550549
return obj;
551550
};
552551

553-
const { RuntimeModule } = webpack;
554-
555552
class CssLoadingRuntimeModule extends RuntimeModule {
556553
constructor(runtimeRequirements, runtimeOptions) {
557554
super("css loading", 10);

src/plugin-options.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@
6666
"description": "This option allows loading asynchronous chunks with a custom link type",
6767
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#linktype"
6868
},
69-
"noRuntime": {
69+
"runtime": {
7070
"type": "boolean",
71-
"description": "An option to avoid the runtime generation. Assets are still extracted and can be used for a custom loading methods.",
71+
"description": "Enabled/Disables runtime generation. CSS will be still extracted and can be used for a custom loading methods.",
7272
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#noRuntime"
7373
}
7474
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`noRuntime option should work when noRuntime option is "false": DOM 1`] = `
3+
exports[`noRuntime option should work when the 'runtime' option is 'false': DOM 1`] = `
44
"<!DOCTYPE html><html><head>
55
<title>style-loader test</title>
66
<style id=\\"existing-style\\">.existing { color: yellow }</style>
7-
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
7+
<script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
88
<body>
99
<h1>Body</h1>
1010
<div class=\\"target\\"></div>
@@ -14,15 +14,15 @@ exports[`noRuntime option should work when noRuntime option is "false": DOM 1`]
1414
</body></html>"
1515
`;
1616
17-
exports[`noRuntime option should work when noRuntime option is "false": errors 1`] = `Array []`;
17+
exports[`noRuntime option should work when the 'runtime' option is 'false': errors 1`] = `Array []`;
1818
19-
exports[`noRuntime option should work when noRuntime option is "false": warnings 1`] = `Array []`;
19+
exports[`noRuntime option should work when the 'runtime' option is 'false': warnings 1`] = `Array []`;
2020
21-
exports[`noRuntime option should work when noRuntime option is "true": DOM 1`] = `
21+
exports[`noRuntime option should work when the 'runtime' option is 'true': DOM 1`] = `
2222
"<!DOCTYPE html><html><head>
2323
<title>style-loader test</title>
2424
<style id=\\"existing-style\\">.existing { color: yellow }</style>
25-
<script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
25+
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"simple.css\\"><script charset=\\"utf-8\\" src=\\"simple.bundle.js\\"></script></head>
2626
<body>
2727
<h1>Body</h1>
2828
<div class=\\"target\\"></div>
@@ -32,11 +32,11 @@ exports[`noRuntime option should work when noRuntime option is "true": DOM 1`] =
3232
</body></html>"
3333
`;
3434
35-
exports[`noRuntime option should work when noRuntime option is "true": errors 1`] = `Array []`;
35+
exports[`noRuntime option should work when the 'runtime' option is 'true': errors 1`] = `Array []`;
3636
37-
exports[`noRuntime option should work when noRuntime option is "true": warnings 1`] = `Array []`;
37+
exports[`noRuntime option should work when the 'runtime' option is 'true': warnings 1`] = `Array []`;
3838
39-
exports[`noRuntime option should work without noRuntime option: DOM 1`] = `
39+
exports[`noRuntime option should work without the 'runtime' option: DOM 1`] = `
4040
"<!DOCTYPE html><html><head>
4141
<title>style-loader test</title>
4242
<style id=\\"existing-style\\">.existing { color: yellow }</style>
@@ -50,6 +50,6 @@ exports[`noRuntime option should work without noRuntime option: DOM 1`] = `
5050
</body></html>"
5151
`;
5252
53-
exports[`noRuntime option should work without noRuntime option: errors 1`] = `Array []`;
53+
exports[`noRuntime option should work without the 'runtime' option: errors 1`] = `Array []`;
5454
55-
exports[`noRuntime option should work without noRuntime option: warnings 1`] = `Array []`;
55+
exports[`noRuntime option should work without the 'runtime' option: warnings 1`] = `Array []`;

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -117,47 +117,47 @@ exports[`validate options should throw an error on the "linkType" option with "i
117117
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
118118
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
119119
- options has an unknown property 'unknown'. These properties are valid:
120-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
120+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
121121
`;
122122
123123
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
124124
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
125125
- options has an unknown property 'unknown'. These properties are valid:
126-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
126+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
127127
`;
128128
129129
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
130130
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
131131
- options has an unknown property 'unknown'. These properties are valid:
132-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
132+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
133133
`;
134134
135135
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
136136
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
137137
- options has an unknown property 'unknown'. These properties are valid:
138-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
138+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
139139
`;
140140
141141
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
142142
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
143143
- options has an unknown property 'unknown'. These properties are valid:
144-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
144+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
145145
`;
146146
147147
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
148148
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
149149
- options has an unknown property 'unknown'. These properties are valid:
150-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
150+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
151151
`;
152152
153153
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
154154
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
155155
- options has an unknown property 'unknown'. These properties are valid:
156-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
156+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
157157
`;
158158
159159
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
160160
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
161161
- options has an unknown property 'unknown'. These properties are valid:
162-
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, noRuntime? }"
162+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, runtime? }"
163163
`;

test/cases/no-runtime/async.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.async {
2+
color: red;
3+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.async {
2+
color: red;
3+
}
4+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
(self["webpackChunk"] = self["webpackChunk"] || []).push([[0],{
3+
4+
/***/ 2:
5+
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
6+
7+
__webpack_require__.r(__webpack_exports__);
8+
// extracted by mini-css-extract-plugin
9+
10+
11+
/***/ })
12+
13+
}]);
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+

0 commit comments

Comments
 (0)