-
Notifications
You must be signed in to change notification settings - Fork 46
Plugin relies on chunk loading order #122
Description
The plugin still uses the "Webpack 3" way of loading asynchronous chunks in AGGRESSIVE_BUNDLE mode via the window.webpackJsonp function. This works as long as the main (or runtime chunk if using runtimeChunk: 'single') is loaded as the first script. This is however not the default in most configurations e.g. when using the default behavior of the HtmlWebpackPlugin.
As the order is non-deterministic or at least my change without touching the configuration you run into Uncaught ReferenceError: webpackJsonp is not defined when one if the async chunks is processed first.
The closure webpack plugin should instead use a mechanism similar to what Webpack 4+ does (Link) to load async chunks. Here window.webpackJsonp is initialized as (window.webpackJsonp = window.webpackJsonp || []).push(/* ... */);, which makes it independent from the script loading order as the runtime can pick up already loaded scripts from the array.
As a workaround I had to manually specify the loading order in HtmlWebpackPlugin like so:
new HtmlWebpackPlugin({
chunksSortMode: function(a, b) {
const partialOrderedChunks = [
'runtime',
'main'
];
return partialOrderedChunks.indexOf(b.names[0]) - partialOrderedChunks.indexOf(a.names[0]);
}
}),