I just realized that if you load modules dynamically using require.ensure()
, webpack will not analyze and chunk dependencies together. This makes sense in some way that one could argue, that webpack can't know if such modules are transferred ever, but can we force webpack to do the job anyway?
Example is:
app.js:
require.ensure([ 'module1.js' ], ( require ) => {
// at some point
require( 'module1.js' );
}, 'Module1');
require.ensure([ 'module2.js' ], ( require ) => {
// at some point
require( 'module2.js' );
}, 'Module2');
module1.js
let io = require( 'socket.io-client' );
module2.js
let io = require( 'socket.io-client' );
The outcome of this compilation is, that both of these modules get the whole socket-io library "linked" into their chunks. My original expectation was, that the CommonsChunkPlugin will catch those requires
and put that big library into a common chunk.
new webpack.optimize.CommonsChunkPlugin( 'common' ),
Doesn't work however. Of course I could always "resolve" this dependency manually, but I hoped that webpack can do the trick somehow?
minChunks
to2
inCommonsChunkPlugin options
change situation? – Octagon