Commit a879f47f authored by Arunoda Susiripala's avatar Arunoda Susiripala Committed by Guillermo Rauch

Move all NPM modules into a single chunk in dev mode. (#1505)

This will isolate all the NPM modules into a single chunk.
That chunk won't touch by webpack unless, there's a new NPM module.
That gives us much better re-build performance.
parent 85bcfb86
...@@ -93,15 +93,26 @@ export default async function createCompiler (dir, { dev = false, quiet = false, ...@@ -93,15 +93,26 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
name: 'commons', name: 'commons',
filename: 'commons.js', filename: 'commons.js',
minChunks (module, count) { minChunks (module, count) {
// In the dev we use on-deman-entries. // In the dev we use on-demand-entries.
// So, it makes no sense to use commonChunks with that. // So, it makes no sense to use commonChunks based on the minChunks count.
if (dev) return false // Instead, we move all the code in node_modules into this chunk.
// With that, we could gain better performance for page-rebuild process.
if (dev) {
return module.context && module.context.indexOf('node_modules') >= 0
}
// NOTE: it depends on the fact that the entry funtion is always called // NOTE: it depends on the fact that the entry funtion is always called
// before applying CommonsChunkPlugin // before applying CommonsChunkPlugin
return count >= minChunks return count >= minChunks
} }
}), }),
// This chunk contains all the webpack related code. So, all the changes
// related to that happens to this chunk.
// It won't touch commons.js and that gives us much better re-build perf.
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
filename: 'manifest.js'
}),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production') 'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production')
}), }),
...@@ -121,7 +132,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false, ...@@ -121,7 +132,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
} else { } else {
plugins.push( plugins.push(
new CombineAssetsPlugin({ new CombineAssetsPlugin({
input: ['commons.js', 'main.js'], input: ['manifest.js', 'commons.js', 'main.js'],
output: 'app.js' output: 'app.js'
}), }),
new webpack.optimize.UglifyJsPlugin({ new webpack.optimize.UglifyJsPlugin({
......
...@@ -78,6 +78,7 @@ export class NextScript extends Component { ...@@ -78,6 +78,7 @@ export class NextScript extends Component {
if (dev) { if (dev) {
return ( return (
<div> <div>
{ this.getChunkScript('manifest.js') }
{ this.getChunkScript('commons.js') } { this.getChunkScript('commons.js') }
{ this.getChunkScript('main.js') } { this.getChunkScript('main.js') }
</div> </div>
......
...@@ -90,6 +90,12 @@ export default class Server { ...@@ -90,6 +90,12 @@ export default class Server {
await this.serveStatic(req, res, p) await this.serveStatic(req, res, p)
}, },
'/_next/:hash/manifest.js': async (req, res, params) => {
this.handleBuildHash('manifest.js', params.hash, res)
const p = join(this.dir, '.next/manifest.js')
await this.serveStatic(req, res, p)
},
'/_next/:hash/main.js': async (req, res, params) => { '/_next/:hash/main.js': async (req, res, params) => {
this.handleBuildHash('main.js', params.hash, res) this.handleBuildHash('main.js', params.hash, res)
const p = join(this.dir, '.next/main.js') const p = join(this.dir, '.next/main.js')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment