Skip to content

Commit

Permalink
fix: inject javascripts in the <head> tag for inject:true and scriptL…
Browse files Browse the repository at this point in the history
…oading:'defer'
  • Loading branch information
jantimon committed Feb 3, 2021
1 parent 13af0fb commit 833b46b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function hookIntoCompiler (compiler, options, plugin) {
.then(({ assetTags }) => {
// Inject scripts to body unless it set explicitly to head
const scriptTarget = options.inject === 'head' ||
(options.inject === false && options.scriptLoading !== 'blocking') ? 'head' : 'body';
(options.inject !== 'body' && options.scriptLoading !== 'blocking') ? 'head' : 'body';
// Group assets to `head` and `body` tag arrays
const assetGroups = generateAssetGroups(assetTags, scriptTarget);
// Allow third-party-plugin authors to reorder and change the assetTags once they are grouped
Expand Down
23 changes: 23 additions & 0 deletions spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,29 @@ describe('HtmlWebpackPlugin', () => {
}, ['<head><link href="styles.css" rel="stylesheet"></head>', '<script src="index_bundle.js"></script></body>'], null, done);
});

it('should add the javascript assets to the head for inject:true with scriptLoading:defer', done => {
testHtmlPlugin({
mode: 'production',
entry: path.join(__dirname, 'fixtures/theme.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
module: {
rules: [
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] }
]
},
plugins: [
new MiniCssExtractPlugin({ filename: 'styles.css' }),
new HtmlWebpackPlugin({
scriptLoading: 'defer',
inject: true
})
]
}, ['<script defer="defer" src="index_bundle.js"></script><link href="styles.css" rel="stylesheet"></head>'], null, done);
});

it('should allow to use headTags and bodyTags directly in string literals', done => {
testHtmlPlugin({
mode: 'production',
Expand Down

0 comments on commit 833b46b

Please sign in to comment.