Skip to content

Commit 833b46b

Browse files
committed
fix: inject javascripts in the <head> tag for inject:true and scriptLoading:'defer'
1 parent 13af0fb commit 833b46b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ function hookIntoCompiler (compiler, options, plugin) {
302302
.then(({ assetTags }) => {
303303
// Inject scripts to body unless it set explicitly to head
304304
const scriptTarget = options.inject === 'head' ||
305-
(options.inject === false && options.scriptLoading !== 'blocking') ? 'head' : 'body';
305+
(options.inject !== 'body' && options.scriptLoading !== 'blocking') ? 'head' : 'body';
306306
// Group assets to `head` and `body` tag arrays
307307
const assetGroups = generateAssetGroups(assetTags, scriptTarget);
308308
// Allow third-party-plugin authors to reorder and change the assetTags once they are grouped

spec/basic.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,29 @@ describe('HtmlWebpackPlugin', () => {
25402540
}, ['<head><link href="styles.css" rel="stylesheet"></head>', '<script src="index_bundle.js"></script></body>'], null, done);
25412541
});
25422542

2543+
it('should add the javascript assets to the head for inject:true with scriptLoading:defer', done => {
2544+
testHtmlPlugin({
2545+
mode: 'production',
2546+
entry: path.join(__dirname, 'fixtures/theme.js'),
2547+
output: {
2548+
path: OUTPUT_DIR,
2549+
filename: 'index_bundle.js'
2550+
},
2551+
module: {
2552+
rules: [
2553+
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] }
2554+
]
2555+
},
2556+
plugins: [
2557+
new MiniCssExtractPlugin({ filename: 'styles.css' }),
2558+
new HtmlWebpackPlugin({
2559+
scriptLoading: 'defer',
2560+
inject: true
2561+
})
2562+
]
2563+
}, ['<script defer="defer" src="index_bundle.js"></script><link href="styles.css" rel="stylesheet"></head>'], null, done);
2564+
});
2565+
25432566
it('should allow to use headTags and bodyTags directly in string literals', done => {
25442567
testHtmlPlugin({
25452568
mode: 'production',

0 commit comments

Comments
 (0)