Skip to content

Commit 1e42625

Browse files
noppajantimon
authored andcommitted
feat: Support type=module via scriptLoading option
Add third option, `'module'`, to the `scriptLoading` option. This partially fixes issue #1663, which is also where this solution to the problem was proposed. Although this patch doesn't give a way to freely set `type` attribute to any value, setting `scriptLoading: "module" now makes the scripts have attribute `type="module"`, which was the original motivation behind the issue.
1 parent 7d3645b commit 1e42625

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Allowed values are as follows:
149149
|**`templateParameters`**|`{Boolean\|Object\|Function}`| `false`| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) |
150150
|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element. Passing `true` will add it to the head/body depending on the `scriptLoading` option. Passing `false` will disable automatic injections. - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)|
151151
|**`publicPath`**|`{String\|'auto'}`|`'auto'`|The publicPath used for script and link tags|
152-
|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'defer'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. |
152+
|**`scriptLoading`**|`{'blocking'\|'defer'\|'module'}`|`'defer'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. Setting to `'module'` adds attribute [`type="module"`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#applying_the_module_to_your_html). This also implies "defer", since modules are automatically deferred. |
153153
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
154154
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
155155
|**`base`**|`{Object\|String\|false}`|`false`|Inject a [`base`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tag. E.g. `base: "https://example.com/path/page.html`|

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class HtmlWebpackPlugin {
7070
this.options = options;
7171

7272
// Assert correct option spelling
73-
assert(options.scriptLoading === 'defer' || options.scriptLoading === 'blocking', 'scriptLoading needs to be set to "defer" or "blocking');
73+
assert(options.scriptLoading === 'defer' || options.scriptLoading === 'blocking' || options.scriptLoading === 'module', 'scriptLoading needs to be set to "defer", "blocking" or "module"');
7474
assert(options.inject === true || options.inject === false || options.inject === 'head' || options.inject === 'body', 'inject needs to be set to true, false, "head" or "body');
7575

7676
// Default metaOptions if no template is provided
@@ -741,7 +741,8 @@ function hookIntoCompiler (compiler, options, plugin) {
741741
voidTag: false,
742742
meta: { plugin: 'html-webpack-plugin' },
743743
attributes: {
744-
defer: options.scriptLoading !== 'blocking',
744+
defer: options.scriptLoading === 'defer',
745+
type: options.scriptLoading === 'module' ? 'module' : undefined,
745746
src: scriptAsset
746747
}
747748
}));

spec/basic.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,20 @@ describe('HtmlWebpackPlugin', () => {
25262526
}, [/<script defer="defer" .+<body>/], null, done);
25272527
});
25282528

2529+
it('should allow to inject scripts with a type="module" attribute', done => {
2530+
testHtmlPlugin({
2531+
mode: 'production',
2532+
entry: path.join(__dirname, 'fixtures/index.js'),
2533+
output: {
2534+
path: OUTPUT_DIR,
2535+
filename: 'index_bundle.js'
2536+
},
2537+
plugins: [new HtmlWebpackPlugin({
2538+
scriptLoading: 'module'
2539+
})]
2540+
}, [/<script type="module" src="index_bundle.js"><\/script>.+<body>/], null, done);
2541+
});
2542+
25292543
it('should allow to inject scripts with a defer="defer" attribute to the body', done => {
25302544
testHtmlPlugin({
25312545
mode: 'production',

typings.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ declare namespace HtmlWebpackPlugin {
9696
*
9797
* @default 'defer'
9898
*/
99-
scriptLoading?: "blocking" | "defer";
99+
scriptLoading?: "blocking" | "defer" | "module";
100100
/**
101101
* Inject meta tags
102102
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy