Created by: Kingwl
Fixes #10752
This PR add a branch to detect what if defer
attribute has been specified on script tag or not.
And if so, All script will be wrapped into a DOMContentLoaded
event listener.
Here's an example for the changes:
We have a script:
alert(document.body.innerText);
And a html tmeplate:
<html>
<body>
<div>foo</div>
</body>
</html>
And a webpack config:
module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: 'index.html'
}),
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/.*/])
]
}
Before changes:
<html>
<head>
<script>alert(document.body.innerText);</script>
</head>
<body>
<div>foo</div>
</body>
</html>
And It will caused an exception:
Uncaught TypeError: Cannot read property 'innerText' of null
** before changes with blocking **
<html>
<body>
<div>foo</div>
<script>alert(document.body.innerText);</script>
</body>
</html>
That works fine.
After changes (whatever blocking or not):
<html>
<body>
<div>foo</div>
<script>alert(document.body.innerText);</script>
</body>
</html>
And everything works fine.