refactor(kernel-utils): replace vat bundler NODE_ENV define with a plugin#967
Open
rekmarks wants to merge 4 commits into
Open
refactor(kernel-utils): replace vat bundler NODE_ENV define with a plugin#967rekmarks wants to merge 4 commits into
rekmarks wants to merge 4 commits into
Conversation
…ugin Move `process.env.NODE_ENV` injection out of the `bundleVat` build config and into a dedicated, auto-detecting Rolldown transform plugin (`replaceNodeEnvPlugin`). The plugin inlines `process.env.NODE_ENV` as the literal `"production"` in any module that references it and no-ops on modules that don't, matching the previous `define` behavior while making the logic a first-class, unit-tested plugin instead of a build-config detail. Closes #812 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…able
Accept an options bag `{ value }` on `replaceNodeEnvPlugin`, defaulting to
`'production'`, so callers can inline a different `process.env.NODE_ENV`
value. Export the `ReplaceNodeEnvPluginOptions` type.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Summary
Moves
process.env.NODE_ENVinjection out of thebundleVatbuild config and into a dedicated, auto-detecting Rolldown transform plugin, resolving #812.Previously
bundleVatconfigured a Vitedefinethat textually substitutedprocess.env.NODE_ENV→"production"on every build. It was a workaround for libraries (e.g. immer) that branch onprocess.env.NODE_ENV, but baking it into the build config coupled bundling to an env-shimming concern. The newreplaceNodeEnvPluginmakes that logic a first-class, unit-tested plugin instead.Changes
replaceNodeEnvPlugin(packages/kernel-utils/src/vite-plugins/replace-node-env-plugin.ts) — a Rolldowntransformplugin that inlinesprocess.env.NODE_ENVas a string literal in any module referencing it, and no-ops otherwise. Mirrors the siblingremoveDynamicImportsPlugin.{ value }(typeReplaceNodeEnvPluginOptions), defaulting to'production', so callers can inline a differentprocess.env.NODE_ENVvalue.bundle-vat.ts— removed thedefineblock (and its TODO) and addedreplaceNodeEnvPlugin()to therolldownOptions.pluginsarray (uses the'production'default).index.ts— exports the new plugin and its options type alongsideremoveDynamicImportsPlugin.replace-node-env-plugin.test.tscovers positive/negative cases, the quoted-literal assertion, the word-boundary near-miss (NODE_ENVIRONMENT), and a configured non-default value.Behavior
For the default value, output is identical to the old
definefor any bundle referencingprocess.env.NODE_ENV— the logic is just relocated to a named, tested plugin.Verification
yarn workspace @metamask/kernel-utils build— passes.yarn workspace @metamask/kernel-utils test:dev:quiet— passes (new + existing plugin/index tests).process.env.NODE_ENVproducedvar isProd = true; var rawMode = "production";— fully inlined and constant-folded, no residualprocess.env.NODE_ENVreference.yarn lint— clean.Closes #812
🤖 Generated with Claude Code
Note
Low Risk
Build-time bundling refactor with equivalent default behavior; limited to kernel-utils vat bundling and covered by new tests.
Overview
bundleVatno longer sets a global Vitedefineforprocess.env.NODE_ENV. That injection is handled by a new RolldownreplaceNodeEnvPlugin, wired into the vat bundle pipeline alongside the existing dynamic-import stripper.The plugin only transforms modules that reference
process.env.NODE_ENV, replacing them with a JSON-stringified literal (default"production", overridable via{ value }). That keeps vat bundles safe for deps like immer when there is no runtimeprocessglobal, without coupling every build to a blanket define.replaceNodeEnvPluginandReplaceNodeEnvPluginOptionsare exported from./vite-plugins, with unit tests covering replacements, no-ops, word boundaries, and custom values. CHANGELOG documents the addition.Reviewed by Cursor Bugbot for commit 8f73140. Bugbot is set up for automated code reviews on this repo. Configure here.