Skip to content

Commit db668b7

Browse files
committed
move fake document to ConfigTestCases
minor style issues add test case
1 parent 0d21ce1 commit db668b7

File tree

9 files changed

+161
-138
lines changed

9 files changed

+161
-138
lines changed

lib/Chunk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ class Chunk {
610610
getChildIdsByOrdersMap(includeDirectChildren) {
611611
const chunkMaps = Object.create(null);
612612

613-
function addChildIdsByOrdersToMap(chunk) {
613+
const addChildIdsByOrdersToMap = chunk => {
614614
const data = chunk.getChildIdsByOrders();
615615
for (const key of Object.keys(data)) {
616616
let chunkMap = chunkMaps[key];
@@ -619,7 +619,7 @@ class Chunk {
619619
}
620620
chunkMap[chunk.id] = data[key];
621621
}
622-
}
622+
};
623623

624624
if (includeDirectChildren) {
625625
addChildIdsByOrdersToMap(this);

lib/MainTemplate.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ module.exports = class MainTemplate extends Tapable {
9494
requireExtensions: new SyncWaterfallHook(["source", "chunk", "hash"]),
9595
beforeStartup: new SyncWaterfallHook(["source", "chunk", "hash"]),
9696
startup: new SyncWaterfallHook(["source", "chunk", "hash"]),
97+
afterStartup: new SyncWaterfallHook(["source", "chunk", "hash"]),
9798
render: new SyncWaterfallHook([
9899
"source",
99100
"chunk",
@@ -132,7 +133,7 @@ module.exports = class MainTemplate extends Tapable {
132133
if (chunk.entryModule) {
133134
buf.push("// Load entry module and return exports");
134135
buf.push(
135-
`return ${this.renderRequireFunctionForModule(
136+
`bootstrapReturn = ${this.renderRequireFunctionForModule(
136137
hash,
137138
chunk,
138139
JSON.stringify(chunk.entryModule.id)
@@ -386,8 +387,11 @@ module.exports = class MainTemplate extends Tapable {
386387
Template.asString(this.hooks.requireExtensions.call("", chunk, hash))
387388
);
388389
buf.push("");
390+
buf.push("var bootstrapReturn;");
389391
buf.push(Template.asString(this.hooks.beforeStartup.call("", chunk, hash)));
390392
buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
393+
buf.push(Template.asString(this.hooks.afterStartup.call("", chunk, hash)));
394+
buf.push("return bootstrapReturn;");
391395
let source = this.hooks.render.call(
392396
new OriginalSource(
393397
Template.prefix(buf, " \t") + "\n",

lib/web/JsonpMainTemplatePlugin.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,21 @@ class JsonpMainTemplatePlugin {
236236
mainTemplate.hooks.linkPrefetch.tap(
237237
"JsonpMainTemplatePlugin",
238238
(_, chunk, hash) => {
239+
const crossOriginLoading =
240+
mainTemplate.outputOptions.crossOriginLoading;
241+
239242
return Template.asString([
240243
"var link = document.createElement('link');",
244+
crossOriginLoading
245+
? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
246+
: "",
247+
`if (${mainTemplate.requireFn}.nc) {`,
248+
Template.indent(
249+
`link.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
250+
),
251+
"}",
241252
'link.rel = "prefetch";',
253+
'link.as = "script";',
242254
"link.href = jsonpScriptSrc(chunkId);"
243255
]);
244256
}
@@ -292,7 +304,7 @@ class JsonpMainTemplatePlugin {
292304
"",
293305
"// chunk preloadng for javascript",
294306
"",
295-
`var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")}`,
307+
`var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")};`,
296308
"",
297309
"var chunkPreloadData = chunkPreloadMap[chunkId];",
298310
"if(chunkPreloadData) {",
@@ -344,7 +356,7 @@ class JsonpMainTemplatePlugin {
344356
"var chunkIds = data[0];",
345357
"var moreModules = data[1];",
346358
withDefer ? "var executeModules = data[2];" : "",
347-
...(withPrefetch ? ["var prefetchChunks = data[3] || []"] : []),
359+
withPrefetch ? "var prefetchChunks = data[3] || [];" : "",
348360
'// add "moreModules" to the modules object,',
349361
'// then flag all "chunkIds" as loaded and fire callback',
350362
"var moduleId, chunkId, i = 0, resolves = [];",
@@ -382,11 +394,11 @@ class JsonpMainTemplatePlugin {
382394
"deferredModules.push.apply(deferredModules, executeModules || []);",
383395
"",
384396
"// run deferred modules when all chunks ready",
385-
"return checkDeferredModules();"
397+
"var deferResult = checkDeferredModules();"
386398
])
387399
: "",
388-
...(withPrefetch
389-
? [
400+
withPrefetch
401+
? Template.asString([
390402
"// chunk prefetching for javascript",
391403
"var head = document.getElementsByTagName('head')[0];",
392404
"prefetchChunks.forEach(function(chunkId) {",
@@ -400,8 +412,9 @@ class JsonpMainTemplatePlugin {
400412
"}"
401413
]),
402414
"});"
403-
]
404-
: [])
415+
])
416+
: "",
417+
withDefer ? "return deferResult;" : ""
405418
]),
406419
"};",
407420
withDefer
@@ -463,7 +476,7 @@ class JsonpMainTemplatePlugin {
463476
return source;
464477
}
465478
);
466-
mainTemplate.hooks.beforeStartup.tap(
479+
mainTemplate.hooks.afterStartup.tap(
467480
"JsonpMainTemplatePlugin",
468481
(source, chunk, hash) => {
469482
const prefetchChunks = chunk.getChildIdsByOrders().prefetch;
@@ -474,9 +487,9 @@ class JsonpMainTemplatePlugin {
474487
) {
475488
return Template.asString([
476489
source,
477-
`webpackJsonpCallback([${JSON.stringify(
478-
chunk.ids
479-
)}, {}, null, ${JSON.stringify(prefetchChunks)}])`
490+
`webpackJsonpCallback([[], {}, 0, ${JSON.stringify(
491+
prefetchChunks
492+
)}]);`
480493
]);
481494
}
482495
return source;
@@ -500,7 +513,7 @@ class JsonpMainTemplatePlugin {
500513
.map(e => JSON.stringify(e))
501514
.join(", ")});`,
502515
"// run deferred modules when ready",
503-
"return checkDeferredModules();"
516+
"bootstrapReturn = checkDeferredModules();"
504517
]);
505518
} else {
506519
return Template.asString([

test/ConfigTestCases.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const vm = require("vm");
77
const mkdirp = require("mkdirp");
88
const rimraf = require("rimraf");
99
const checkArrayExpectation = require("./checkArrayExpectation");
10+
const FakeDocument = require("./helpers/FakeDocument");
1011

1112
const Stats = require("../lib/Stats");
1213
const webpack = require("../lib/webpack");
@@ -176,7 +177,8 @@ describe("ConfigTestCases", () => {
176177
console: console,
177178
expect: expect,
178179
setTimeout: setTimeout,
179-
clearTimeout: clearTimeout
180+
clearTimeout: clearTimeout,
181+
document: new FakeDocument()
180182
};
181183

182184
function _require(currentDirectory, module) {

0 commit comments

Comments
 (0)