Skip to content

Commit

Permalink
improve CommonsChunkPlugin
Browse files Browse the repository at this point in the history
inspired by zeit next
  • Loading branch information
Pooya Parsa committed Aug 18, 2017
1 parent a721232 commit d6cb2cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/builder/builder.js
Expand Up @@ -236,6 +236,9 @@ export default class Builder extends Tapable {
this.options.router.extendRoutes(templateVars.router.routes, r)
}

// Make routes accessible for other modules and webpack configs
this.routes = templateVars.router.routes

// -- Store --
// Add store if needed
if (this.options.store) {
Expand Down
18 changes: 16 additions & 2 deletions lib/builder/webpack/client.config.js
Expand Up @@ -31,18 +31,32 @@ export default function webpackClientConfig () {
config.entry.vendor.push('vuex')
}
config.entry.vendor = config.entry.vendor.concat(this.options.build.vendor)

// Extract vendor chunks for better caching
const _this = this
config.plugins.push(
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: this.options.build.filenames.vendor,
minChunks (module) {
minChunks (module, count) {
// In the dev we use on-demand-entries.
// So, it makes no sense to use commonChunks based on the minChunks count.
// Instead, we move all the code in node_modules into each of the pages.
if (_this.options.dev) {
return false
}

// Total pages
const totalPages = _this.routes.length

// A module is extracted into the vendor chunk when...
return (
// If it's inside node_modules
/node_modules/.test(module.context) &&
// Do not externalize if the request is a CSS file
!/\.(css|less|scss|sass|styl|stylus)$/.test(module.request)
!/\.(css|less|scss|sass|styl|stylus)$/.test(module.request) &&
// Used in at-least 1/2 of the total pages
(totalPages <= 2 ? count >= totalPages : count >= totalPages * 0.5)
)
}
})
Expand Down
2 changes: 1 addition & 1 deletion lib/common/options.js
Expand Up @@ -166,7 +166,7 @@ Options.defaults = {
filenames: {
css: 'common.[contenthash].css',
manifest: 'manifest.[hash].js',
vendor: 'vendor.[chunkhash].js',
vendor: 'common.[chunkhash].js',
app: 'app.[chunkhash].js',
chunk: '[name].[chunkhash].js'
},
Expand Down

0 comments on commit d6cb2cb

Please sign in to comment.