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

Commit c627bad

Browse files
use the new resolve.builtins vite config to specify the cloudflare and node builtins
1 parent 66a00f9 commit c627bad

File tree

4 files changed

+143
-410
lines changed

4 files changed

+143
-410
lines changed

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

Lines changed: 2 additions & 7 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 { getNodeCompatExternals } from './node-js-compat';
@@ -134,6 +133,7 @@ export function createCloudflareEnvironmentOptions(
134133
noExternal: true,
135134
// We want to use `workerd` package exports if available (e.g. for postgres).
136135
conditions: ['workerd', 'module', 'browser', 'development|production'],
136+
builtins: [...cloudflareBuiltInModules],
137137
},
138138
dev: {
139139
createEnvironment(name, config) {
@@ -155,17 +155,12 @@ export function createCloudflareEnvironmentOptions(
155155
input: userConfig.root
156156
? path.resolve(userConfig.root, options.main)
157157
: path.resolve(options.main),
158-
external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()],
158+
external: [...getNodeCompatExternals()],
159159
},
160160
},
161161
optimizeDeps: {
162162
// Note: ssr pre-bundling is opt-in, and we need to enabled it by setting noDiscovery to false
163163
noDiscovery: false,
164-
exclude: [
165-
...cloudflareBuiltInModules,
166-
// we have to exclude all node modules to work in dev-mode not just the unenv externals...
167-
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
168-
],
169164
esbuildOptions: {
170165
platform: 'neutral',
171166
resolveExtensions: [

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'node:fs';
2+
import { builtinModules } from 'node:module';
23
import path from 'node:path';
34
import { createMiddleware } from '@hattip/adapter-node';
45
import { Miniflare } from 'miniflare';
@@ -15,6 +16,7 @@ import {
1516
import {
1617
getNodeCompatAliases,
1718
injectGlobalCode,
19+
isNodeCompat,
1820
resolveNodeAliases,
1921
} from './node-js-compat';
2022
import { normalizePluginConfig } from './plugin-config';
@@ -96,6 +98,9 @@ export function cloudflare<T extends Record<string, WorkerOptions>>(
9698
pluginConfig,
9799
resolvedConfig,
98100
);
101+
Object.keys(normalizedPluginConfig.workers).forEach((name) =>
102+
addNodeBuiltinsIfNeeded(resolvedConfig, name, normalizedPluginConfig),
103+
);
99104
},
100105
async resolveId(source) {
101106
const worker = normalizedPluginConfig.workers[this.environment.name];
@@ -206,3 +211,38 @@ export function cloudflare<T extends Record<string, WorkerOptions>>(
206211
},
207212
};
208213
}
214+
215+
/**
216+
* Adds to the worker resolved configuration the necessary node builtin config options if the worker is under nodejs compat
217+
*
218+
* @param resolvedConfig the vite resolved config (that will be side-effectfully updated)
219+
* @param name the name of the worker
220+
* @param normalizedPluginConfig the normalized/resolved plugin config
221+
*/
222+
function addNodeBuiltinsIfNeeded(
223+
resolvedConfig: vite.ResolvedConfig,
224+
name: string,
225+
normalizedPluginConfig: NormalizedPluginConfig,
226+
) {
227+
invariant(
228+
resolvedConfig.environments[name] && normalizedPluginConfig.workers[name],
229+
`environment configs for worker "${name}" not found`,
230+
);
231+
232+
const nodeCompat = isNodeCompat(
233+
normalizedPluginConfig.workers[name].workerOptions,
234+
);
235+
236+
const nodeCompatModules = nodeCompat
237+
? [...builtinModules.concat(builtinModules.map((m) => `node:${m}`))]
238+
: [];
239+
240+
resolvedConfig.environments[name].resolve.builtins = [
241+
...resolvedConfig.environments[name].resolve.builtins,
242+
...nodeCompatModules,
243+
];
244+
resolvedConfig.environments[name].optimizeDeps.exclude = [
245+
...(resolvedConfig.environments[name].optimizeDeps.exclude ?? []),
246+
...nodeCompatModules,
247+
];
248+
}

0 commit comments

Comments
 (0)