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) => {
|
|
|
|
compilation.plugin('additional-chunk-assets', (chunks) => {
|
|
|
|
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-10-30 14:57:35 +00:00
|
|
|
concat.add(asset)
|
2017-03-24 07:51:34 +00:00
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
// We keep existing assets since that helps when analyzing the bundle
|
|
|
|
})
|
2017-10-19 20:11:37 +00:00
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
compilation.additionalChunkAssets.push(this.output)
|
|
|
|
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))
|
|
|
|
})
|
2017-03-24 07:51:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|