diff --git a/README.md b/README.md index 9ccdc9e4..7640b6db 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}` | `document.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` @@ -114,7 +115,7 @@ See [examples](#remove-order-warnings) below for details. #### `insert` Type: `String|Function` -Default: `var head = document.getElementsByTagName("head")[0]; head.appendChild(linkTag);` +Default: `document.head.appendChild(linkTag);` By default, the `extract-css-chunks-plugin` appends styles (`` elements) to `document.head` of the current `window`. @@ -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..beecd4a9 100644 --- a/src/index.js +++ b/src/index.js @@ -44,14 +44,17 @@ 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 : {}; + // Todo in next major release set default to "false" + const linkType = + options.linkType === true || typeof options.linkType === 'undefined' + ? 'text/css' + : options.linkType; + this.options = Object.assign( { filename: DEFAULT_FILENAME, @@ -62,6 +65,7 @@ class MiniCssExtractPlugin { this.runtimeOptions = { insert, + linkType, }; this.runtimeOptions.attributes = Template.asString( @@ -398,7 +402,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`] = ` +"
+