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

Commit 581d0eb

Browse files
committed
Replace runtime's chunk manifest with lookup
1 parent 401e202 commit 581d0eb

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/chunk-manifest-webpack-plugin.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ChunkManifestPlugin {
2424

2525
compiler.plugin("this-compilation", function(compilation) {
2626
const mainTemplate = compilation.mainTemplate;
27-
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
27+
mainTemplate.plugin("require-ensure", function(source, chunk, hash) {
2828
const filename =
2929
this.outputOptions.chunkFilename || this.outputOptions.filename;
3030

@@ -61,24 +61,27 @@ class ChunkManifestPlugin {
6161
);
6262
}
6363

64-
return _;
64+
return source;
6565
});
6666
});
6767

6868
compiler.plugin("compilation", function(compilation) {
6969
compilation.mainTemplate.plugin("require-ensure", function(
70-
_,
70+
source,
7171
chunk,
7272
hash,
7373
chunkIdVariableName
7474
) {
7575
if (oldChunkFilename) {
7676
this.outputOptions.chunkFilename = oldChunkFilename;
7777
}
78-
return _.replace(
78+
79+
const updatedSource = source.replace(
7980
/"__CHUNK_MANIFEST__"/,
8081
`window["${manifestVariable}"][${chunkIdVariableName}]`
8182
);
83+
84+
return updatedSource;
8285
});
8386
});
8487
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import test from "ava";
2+
3+
import ChunkManifestPlugin from "../src/chunk-manifest-webpack-plugin";
4+
5+
test.cb("replace runtime's chunk manifest with lookup", t => {
6+
const chunkIdVariableName = "a-chunk-id-variable";
7+
const manifestVariable = "manifest-variable";
8+
const placeholder = `"__CHUNK_MANIFEST__"`;
9+
10+
const runtimeSource = [
11+
"runtime-source-start",
12+
placeholder,
13+
";",
14+
"runtime-source-end"
15+
];
16+
17+
let expected = [...runtimeSource];
18+
expected[1] = `window["${manifestVariable}"][${chunkIdVariableName}]`;
19+
20+
const compilationPluginEvent = (compilationEvent, ensure) => {
21+
if (compilationEvent === "require-ensure") {
22+
const updatedSource = ensure(
23+
runtimeSource.join(""),
24+
undefined,
25+
undefined,
26+
chunkIdVariableName
27+
);
28+
t.is(updatedSource, expected.join(""));
29+
t.end();
30+
}
31+
};
32+
33+
const pluginEvent = (event, compile) => {
34+
if (event === "compilation") {
35+
const compilation = {
36+
mainTemplate: {
37+
plugin: compilationPluginEvent
38+
}
39+
};
40+
compile(compilation);
41+
}
42+
};
43+
44+
const fakeCompiler = { plugin: pluginEvent };
45+
46+
const plugin = new ChunkManifestPlugin({
47+
manifestVariable
48+
});
49+
50+
plugin.plugins = [{ apply: () => {} }];
51+
plugin.apply(fakeCompiler);
52+
});

0 commit comments

Comments
 (0)