Repository navigation
Conversation
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
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
reviewed
Nov 4, 2026
georgexu99
left a comment
Contributor
There was a problem hiding this comment.
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
…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
georgexu99
approved these changes
Nov 4, 2026
georgexu99
left a comment
Contributor
There was a problem hiding this comment.
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
marked this pull request as ready for review
November 4, 2026 07:03
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Erick · project thread
Summarize your changes:
Before: the Vite plugin built the main process and preload scripts as browser-environment bundles.
import.meta.urlandimport.meta.dirnamecompiled to{}.url/{}.dirnamein the emitted.cjs, so they were silentlyundefinedat 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.urlbecomes a real file URL andimport.meta.dirnamebecomes__dirname.process.envreads stay live at runtime, and no module-preload helpers are emitted. Existing configs keep behaving the same way.How:
vite.main.config.tsandvite.preload.config.tssetbuild.ssr,build.ssrEmitAssets,build.modulePreload: false,ssr.noExternal: trueandprocess.envself-defines, which are applied before Forge's*_VITE_*defines so those still win. Main's own top-levelresolveblock is removed, because thessrdefaults already resolve Node entries first.ssrenvironment ignores the top-levelresolve. When a user's main or preload config setsresolve.conditions/resolve.mainFieldsand not theirssr.resolvecounterparts,carryResolveToSsrcopies them over, so those configs resolve the same as before.ssr.resolvewith Vite'sdefaultClientConditions/defaultClientMainFields. It still inlinesprocess.env.NODE_ENV, as the old browser build did. Itsimport.meta.url/filename/dirnameare guarded withtypeof __filename, so sandboxed preloads (Electron's default, with no__filename) still getundefinedinstead of aReferenceError. I checked both modes in Electron 43.4.0.build.lib.fileName. Main therefore setsrollupOptions.output.entryFileNamesto name the entry the way a library build would, for Forge's defaultlib(main.cjs) and for a user-supplied one, unless the user setsentryFileNamesthemselves. Thesubprocess-buildfixture now declares"type": "commonjs"like a template project; before, it inherited the plugin's"type": "module", which hid this.emptyOutDirand the hot-restart/reload plugins are unchanged.subprocess-workerspecs run real builds. They cover liveprocess.envreads, valid CJS forimport.meta, the*_VITE_*defines, preload's browser-field resolution andNODE_ENVinlining, sandboxed and non-sandboxed preloadimport.meta, carrying top-levelresolveover, userlib.fileNamenaming, and the absence of modulepreload helpers.Notes:
process.envself-defines don't change the output. They are insurance against build-time inlining.process.env.FOOused to compile to{}.FOO, which was alwaysundefined. It now reads the real value at runtime.package.json) launches in Electron 43.4.0 and reaches both its main and preload code.Locally,
yarn lint:jsis clean andyarn test:fastpasses (721 tests).🤖 Generated with Claude Code
https://claude.ai/code/session_01WgVWip5B66qeC8GhQncYbz