Skip to content

Commit

Permalink
Merge pull request #14692 from webpack/feature/chunk-loading-false
Browse files Browse the repository at this point in the history
add `chunkLoading: false` support
  • Loading branch information
sokra committed Nov 9, 2021
2 parents 3c17f90 + 9570a12 commit 9cdd167
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/buildChunkGraph.js
Expand Up @@ -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
*/

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -518,7 +538,7 @@ const visitModules = (
chunkGroup: c,
chunkGroupInfo: cgi
});
} else {
} else if (entrypoint !== undefined) {
chunkGroupInfo.chunkGroup.addAsyncEntrypoint(entrypoint);
}
};
Expand Down
12 changes: 12 additions & 0 deletions test/configCases/entry/no-chunking/a.js
@@ -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"
);
});
3 changes: 3 additions & 0 deletions test/configCases/entry/no-chunking/async.js
@@ -0,0 +1,3 @@
// This is the async chunk
export default 42;
export const nested = () => import("./nested");
12 changes: 12 additions & 0 deletions test/configCases/entry/no-chunking/b.js
@@ -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"
);
});
2 changes: 2 additions & 0 deletions test/configCases/entry/no-chunking/nested.js
@@ -0,0 +1,2 @@
// This is the nested async chunk
export default 43;
5 changes: 5 additions & 0 deletions test/configCases/entry/no-chunking/test.config.js
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["./a.js", "./b.js"];
}
};
13 changes: 13 additions & 0 deletions test/configCases/entry/no-chunking/webpack.config.js
@@ -0,0 +1,13 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
entry: {
a: "./a.js",
b: {
import: "./b.js",
chunkLoading: false
}
},
output: {
filename: "[name].js"
}
};

0 comments on commit 9cdd167

Please sign in to comment.