Skip to content

fix(plugin-vite): build main and preload bundles as Node targets - #4465

Open
erickzhao wants to merge 6 commits into
mainfrom
claude/project-thread-gssthv
Open

erickzhao wants to merge 6 commits into
mainfrom
claude/project-thread-gssthv

Conversation

@erickzhao

@erickzhao erickzhao commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Requested by Erick · project thread

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project follows, as appropriate.
  • The changes are appropriately documented (if applicable).
  • The changes have sufficient test coverage (if applicable).
  • The testsuite passes successfully on my local machine (if applicable).

Summarize your changes:

Before: the Vite plugin built the main process and preload scripts as browser-environment bundles. import.meta.url and import.meta.dirname compiled to {}.url / {}.dirname in the emitted .cjs, so they were silently undefined at runtime (the bug class in #3439).

After: both are built as Vite SSR (Node) targets, as electron-vite does. In the main process and non-sandboxed preloads, import.meta.url becomes a real file URL and import.meta.dirname becomes __dirname. process.env reads stay live at runtime, and no module-preload helpers are emitted. Existing configs keep behaving the same way.

How:

  • vite.main.config.ts and vite.preload.config.ts set build.ssr, build.ssrEmitAssets, build.modulePreload: false, ssr.noExternal: true and process.env self-defines, which are applied before Forge's *_VITE_* defines so those still win. Main's own top-level resolve block is removed, because the ssr defaults already resolve Node entries first.
  • The ssr environment ignores the top-level resolve. When a user's main or preload config sets resolve.conditions / resolve.mainFields and not their ssr.resolve counterparts, carryResolveToSsr copies them over, so those configs resolve the same as before.
  • Preload runs in Chromium, so it overrides ssr.resolve with Vite's defaultClientConditions / defaultClientMainFields. It still inlines process.env.NODE_ENV, as the old browser build did. Its import.meta.url / filename / dirname are guarded with typeof __filename, so sandboxed preloads (Electron's default, with no __filename) still get undefined instead of a ReferenceError. I checked both modes in Electron 43.4.0.
  • An SSR build ignores build.lib.fileName. Main therefore sets rollupOptions.output.entryFileNames to name the entry the way a library build would, for Forge's default lib (main.cjs) and for a user-supplied one, unless the user sets entryFileNames themselves. The subprocess-build fixture now declares "type": "commonjs" like a template project; before, it inherited the plugin's "type": "module", which hid this.
  • Output file names, CommonJS format, externals, emptyOutDir and the hot-restart/reload plugins are unchanged.
  • Fixtures and subprocess-worker specs run real builds. They cover live process.env reads, valid CJS for import.meta, the *_VITE_* defines, preload's browser-field resolution and NODE_ENV inlining, sandboxed and non-sandboxed preload import.meta, carrying top-level resolve over, user lib.fileName naming, and the absence of modulepreload helpers.

Notes:

  • On Vite 8.3 the process.env self-defines don't change the output. They are insurance against build-time inlining.
  • In preload, process.env.FOO used to compile to {}.FOO, which was always undefined. It now reads the real value at runtime.
  • Dev/HMR mode was only checked by hand. A locally built template app (CommonJS package.json) launches in Electron 43.4.0 and reaches both its main and preload code.

Locally, yarn lint:js is clean and yarn test:fast passes (721 tests).

🤖 Generated with Claude Code

https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz

Forge built the main process (a Vite library build) and preload scripts
(a plain `rollupOptions.input` build) as browser-environment bundles,
steering main's resolution with `resolve.conditions: ['node']`.
Vite still treated the output as browser code, so `import.meta.url` and
`import.meta.dirname` lowered to the browser shims and silently compiled
to `{}.url` / `{}.dirname` in the emitted `.cjs` file, and module preload
helpers referencing `self` could leak in.

Flag both builds as SSR targets instead, which is what electron-vite
does. `build.ssr` switches Vite to its `ssr` environment, whose default
resolution (`['module', 'node', 'development|production']` plus main
fields `['module', 'jsnext:main', 'jsnext']`) is already the Node-first
set the old `resolve.conditions` was aiming at — and the top-level
`resolve` only configures the `client` environment, so it no longer
applies. Preload scripts are a hybrid environment, so they override
`ssr.resolve` with Vite's browser-first lists to keep resolving `browser`
entry points. `process.env` is pinned to itself so a Node bundle can
never get build-time environment values baked in.

