Skip to content

fix: rewrite electron imports in bundle to bypass Node.js v24 CJS static analysis#3214

Open
keshav-k3 wants to merge 1 commit intowavetermdev:mainfrom
keshav-k3:issue-3213
Open

fix: rewrite electron imports in bundle to bypass Node.js v24 CJS static analysis#3214
keshav-k3 wants to merge 1 commit intowavetermdev:mainfrom
keshav-k3:issue-3213

Conversation

@keshav-k3
Copy link
Copy Markdown

Summary

Fixes #3213

Electron 41 ships Node.js v24, whose stricter cjs-module-lexer cannot detect lazy-getter exports (BaseWindow, BrowserWindow) from Electron's CJS module. This causes a SyntaxError at parse time before any code executes, making task electron:quickdev and npm run dev crash immediately on a clean clone of main.

This PR adds a Rollup renderChunk plugin to electron.vite.config.ts that rewrites all named and namespace imports from "electron" into default-import + runtime destructuring in the final bundle output, bypassing Node.js v24's static analysis entirely.

Changes Made

  • Added electronEsmInteropPlugin() to electron.vite.config.ts — a Rollup renderChunk plugin that transforms three import patterns in the bundled output:
    • import * as electron from "electron"import electron from "electron"
    • import default, { named } from "electron"import default from "electron"; const { named } = default;
    • import { named } from "electron"import __electron__ from "electron"; const { named } = __electron__;
  • Registered the plugin in the main build config's plugins array

Test Plan

  • npx electron-vite build --mode development succeeds without errors
  • npx electron-vite preview launches Electron — no SyntaxError
  • Verified dist/main/index.js contains only default imports from "electron" (no named or namespace imports)

…tic analysis

Electron 41 ships Node.js v24, whose stricter cjs-module-lexer cannot
detect lazy-getter exports (BaseWindow, BrowserWindow) from Electron's
CJS module, causing a SyntaxError at parse time.

Add a Rollup renderChunk plugin that rewrites all named/namespace
imports from "electron" into default-import + runtime destructuring
in the final bundle output, bypassing static analysis entirely.

Fixes wavetermdev#3213
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 11, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 11, 2026

Walkthrough

A custom Vite plugin called electronEsmInteropPlugin() was added to the main-process Vite configuration in electron.vite.config.ts. The plugin intercepts and rewrites ESM import statements from the "electron" package, converting them into alternative forms that route all module access through Electron's CJS default export. Three specific import patterns are transformed: namespace imports, named imports, and mixed default-plus-named imports. The plugin is now included in the main-process plugin pipeline while preload and renderer configurations remain unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: a fix that rewrites electron imports in the bundle to work around Node.js v24 CJS static analysis limitations.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem, the solution, and providing a clear test plan with specific verification steps.
Linked Issues check ✅ Passed The PR fully addresses the objectives from issue #3213 by implementing the proposed solution: a plugin that transforms electron imports from named/namespace to default imports with runtime destructuring, bypassing Node.js v24's static analysis.
Out of Scope Changes check ✅ Passed All changes are focused on implementing the electronEsmInteropPlugin in electron.vite.config.ts to address the specific issue; no out-of-scope modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@electron.vite.config.ts`:
- Around line 58-63: The current global replace in result.replace with the regex
and replacement callback injects an import __electron__ declaration for every
match, causing duplicate imports and possible name collisions; modify the logic
in the block that uses result.replace and importAsToDestructure so you only
generate one default import binding and reuse it for all matched destructurings:
gather all matched named specifiers (or run a replace that only emits the
destructuring fragments), choose a single unique identifier (avoid
"__electron__" if already present by checking the chunk) and prepend one import
<unique> from "electron" plus combined const { ... } = <unique> once (set
transformed = true), rather than emitting an import on every match. Reference:
the result.replace call, the importAsToDestructure helper, transformed flag, and
the "__electron__" identifier.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 5c94eefd-fa63-46ad-87e9-485cf5e529c9

📥 Commits

Reviewing files that changed from the base of the PR and between 263eda4 and 0776646.

📒 Files selected for processing (1)
  • electron.vite.config.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Dev mode crashes with SyntaxError: The requested module electron does not provide an export named BaseWindow

2 participants