fix: rewrite electron imports in bundle to bypass Node.js v24 CJS static analysis#3214
fix: rewrite electron imports in bundle to bypass Node.js v24 CJS static analysis#3214keshav-k3 wants to merge 1 commit intowavetermdev:mainfrom
Node.js v24 CJS static analysis#3214Conversation
…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
WalkthroughA custom Vite plugin called Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
electron.vite.config.ts
Summary
Fixes #3213
Electron 41 ships Node.js v24, whose stricter
cjs-module-lexercannot detect lazy-getter exports (BaseWindow,BrowserWindow) from Electron's CJS module. This causes aSyntaxErrorat parse time before any code executes, makingtask electron:quickdevandnpm run devcrash immediately on a clean clone ofmain.This PR adds a Rollup
renderChunkplugin toelectron.vite.config.tsthat 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
electronEsmInteropPlugin()toelectron.vite.config.ts— a RolluprenderChunkplugin 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__;mainbuild config'spluginsarrayTest Plan
npx electron-vite build --mode developmentsucceeds without errorsnpx electron-vite previewlaunches Electron — noSyntaxErrordist/main/index.jscontains only default imports from"electron"(no named or namespace imports)