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

Commit e6c8dcf

Browse files
committed
Runtime source with chunk manifest lookup
1 parent 401e202 commit e6c8dcf

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import test from "ava";
2+
3+
import ChunkManifestPlugin from "../src/chunk-manifest-webpack-plugin";
4+
5+
/*compiler.plugin("compilation", function(compilation) {
6+
compilation.mainTemplate.plugin("require-ensure", function(
7+
source,
8+
chunk,
9+
hash,
10+
chunkIdVariableName
11+
) {
12+
if (oldChunkFilename) {
13+
this.outputOptions.chunkFilename = oldChunkFilename;
14+
}
15+
return source.replace(
16+
/"__CHUNK_MANIFEST__"/,
17+
`window["${manifestVariable}"][${chunkIdVariableName}]`
18+
);
19+
});
20+
});*/
21+
22+
test.cb("replace runtime with lookup", t => {
23+
const chunkIdVariableName = "a-chunk-id-variable";
24+
const manifestVariable = "manifest-variable";
25+
const placeholder = `"__CHUNK_MANIFEST__"`;
26+
27+
const runtimeSource = [
28+
"runtime-source-start",
29+
placeholder,
30+
";",
31+
"runtime-source-end"
32+
];
33+
34+
let expected = [...runtimeSource];
35+
expected[1] = `window["${manifestVariable}"][${chunkIdVariableName}]`;
36+
37+
const compilationPluginEvent = (compilationEvent, ensure) => {
38+
if (compilationEvent === "require-ensure") {
39+
const updatedSource = ensure(
40+
runtimeSource.join(""),
41+
undefined,
42+
undefined,
43+
chunkIdVariableName
44+
);
45+
t.is(updatedSource, expected.join(""));
46+
t.end();
47+
}
48+
};
49+
50+
const pluginEvent = (event, compile) => {
51+
if (event === "compilation") {
52+
const compilation = {
53+
mainTemplate: {
54+
plugin: compilationPluginEvent
55+
}
56+
};
57+
compile(compilation);
58+
}
59+
};
60+
61+
const fakeCompiler = { plugin: pluginEvent };
62+
63+
const plugin = new ChunkManifestPlugin({
64+
manifestVariable
65+
});
66+
67+
plugin.plugins = [{ apply: () => {} }];
68+
plugin.apply(fakeCompiler);
69+
});

0 commit comments

Comments
 (0)