| 10 | } |
| 11 | |
| 12 | apply (compiler) { |
| 13 | compiler.plugin('emit', (compilation, cb) => { |
| 14 | const stats = compilation.getStats().toJson() |
| 15 | |
| 16 | const allFiles = uniq(stats.assets |
| 17 | .map(a => a.name)) |
| 18 | |
| 19 | const initialFiles = uniq(Object.keys(stats.entrypoints) |
| 20 | .map(name => stats.entrypoints[name].assets) |
| 21 | .reduce((assets, all) => all.concat(assets), []) |
| 22 | .filter(isJS)) |
| 23 | |
| 24 | const asyncFiles = allFiles |
| 25 | .filter(isJS) |
| 26 | .filter(file => initialFiles.indexOf(file) < 0) |
| 27 | |
| 28 | const manifest = { |
| 29 | publicPath: stats.publicPath, |
| 30 | all: allFiles, |
| 31 | initial: initialFiles, |
| 32 | async: asyncFiles, |
| 33 | modules: { /* [identifier: string]: Array<index: number> */ } |
| 34 | } |
| 35 | |
| 36 | const assetModules = stats.modules.filter(m => m.assets.length) |
| 37 | const fileToIndex = file => manifest.all.indexOf(file) |
| 38 | stats.modules.forEach(m => { |
| 39 | // ignore modules duplicated in multiple chunks |
| 40 | if (m.chunks.length === 1) { |
| 41 | const cid = m.chunks[0] |
| 42 | const chunk = stats.chunks.find(c => c.id === cid) |
| 43 | if (!chunk || !chunk.files) { |
| 44 | return |
| 45 | } |
| 46 | const files = manifest.modules[hash(m.identifier)] = chunk.files.map(fileToIndex) |
| 47 | // find all asset modules associated with the same chunk |
| 48 | assetModules.forEach(m => { |
| 49 | if (m.chunks.some(id => id === cid)) { |
| 50 | files.push.apply(files, m.assets.map(fileToIndex)) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | // const debug = (file, obj) => { |
| 57 | // require('fs').writeFileSync(__dirname + '/' + file, JSON.stringify(obj, null, 2)) |
| 58 | // } |
| 59 | // debug('stats.json', stats) |
| 60 | // debug('client-manifest.json', manifest) |
| 61 | |
| 62 | const json = JSON.stringify(manifest, null, 2) |
| 63 | compilation.assets[this.options.filename] = { |
| 64 | source: () => json, |
| 65 | size: () => json.length |
| 66 | } |
| 67 | cb() |
| 68 | }) |
| 69 | } |