fix(javascript): follow bundler namespace interop for Electron APIs (#1827)

* fix(javascript): follow bundler namespace interop for Electron APIs

Rolldown/tsdown emit `let electron = require("electron"); electron =
__toESM(electron);` and esbuild/tsc emit `__toESM(require("electron"))`
or `__importStar(require("electron"))` for namespace imports. The
reassignment or the helper call hid the Electron origin, so bundled
main processes reported zero BrowserWindows and preloads.

Locally declared, never reassigned interop helpers recognized by name
now pass the module through, and a binding's rewrap of its own value is
not treated as a reassignment. Other writes stay unresolved.

Fixes #1802

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(javascript): require emitted interop helper definitions

Only trust an interop helper name when it has a single program-scope
function definition, and state that helper bodies are not evaluated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-11 06:28:19 -07:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 91f843bf38
commit 6ac5c36503
5 changed files with 187 additions and 14 deletions
@@ -73,4 +73,51 @@ describe("Electron API identity", () => {
},
]);
});
it("follows bundler namespace interop helpers without accepting other writes", () => {
const analysis = analyzeJavaScriptStaticSource(`
var __toESM = (mod) => mod;
var __importStar = (this && this.__importStar) || ((mod) => mod);
let electron = require("electron");
electron = __toESM(electron);
new electron.BrowserWindow({ webPreferences: { preload: "./rolldown.js" } });
var import_electron = __toESM(require("electron"));
new import_electron.BrowserWindow({ webPreferences: { preload: "./esbuild.js" } });
function _interopRequireWildcard(mod) { return mod; }
const Electron = __importStar(require("electron"));
new Electron.BrowserWindow({ webPreferences: { preload: "./tsc.js" } });
let replaced = require("electron");
replaced = __toESM(require("./fake"));
new replaced.BrowserWindow({ webPreferences: { preload: "./replaced.js" } });
let rewritten = require("electron");
rewritten = __toESM(rewritten);
rewritten = fake;
new rewritten.BrowserWindow({ webPreferences: { preload: "./rewritten.js" } });
const babel = _interopRequireWildcard(require("electron"));
new babel.BrowserWindow({ webPreferences: { preload: "./babel.js" } });
function nested() {
const __toESM = (mod) => mod;
const scoped = __toESM(require("electron"));
new scoped.BrowserWindow({ webPreferences: { preload: "./nested.js" } });
}
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./rolldown.js", "./esbuild.js", "./tsc.js", "./babel.js"]);
});
it("does not trust unbound helper names or non-function helper values", () => {
const analysis = analyzeJavaScriptStaticSource(`
const __toESM = makeLookalike();
const electron = __toESM(require("electron"));
new electron.BrowserWindow({});
let unbound = require("electron");
unbound = __importStar(unbound);
new unbound.BrowserWindow({});
`);
expect(analysis.electron.browser_windows).toEqual([]);
});
});