Skip to content

Commit b642403

Browse files
committed
Add output.entryPrefetchFunction option, and don't immediately prefetch
Rather than calling the prefetch function on its own during bootstrap, this change exposes the function as a function that can be called from the compiled code.
1 parent eefacf3 commit b642403

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/WebpackOptionsDefaulter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
161161
this.set("output.hashDigestLength", 20);
162162
this.set("output.devtoolLineToLine", false);
163163
this.set("output.strictModuleExceptionHandling", false);
164+
this.set("output.entryPrefetchFunction", "make", options => {
165+
return Template.toIdentifier(
166+
"webpackEntryPrefetch" + Template.toIdentifier(options.output.library)
167+
);
168+
});
164169

165170
this.set("node", "call", value => {
166171
if (typeof value === "boolean") {

lib/web/JsonpMainTemplatePlugin.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class JsonpMainTemplatePlugin {
114114
});
115115
};
116116

117-
const linkPreload = mainTemplate => {
117+
const linkPreload = () => {
118118
const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading;
119119
const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
120120
return Template.asString([
@@ -181,16 +181,19 @@ class JsonpMainTemplatePlugin {
181181
}
182182
if (needEntryChunkPrefetch(chunk)) {
183183
let preloadPrefetchChildren = chunk.getChildIdsByOrders();
184+
let entryPrefetchFunction = mainTemplate.outputOptions.entryPrefetchFunction;
185+
let globalObject = mainTemplate.outputOptions.globalObject;
186+
184187
extraCode.push(
185188
"",
186189
"// preload or prefetch split chunks from entry chunk",
187-
"(function prefetchOrPreloadFromEntry() {",
190+
`${globalObject}['${entryPrefetchFunction}'] = () => {`,
188191
preloadPrefetchChildren.preload
189192
? Template.indent([
190193
`${JSON.stringify(
191194
preloadPrefetchChildren.preload
192195
)}.map(chunkId => {`,
193-
Template.indent([linkPreload(mainTemplate)]),
196+
Template.indent([linkPreload()]),
194197
`});`
195198
])
196199
: "",
@@ -203,7 +206,7 @@ class JsonpMainTemplatePlugin {
203206
`});`
204207
])
205208
: "",
206-
"})();"
209+
"}",
207210
);
208211
}
209212
if (extraCode.length === 0) return source;
@@ -271,7 +274,7 @@ class JsonpMainTemplatePlugin {
271274
mainTemplate.hooks.linkPreload.tap(
272275
"JsonpMainTemplatePlugin",
273276
(_, chunk, hash) => {
274-
return linkPreload(mainTemplate);
277+
return linkPreload();
275278
}
276279
);
277280
mainTemplate.hooks.linkPrefetch.tap(

test/configCases/web/prefetch-preload/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ it("should prefetch and preload child chunks on chunk load", (done) => {
2121
__webpack_nonce__ = "nonce";
2222
__webpack_public_path__ = "/public/path/";
2323

24-
const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1");
24+
const promise = import(/* webpackChunkName: "chunk1", webpackPrefetch: true */ "./chunk1");
2525
expect(document.head._children).toHaveLength(2);
2626
const script = document.head._children[0];
2727
expect(script._type).toBe("script");

0 commit comments

Comments
 (0)