mirror of https://github.com/webpack/webpack.git
Split the map among the chunks
This commit is contained in:
parent
685cbcdf54
commit
8a995da749
13
lib/Chunk.js
13
lib/Chunk.js
|
|
@ -607,10 +607,10 @@ class Chunk {
|
|||
return result;
|
||||
}
|
||||
|
||||
getChildIdsByOrdersMap(includeImmediateChildren) {
|
||||
getChildIdsByOrdersMap() {
|
||||
const chunkMaps = Object.create(null);
|
||||
|
||||
function addChunkChildrenToMap(chunk) {
|
||||
for (const chunk of this.getAllAsyncChunks()) {
|
||||
const data = chunk.getChildIdsByOrders();
|
||||
for (const key of Object.keys(data)) {
|
||||
let chunkMap = chunkMaps[key];
|
||||
|
|
@ -620,15 +620,6 @@ class Chunk {
|
|||
chunkMap[chunk.id] = data[key];
|
||||
}
|
||||
}
|
||||
|
||||
for (const chunk of this.getAllAsyncChunks()) {
|
||||
addChunkChildrenToMap(chunk);
|
||||
}
|
||||
|
||||
if (includeImmediateChildren) {
|
||||
addChunkChildrenToMap(this);
|
||||
}
|
||||
|
||||
return chunkMaps;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,16 @@ class JsonpChunkTemplatePlugin {
|
|||
);
|
||||
if (entries.length > 0) {
|
||||
source.add(`,${JSON.stringify(entries)}`);
|
||||
} else {
|
||||
source.add(`,null`);
|
||||
}
|
||||
if (
|
||||
chunk.getChildIdsByOrders().prefetch &&
|
||||
chunk.getChildIdsByOrders().prefetch.length
|
||||
) {
|
||||
source.add(
|
||||
`,${JSON.stringify(chunk.getChildIdsByOrders().prefetch)}`
|
||||
);
|
||||
}
|
||||
source.add("])");
|
||||
return source;
|
||||
|
|
|
|||
|
|
@ -330,7 +330,6 @@ class JsonpMainTemplatePlugin {
|
|||
(source, chunk, hash) => {
|
||||
if (needChunkLoadingCode(chunk)) {
|
||||
const withDefer = needEntryDeferringCode(chunk);
|
||||
const chunkMap = chunk.getChildIdsByOrdersMap(true).prefetch;
|
||||
return Template.asString([
|
||||
source,
|
||||
"",
|
||||
|
|
@ -340,6 +339,7 @@ class JsonpMainTemplatePlugin {
|
|||
"var chunkIds = data[0];",
|
||||
"var moreModules = data[1];",
|
||||
withDefer ? "var executeModules = data[2];" : "",
|
||||
"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 = [];",
|
||||
|
|
@ -382,31 +382,18 @@ class JsonpMainTemplatePlugin {
|
|||
: "",
|
||||
"",
|
||||
"// chunk prefetching for javascript",
|
||||
"",
|
||||
`var chunkPrefetchMap = ${JSON.stringify(chunkMap, null, "\t")}`,
|
||||
"",
|
||||
"for(let j = 0;j < chunkIds.length; j++) {",
|
||||
"var head = document.getElementsByTagName('head')[0];",
|
||||
"prefetchChunks.forEach(function(chunkId) {",
|
||||
Template.indent([
|
||||
"chunkId = chunkIds[j];",
|
||||
"var chunkPrefetchData = chunkPrefetchMap[chunkId];",
|
||||
"if(chunkPrefetchData) {",
|
||||
"if(installedChunks[chunkId] === undefined) {",
|
||||
Template.indent([
|
||||
"var head = document.getElementsByTagName('head')[0];",
|
||||
"chunkPrefetchData.forEach(function(chunkId) {",
|
||||
Template.indent([
|
||||
"if(installedChunks[chunkId] === undefined) {",
|
||||
Template.indent([
|
||||
"installedChunks[chunkId] = null;",
|
||||
mainTemplate.hooks.linkPrefetch.call("", chunk, hash),
|
||||
"head.appendChild(link);"
|
||||
]),
|
||||
"}"
|
||||
]),
|
||||
"});"
|
||||
"installedChunks[chunkId] = null;",
|
||||
mainTemplate.hooks.linkPrefetch.call("", chunk, hash),
|
||||
"head.appendChild(link);"
|
||||
]),
|
||||
"}"
|
||||
]),
|
||||
"}"
|
||||
"});"
|
||||
]),
|
||||
"};",
|
||||
withDefer
|
||||
|
|
@ -474,7 +461,11 @@ class JsonpMainTemplatePlugin {
|
|||
if (needChunkLoadingCode(chunk)) {
|
||||
return Template.asString([
|
||||
source,
|
||||
`webpackJsonpCallback([${JSON.stringify(chunk.ids)}, {}])`
|
||||
`webpackJsonpCallback([${JSON.stringify(
|
||||
chunk.ids
|
||||
)}, {}, null, ${JSON.stringify(
|
||||
chunk.getChildIdsByOrders().prefetch
|
||||
)}])`
|
||||
]);
|
||||
}
|
||||
return source;
|
||||
|
|
|
|||
Loading…
Reference in New Issue