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

Commit 2b0b764

Browse files
committed
Generate chunk manifest asset
1 parent 18bacb7 commit 2b0b764

File tree

2 files changed

+86
-3
lines changed

2 files changed

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

0 commit comments

Comments
 (0)