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

Commit 97a5d54

Browse files
committed
Generate chunk manifest asset
1 parent 18bacb7 commit 97a5d54

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

src/chunk-manifest-webpack-plugin.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ChunkManifestPlugin {
4242
if (c.hasRuntime()) {
4343
manifest[c.id] = undefined;
4444
} else {
45-
const asyncAssets = mainTemplate.applyPluginsWaterfall(
45+
const assetFilename = mainTemplate.applyPluginsWaterfall(
4646
"asset-path",
4747
chunkFilename,
4848
{
@@ -51,14 +51,15 @@ class ChunkManifestPlugin {
5151
}
5252
);
5353

54-
manifest[c.id] = asyncAssets;
54+
manifest[c.id] = assetFilename;
5555
}
56+
5657
return c.chunks.reduce(registerChunk, manifest);
5758
},
5859
{});
5960

6061
this.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";
61-
// mark as asset for emitting
62+
6263
compilation.assets[manifestFilename] = new RawSource(
6364
JSON.stringify(chunkManifest)
6465
);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import test from "ava";
2+
3+
import ChunkManifestPlugin from "../src/chunk-manifest-webpack-plugin";
4+
5+
const RawSource = require("webpack-sources").RawSource;
6+
7+
test.cb("generate asset for chunk manifest", t => {
8+
const manifestFilename = "a.manifest";
9+
const filenamePlaceholder = "[filename]";
10+
const moduleSource = "";
11+
12+
const chunk = {
13+
id: 1,
14+
hasRuntime: () => true,
15+
chunks: [
16+
{
17+
id: 2,
18+
hasRuntime: () => false,
19+
chunks: []
20+
},
21+
{
22+
id: 2
23+
}
24+
]
25+
};
26+
27+
let expected = {};
28+
expected[manifestFilename] = new RawSource(
29+
JSON.stringify({
30+
"2": "2-a1234.js"
31+
})
32+
);
33+
34+
const compilationPluginEvent = (compilationEvent, ensure) => {
35+
if (compilationEvent === "require-ensure") {
36+
ensure.apply(
37+
{
38+
outputOptions: {
39+
chunkFilename: filenamePlaceholder
40+
}
41+
},
42+
[undefined, chunk, "a1234"]
43+
);
44+
}
45+
};
46+
47+
const applyPluginsWaterfall = (event, filename, data) => {
48+
if (event === "asset-path")
49+
return filename.replace(
50+
filenamePlaceholder,
51+
`${data.chunk.id}-${data.hash}.js`
52+
);
53+
54+
t.fail();
55+
};
56+
57+
const pluginEvent = (event, compile) => {
58+
if (event === "this-compilation") {
59+
const compilation = {
60+
mainTemplate: {
61+
plugin: compilationPluginEvent,
62+
applyPluginsWaterfall
63+
},
64+
assets: {}
65+
};
66+
67+
compile(compilation);
68+
69+
t.deepEqual(compilation.assets, expected);
70+
t.end();
71+
}
72+
};
73+
74+
const fakeCompiler = { plugin: pluginEvent };
75+
76+
const plugin = new ChunkManifestPlugin({
77+
filename: manifestFilename
78+
});
79+
80+
plugin.apply(fakeCompiler);
81+
});

0 commit comments

Comments
 (0)