From c23046dc79b80c754602339845d8eeeb5becccce Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 22 Dec 2017 10:25:35 +0100 Subject: [PATCH] Rewrite combine-asset-plugin using correct event (#3486) * Speed up next build * Remove comment * Add comment * Add comments --- package.json | 4 ++-- server/build/plugins/combine-assets-plugin.js | 15 ++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 6f0e6cff..651a5e1d 100644 --- a/package.json +++ b/package.json @@ -104,7 +104,8 @@ "webpack-dev-middleware": "1.12.0", "webpack-hot-middleware": "2.19.1", "write-file-webpack-plugin": "4.2.0", - "xss-filters": "1.2.7" + "xss-filters": "1.2.7", + "uglifyjs-webpack-plugin": "^1.1.1" }, "devDependencies": { "@taskr/babel": "1.1.0", @@ -134,7 +135,6 @@ "react-dom": "16.0.0", "standard": "9.0.2", "taskr": "1.1.0", - "uglifyjs-webpack-plugin": "^1.1.1", "wd": "1.4.1" }, "peerDependencies": { diff --git a/server/build/plugins/combine-assets-plugin.js b/server/build/plugins/combine-assets-plugin.js index b7bae4a4..274ac788 100644 --- a/server/build/plugins/combine-assets-plugin.js +++ b/server/build/plugins/combine-assets-plugin.js @@ -11,26 +11,23 @@ export default class CombineAssetsPlugin { apply (compiler) { compiler.plugin('compilation', (compilation) => { - compilation.plugin('additional-chunk-assets', (chunks) => { + // This is triggered after uglify and other optimizers have ran. + compilation.plugin('after-optimize-chunk-assets', (chunks) => { const concat = new ConcatSource() this.input.forEach((name) => { const asset = compilation.assets[name] if (!asset) return + // We add each matched asset from this.input to a new bundle concat.add(asset) - // We keep existing assets since that helps when analyzing the bundle + // The original assets are kept because they show up when analyzing the bundle using webpack-bundle-analyzer + // See https://github.com/zeit/next.js/tree/canary/examples/with-webpack-bundle-analyzer }) - compilation.additionalChunkAssets.push(this.output) + // Creates a new asset holding the concatted source compilation.assets[this.output] = concat - - // Register the combined file as an output of the associated chunks - chunks.filter((chunk) => { - return chunk.files.reduce((prev, file) => prev || this.input.includes(file), false) - }) - .forEach((chunk) => chunk.files.push(this.output)) }) }) }