2018-04-12 07:47:42 +00:00
|
|
|
// @flow
|
|
|
|
import { RawSource } from 'webpack-sources'
|
2018-06-16 17:23:02 +00:00
|
|
|
import {BUILD_MANIFEST} from '../../../lib/constants'
|
2018-04-12 07:47:42 +00:00
|
|
|
|
|
|
|
// This plugin creates a build-manifest.json for all assets that are being output
|
|
|
|
// It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
|
|
|
|
export default class BuildManifestPlugin {
|
|
|
|
apply (compiler: any) {
|
|
|
|
compiler.plugin('emit', (compilation, callback) => {
|
|
|
|
const {chunks} = compilation
|
|
|
|
const assetMap = {pages: {}, css: []}
|
|
|
|
|
|
|
|
for (const chunk of chunks) {
|
|
|
|
if (!chunk.name || !chunk.files) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = []
|
|
|
|
|
|
|
|
for (const file of chunk.files) {
|
|
|
|
if (/\.map$/.test(file)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/\.hot-update\.js$/.test(file)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/\.css$/.exec(file)) {
|
|
|
|
assetMap.css.push(file)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
files.push(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length > 0) {
|
|
|
|
assetMap[chunk.name] = files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compilation.assets[BUILD_MANIFEST] = new RawSource(JSON.stringify(assetMap))
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|