2017-10-30 14:57:35 +00:00
|
|
|
import { ConcatSource } from 'webpack-sources'
|
|
|
|
|
2017-03-24 07:51:34 +00:00
|
|
|
// This plugin combines a set of assets into a single asset
|
|
|
|
// This should be only used with text assets,
|
|
|
|
// otherwise the result is unpredictable.
|
|
|
|
export default class CombineAssetsPlugin {
|
|
|
|
constructor ({ input, output }) {
|
|
|
|
this.input = input
|
|
|
|
this.output = output
|
|
|
|
}
|
|
|
|
|
|
|
|
apply (compiler) {
|
2017-10-30 14:57:35 +00:00
|
|
|
compiler.plugin('compilation', (compilation) => {
|
2017-12-22 09:25:35 +00:00
|
|
|
// This is triggered after uglify and other optimizers have ran.
|
|
|
|
compilation.plugin('after-optimize-chunk-assets', (chunks) => {
|
2017-10-30 14:57:35 +00:00
|
|
|
const concat = new ConcatSource()
|
2017-06-28 19:16:21 +00:00
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
this.input.forEach((name) => {
|
|
|
|
const asset = compilation.assets[name]
|
|
|
|
if (!asset) return
|
2017-03-24 07:51:34 +00:00
|
|
|
|
2017-12-22 09:25:35 +00:00
|
|
|
// We add each matched asset from this.input to a new bundle
|
2017-10-30 14:57:35 +00:00
|
|
|
concat.add(asset)
|
2017-12-22 09:25:35 +00:00
|
|
|
// 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
|
2017-10-30 14:57:35 +00:00
|
|
|
})
|
2017-10-19 20:11:37 +00:00
|
|
|
|
2017-12-22 09:25:35 +00:00
|
|
|
// Creates a new asset holding the concatted source
|
2017-10-30 14:57:35 +00:00
|
|
|
compilation.assets[this.output] = concat
|
|
|
|
})
|
2017-03-24 07:51:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|