Skip to content

Commit dd063d2

Browse files
fix: report a chunk loading that is not import only when it is not
`reportChunkImportBailout` recorded the reason whatever the chunk loading was. Its one caller asks only for a chunk the ESM loader does not serve, so nothing wrong reached a build — but the method now answers for itself. The unit tests asked `supportsAnalyzable` for a worker's chunk loading, which the chunk loader decides now; they ask the reporting method instead.
1 parent 927d87d commit dd063d2

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

‎lib/RuntimeTemplate.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,6 +3762,7 @@ class RuntimeTemplate {
37623762
entryOptions && entryOptions.chunkLoading !== undefined
37633763
? entryOptions.chunkLoading
37643764
: this.outputOptions.chunkLoading;
3765+
if (chunkLoading === "import") return;
37653766
const subject = entryOptions && entryOptions.worker ? "worker" : "runtime";
37663767
const reason = `this ${subject} loads its chunks with ${JSON.stringify(chunkLoading)}, not "import"`;
37673768
for (const module of chunkGraph.getChunkEntryModulesIterable(chunk)) {

‎test/RuntimeTemplate.unittest.js‎

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ describe("RuntimeTemplate.supportsAnalyzable", () => {
332332
getEntryOptions: () => ({ worker: true, chunkLoading })
333333
});
334334

335+
/**
336+
* @returns {ChunkGraph} a chunk graph whose chunk carries one entry module
337+
*/
338+
const entryChunkGraph = () =>
339+
/** @type {ChunkGraph} */ (
340+
/** @type {unknown} */ ({
341+
getChunkEntryModulesIterable: () => [module]
342+
})
343+
);
344+
335345
it("should refuse every form unless the build emits ESM", () => {
336346
const template = create({ output: { module: false } });
337347
const { chunkGraph } = countingChunkGraph();
@@ -386,42 +396,50 @@ describe("RuntimeTemplate.supportsAnalyzable", () => {
386396
}).toEqual({ array: false, renamed: false, both: true });
387397
});
388398

389-
// A worker on its own chunk loader keeps that runtime; one on `import` shares the
390-
// ESM loader with the main graph, so the literal reaches the same chunk.
391-
it("should follow a worker entry's own chunk loading", () => {
392-
const cases = {
393-
importWorker: workerChunk("import"),
394-
jsonpWorker: workerChunk("jsonp"),
395-
nodeWorker: workerChunk("async-node")
396-
};
397-
/** @type {Record<string, boolean>} */
398-
const answers = {};
399-
for (const [name, chunk] of Object.entries(cases)) {
400-
const { chunkGraph } = countingChunkGraph([chunk]);
401-
answers[name] = create({}).supportsAnalyzable(
402-
"import",
403-
chunkGraph,
404-
module
399+
// A worker on its own chunk loader gets no ESM loader to name its chunks, so the
400+
// reason is recorded against the entry that chose it rather than asked per module.
401+
it("should report a worker entry's own chunk loading", () => {
402+
/** @type {Record<string, string[]>} */
403+
const reported = {};
404+
for (const [name, chunkLoading] of [
405+
["importWorker", "import"],
406+
["jsonpWorker", "jsonp"],
407+
["nodeWorker", "async-node"]
408+
]) {
409+
/** @type {string[]} */
410+
const bailouts = [];
411+
create({ bailouts }).reportChunkImportBailout(
412+
/** @type {EXPECTED_ANY} */ (workerChunk(chunkLoading)),
413+
entryChunkGraph()
405414
);
415+
reported[name] = bailouts;
406416
}
407417

408-
expect(answers).toEqual({
409-
importWorker: true,
410-
jsonpWorker: false,
411-
nodeWorker: false
418+
expect(reported).toEqual({
419+
importWorker: [],
420+
jsonpWorker: [
421+
'Analyzable ESM bailout: this worker loads its chunks with "jsonp", not "import"'
422+
],
423+
nodeWorker: [
424+
'Analyzable ESM bailout: this worker loads its chunks with "async-node", not "import"'
425+
]
412426
});
413427
});
414428

415-
// A non-worker entry carries `chunkLoading` too, and it is not the worker's.
416-
it("should ignore chunk loading on an entry that is not a worker", () => {
417-
const { chunkGraph } = countingChunkGraph([
418-
{ getEntryOptions: () => ({ chunkLoading: "jsonp" }) },
419-
{ getEntryOptions: () => undefined }
420-
]);
421-
422-
expect(create({}).supportsAnalyzable("import", chunkGraph, module)).toBe(
423-
true
429+
// A non-worker entry carries `chunkLoading` too, and the reason names it as such.
430+
it("should report an entry that is not a worker as a runtime", () => {
431+
/** @type {string[]} */
432+
const bailouts = [];
433+
create({ bailouts }).reportChunkImportBailout(
434+
/** @type {EXPECTED_ANY} */ ({
435+
getEntryOptions: () => ({ chunkLoading: "jsonp" })
436+
}),
437+
entryChunkGraph()
424438
);
439+
440+
expect(bailouts).toEqual([
441+
'Analyzable ESM bailout: this runtime loads its chunks with "jsonp", not "import"'
442+
]);
425443
});
426444

427445
// `eval` devtool wraps each module in `eval(...)`, where `import.meta` is a syntax
@@ -469,11 +487,11 @@ describe("RuntimeTemplate.supportsAnalyzable", () => {
469487
// unit-testable — the decision short-circuits before reading it unless a real
470488
// build registered a `__webpack_public_path__` reassignment — so the case that
471489
// pins it is configCases/wasm/analyzable-runtime-scope.
472-
it("should walk the origin module's chunks once for an import", () => {
490+
it("should read no chunk of the origin module for an import", () => {
473491
const { chunkGraph, reads } = countingChunkGraph();
474492
create({}).supportsAnalyzable("import", chunkGraph, module);
475493

476-
expect(reads()).toBe(1);
494+
expect(reads()).toBe(0);
477495
});
478496

479497
// `stats.optimizationBailout` is the channel this reports through, and one module

0 commit comments

Comments
 (0)