Skip to content

Commit a879f47

Browse files
arunodarauchg
authored andcommitted
Move all NPM modules into a single chunk in dev mode. (vercel#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.
1 parent 85bcfb8 commit a879f47

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

server/build/webpack.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,26 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
9393
name: 'commons',
9494
filename: 'commons.js',
9595
minChunks (module, count) {
96-
// In the dev we use on-deman-entries.
97-
// So, it makes no sense to use commonChunks with that.
98-
if (dev) return false
96+
// In the dev we use on-demand-entries.
97+
// So, it makes no sense to use commonChunks based on the minChunks count.
98+
// Instead, we move all the code in node_modules into this chunk.
99+
// With that, we could gain better performance for page-rebuild process.
100+
if (dev) {
101+
return module.context && module.context.indexOf('node_modules') >= 0
102+
}
99103

100104
// NOTE: it depends on the fact that the entry funtion is always called
101105
// before applying CommonsChunkPlugin
102106
return count >= minChunks
103107
}
104108
}),
109+
// This chunk contains all the webpack related code. So, all the changes
110+
// related to that happens to this chunk.
111+
// It won't touch commons.js and that gives us much better re-build perf.
112+
new webpack.optimize.CommonsChunkPlugin({
113+
name: 'manifest',
114+
filename: 'manifest.js'
115+
}),
105116
new webpack.DefinePlugin({
106117
'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production')
107118
}),
@@ -121,7 +132,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
121132
} else {
122133
plugins.push(
123134
new CombineAssetsPlugin({
124-
input: ['commons.js', 'main.js'],
135+
input: ['manifest.js', 'commons.js', 'main.js'],
125136
output: 'app.js'
126137
}),
127138
new webpack.optimize.UglifyJsPlugin({

server/document.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class NextScript extends Component {
7878
if (dev) {
7979
return (
8080
<div>
81+
{ this.getChunkScript('manifest.js') }
8182
{ this.getChunkScript('commons.js') }
8283
{ this.getChunkScript('main.js') }
8384
</div>

server/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ export default class Server {
9090
await this.serveStatic(req, res, p)
9191
},
9292

93+
'/_next/:hash/manifest.js': async (req, res, params) => {
94+
this.handleBuildHash('manifest.js', params.hash, res)
95+
const p = join(this.dir, '.next/manifest.js')
96+
await this.serveStatic(req, res, p)
97+
},
98+
9399
'/_next/:hash/main.js': async (req, res, params) => {
94100
this.handleBuildHash('main.js', params.hash, res)
95101
const p = join(this.dir, '.next/main.js')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy