Skip to content

Commit 2ad555b

Browse files
rekmarksclaude
andcommitted
feat(kernel-utils): make replaceNodeEnvPlugin NODE_ENV value configurable
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>
1 parent a8c0589 commit 2ad555b

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

packages/kernel-utils/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add an optional `required` field to `MethodSchema` (mirroring `required` on object `JsonSchema`) naming which arguments are required, and a `{ required }` option on `methodArgsToStruct` that validates unlisted arguments as optional, so a method's argument schema can faithfully represent the optional trailing arguments its guard already allows ([#958](https://github.com/MetaMask/ocap-kernel/pull/958))
1414
- Add `getLibp2pRelayHome()` to the `./nodejs` exports, returning the libp2p relay's bookkeeping directory (default `~/.libp2p-relay`, overridable via `$LIBP2P_RELAY_HOME`) — kept separate from `$OCAP_HOME` so one relay can serve daemons with different OCAP_HOMEs ([#952](https://github.com/MetaMask/ocap-kernel/pull/952))
1515
- `startRelay()` accepts an optional `publicIp` that is fed to libp2p's `appendAnnounce`, so a relay running on a NAT-backed host can announce its publicly-reachable IPv4 alongside its bound NIC addresses ([#952](https://github.com/MetaMask/ocap-kernel/pull/952))
16-
- Add `replaceNodeEnvPlugin` to the `./vite-plugins` exports, a Rolldown transform plugin that inlines `process.env.NODE_ENV` as the literal `"production"` in any module referencing it — used by `bundleVat` in place of a build-config `define`, so vats that pull in libraries like immer resolve the reference at bundle time ([#967](https://github.com/MetaMask/ocap-kernel/pull/967))
16+
- Add `replaceNodeEnvPlugin` to the `./vite-plugins` exports, a Rolldown transform plugin that inlines `process.env.NODE_ENV` as a string literal (configurable via `{ value }`, defaulting to `'production'`) in any module referencing it — used by `bundleVat` in place of a build-config `define`, so vats that pull in libraries like immer resolve the reference at bundle time ([#967](https://github.com/MetaMask/ocap-kernel/pull/967))
1717

1818
## [0.5.0]
1919

packages/kernel-utils/src/vite-plugins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { bundleVat } from './bundle-vat.ts';
55
export { bundleVat, removeDynamicImportsPlugin } from './bundle-vat.ts';
66
export type { VatBundle } from './bundle-vat.ts';
77
export { replaceNodeEnvPlugin } from './replace-node-env-plugin.ts';
8+
export type { ReplaceNodeEnvPluginOptions } from './replace-node-env-plugin.ts';
89

910
type VatEntry = {
1011
/** Absolute path to the vat source file */

packages/kernel-utils/src/vite-plugins/replace-node-env-plugin.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,17 @@ describe('replaceNodeEnvPlugin', () => {
5555
const code = `const x = process.env.NODE_ENVIRONMENT;`;
5656
expect(transform(code, 'test.ts')).toBeNull();
5757
});
58+
59+
it('inlines a configured value instead of the default', () => {
60+
const devTransform = replaceNodeEnvPlugin({ value: 'development' })
61+
.transform as TransformFn;
62+
const result = devTransform(
63+
`const mode = process.env.NODE_ENV;`,
64+
'test.ts',
65+
);
66+
expect(result).toStrictEqual({
67+
code: `const mode = "development";`,
68+
map: null,
69+
});
70+
});
5871
});

packages/kernel-utils/src/vite-plugins/replace-node-env-plugin.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import type { Plugin as RolldownPlugin } from 'rolldown';
22

3+
export type ReplaceNodeEnvPluginOptions = {
4+
/**
5+
* The value to inline for `process.env.NODE_ENV`. Defaults to `'production'`.
6+
*/
7+
value?: string;
8+
};
9+
310
/**
4-
* A Rolldown plugin that inlines `process.env.NODE_ENV` as the literal
5-
* `"production"` in any module that references it.
11+
* A Rolldown plugin that inlines `process.env.NODE_ENV` as a string literal in
12+
* any module that references it.
613
*
714
* This replaces the former build-config `define` (issue #812). Libraries such
815
* as immer branch on `process.env.NODE_ENV`, and vats have no `process` global
@@ -13,9 +20,15 @@ import type { Plugin as RolldownPlugin } from 'rolldown';
1320
* {@link removeDynamicImportsPlugin}) and handles the dotted
1421
* `process.env.NODE_ENV` form, consistent with the `define` it replaces.
1522
*
23+
* @param options - Plugin options.
24+
* @param options.value - The value to inline for `process.env.NODE_ENV`.
25+
* Defaults to `'production'`.
1626
* @returns A Rolldown plugin.
1727
*/
18-
export function replaceNodeEnvPlugin(): RolldownPlugin {
28+
export function replaceNodeEnvPlugin({
29+
value = 'production',
30+
}: ReplaceNodeEnvPluginOptions = {}): RolldownPlugin {
31+
const replacement = JSON.stringify(value);
1932
return {
2033
name: 'ocap-kernel:replace-node-env',
2134
transform(code) {
@@ -25,7 +38,7 @@ export function replaceNodeEnvPlugin(): RolldownPlugin {
2538

2639
const transformed = code.replace(
2740
/\bprocess\.env\.NODE_ENV\b/gu,
28-
JSON.stringify('production'),
41+
replacement,
2942
);
3043

3144
if (transformed === code) {

0 commit comments

Comments
 (0)