Skip to content

refactor(kernel-utils): replace vat bundler NODE_ENV define with a plugin#967

Open
rekmarks wants to merge 4 commits into
mainfrom
fix/812-node-env-bundler-plugin
Open

refactor(kernel-utils): replace vat bundler NODE_ENV define with a plugin#967
rekmarks wants to merge 4 commits into
mainfrom
fix/812-node-env-bundler-plugin

Conversation

@rekmarks

@rekmarks rekmarks commented Jul 9, 2026

Copy link
Copy Markdown
Member

Summary

Moves process.env.NODE_ENV injection out of the bundleVat build config and into a dedicated, auto-detecting Rolldown transform plugin, resolving #812.

Previously bundleVat configured a Vite define that textually substituted process.env.NODE_ENV"production" on every build. It was a workaround for libraries (e.g. immer) that branch on process.env.NODE_ENV, but baking it into the build config coupled bundling to an env-shimming concern. The new replaceNodeEnvPlugin makes that logic a first-class, unit-tested plugin instead.

Changes

  • New replaceNodeEnvPlugin (packages/kernel-utils/src/vite-plugins/replace-node-env-plugin.ts) — a Rolldown transform plugin that inlines process.env.NODE_ENV as a string literal in any module referencing it, and no-ops otherwise. Mirrors the sibling removeDynamicImportsPlugin.
  • Configurable value — the plugin accepts an options bag { value } (type ReplaceNodeEnvPluginOptions), defaulting to 'production', so callers can inline a different process.env.NODE_ENV value.
  • bundle-vat.ts — removed the define block (and its TODO) and added replaceNodeEnvPlugin() to the rolldownOptions.plugins array (uses the 'production' default).
  • index.ts — exports the new plugin and its options type alongside removeDynamicImportsPlugin.
  • Testsreplace-node-env-plugin.test.ts covers 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 define for any bundle referencing process.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).
  • End-to-end: bundling a fixture that reads process.env.NODE_ENV produced var isProd = true; var rawMode = "production"; — fully inlined and constant-folded, no residual process.env.NODE_ENV reference.
  • 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
bundleVat no longer sets a global Vite define for process.env.NODE_ENV. That injection is handled by a new Rolldown replaceNodeEnvPlugin, 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 runtime process global, without coupling every build to a blanket define.

replaceNodeEnvPlugin and ReplaceNodeEnvPluginOptions are 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.

…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>
@rekmarks rekmarks requested a review from a team as a code owner July 9, 2026 20:16
rekmarks and others added 2 commits July 9, 2026 13:16
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>
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 71.38%
⬆️ +0.02%
8869 / 12424
🔵 Statements 71.21%
⬆️ +0.02%
9019 / 12665
🔵 Functions 72.59%
⬆️ +0.02%
2140 / 2948
🔵 Branches 64.9%
⬆️ +0.04%
3584 / 5522
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/kernel-utils/src/vite-plugins/bundle-vat.ts 41.66%
🟰 ±0%
50%
🟰 ±0%
40%
🟰 ±0%
41.66%
🟰 ±0%
76-131
packages/kernel-utils/src/vite-plugins/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-utils/src/vite-plugins/replace-node-env-plugin.ts 100% 100% 100% 100%
Generated in workflow #4501 for commit 8f73140 by the Vitest Coverage Report Action

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.

Move process.env.NODE_ENV injection out of vat bundler

1 participant