Skip to content

[Bug]: Multiple entries build in commonjs format incorrectly injects require of the first entry into the second entry #9333

@subframe7536

Description

@subframe7536

Reproduction link or steps

  1. Clone the repository: https://github.com/electron-vite/vite-plugin-electron .
  2. Install dependencies via pnpm install.
  3. Run the worker playground dev script via pnpm play:worker.
  4. Emits runtime error because the plugin generate 2 entries, the first entry is loaded in the second entry.
Image

Content of playground/worker/dist-electron/main.js:

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region \0rolldown/runtime.js
// ...
//#endregion
let node_path = require("node:path");
node_path = __toESM(node_path);
let node_worker_threads = require("node:worker_threads");
let electron = require("electron");
//#region electron/main.ts
var win;
console.log("typeof app:", typeof electron.app);
electron.app.whenReady().then(() => {
    // ...
	new node_worker_threads.Worker(node_path.default.join(__dirname, "./worker.js")).on("message", (value) => {
		console.log("[worker message]:", value);
	});
});
//#endregion
exports.__toESM = __toESM;

What is expected?

Content of playground/worker/dist-electron/worker.js:

let node_worker_threads = require("node:worker_threads");
//#region electron/worker.ts
setTimeout(() => {
	node_worker_threads.parentPort?.postMessage(`worker_threads ${(/* @__PURE__ */ new Date()).toLocaleTimeString()}`);
}, 999);
//#endregion

What is actually happening?

Content of playground/worker/dist-electron/worker.js:

require("./main.js");
let node_worker_threads = require("node:worker_threads");
//#region electron/worker.ts
setTimeout(() => {
	node_worker_threads.parentPort?.postMessage(`worker_threads ${(/* @__PURE__ */ new Date()).toLocaleTimeString()}`);
}, 999);
//#endregion

The first line should be deleted

System Info

System:
    OS: macOS 15.7.2
    CPU: (10) arm64 Apple M4
    Memory: 922.75 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.14.0 - /Users/subf/.local/state/fnm_multishells/87543_1778335352187/bin/node
    npm: 11.11.0 - /Users/subf/.local/state/fnm_multishells/87543_1778335352187/bin/npm
    pnpm: 10.26.1 - /Users/subf/Library/pnpm/pnpm
    bun: 1.3.13 - /Users/subf/.bun/bin/bun
  Browsers:
    Chrome: 147.0.7727.102
    Safari: 26.1

Any additional comments?

Works well in esm format, see in playground/multi-env and pnpm play:multi-env

Metadata

Metadata

Type

Priority

None yet

Effort

None yet

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions