I need help migrating the following code from webpack 3 to 4.
new webpack.optimize.CommonsChunkPlugin({
minChunks: module => module.context && module.context.indexOf("node_modules") !== -1,
name: "vendor",
chunks: ["main"]
})
I have two entry files and want only the dependencies of the first one to be included in the vendor chunk. The dependencies of the second entry should all stay in its own bundle.
As of webpack v4 the CommonsChunkPlugin is deprecated.
Deprecated
You no longer need to use these plugins:
DedupePlugin has been removed too in v4
NoEmitOnErrorsPlugin -> optimization.noEmitOnErrors (on by default in production mode) ModuleConcatenationPlugin -> optimization.concatenateModules (on by default in prod mode) NamedModulesPlugin -> optimization.namedModules (on by default in dev mode)
Recommendations for webpack 4
Use
mini-css-extract-plugin
instead oftext-extract-plugin
. Usewebpack-bundle-analyzer
to analyze your bundled output in graphical way.Entry scripts are real "Entry-Scripts" to your application, don't add vendor files explicitly to
entry:
inwebpack.config.js
. SPA apps have one entry and Multi-Page-Apps like classicASP.NET MVC
apps have multiple entry points. Webpack will build a dependenc graph out of your entry scripts and generate optimized bundles for your app.If you want to migrate from an older webpack version, it's best to checkout the migration guide
Tree shaking (dead code elimination) is only enabled in production mode.
Webpack 4, the new way of bundling assets
(You have to remove your CommonsChunkPlugin-thinking from your head)
!!! Meanwhile the webpack doc has been updated, a section
SplitChunks
was added !!!It follows a new philosophy:
Webpack 4 now by default does optimizations automatically. It analyzes your dependency graph and creates optimal bundles (output), based on the following conditions:
All this can be tweaked using the SplitChunksPlugin! (see SplitChunksPlugin documentation)
A more detailed explanation on how to use the new
optimization.splitChunks
API.CommonsChunkPlugin was removed because it has a lot of problems:
The SplitChunksPlugin also has some great properties:
--> Source
Regarding your issue, you want to split all deps of entry1 and entry2 into separate bundles.
If you don't add the optimization:splitChunks entry the default setting is as follows:
You can set optimization.splitChunks.cacheGroups.default to false to disable the default cache group, same for vendors cache group!
Up-to-date interface implementations for
SplitChunksOptions
,CachGroupOptions
andOptimization
can be found here.The interface definitions below may not be 100% accurate, but good for a simple overview:
SplitChunksOptions
interfaceCacheGroupsOptions
interface:Optimization
InterfaceAssuming your entrypoints are
main
andsecondary
:Using webpack-4 You can extract only the
vendors
modules frommain
chunk but leave other third parties modules referenced insecondary
inside that chunk using thetest
function of thecacheGroups
you want to create.This took me a while to figure out, but the key realization for me was that the
chunks
argument in webpack 4 now takes a function, which allows you to only include a specific entry. I'm assuming this is a recent change, because at the time of posting it wasn't in the official documentation.