Commit f309811
refactor(kernel-utils): replace vat bundler NODE_ENV define with a plugin (#967)
## 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`.
- **Tests** — `replace-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](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!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.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
8f73140. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 9ff2ab7 commit f309811
5 files changed
Lines changed: 127 additions & 6 deletions
File tree
- packages/kernel-utils
- src/vite-plugins
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | 92 | | |
98 | 93 | | |
99 | 94 | | |
| |||
109 | 104 | | |
110 | 105 | | |
111 | 106 | | |
| 107 | + | |
112 | 108 | | |
113 | 109 | | |
114 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
Lines changed: 71 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
Lines changed: 51 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
0 commit comments