Output file names, the CommonJS format, the externals list,
`emptyOutDir` and the hot restart/reload plugins are unchanged: the
bundle the Vite template produces is byte-identical to before.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015hjXsxhhatdiFFHYs66oYp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz
@erickzhao erickzhao self-assigned this Nov 2, 2026
An SSR build ignores `build.lib.fileName` and names entries from the
nearest package.json's `type`, so a CommonJS project (like the Vite
template) got `.vite/build/main.js` and Electron could not find
`main.cjs`. Set `entryFileNames` explicitly for Forge's default lib.

The subprocess-build fixture now declares `"type": "commonjs"` so the
worker specs build the way a template project does; without it they
inherited the plugin's `"type": "module"` and hid this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz

@georgexu99 georgexu99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not stamping yet- one silent breakage for users who pass their own build.lib (inline), rest is non blocking. Also IMO this wants a feat!: a few user-facing config behaviours change (top-level resolve, preload NODE_ENV inlining).

🤖 - a message from George's lil' guy

Comment thread docs/config/plugins/vite.mdx Resolved
…er SSR

- Name main entries the way a Vite library build would, including a
  user-supplied `build.lib.fileName`, since an SSR build ignores it.
- Inline `process.env.NODE_ENV` in preload bundles again, so browser
  dependencies drop their development builds.
- Use Vite's exported `defaultClientConditions` / `defaultClientMainFields`
  for preload resolution.
- Docs: sandboxed preloads have no `__filename`, and top-level `resolve`
  options move to `ssr.resolve`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz
@erickzhao erickzhao changed the title feat(plugin-vite): build main and preload bundles as Node targets feat(plugin-vite)!: build main and preload bundles as Node targets Nov 4, 2026

@georgexu99 georgexu99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stamped. Checked libEntryFileNames against vite's resolveLibFilename (fn/string fileName, pkg-name fallback, type-based ext)- matches. Preload NODE_ENV + docs look good too.

🤖 - a message from George's lil' guy

…anged

- Copy a user's top-level `resolve.conditions` / `resolve.mainFields`
  into `ssr.resolve` for main and preload, unless they set it there,
  since the `ssr` environment ignores the top-level options.
- Guard preload's `import.meta.url` / `filename` / `dirname` so they stay
  `undefined` in sandboxed preloads, which have no `__filename`, instead
  of throwing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz
@erickzhao erickzhao changed the title feat(plugin-vite)!: build main and preload bundles as Node targets fix(plugin-vite): build main and preload bundles as Node targets Nov 4, 2026
@erickzhao
erickzhao requested a review from georgexu99 November 4, 2026 04:51
@erickzhao
erickzhao marked this pull request as ready for review November 4, 2026 07:03
@erickzhao
erickzhao requested a review from a team as a code owner November 4, 2026 07:03
@erickzhao

Copy link
Copy Markdown
Member Author

@claude review

…port.meta

- Name main entries from a per-output `outputOptions` plugin, so array
  outputs and multi-format `build.lib` configs each get their own
  library-style name instead of one shared name or Vite's SSR default.
- Only guard preload `import.meta` for CommonJS output, so ES module
  preloads keep their native `import.meta`.
- When carrying a user's top-level `resolve` lists over for main, keep
  Forge's old Node-first lists ahead of them, as the old merge did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz
…rolldownOptions output

Preload now inlines `global.process.env.NODE_ENV` and
`globalThis.process.env.NODE_ENV` as well as `process.env.NODE_ENV`, as
the old browser build did. A sandboxed preload has no
`globalThis.process`, so those reads would throw there.

The main entry-naming plugin and the preload `import.meta` guard now
read the user's output options from `build.rolldownOptions` (Vite 8's
primary key) before falling back to `build.rollupOptions`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

This branch has not been deployed

No deployments
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.

3 participants