I have a React application with an index.html file in the public folder:
<!DOCTYPE html>
<html lang="ru-RU">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Test</title>
<link rel="dx-theme" data-theme="generic.dark" href="/css/some.css" data-active="false">
<base href="/" />
</head>
<body class="dx-viewport">
<div id="root"></div>
<script src="/scripts/some-script.js"></script>
</body>
</html>
HtmlWebpackPlugin settings:
new HtmlWebpackPlugin({
favicon: 'public/faviconDOCs.ico',
template: 'public/index.html',
scriptLoading: 'blocking',
minify: {
removeComments: true,
},
}),
As a result, Webpack generates the following index.html in the dist folder:
<!DOCTYPE html>
<html lang="ru-RU">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Test</title>
<link rel="dx-theme" data-theme="generic.dark" href="/css/some.css" data-active="false">
<base href="/">
<link rel="icon" href="/faviconDOCs.ico">
</head>
<body class="dx-viewport">
<div id="root"></div>
<script src="/scripts/some-script.js"></script>
<script src="/main.bundle.0cec3d5e983e6fa0c00b.js"></script>
</body>
</html>
But I need the to be included after all the bundled scripts. How can I achieve this?
I tried to add templateParameters with additionalScript
new HtmlWebpackPlugin({
favicon: 'public/faviconDOCs.ico',
template: 'public/index.html',
scriptLoading: 'blocking',
inject: 'body', // Scripts are added at the end of the body
minify: {
removeComments: true,
},
templateParameters: {
additionalScript: '<script src="/tflex-viewer/tflex-viewer3d-widget.js"></script>'
}
}),
and html…
<!DOCTYPE html>
<html lang="ru-RU">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Test</title>
<link rel="dx-theme" data-theme="generic.dark" href="/css/some.css" data-active="false">
<base href="/" />
</head>
<body class="dx-viewport">
<div id="root"></div>
<%= additionalScript %>
</body>
</html>
But the output is the same
<body class="dx-viewport">
<div id="root"></div>
<script src="/scripts/some-script.js"></script>
<script src="/main.bundle.0cec3d5e983e6fa0c00b.js"></script>
</body>
How can I make the script be included at the end of all scripts in index.html?
Thank you in advance.
2
Answers
By specifying the order of scripts in the HTML template, we can achieve the desired result.
And for the plugin, we set inject: false to disable automatic script insertion:
Now, the output will be as expected - Webpack will generate the following index.html:
As a result, the required script (script-test.js) is included at the end (As required by the task conditions)
The HtmlWebpackPlugin adds
<script>
at bottom of head or body tag. But you can’t exactly define the place in HTML.Try to use modern html-bundler-webpack-plugin.
Using this plugin you can specify all source files directly in HTML wherever you want. The plugin resolves source files of assets in templates and replaces them with correct output URLs in the generated HTML. The defined
<script>
tag stay exactly on the same place.For example, there is the HTML template:
Minimal Webpack config:
The generated HTML contains hashed output URL of assets.
All
<script>
tags will be exactly on the same place:Using this plugin you don’t need define source JS scrips in Webpack
entry
option. You specify source JS files (and other assets) directly in HTML.