add `chunkLoading: false` support

This commit is contained in:
Tobias Koppers 2021-11-09 19:57:57 +01:00
parent 3c17f90bb3
commit 9570a12d6b
7 changed files with 72 additions and 5 deletions

View File

@ -50,6 +50,7 @@ const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
* @property {Set<ChunkGroupInfo>} availableChildren set of chunk groups which depend on the this chunk group as availableSource
* @property {number} preOrderIndex next pre order index
* @property {number} postOrderIndex next post order index
* @property {boolean} asyncChunkLoading create async chunks
*/
/**
@ -304,7 +305,11 @@ const visitModules = (
availableSources: undefined,
availableChildren: undefined,
preOrderIndex: 0,
postOrderIndex: 0
postOrderIndex: 0,
asyncChunkLoading:
chunkGroup.options.chunkLoading === false
? false
: compilation.outputOptions.chunkLoading !== false
};
chunkGroup.index = nextChunkGroupIndex++;
if (chunkGroup.getNumberOfParents() > 0) {
@ -418,7 +423,11 @@ const visitModules = (
availableSources: undefined,
availableChildren: undefined,
preOrderIndex: 0,
postOrderIndex: 0
postOrderIndex: 0,
asyncChunkLoading:
entryOptions.chunkLoading !== undefined
? entryOptions.chunkLoading !== false
: chunkGroupInfo.asyncChunkLoading
};
chunkGroupInfoMap.set(entrypoint, cgi);
@ -442,8 +451,18 @@ const visitModules = (
chunkGroup: entrypoint,
chunkGroupInfo: cgi
});
} else if (!chunkGroupInfo.asyncChunkLoading) {
// Just queue the block into the current chunk group
queue.push({
action: PROCESS_BLOCK,
block: b,
module: module,
chunk,
chunkGroup,
chunkGroupInfo
});
} else {
cgi = namedChunkGroups.get(chunkName);
cgi = chunkName && namedChunkGroups.get(chunkName);
if (!cgi) {
c = compilation.addChunkInGroup(
b.groupOptions || b.chunkName,
@ -464,7 +483,8 @@ const visitModules = (
availableSources: undefined,
availableChildren: undefined,
preOrderIndex: 0,
postOrderIndex: 0
postOrderIndex: 0,
asyncChunkLoading: chunkGroupInfo.asyncChunkLoading
};
allCreatedChunkGroups.add(c);
chunkGroupInfoMap.set(c, cgi);
@ -518,7 +538,7 @@ const visitModules = (
chunkGroup: c,
chunkGroupInfo: cgi
});
} else {
} else if (entrypoint !== undefined) {
chunkGroupInfo.chunkGroup.addAsyncEntrypoint(entrypoint);
}
};

View File

@ -0,0 +1,12 @@
import fs from "fs";
it("should load chunks on demand", async () => {
expect((await import("./async")).default).toEqual(42);
expect((await (await import("./async")).nested()).default).toEqual(43);
expect(fs.readFileSync(__filename, "utf-8")).not.toContain(
"This is the" + " async chunk"
);
expect(fs.readFileSync(__filename, "utf-8")).not.toContain(
"This is the" + " nested async chunk"
);
});

View File

@ -0,0 +1,3 @@
// This is the async chunk
export default 42;
export const nested = () => import("./nested");

View File

@ -0,0 +1,12 @@
import fs from "fs";
it("should include all async imports in the main chunk", async () => {
expect((await import("./async")).default).toEqual(42);
expect((await (await import("./async")).nested()).default).toEqual(43);
expect(fs.readFileSync(__filename, "utf-8")).toContain(
"This is the async chunk"
);
expect(fs.readFileSync(__filename, "utf-8")).toContain(
"This is the nested async chunk"
);
});

View File

@ -0,0 +1,2 @@
// This is the nested async chunk
export default 43;

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["./a.js", "./b.js"];
}
};

View File

@ -0,0 +1,13 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
entry: {
a: "./a.js",
b: {
import: "./b.js",
chunkLoading: false
}
},
output: {
filename: "[name].js"
}
};