Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a87f569

Browse files
authored
fix(esm): fix the import of missing dependency chunks (#19782)
1 parent 4fdb0e1 commit a87f569

File tree

26 files changed

+308
-27
lines changed

26 files changed

+308
-27
lines changed

lib/esm/ModuleChunkFormatPlugin.js

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,30 @@ class ModuleChunkFormatPlugin {
168168
PLUGIN_NAME,
169169
(modules, _lastModule, renderContext) => {
170170
const { chunk, chunkGraph } = renderContext;
171-
if (!chunk.hasRuntime()) {
172-
return modules;
173-
}
174-
const entryDependentChunks =
175-
chunkGraph.getChunkEntryDependentChunksIterable(chunk);
176-
const sourceWithDependentChunks = withDependentChunks(
177-
/** @type {Set<Chunk>} */ (entryDependentChunks),
178-
chunkGraph,
179-
chunk
180-
);
181-
if (!sourceWithDependentChunks) {
182-
return modules;
183-
}
184-
if (modules.size() === 0) {
185-
return sourceWithDependentChunks;
171+
if (
172+
chunkGraph.getNumberOfEntryModules(chunk) > 0 &&
173+
chunk.hasRuntime()
174+
) {
175+
const entryDependentChunks =
176+
chunkGraph.getChunkEntryDependentChunksIterable(chunk);
177+
const sourceWithDependentChunks = withDependentChunks(
178+
/** @type {Set<Chunk>} */ (entryDependentChunks),
179+
chunkGraph,
180+
chunk
181+
);
182+
if (!sourceWithDependentChunks) {
183+
return modules;
184+
}
185+
if (modules.size() === 0) {
186+
return sourceWithDependentChunks;
187+
}
188+
const source = new ConcatSource();
189+
source.add(sourceWithDependentChunks);
190+
source.add("\n");
191+
source.add(modules);
192+
return source;
186193
}
187-
const source = new ConcatSource();
188-
source.add(sourceWithDependentChunks);
189-
source.add("\n");
190-
source.add(modules);
191-
return source;
194+
return modules;
192195
}
193196
);
194197
hooks.renderChunk.tap(PLUGIN_NAME, (modules, renderContext) => {
@@ -238,20 +241,14 @@ class ModuleChunkFormatPlugin {
238241
}
239242
const final = i + 1 === entries.length;
240243
const moduleId = chunkGraph.getModuleId(module);
241-
const entryDependentChunks = /** @type {Set<Chunk>} */ (
242-
chunkGraph.getChunkEntryDependentChunksIterable(chunk)
243-
);
244244
const chunks = getAllChunks(
245245
/** @type {Entrypoint} */ (entrypoint),
246246
/** @type {Chunk} */ (runtimeChunk),
247247
undefined
248248
);
249249
const processChunks = new Set();
250250
for (const _chunk of chunks) {
251-
if (
252-
loadedChunks.has(_chunk) ||
253-
entryDependentChunks.has(_chunk)
254-
) {
251+
if (loadedChunks.has(_chunk)) {
255252
continue;
256253
}
257254
loadedChunks.add(_chunk);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "common";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import common from "./common";
2+
3+
it("should compile", () => {
4+
expect(common).toBe("common");
5+
});
6+
export default "main";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "separate";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = {
4+
findBundle() {
5+
return ["./main.mjs"];
6+
}
7+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const EntryPlugin = require("../../../../").EntryPlugin;
4+
5+
/** @type {import("../../../../types").Configuration} */
6+
module.exports = () => ({
7+
devtool: false,
8+
mode: "development",
9+
entry: {
10+
main: {
11+
import: "./index.js",
12+
dependOn: "shared"
13+
},
14+
shared: "./common.js"
15+
},
16+
output: {
17+
filename: "[name].mjs",
18+
library: {
19+
type: "module"
20+
}
21+
},
22+
target: ["web", "es2020"],
23+
experiments: {
24+
outputModule: true
25+
},
26+
optimization: {
27+
minimize: false,
28+
runtimeChunk: false,
29+
splitChunks: {
30+
cacheGroups: {
31+
separate: {
32+
test: /separate/,
33+
chunks: "all",
34+
filename: "separate.mjs",
35+
enforce: true
36+
}
37+
}
38+
}
39+
},
40+
plugins: [new EntryPlugin(__dirname, "./separate.js", "main")]
41+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import separate from "./separate";
2+
export default separate;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import separate from "./separate";
2+
import common from "./common";
3+
it("should compile", () => {
4+
expect(separate).toBe("separate");
5+
expect(common).toBe("separate");
6+
});
7+
export default "main";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "separate";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = {
4+
findBundle() {
5+
return ["./main.mjs"];
6+
}
7+
};

0 commit comments

Comments
 (0)