Saturday, August 11, 2018

Migration from Webpack 3.x to 4.x

Leave a Comment

I need help on migration of widgets-like product from Webpack 3.x to 4.x. I've stuck due to removal of CommonsChunkPlugin and now I can't achieve the same behavior using SplitChunksPlugin.

Here is a repo with a small demo to show the problem. Is there any way to achieve the same code splitting using Webpack 4.x.

UPD: The goal is to keep common modules in loader entry bundle, everything else should reuse them. Please, check out webpack-4 branch, maybe I'm missing something there.

1 Answers

Answers 1

After some time I came to the solution by myself.

In Webpack 4 there's no way to put the common modules into one of the entries, so the only way is to put them into common.js (of course, if there is a big common bundle, it makes sense to split it into several smaller ones and to load them only when needed).

splitChunks: {   chunks: 'all'   name: 'common',   minChunks: 2, }, 

Then, in order to make the loader executable without common.js, it should be ignored by splitChunks:

splitChunks: {   chunks: chunk => chunk.name !== 'loader',   name: 'common',   minChunks: 2, }, 

As a result from loader code other entries can be loaded alongside with commons

Promise.all([   loadCommon(),   loadEntry(), ]).then(() => runEntry()); 

Sorry for the silly question, the solution turned out to be on a surface.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment