From e95663e310cec3e2fe4d4ed8e70904cbef3a08db Mon Sep 17 00:00:00 2001
From: evilebottnawi
Date: Sat, 11 Sep 2021 18:46:35 +0300
Subject: [PATCH] feat: refactor the `runtime` option
---
README.md | 14 +-
src/index.js | 15 +-
src/plugin-options.json | 4 +-
...st.js.snap => runtime-option.test.js.snap} | 22 +-
.../validate-plugin-options.test.js.snap | 16 +-
test/cases/no-runtime/async.css | 3 +
test/cases/no-runtime/expected/async.css | 4 +
test/cases/no-runtime/expected/async.js | 13 +
test/cases/no-runtime/expected/main.css | 4 +
test/cases/no-runtime/expected/main.js | 262 ++++++++++++++++++
test/cases/no-runtime/index.js | 3 +
test/cases/no-runtime/style.css | 3 +
test/cases/no-runtime/webpack.config.js | 19 ++
...-option.test.js => runtime-option.test.js} | 22 +-
14 files changed, 358 insertions(+), 46 deletions(-)
rename test/__snapshots__/{noRuntime-option.test.js.snap => runtime-option.test.js.snap} (57%)
create mode 100644 test/cases/no-runtime/async.css
create mode 100644 test/cases/no-runtime/expected/async.css
create mode 100644 test/cases/no-runtime/expected/async.js
create mode 100644 test/cases/no-runtime/expected/main.css
create mode 100644 test/cases/no-runtime/expected/main.js
create mode 100644 test/cases/no-runtime/index.js
create mode 100644 test/cases/no-runtime/style.css
create mode 100644 test/cases/no-runtime/webpack.config.js
rename test/{noRuntime-option.test.js => runtime-option.test.js} (91%)
diff --git a/README.md b/README.md
index fab71bbd..4f2edb57 100644
--- a/README.md
+++ b/README.md
@@ -87,7 +87,7 @@ module.exports = {
| **[`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 |
| **[`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 |
| **[`linkType`](#linkType)** | `{String\|Boolean}` | `text/css` | Allows loading asynchronous chunks with a custom link type |
-| **[`noRuntime`](#linkType)** | `{Boolean}` | `false` | Skips the runtime generation |
+| **[`runtime`](#runtime)** | `{Boolean}` | `true` | Allows to enable/disable the runtime generation |
| **[`experimentalUseImportModule`](#experimentalUseImportModule)** | `{Boolean}` | `false` | Use an experimental webpack API to execute modules instead of child compilers |
#### `filename`
@@ -266,11 +266,14 @@ module.exports = {
};
```
-#### `noRuntime`
+#### `runtime`
-##### `Boolean`
+Type: `Boolean`
+Default: `true`
-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.
+Allows to enable/disable the runtime generation.
+CSS will be 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.
`true` to skip.
@@ -278,10 +281,11 @@ An option to avoid the runtime generation. Assets are still extracted and can be
```js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
+
module.exports = {
plugins: [
new MiniCssExtractPlugin({
- skipRuntimeLoading: true,
+ runtime: false,
}),
],
module: {
diff --git a/src/index.js b/src/index.js
index 1378946d..9dd313be 100644
--- a/src/index.js
+++ b/src/index.js
@@ -337,7 +337,7 @@ class MiniCssExtractPlugin {
filename: DEFAULT_FILENAME,
ignoreOrder: false,
experimentalUseImportModule: false,
- noRuntime: false,
+ runtime: true,
},
options
);
@@ -521,13 +521,12 @@ class MiniCssExtractPlugin {
}
});
- // All the code below is dedicated to the runtime and
- // can be skipped in a noRuntime context
- if (this.options.noRuntime) return;
-
- const { Template } = webpack;
+ // All the code below is dedicated to the runtime and can be skipped when the `runtime` option is `false`
+ if (!this.options.runtime) {
+ return;
+ }
- const { RuntimeGlobals, runtime } = webpack;
+ const { Template, RuntimeGlobals, RuntimeModule, runtime } = webpack;
// eslint-disable-next-line no-shadow
const getCssChunkObject = (mainChunk, compilation) => {
@@ -550,8 +549,6 @@ class MiniCssExtractPlugin {
return obj;
};
- const { RuntimeModule } = webpack;
-
class CssLoadingRuntimeModule extends RuntimeModule {
constructor(runtimeRequirements, runtimeOptions) {
super("css loading", 10);
diff --git a/src/plugin-options.json b/src/plugin-options.json
index 7d70379d..8da511d4 100644
--- a/src/plugin-options.json
+++ b/src/plugin-options.json
@@ -66,9 +66,9 @@
"description": "This option allows loading asynchronous chunks with a custom link type",
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#linktype"
},
- "noRuntime": {
+ "runtime": {
"type": "boolean",
- "description": "An option to avoid the runtime generation. Assets are still extracted and can be used for a custom loading methods.",
+ "description": "Enabled/Disables runtime generation. CSS will be still extracted and can be used for a custom loading methods.",
"link": "https://github.com/webpack-contrib/mini-css-extract-plugin#noRuntime"
}
}
diff --git a/test/__snapshots__/noRuntime-option.test.js.snap b/test/__snapshots__/runtime-option.test.js.snap
similarity index 57%
rename from test/__snapshots__/noRuntime-option.test.js.snap
rename to test/__snapshots__/runtime-option.test.js.snap
index 93ab1046..56ffd112 100644
--- a/test/__snapshots__/noRuntime-option.test.js.snap
+++ b/test/__snapshots__/runtime-option.test.js.snap
@@ -1,10 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`noRuntime option should work when noRuntime option is "false": DOM 1`] = `
+exports[`noRuntime option should work when the 'runtime' option is 'false': DOM 1`] = `
"
style-loader test
-
+
Body
@@ -14,15 +14,15 @@ exports[`noRuntime option should work when noRuntime option is "false": DOM 1`]