I'm partially updating an existing web application with new (react) code and am using webpack to bundle everything together for production. Because the existing HTML page (in fact it is XML converted to HTML) is already there, I can't use the index.html that is generated by the HtmlWebpackPlugin.
What I would like to achieve is that webpack generates a small runtime.bundle.js which will dynamically load the other generated chunks (main.[contenthash] and vendor.[contenthash]), instead of adding these entries as script tags to the index.html. This way the runtime.bundle.js can be set to nocache whereas the large other chunks can be cached by the browser and correctly fetched on code changes.
As an example, here is the body block of the generated index.html, note the comment:
<html>
<head>...</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="text/javascript" src="runtime.bundle.js"></script>
<!-- I want these two files below not injected as script tags,
but loaded from the runtime.bundle.js file above -->
<script type="text/javascript" src="vendors.31b8acd750477817012d.js"></script>
<script type="text/javascript" src="main.1e4a456d496cdd2e1771.js"></script>
</body>
</html>
The runtime file is already loading a different chunk that is imported dynamically from JS with the following code:
const App = React.lazy(() => import(/* webpackChunkName: "modulex" */ './App'));
This creates the following snippet somewhere within the runtime.bundle.js
a = document.createElement('script');
(a.charset = 'utf-8'),
(a.timeout = 120),
i.nc && a.setAttribute('nonce', i.nc),
(a.src = (function(e) {
return (
i.p +
'' +
({ 1: 'modulex' }[e] || e) +
'.' +
{ 1: '0e0c4000d075e81c1e5b' }[e] +
'.js'
);
So can the same be achieved for the vendors and main chunk?
The only other alternative solution that I can think of is to use the WebpackManifestPlugin to generate the manifest.json and use this to inject the chunks into the already existing HTML file.
manifest.jsoncreate by theWebpackManifestPlugin. This script can be called as an npm script. I can share if you like...