From 7179273fed23092c2f25923ce73b3f31e55a0ef3 Mon Sep 17 00:00:00 2001
From: cap-Bernardito
Date: Wed, 21 Oct 2020 16:04:27 +0300
Subject: [PATCH 1/3] feat: add linkType option
---
README.md | 74 ++++++++++++--
src/CssLoadingRuntimeModule.js | 4 +-
src/index.js | 17 +++-
src/plugin-options.json | 10 ++
.../__snapshots__/linkTag-option.test.js.snap | 55 +++++++++++
.../validate-plugin-options.test.js.snap | 36 +++++++
test/cases/hmr/expected/webpack-5/main.js | 3 +-
.../expected/webpack-4/main.js | 3 +-
.../expected/webpack-5/main.js | 3 +-
test/linkTag-option.test.js | 97 +++++++++++++++++++
test/validate-plugin-options.test.js | 4 +
11 files changed, 287 insertions(+), 19 deletions(-)
create mode 100644 test/__snapshots__/linkTag-option.test.js.snap
create mode 100644 test/linkTag-option.test.js
diff --git a/README.md b/README.md
index 9ccdc9e4..f9d80222 100644
--- a/README.md
+++ b/README.md
@@ -75,13 +75,14 @@ module.exports = {
### Plugin Options
-| Name | Type | Default | Description |
-| :-----------------------------------: | :------------------: | :------------------------------------------------------------------------------: | :------------------------------------------------------- |
-| **[`filename`](#filename)** | `{String\|Function}` | `[name].css` | This option determines the name of each output CSS file |
-| **[`chunkFilename`](#chunkFilename)** | `{String\|Function}` | `based on filename` | This option determines the name of non-entry chunk files |
-| **[`ignoreOrder`](#ignoreOrder)** | `{Boolean}` | `false` | Remove Order Warnings |
-| **[`insert`](#insert)** | `{String\|Function}` | `var head = document.getElementsByTagName("head")[0];head.appendChild(linkTag);` | Inserts `` at the given position |
-| **[`attributes`](#attributes)** | `{Object}` | `{}` | Adds custom attributes to tag |
+| Name | Type | Default | Description |
+| :-----------------------------------: | :------------------: | :------------------------------------------------------------------------------: | :--------------------------------------------------------- |
+| **[`filename`](#filename)** | `{String\|Function}` | `[name].css` | This option determines the name of each output CSS file |
+| **[`chunkFilename`](#chunkFilename)** | `{String\|Function}` | `based on filename` | This option determines the name of non-entry chunk files |
+| **[`ignoreOrder`](#ignoreOrder)** | `{Boolean}` | `false` | Remove Order Warnings |
+| **[`insert`](#insert)** | `{String\|Function}` | `var head = document.getElementsByTagName("head")[0];head.appendChild(linkTag);` | Inserts `` 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 |
#### `filename`
@@ -196,6 +197,65 @@ module.exports = {
Note: It's only applied to dynamically loaded css chunks, if you want to modify link attributes inside html file, please using [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin)
+#### `linkType`
+
+Type: `String|Boolean`
+Default: `text/css`
+
+This option allows loading asynchronous chunks with a custom link type, such as .
+
+##### `String`
+
+Possible values: `text/css`
+
+**webpack.config.js**
+
+```js
+const MiniCssExtractPlugin = require('mini-css-extract-plugin');
+
+module.exports = {
+ plugins: [
+ new MiniCssExtractPlugin({
+ linkType: 'text/css',
+ }),
+ ],
+ module: {
+ rules: [
+ {
+ test: /\.css$/i,
+ use: [MiniCssExtractPlugin.loader, 'css-loader'],
+ },
+ ],
+ },
+};
+```
+
+##### `Boolean`
+
+`false` disables the link `type` attribute
+
+**webpack.config.js**
+
+```js
+const MiniCssExtractPlugin = require('mini-css-extract-plugin');
+
+module.exports = {
+ plugins: [
+ new MiniCssExtractPlugin({
+ linkType: false,
+ }),
+ ],
+ module: {
+ rules: [
+ {
+ test: /\.css$/i,
+ use: [MiniCssExtractPlugin.loader, 'css-loader'],
+ },
+ ],
+ },
+};
+```
+
### Loader Options
| Name | Type | Default | Description |
diff --git a/src/CssLoadingRuntimeModule.js b/src/CssLoadingRuntimeModule.js
index e259fc5d..be0ff24e 100644
--- a/src/CssLoadingRuntimeModule.js
+++ b/src/CssLoadingRuntimeModule.js
@@ -57,7 +57,9 @@ module.exports = class CssLoadingRuntimeModule extends RuntimeModule {
'var linkTag = document.createElement("link");',
this.runtimeOptions.attributes,
'linkTag.rel = "stylesheet";',
- 'linkTag.type = "text/css";',
+ this.runtimeOptions.linkType
+ ? `linkTag.type = ${JSON.stringify(this.runtimeOptions.linkType)};`
+ : '',
'linkTag.onload = resolve;',
'linkTag.onerror = function(event) {',
Template.indent([
diff --git a/src/index.js b/src/index.js
index 03acebe2..c6643a78 100644
--- a/src/index.js
+++ b/src/index.js
@@ -44,14 +44,16 @@ class MiniCssExtractPlugin {
`var target = document.querySelector("${options.insert}");`,
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
])
- : Template.asString([
- 'var head = document.getElementsByTagName("head")[0];',
- 'head.appendChild(linkTag);',
- ]);
+ : Template.asString(['document.head.appendChild(linkTag);']);
const attributes =
typeof options.attributes === 'object' ? options.attributes : {};
+ const linkType =
+ options.linkType === true || typeof options.linkType === 'undefined'
+ ? 'text/css'
+ : options.linkType;
+
this.options = Object.assign(
{
filename: DEFAULT_FILENAME,
@@ -62,6 +64,7 @@ class MiniCssExtractPlugin {
this.runtimeOptions = {
insert,
+ linkType,
};
this.runtimeOptions.attributes = Template.asString(
@@ -398,7 +401,11 @@ class MiniCssExtractPlugin {
'var linkTag = document.createElement("link");',
this.runtimeOptions.attributes,
'linkTag.rel = "stylesheet";',
- 'linkTag.type = "text/css";',
+ this.runtimeOptions.linkType
+ ? `linkTag.type = ${JSON.stringify(
+ this.runtimeOptions.linkType
+ )};`
+ : '',
'linkTag.onload = resolve;',
'linkTag.onerror = function(event) {',
Template.indent([
diff --git a/src/plugin-options.json b/src/plugin-options.json
index 6fd30ee0..94aa6d8e 100644
--- a/src/plugin-options.json
+++ b/src/plugin-options.json
@@ -39,6 +39,16 @@
"attributes": {
"description": "Adds custom attributes to tag (https://github.com/webpack-contrib/mini-css-extract-plugin#attributes).",
"type": "object"
+ },
+ "linkType": {
+ "anyOf": [
+ {
+ "enum": ["text/css"]
+ },
+ {
+ "type": "boolean"
+ }
+ ]
}
}
}
diff --git a/test/__snapshots__/linkTag-option.test.js.snap b/test/__snapshots__/linkTag-option.test.js.snap
new file mode 100644
index 00000000..8a4e62d9
--- /dev/null
+++ b/test/__snapshots__/linkTag-option.test.js.snap
@@ -0,0 +1,55 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`linkType option should work when linkType option is "false": DOM 1`] = `
+"
+ style-loader test
+
+
+
+ Body
+
+