|
| 1 | +var RawSource = require("webpack-core/lib/RawSource"); |
| 2 | + |
| 3 | +function ChunkManifestPlugin(options) { |
| 4 | + options = options || {}; |
| 5 | + this.manifestFilename = options.filename || "manifest.json"; |
| 6 | + this.manifestVariable = options.manifestVariable || "webpackManifest"; |
| 7 | + this.inlineManifest = options.inlineManifest || false; |
| 8 | +} |
| 9 | +module.exports = ChunkManifestPlugin; |
| 10 | + |
| 11 | +ChunkManifestPlugin.prototype.constructor = ChunkManifestPlugin; |
| 12 | +ChunkManifestPlugin.prototype.apply = function(compiler) { |
| 13 | + var manifestFilename = this.manifestFilename; |
| 14 | + var manifestVariable = this.manifestVariable; |
| 15 | + var inlineManifest = this.inlineManifest; |
| 16 | + var oldChunkFilename; |
| 17 | + var chunkManifest; |
| 18 | + |
| 19 | + compiler.plugin("this-compilation", function(compilation) { |
| 20 | + var mainTemplate = compilation.mainTemplate; |
| 21 | + mainTemplate.plugin("require-ensure", function(_, chunk, hash) { |
| 22 | + var filename = |
| 23 | + this.outputOptions.chunkFilename || this.outputOptions.filename; |
| 24 | + |
| 25 | + if (filename) { |
| 26 | + chunkManifest = [chunk].reduce(function registerChunk(manifest, c) { |
| 27 | + if (c.id in manifest) return manifest; |
| 28 | + var hasRuntime = typeof c.hasRuntime === "function" |
| 29 | + ? c.hasRuntime() |
| 30 | + : c.entry; |
| 31 | + if (hasRuntime) { |
| 32 | + manifest[c.id] = undefined; |
| 33 | + } else { |
| 34 | + manifest[ |
| 35 | + c.id |
| 36 | + ] = mainTemplate.applyPluginsWaterfall("asset-path", filename, { |
| 37 | + hash: hash, |
| 38 | + chunk: c |
| 39 | + }); |
| 40 | + } |
| 41 | + return c.chunks.reduce(registerChunk, manifest); |
| 42 | + }, {}); |
| 43 | + oldChunkFilename = this.outputOptions.chunkFilename; |
| 44 | + this.outputOptions.chunkFilename = "__CHUNK_MANIFEST__"; |
| 45 | + // mark as asset for emitting |
| 46 | + compilation.assets[manifestFilename] = new RawSource( |
| 47 | + JSON.stringify(chunkManifest) |
| 48 | + ); |
| 49 | + chunk.files.push(manifestFilename); |
| 50 | + } |
| 51 | + |
| 52 | + return _; |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + compiler.plugin("compilation", function(compilation) { |
| 57 | + compilation.mainTemplate.plugin("require-ensure", function( |
| 58 | + _, |
| 59 | + chunk, |
| 60 | + hash, |
| 61 | + chunkIdVar |
| 62 | + ) { |
| 63 | + if (oldChunkFilename) { |
| 64 | + this.outputOptions.chunkFilename = oldChunkFilename; |
| 65 | + } |
| 66 | + |
| 67 | + return _.replace( |
| 68 | + '"__CHUNK_MANIFEST__"', |
| 69 | + 'window["' + manifestVariable + '"][' + chunkIdVar + "]" |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + if (inlineManifest) { |
| 74 | + compilation.plugin("html-webpack-plugin-before-html-generation", function( |
| 75 | + data, |
| 76 | + callback |
| 77 | + ) { |
| 78 | + var manifestHtml = |
| 79 | + "<script>window." + |
| 80 | + manifestVariable + |
| 81 | + "=" + |
| 82 | + JSON.stringify(chunkManifest) + |
| 83 | + "</script>"; |
| 84 | + callback(null, (data.assets[manifestVariable] = manifestHtml)); |
| 85 | + }); |
| 86 | + } |
| 87 | + }); |
| 88 | +}; |
0 commit comments