Skip to content

Commit ec5ddea

Browse files
feat: report an entry whose exports a consumer cannot drop
An entry other modules reference is not inlined, so its exports are read off the exports object and no bundler consuming the library can drop them. Report that through the analyzable-ESM bailout channel, where the answer is actionable. Refs #17121
1 parent c1a6810 commit ec5ddea

6 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,12 @@ class JavascriptModulesPlugin {
17021702
buf2.push(
17031703
"// This entry module is referenced by other modules so it can't be inlined"
17041704
);
1705+
// Its exports are then read off the exports object, and a property
1706+
// read is not something a consumer of the bundle can drop.
1707+
runtimeTemplate.analyzableBailout(
1708+
entryModule,
1709+
"the entry is referenced by other modules, so its exports are read off the exports object and a consumer cannot drop them"
1710+
);
17051711
result.allowInlineStartup = false;
17061712
}
17071713

‎lib/template/RuntimeTemplate.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,16 @@ class RuntimeTemplate {
830830
return true;
831831
}
832832

833+
/**
834+
* Reports an analyzable-ESM bailout from outside this class.
835+
* @param {Module | undefined} module the module the reference is emitted into
836+
* @param {string} reason why no literal could be baked
837+
* @returns {void}
838+
*/
839+
analyzableBailout(module, reason) {
840+
this._analyzableBailout(module, reason);
841+
}
842+
833843
/**
834844
* Records why a reference kept the runtime form, on the module that wrote it —
835845
* the channel `ModuleConcatenationPlugin` already reports through, so it reaches
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { helper } from "./refs-entry.js";
2+
3+
export const value = 42;
4+
5+
it("should still compute", () => {
6+
expect(helper()).toBe(42);
7+
});
8+
9+
it("should report why the entry's exports cannot be dropped", () => {
10+
const entry = __STATS__.modules.find((module) =>
11+
module.name.endsWith("index.js")
12+
);
13+
14+
expect(entry.optimizationBailout).toContainEqual(
15+
expect.stringContaining(
16+
"Analyzable ESM bailout: the entry is referenced by other modules"
17+
)
18+
);
19+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Importing the entry back keeps webpack from inlining it, which is what the
2+
// bailout below reports.
3+
import { value } from "./index.js";
4+
5+
export function helper() {
6+
return value;
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../types").Configuration} */
4+
module.exports = {
5+
target: "node",
6+
mode: "production",
7+
output: {
8+
filename: "bundle0.mjs",
9+
library: { type: "module" },
10+
module: true
11+
},
12+
optimization: {
13+
// Concatenation would pull the referencing module in and leave the entry
14+
// inlinable, which is the case that needs no report.
15+
concatenateModules: false
16+
},
17+
stats: { optimizationBailout: true },
18+
experiments: { outputModule: true }
19+
};

‎types.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26582,6 +26582,11 @@ declare abstract class RuntimeTemplate {
2658226582
runtime?: RuntimeSpec
2658326583
): boolean;
2658426584

26585+
/**
26586+
* Reports an analyzable-ESM bailout from outside this class.
26587+
*/
26588+
analyzableBailout(module: undefined | Module, reason: string): void;
26589+
2658526590
/**
2658626591
* Names the module whose code generation starts, so the reasons its references
2658726592
* record are handed back by `finishAnalyzableBailouts` and attributed to it.

0 commit comments

Comments
 (0)