Skip to content

Commit de315eb

Browse files
committedMar 17, 2020
feat: Add defer script loading
1 parent 7df269f commit de315eb

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Allowed values are as follows
146146
|**`template`**|`{String}`|``|`webpack` relative or absolute path to the template. By default it will use `src/index.ejs` if it exists. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
147147
|**`templateParameters`**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) |
148148
|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)|
149+
|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'blocking'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. |
149150
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
150151
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
151152
|**`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

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class HtmlWebpackPlugin {
4343
templateParameters: templateParametersGenerator,
4444
filename: 'index.html',
4545
hash: false,
46-
inject: true,
46+
inject: userOptions.scriptLoading !== 'defer' ? 'body' : 'head',
47+
scriptLoading: 'blocking',
4748
compile: true,
4849
favicon: false,
4950
minify: 'auto',
@@ -701,6 +702,7 @@ class HtmlWebpackPlugin {
701702
tagName: 'script',
702703
voidTag: false,
703704
attributes: {
705+
defer: this.options.scriptLoading !== 'blocking',
704706
src: scriptAsset
705707
}
706708
}));

‎spec/basic.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -2252,4 +2252,34 @@ describe('HtmlWebpackPlugin', () => {
22522252
})]
22532253
}, [/<!doctype html>\s+<html>\s+<head>\s+<meta charset="utf-8">/], null, done);
22542254
});
2255+
2256+
it('should allow to inject scripts with a defer attribute', done => {
2257+
testHtmlPlugin({
2258+
mode: 'production',
2259+
entry: path.join(__dirname, 'fixtures/index.js'),
2260+
output: {
2261+
path: OUTPUT_DIR,
2262+
filename: 'index_bundle.js'
2263+
},
2264+
plugins: [new HtmlWebpackPlugin({
2265+
scriptLoading: 'defer'
2266+
2267+
})]
2268+
}, [/<script defer="defer" .+<body>/], null, done);
2269+
});
2270+
2271+
it('should allow to inject scripts with a defer attribute to the body', done => {
2272+
testHtmlPlugin({
2273+
mode: 'production',
2274+
entry: path.join(__dirname, 'fixtures/index.js'),
2275+
output: {
2276+
path: OUTPUT_DIR,
2277+
filename: 'index_bundle.js'
2278+
},
2279+
plugins: [new HtmlWebpackPlugin({
2280+
scriptLoading: 'defer',
2281+
inject: 'body'
2282+
})]
2283+
}, [/<body>.*<script defer="defer"/], null, done);
2284+
});
22552285
});

‎typings.d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ declare namespace HtmlWebpackPlugin {
6262
| false // Don't inject scripts
6363
| true // Inject scripts into body
6464
| "body" // Inject scripts into body
65-
| "head"; // Inject scripts into head
65+
| "head" // Inject scripts into head
66+
/**
67+
* Set up script loading
68+
* blocking will result in <script src="..."></script>
69+
* defer will result in <script defer src="..."></script>
70+
*
71+
* The default behaviour is blocking
72+
*/
73+
scriptLoading:
74+
| "blocking"
75+
| "defer"
6676
/**
6777
* Inject meta tags
6878
*/

0 commit comments

Comments
 (0)
Please sign in to comment.