Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit 0e01a4f

Browse files
committed
Refactor plugin to ES2015 class
1 parent 7d5b186 commit 0e01a4f

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed
Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,74 @@
1+
"use strict";
2+
13
var RawSource = require("webpack-core/lib/RawSource");
24

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+
}
911

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;
1617

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;
2223

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+
}
4849

49-
return _;
50+
return _;
51+
});
5052
});
51-
});
5253

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+
}
6364

64-
return _.replace(
65-
'"__CHUNK_MANIFEST__"',
66-
'window["' + manifestVariable + '"][' + chunkIdVar + "]"
67-
);
65+
return _.replace(
66+
'"__CHUNK_MANIFEST__"',
67+
'window["' + manifestVariable + '"][' + chunkIdVar + "]"
68+
);
69+
});
6870
});
69-
});
70-
};
71+
}
72+
}
73+
74+
module.exports = ChunkManifestPlugin;

0 commit comments

Comments
 (0)