Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add skipRuntimeLoading option to skip generating runtime code #807

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.exports = {
| **[`insert`](#insert)** | `{String\|Function}` | `document.head.appendChild(linkTag);` | Inserts `<link>` at the given position |
| **[`attributes`](#attributes)** | `{Object}` | `{}` | Adds custom attributes to tag |
| **[`linkType`](#linkType)** | `{String\|Boolean}` | `text/css` | Allows loading asynchronous chunks with a custom link type |
| **[`skipRuntimeLoading`](#skipruntimeloading)** | `{Boolean}` | `false` | Whether skip runtime loading asynchronous chunks |
| **[`experimentalUseImportModule`](#experimentalUseImportModule)** | `{Boolean}` | `false` | Use an experimental webpack API to execute modules instead of child compilers |

#### `filename`
Expand Down Expand Up @@ -257,6 +258,36 @@ module.exports = {
};
```

#### `skipRuntimeLoading`

##### `Boolean`

An option to skip runtime loading asynchronous chunks by the current plugin, and developers can determine when to load by using other plugins.

`true` to skip.

**webpack.config.js**

```js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
plugins: [
new MiniCssExtractPlugin({
skipRuntimeLoading: true,
}),
],
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
};
```

#### `experimentalUseImportModule`

Use an experimental webpack API to execute modules instead of child compilers.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"prebuild": "npm run clean",
"build": "cross-env NODE_ENV=production babel src -d dist --ignore \"src/**/*.test.js\" --copy-files",
"postbuild": "es-check es5 dist/hmr/hotModuleReplacement.js",
"clean": "del-cli dist",
"clean": "node -p \"require('fs').existsSync('dist') && require('fs').rmdirSync('dist', {recursive: true})\"",
"commitlint": "commitlint --from=master",
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
"lint:js": "eslint --cache .",
Expand Down Expand Up @@ -57,7 +57,6 @@
"cross-env": "^7.0.3",
"css-loader": "^5.2.6",
"del": "^6.0.0",
"del-cli": "^4.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't touch this

Copy link
Author

@aleen42 aleen42 Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The library has broken under Windows. sindresorhus/del-cli#13

"es-check": "^6.0.0",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.1.0",
Expand Down
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class MiniCssExtractPlugin {
}

generate() {
const { chunk, runtimeRequirements } = this;
const { chunk, runtimeRequirements, runtimeOptions } = this;
const {
runtimeTemplate,
outputOptions: { crossOriginLoading },
Expand All @@ -568,7 +568,7 @@ class MiniCssExtractPlugin {
RuntimeGlobals.hmrDownloadUpdateHandlers
);

if (!withLoading && !withHmr) {
if (runtimeOptions.skipRuntimeLoading || (!withLoading && !withHmr)) {
return null;
}

Expand All @@ -577,9 +577,9 @@ class MiniCssExtractPlugin {
"chunkId, fullhref, resolve, reject",
[
'var linkTag = document.createElement("link");',
this.runtimeOptions.attributes
runtimeOptions.attributes
? Template.asString(
Object.entries(this.runtimeOptions.attributes).map(
Object.entries(runtimeOptions.attributes).map(
(entry) => {
const [key, value] = entry;

Expand All @@ -591,9 +591,9 @@ class MiniCssExtractPlugin {
)
: "",
'linkTag.rel = "stylesheet";',
this.runtimeOptions.linkType
runtimeOptions.linkType
? `linkTag.type = ${JSON.stringify(
this.runtimeOptions.linkType
runtimeOptions.linkType
)};`
: "",
`var onLinkComplete = ${runtimeTemplate.basicFunction("event", [
Expand Down Expand Up @@ -627,11 +627,11 @@ class MiniCssExtractPlugin {
"}",
])
: "",
typeof this.runtimeOptions.insert !== "undefined"
? typeof this.runtimeOptions.insert === "function"
? `(${this.runtimeOptions.insert.toString()})(linkTag)`
typeof runtimeOptions.insert !== "undefined"
? typeof runtimeOptions.insert === "function"
? `(${runtimeOptions.insert.toString()})(linkTag)`
: Template.asString([
`var target = document.querySelector("${this.runtimeOptions.insert}");`,
`var target = document.querySelector("${runtimeOptions.insert}");`,
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
])
: Template.asString(["document.head.appendChild(linkTag);"]),
Expand Down
5 changes: 5 additions & 0 deletions src/plugin-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
],
"description": "This option allows loading asynchronous chunks with a custom link type",
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#linktype"
},
"skipRuntimeLoading": {
"type": "boolean",
"description": "An option to skip runtime loading asynchronous chunks by the current plugin, and developers can determine when to load by using other plugins.",
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#experimentaluseimportmodule"
}
}
}
16 changes: 8 additions & 8 deletions test/__snapshots__/validate-plugin-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -117,47 +117,47 @@ exports[`validate options should throw an error on the "linkType" option with "i
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;

exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
- options has an unknown property 'unknown'. These properties are valid:
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType? }"
object { filename?, chunkFilename?, experimentalUseImportModule?, ignoreOrder?, insert?, attributes?, linkType?, skipRuntimeLoading? }"
`;