Answer the question
In order to leave comments, you need to log in
How to make a separate vendor for some entries in Webpack 4?
I have 3 entrypoints in my webpack config. One is the application itself, the other two are for individual pages with public access.
entry: {
bundle: './index.js',
login: './login.js',
offer: './offer.js'
}
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'initial'
}
}
}
}
Answer the question
In order to leave comments, you need to log in
After a lot of searching on the internet, I wrote my solution:
Tested on Webpack version 4.29
const SplitChunksPlugin = require("webpack/lib/optimize/SplitChunksPlugin");
const filterByEntryPoint = (entry, test = /[\\/]node_modules[\\/]/) => {
const recursiveIssuer = m => m.issuer ? recursiveIssuer(m.issuer) : m.name;
return function (module, c) {
const name = recursiveIssuer(module);
if ((typeof entry === "string" && name === entry) ||
(Array.isArray(entry) && entry.indexOf(name) !== -1)) {
return SplitChunksPlugin.checkTest(test, module);
}
return false;
}
};
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
name: "vendor",
chunks: "all",
test: filterByEntryPoint("bundle")
},
admin_vendor: {
name: "vendor-login-offer",
chunks: "all",
test: filterByEntryPoint(["login","offer"])
}
}
}
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question