Skip to content

Commit

Permalink
move fake document to ConfigTestCases
Browse files Browse the repository at this point in the history
minor style issues
add test case
  • Loading branch information
sokra committed Jun 4, 2018
1 parent 0d21ce1 commit db668b7
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 138 deletions.
4 changes: 2 additions & 2 deletions lib/Chunk.js
Expand Up @@ -610,7 +610,7 @@ class Chunk {
getChildIdsByOrdersMap(includeDirectChildren) {
const chunkMaps = Object.create(null);

function addChildIdsByOrdersToMap(chunk) {
const addChildIdsByOrdersToMap = chunk => {
const data = chunk.getChildIdsByOrders();
for (const key of Object.keys(data)) {
let chunkMap = chunkMaps[key];
Expand All @@ -619,7 +619,7 @@ class Chunk {
}
chunkMap[chunk.id] = data[key];
}
}
};

if (includeDirectChildren) {
addChildIdsByOrdersToMap(this);
Expand Down
6 changes: 5 additions & 1 deletion lib/MainTemplate.js
Expand Up @@ -94,6 +94,7 @@ module.exports = class MainTemplate extends Tapable {
requireExtensions: new SyncWaterfallHook(["source", "chunk", "hash"]),
beforeStartup: new SyncWaterfallHook(["source", "chunk", "hash"]),
startup: new SyncWaterfallHook(["source", "chunk", "hash"]),
afterStartup: new SyncWaterfallHook(["source", "chunk", "hash"]),
render: new SyncWaterfallHook([
"source",
"chunk",
Expand Down Expand Up @@ -132,7 +133,7 @@ module.exports = class MainTemplate extends Tapable {
if (chunk.entryModule) {
buf.push("// Load entry module and return exports");
buf.push(
`return ${this.renderRequireFunctionForModule(
`bootstrapReturn = ${this.renderRequireFunctionForModule(
hash,
chunk,
JSON.stringify(chunk.entryModule.id)
Expand Down Expand Up @@ -386,8 +387,11 @@ module.exports = class MainTemplate extends Tapable {
Template.asString(this.hooks.requireExtensions.call("", chunk, hash))
);
buf.push("");
buf.push("var bootstrapReturn;");
buf.push(Template.asString(this.hooks.beforeStartup.call("", chunk, hash)));
buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
buf.push(Template.asString(this.hooks.afterStartup.call("", chunk, hash)));
buf.push("return bootstrapReturn;");
let source = this.hooks.render.call(
new OriginalSource(
Template.prefix(buf, " \t") + "\n",
Expand Down
37 changes: 25 additions & 12 deletions lib/web/JsonpMainTemplatePlugin.js
Expand Up @@ -236,9 +236,21 @@ class JsonpMainTemplatePlugin {
mainTemplate.hooks.linkPrefetch.tap(
"JsonpMainTemplatePlugin",
(_, chunk, hash) => {
const crossOriginLoading =
mainTemplate.outputOptions.crossOriginLoading;

return Template.asString([
"var link = document.createElement('link');",
crossOriginLoading
? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
: "",
`if (${mainTemplate.requireFn}.nc) {`,
Template.indent(
`link.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
),
"}",
'link.rel = "prefetch";',
'link.as = "script";',
"link.href = jsonpScriptSrc(chunkId);"
]);
}
Expand Down Expand Up @@ -292,7 +304,7 @@ class JsonpMainTemplatePlugin {
"",
"// chunk preloadng for javascript",
"",
`var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")}`,
`var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")};`,
"",
"var chunkPreloadData = chunkPreloadMap[chunkId];",
"if(chunkPreloadData) {",
Expand Down Expand Up @@ -344,7 +356,7 @@ class JsonpMainTemplatePlugin {
"var chunkIds = data[0];",
"var moreModules = data[1];",
withDefer ? "var executeModules = data[2];" : "",
...(withPrefetch ? ["var prefetchChunks = data[3] || []"] : []),
withPrefetch ? "var prefetchChunks = data[3] || [];" : "",
'// add "moreModules" to the modules object,',
'// then flag all "chunkIds" as loaded and fire callback',
"var moduleId, chunkId, i = 0, resolves = [];",
Expand Down Expand Up @@ -382,11 +394,11 @@ class JsonpMainTemplatePlugin {
"deferredModules.push.apply(deferredModules, executeModules || []);",
"",
"// run deferred modules when all chunks ready",
"return checkDeferredModules();"
"var deferResult = checkDeferredModules();"
])
: "",
...(withPrefetch
? [
withPrefetch
? Template.asString([
"// chunk prefetching for javascript",
"var head = document.getElementsByTagName('head')[0];",
"prefetchChunks.forEach(function(chunkId) {",
Expand All @@ -400,8 +412,9 @@ class JsonpMainTemplatePlugin {
"}"
]),
"});"
]
: [])
])
: "",
withDefer ? "return deferResult;" : ""
]),
"};",
withDefer
Expand Down Expand Up @@ -463,7 +476,7 @@ class JsonpMainTemplatePlugin {
return source;
}
);
mainTemplate.hooks.beforeStartup.tap(
mainTemplate.hooks.afterStartup.tap(
"JsonpMainTemplatePlugin",
(source, chunk, hash) => {
const prefetchChunks = chunk.getChildIdsByOrders().prefetch;
Expand All @@ -474,9 +487,9 @@ class JsonpMainTemplatePlugin {
) {
return Template.asString([
source,
`webpackJsonpCallback([${JSON.stringify(
chunk.ids
)}, {}, null, ${JSON.stringify(prefetchChunks)}])`
`webpackJsonpCallback([[], {}, 0, ${JSON.stringify(
prefetchChunks
)}]);`
]);
}
return source;
Expand All @@ -500,7 +513,7 @@ class JsonpMainTemplatePlugin {
.map(e => JSON.stringify(e))
.join(", ")});`,
"// run deferred modules when ready",
"return checkDeferredModules();"
"bootstrapReturn = checkDeferredModules();"
]);
} else {
return Template.asString([
Expand Down
4 changes: 3 additions & 1 deletion test/ConfigTestCases.test.js
Expand Up @@ -7,6 +7,7 @@ const vm = require("vm");
const mkdirp = require("mkdirp");
const rimraf = require("rimraf");
const checkArrayExpectation = require("./checkArrayExpectation");
const FakeDocument = require("./helpers/FakeDocument");

const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
Expand Down Expand Up @@ -176,7 +177,8 @@ describe("ConfigTestCases", () => {
console: console,
expect: expect,
setTimeout: setTimeout,
clearTimeout: clearTimeout
clearTimeout: clearTimeout,
document: new FakeDocument()
};

function _require(currentDirectory, module) {
Expand Down

0 comments on commit db668b7

Please sign in to comment.