Skip to content

Commit

Permalink
Add output.entryPrefetchFunction option, and don't immediately prefetch
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MLoughry committed May 29, 2018
1 parent eefacf3 commit b642403
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/WebpackOptionsDefaulter.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
this.set("output.hashDigestLength", 20);
this.set("output.devtoolLineToLine", false);
this.set("output.strictModuleExceptionHandling", false);
this.set("output.entryPrefetchFunction", "make", options => {
return Template.toIdentifier(
"webpackEntryPrefetch" + Template.toIdentifier(options.output.library)
);
});

this.set("node", "call", value => {
if (typeof value === "boolean") {
Expand Down
13 changes: 8 additions & 5 deletions lib/web/JsonpMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class JsonpMainTemplatePlugin {
});
};

const linkPreload = mainTemplate => {
const linkPreload = () => {
const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading;
const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
return Template.asString([
Expand Down Expand Up @@ -181,16 +181,19 @@ class JsonpMainTemplatePlugin {
}
if (needEntryChunkPrefetch(chunk)) {
let preloadPrefetchChildren = chunk.getChildIdsByOrders();
let entryPrefetchFunction = mainTemplate.outputOptions.entryPrefetchFunction;
let globalObject = mainTemplate.outputOptions.globalObject;

extraCode.push(
"",
"// preload or prefetch split chunks from entry chunk",
"(function prefetchOrPreloadFromEntry() {",
`${globalObject}['${entryPrefetchFunction}'] = () => {`,
preloadPrefetchChildren.preload
? Template.indent([
`${JSON.stringify(
preloadPrefetchChildren.preload
)}.map(chunkId => {`,
Template.indent([linkPreload(mainTemplate)]),
Template.indent([linkPreload()]),
`});`
])
: "",
Expand All @@ -203,7 +206,7 @@ class JsonpMainTemplatePlugin {
`});`
])
: "",
"})();"
"}",
);
}
if (extraCode.length === 0) return source;
Expand Down Expand Up @@ -271,7 +274,7 @@ class JsonpMainTemplatePlugin {
mainTemplate.hooks.linkPreload.tap(
"JsonpMainTemplatePlugin",
(_, chunk, hash) => {
return linkPreload(mainTemplate);
return linkPreload();
}
);
mainTemplate.hooks.linkPrefetch.tap(
Expand Down
2 changes: 1 addition & 1 deletion test/configCases/web/prefetch-preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ it("should prefetch and preload child chunks on chunk load", (done) => {
__webpack_nonce__ = "nonce";
__webpack_public_path__ = "/public/path/";

const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1");
const promise = import(/* webpackChunkName: "chunk1", webpackPrefetch: true */ "./chunk1");
expect(document.head._children).toHaveLength(2);
const script = document.head._children[0];
expect(script._type).toBe("script");
Expand Down

0 comments on commit b642403

Please sign in to comment.