-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf: replace LimitChunkCountPlugin with more naive MergeChunksPlugin
Use more naive implementation of `MergeChunksPlugin` to concatenate all chunks in a single `server-main.js` bundle when building for server, in place of `LimitChunkCountPlugin` which seems to have quadratic runtime complexity and appears to be very slow for builds that include hundreds of async chunks (vs linear in the `MergeChunksPlugin` case). Before: ``` [client] Build completed in 4.15s [server] Build completed in 40.967s ``` After: ``` [client] Build completed in 4.195s [server] Build completed in 6.801s ```
- Loading branch information
1 parent
91def84
commit c46971f
Showing
4 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** Copyright (c) 2018 Uber Technologies, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
/*eslint-env node */ | ||
|
||
const PLUGIN_NAME = 'MergeChunksPlugin'; | ||
|
||
class MergeChunksPlugin { | ||
apply(compiler /*: Object */) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
compilation.hooks.optimizeChunks.tap( | ||
{ | ||
name: PLUGIN_NAME, | ||
// @see https://github.com/webpack/webpack/blob/9fcaa243573005d6fdece9a3f8d89a0e8b399613/lib/OptimizationStages.js#L10 | ||
stage: 10, | ||
}, | ||
() => { | ||
if (compilation.chunks.size < 2) { | ||
return; | ||
} | ||
|
||
const chunkGraph = compilation.chunkGraph; | ||
const chunks = Array.from(compilation.chunks); | ||
|
||
const rootChunk = chunks[0]; | ||
for (let i = 1, len = chunks.length; i < len; i++) { | ||
const chunk = chunks[i]; | ||
if (!chunkGraph.canChunksBeIntegrated(rootChunk, chunk)) { | ||
throw new Error( | ||
'Expected to be able to merge all chunks into one' | ||
); | ||
} | ||
|
||
chunkGraph.integrateChunks(rootChunk, chunk); | ||
compilation.chunks.delete(chunk); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = MergeChunksPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters