Skip to content

Commit 5732017

Browse files
committed
perf: add skipRuntimeLoading option to skip generating runtime code (webpack-contrib#256)
1 parent cc64284 commit 5732017

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ module.exports = {
7676
### Plugin Options
7777

7878
| Name | Type | Default | Description |
79-
| :---------------------------------------------------------------: | :------------------: | :-----------------------------------: | :---------------------------------------------------------------------------- |
79+
|:-----------------------------------------------------------------:|:--------------------:|:-------------------------------------:|:------------------------------------------------------------------------------|
8080
| **[`filename`](#filename)** | `{String\|Function}` | `[name].css` | This option determines the name of each output CSS file |
8181
| **[`chunkFilename`](#chunkFilename)** | `{String\|Function}` | `based on filename` | This option determines the name of non-entry chunk files |
8282
| **[`ignoreOrder`](#ignoreOrder)** | `{Boolean}` | `false` | Remove Order Warnings |
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);"]),

0 commit comments

Comments
 (0)