Skip to content

Commit c31faed

Browse files
committed
feat: add skipRuntimeLoading option to skip generating runtime code
1 parent cc64284 commit c31faed

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ module.exports = {
8383
| **[`insert`](#insert)** | `{String\|Function}` | `document.head.appendChild(linkTag);` | Inserts `<link>` at the given position |
8484
| **[`attributes`](#attributes)** | `{Object}` | `{}` | Adds custom attributes to tag |
8585
| **[`linkType`](#linkType)** | `{String\|Boolean}` | `text/css` | Allows loading asynchronous chunks with a custom link type |
86+
| **[`skipRuntimeLoading`](#skipruntimeloading)** | `{Boolean}` | `false` | Whether skip runtime loading asynchronous chunks |
8687
| **[`experimentalUseImportModule`](#experimentalUseImportModule)** | `{Boolean}` | `false` | Use an experimental webpack API to execute modules instead of child compilers |
8788

8889
#### `filename`
@@ -257,6 +258,36 @@ module.exports = {
257258
};
258259
```
259260

261+
#### `skipRuntimeLoading`
262+
263+
##### `Boolean`
264+
265+
An option to skip runtime loading asynchronous chunks by the current plugin, and developers can determine when to load by using other plugins.
266+
267+
`true` to skip.
268+
269+
**webpack.config.js**
270+
271+
```js
272+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
273+
274+
module.exports = {
275+
plugins: [
276+
new MiniCssExtractPlugin({
277+
skipRuntimeLoading: true,
278+
}),
279+
],
280+
module: {
281+
rules: [
282+
{
283+
test: /\.css$/i,
284+
use: [MiniCssExtractPlugin.loader, "css-loader"],
285+
},
286+
],
287+
},
288+
};
289+
```
290+
260291
#### `experimentalUseImportModule`
261292

262293
Use an experimental webpack API to execute modules instead of child compilers.

src/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ class MiniCssExtractPlugin {
554554
}
555555

556556
generate() {
557-
const { chunk, runtimeRequirements } = this;
557+
const { chunk, runtimeRequirements, runtimeOptions } = this;
558558
const {
559559
runtimeTemplate,
560560
outputOptions: { crossOriginLoading },
@@ -568,7 +568,7 @@ class MiniCssExtractPlugin {
568568
RuntimeGlobals.hmrDownloadUpdateHandlers
569569
);
570570

571-
if (!withLoading && !withHmr) {
571+
if (runtimeOptions.skipRuntimeLoading || (!withLoading && !withHmr)) {
572572
return null;
573573
}
574574

@@ -577,9 +577,9 @@ class MiniCssExtractPlugin {
577577
"chunkId, fullhref, resolve, reject",
578578
[
579579
'var linkTag = document.createElement("link");',
580-
this.runtimeOptions.attributes
580+
runtimeOptions.attributes
581581
? Template.asString(
582-
Object.entries(this.runtimeOptions.attributes).map(
582+
Object.entries(runtimeOptions.attributes).map(
583583
(entry) => {
584584
const [key, value] = entry;
585585
@@ -591,9 +591,9 @@ class MiniCssExtractPlugin {
591591
)
592592
: "",
593593
'linkTag.rel = "stylesheet";',
594-
this.runtimeOptions.linkType
594+
runtimeOptions.linkType
595595
? `linkTag.type = ${JSON.stringify(
596-
this.runtimeOptions.linkType
596+
runtimeOptions.linkType
597597
)};`
598598
: "",
599599
`var onLinkComplete = ${runtimeTemplate.basicFunction("event", [
@@ -627,11 +627,11 @@ class MiniCssExtractPlugin {
627627
"}",
628628
])
629629
: "",
630-
typeof this.runtimeOptions.insert !== "undefined"
631-
? typeof this.runtimeOptions.insert === "function"
632-
? `(${this.runtimeOptions.insert.toString()})(linkTag)`
630+
typeof runtimeOptions.insert !== "undefined"
631+
? typeof runtimeOptions.insert === "function"
632+
? `(${runtimeOptions.insert.toString()})(linkTag)`
633633
: Template.asString([
634-
`var target = document.querySelector("${this.runtimeOptions.insert}");`,
634+
`var target = document.querySelector("${runtimeOptions.insert}");`,
635635
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
636636
])
637637
: Template.asString(["document.head.appendChild(linkTag);"]),

src/plugin-options.json

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
],
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"
68+
},
69+
"skipRuntimeLoading": {
70+
"type": "boolean",
71+
"description": "An option to skip runtime loading asynchronous chunks by the current plugin, and developers can determine when to load by using other plugins.",
72+
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#experimentaluseimportmodule"
6873
}
6974
}
7075
}

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? }"
120+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
126+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
132+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
138+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
144+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
150+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
156+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
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? }"
162+
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
163163
`;

0 commit comments

Comments
 (0)