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