Webpack v4 - Migrating from CommonsChunkPlugin to SplitChunksPlugin
I'm having a hard time trying to figure out how to migrate my Webpack setup from v3 to v4 with SplitChunksPlugin. The way that I had this set up before was like this:
webpack.mix.js
...
entry: {
main: mainJsEntry,
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["main"],
filename: 'js/[name].js',
minChunks: Infinity
})
],
...
global.js
const Vue = require('vue'); // this ends up in global.js
const myModule = require('global/mymodule.js'); // this is imported into global.js
page1.js
const Vue = require('vue'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page1.js
page2.js
const myModule = require('global/mymodule.js'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page2.js
What I am trying to do is have all Javascript bundles other than global.js reference the existing import from global.js, but do not bundle any module that was not imported inside global.js itself.
After following the documentation for Webpack v4, I can only seem to get Webpack to bundle anything that I import more than once in other modules into global.js, but I don't want that. Use case would be if I want a carousel on two pages, I would import on each page, but I don't want it globally as it's used on 2 of 100+ pages on the website.
...
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: 'global',
filename: 'js/corp/global.js',
chunks: 'all',
minChunks: 2
}
}
}
},
...
I hope what I'm hoping to do is clear. Any help is appreciated.
javascript webpack webpack-splitchunks
add a comment |
I'm having a hard time trying to figure out how to migrate my Webpack setup from v3 to v4 with SplitChunksPlugin. The way that I had this set up before was like this:
webpack.mix.js
...
entry: {
main: mainJsEntry,
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["main"],
filename: 'js/[name].js',
minChunks: Infinity
})
],
...
global.js
const Vue = require('vue'); // this ends up in global.js
const myModule = require('global/mymodule.js'); // this is imported into global.js
page1.js
const Vue = require('vue'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page1.js
page2.js
const myModule = require('global/mymodule.js'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page2.js
What I am trying to do is have all Javascript bundles other than global.js reference the existing import from global.js, but do not bundle any module that was not imported inside global.js itself.
After following the documentation for Webpack v4, I can only seem to get Webpack to bundle anything that I import more than once in other modules into global.js, but I don't want that. Use case would be if I want a carousel on two pages, I would import on each page, but I don't want it globally as it's used on 2 of 100+ pages on the website.
...
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: 'global',
filename: 'js/corp/global.js',
chunks: 'all',
minChunks: 2
}
}
}
},
...
I hope what I'm hoping to do is clear. Any help is appreciated.
javascript webpack webpack-splitchunks
add a comment |
I'm having a hard time trying to figure out how to migrate my Webpack setup from v3 to v4 with SplitChunksPlugin. The way that I had this set up before was like this:
webpack.mix.js
...
entry: {
main: mainJsEntry,
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["main"],
filename: 'js/[name].js',
minChunks: Infinity
})
],
...
global.js
const Vue = require('vue'); // this ends up in global.js
const myModule = require('global/mymodule.js'); // this is imported into global.js
page1.js
const Vue = require('vue'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page1.js
page2.js
const myModule = require('global/mymodule.js'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page2.js
What I am trying to do is have all Javascript bundles other than global.js reference the existing import from global.js, but do not bundle any module that was not imported inside global.js itself.
After following the documentation for Webpack v4, I can only seem to get Webpack to bundle anything that I import more than once in other modules into global.js, but I don't want that. Use case would be if I want a carousel on two pages, I would import on each page, but I don't want it globally as it's used on 2 of 100+ pages on the website.
...
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: 'global',
filename: 'js/corp/global.js',
chunks: 'all',
minChunks: 2
}
}
}
},
...
I hope what I'm hoping to do is clear. Any help is appreciated.
javascript webpack webpack-splitchunks
I'm having a hard time trying to figure out how to migrate my Webpack setup from v3 to v4 with SplitChunksPlugin. The way that I had this set up before was like this:
webpack.mix.js
...
entry: {
main: mainJsEntry,
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["main"],
filename: 'js/[name].js',
minChunks: Infinity
})
],
...
global.js
const Vue = require('vue'); // this ends up in global.js
const myModule = require('global/mymodule.js'); // this is imported into global.js
page1.js
const Vue = require('vue'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page1.js
page2.js
const myModule = require('global/mymodule.js'); // this references global.js
const anotherLibrary = require('anotherLibrary'); // this is imported into page2.js
What I am trying to do is have all Javascript bundles other than global.js reference the existing import from global.js, but do not bundle any module that was not imported inside global.js itself.
After following the documentation for Webpack v4, I can only seem to get Webpack to bundle anything that I import more than once in other modules into global.js, but I don't want that. Use case would be if I want a carousel on two pages, I would import on each page, but I don't want it globally as it's used on 2 of 100+ pages on the website.
...
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: 'global',
filename: 'js/corp/global.js',
chunks: 'all',
minChunks: 2
}
}
}
},
...
I hope what I'm hoping to do is clear. Any help is appreciated.
javascript webpack webpack-splitchunks
javascript webpack webpack-splitchunks
asked Dec 28 '18 at 17:54
DevinDevin
81641631
81641631
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962460%2fwebpack-v4-migrating-from-commonschunkplugin-to-splitchunksplugin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962460%2fwebpack-v4-migrating-from-commonschunkplugin-to-splitchunksplugin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown