Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 400c034

Browse files
add node builtins only for workers under nodejs compat
1 parent 781ccb3 commit 400c034

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

packages/vite-plugin-cloudflare/src/cloudflare-environment.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { builtinModules } from 'node:module';
21
import * as path from 'node:path';
32
import * as vite from 'vite';
43
import { INIT_PATH, invariant, UNKNOWN_HOST } from './shared';
@@ -135,11 +134,9 @@ export function createCloudflareEnvironmentOptions(
135134
// Note: in order for ssr pre-bundling to take effect we need to ask vite to treat all
136135
// dependencies as not external
137136
noExternal: true,
138-
builtins: [
139-
// TODO: we should assume that the node modules are built in only if the nodejs_compat flag is set
140-
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
141-
...cloudflareBuiltInModules,
142-
],
137+
// We want to use `workerd` package exports if available (e.g. for postgres).
138+
conditions: ['workerd', 'module', 'browser', 'development|production'],
139+
builtins: [...cloudflareBuiltInModules],
143140
},
144141
dev: {
145142
createEnvironment(name, config) {
@@ -164,10 +161,7 @@ export function createCloudflareEnvironmentOptions(
164161
optimizeDeps: {
165162
// Note: ssr pre-bundling is opt-in, and we need to enabled it by setting noDiscovery to false
166163
noDiscovery: false,
167-
exclude: [
168-
...cloudflareBuiltInModules,
169-
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
170-
],
164+
exclude: [...cloudflareBuiltInModules],
171165
esbuildOptions: {
172166
resolveExtensions: [
173167
'.mjs',

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { builtinModules } from 'node:module';
12
import { createMiddleware } from '@hattip/adapter-node';
23
import { Miniflare } from 'miniflare';
34
import * as vite from 'vite';
@@ -9,6 +10,7 @@ import { getMiniflareOptions } from './miniflare-options';
910
import {
1011
getNodeCompatAliases,
1112
injectGlobalCode,
13+
isNodeCompat,
1214
resolveNodeAliases,
1315
} from './node-js-compat';
1416
import { normalizePluginConfig } from './plugin-config';
@@ -66,6 +68,9 @@ export function cloudflare<T extends Record<string, WorkerOptions>>(
6668
pluginConfig,
6769
resolvedConfig,
6870
);
71+
Object.keys(pluginConfig.workers).forEach((name) =>
72+
addNodeBuiltinsIfNeeded(resolvedConfig, name, normalizedPluginConfig),
73+
);
6974
},
7075
resolveId(source) {
7176
const worker = normalizedPluginConfig.workers[this.environment.name];
@@ -147,3 +152,38 @@ export function cloudflare<T extends Record<string, WorkerOptions>>(
147152
},
148153
};
149154
}
155+
156+
/**
157+
* Adds the a worker resolved configuration the necessary node builtin config options is the worker is using under nodejs compat
158+
*
159+
* @param resolvedConfig the vite resolved config (that will be side-effectfully updated)
160+
* @param name the name of the worker
161+
* @param normalizedPluginConfig the normalized/resolved plugin config
162+
*/
163+
function addNodeBuiltinsIfNeeded(
164+
resolvedConfig: vite.ResolvedConfig,
165+
name: string,
166+
normalizedPluginConfig: NormalizedPluginConfig,
167+
) {
168+
invariant(
169+
resolvedConfig.environments[name] && normalizedPluginConfig.workers[name],
170+
`environment configs for worker "${name}" not found`,
171+
);
172+
173+
const nodeCompat = isNodeCompat(
174+
normalizedPluginConfig.workers[name].workerOptions,
175+
);
176+
177+
const nodeCompatModules = nodeCompat
178+
? [...builtinModules.concat(builtinModules.map((m) => `node:${m}`))]
179+
: [];
180+
181+
resolvedConfig.environments[name].resolve.builtins = [
182+
...resolvedConfig.environments[name].resolve.builtins,
183+
...nodeCompatModules,
184+
];
185+
resolvedConfig.environments[name].optimizeDeps.exclude = [
186+
...(resolvedConfig.environments[name].optimizeDeps.exclude ?? []),
187+
...nodeCompatModules,
188+
];
189+
}

0 commit comments

Comments
 (0)