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

Commit 407efbe

Browse files
fix: remove unnecessary code
Now that we are prebundling, and can turn on npdejs_compat, some of the hacks from before are no longer needed. - remove the fake process global - remove the workerd custom import module - remove the unsafeUseModuleFallbackService property - consolidate environment options creation - move vite override to the workspace catalog
1 parent 711b24f commit 407efbe

File tree

7 files changed

+14
-38
lines changed

7 files changed

+14
-38
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,5 @@
1414
"@ianvs/prettier-plugin-sort-imports": "^4.3.1",
1515
"prettier": "^3.3.3",
1616
"prettier-plugin-packagejson": "^2.5.3"
17-
},
18-
"pnpm": {
19-
"overrides": {
20-
"vite": "https://pkg.pr.new/vite@main"
21-
}
2217
}
2318
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { builtinModules } from 'node:module';
2+
import * as path from 'node:path';
23
import * as vite from 'vite';
34
import { INIT_PATH, invariant, UNKNOWN_HOST } from './shared';
45
import type { NormalizedPluginConfig, WorkerOptions } from './plugin-config';
@@ -118,7 +119,8 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment {
118119
}
119120
}
120121

121-
export function createCloudflareEnvironment(
122+
export function createCloudflareEnvironmentOptions(
123+
name: string,
122124
options: WorkerOptions,
123125
): vite.EnvironmentOptions {
124126
return vite.mergeConfig(
@@ -158,6 +160,7 @@ export function createCloudflareEnvironment(
158160
createEnvironment(name, config) {
159161
return new vite.BuildEnvironment(name, config);
160162
},
163+
outDir: path.join('dist', name),
161164
ssr: true,
162165
rollupOptions: {
163166
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import * as path from 'node:path';
21
import { createMiddleware } from '@hattip/adapter-node';
32
import { Miniflare } from 'miniflare';
43
import * as vite from 'vite';
54
import {
6-
createCloudflareEnvironment,
5+
createCloudflareEnvironmentOptions,
76
initRunners,
87
} from './cloudflare-environment';
98
import { getMiniflareOptions } from './miniflare-options';
@@ -38,19 +37,15 @@ export function cloudflare<T extends Record<string, WorkerOptions>>(
3837
);
3938
},
4039
},
40+
// Ensure there is an environment for each worker
4141
environments: Object.fromEntries(
42-
Object.entries(pluginConfig.workers).map(([name, options]) => {
43-
return [name, createCloudflareEnvironment(options)];
44-
}),
42+
Object.entries(pluginConfig.workers).map(([name, workerOptions]) => [
43+
name,
44+
createCloudflareEnvironmentOptions(name, workerOptions),
45+
]),
4546
),
4647
};
4748
},
48-
configEnvironment(name, options) {
49-
options.build = {
50-
outDir: path.join('dist', name),
51-
...options.build,
52-
};
53-
},
5449
configResolved(resolvedConfig) {
5550
viteConfig = resolvedConfig;
5651
},

packages/vite-plugin-cloudflare/src/miniflare-options.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';
44
import { Log, LogLevel, Response as MiniflareResponse } from 'miniflare';
55
import * as vite from 'vite';
66
import { unstable_getMiniflareWorkerOptions } from 'wrangler';
7-
import { invariant, WORKERD_CUSTOM_IMPORT_PATH } from './shared';
7+
import { invariant } from './shared';
88
import type { CloudflareDevEnvironment } from './cloudflare-environment';
99
import type { NormalizedPluginConfig } from './plugin-config';
1010
import type { MiniflareOptions, SharedOptions, WorkerOptions } from 'miniflare';
@@ -191,7 +191,6 @@ export function getMiniflareOptions(
191191

192192
return {
193193
...workerOptions,
194-
unsafeUseModuleFallbackService: true,
195194
modules: [
196195
{
197196
type: 'ESModule',
@@ -205,12 +204,6 @@ export function getMiniflareOptions(
205204
fileURLToPath(new URL(RUNNER_PATH, import.meta.url)),
206205
),
207206
},
208-
{
209-
// Declared as a CommonJS module so that `require` is made available and we are able to handle cjs imports
210-
type: 'CommonJS',
211-
path: path.join(miniflareModulesRoot, WORKERD_CUSTOM_IMPORT_PATH),
212-
contents: 'module.exports = path => import(path)',
213-
},
214207
],
215208
serviceBindings: {
216209
...workerOptions.serviceBindings,

packages/vite-plugin-cloudflare/src/runner/module-runner.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { ModuleRunner } from 'vite/module-runner';
2-
import { UNKNOWN_HOST, WORKERD_CUSTOM_IMPORT_PATH } from '../shared';
2+
import { UNKNOWN_HOST } from '../shared';
33
import type { WrapperEnv } from './env';
44
import type { FetchResult } from 'vite/module-runner';
55

66
let moduleRunner: ModuleRunner;
77

8-
// TODO: node modules using process.env don't find `process` in the global scope for some reason
9-
// for now we just create a `process` in the global scope but a proper solution needs to be
10-
// implemented (see: https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/22)
11-
(globalThis as Record<string, unknown>).process = { env: {} };
12-
138
export async function createModuleRunner(
149
env: WrapperEnv,
1510
webSocket: WebSocket,
@@ -18,10 +13,6 @@ export async function createModuleRunner(
1813
throw new Error('Runner already initialized');
1914
}
2015

21-
const { default: workerdCustomImport } = await (import(
22-
`/${WORKERD_CUSTOM_IMPORT_PATH}`
23-
) as Promise<{ default: (...args: unknown[]) => Promise<unknown> }>);
24-
2516
moduleRunner = new ModuleRunner(
2617
{
2718
root: env.__VITE_ROOT__,
@@ -70,7 +61,7 @@ export async function createModuleRunner(
7061
},
7162
async runExternalModule(filepath) {
7263
filepath = filepath.replace(/^file:\/\//, '');
73-
return workerdCustomImport(filepath);
64+
return import(filepath);
7465
},
7566
},
7667
);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export const UNKNOWN_HOST = 'http://localhost';
22
export const INIT_PATH = '/__vite_plugin_cloudflare_init__';
3-
export const WORKERD_CUSTOM_IMPORT_PATH = '__WORKERD_CUSTOM_IMPORT_PATH__';
43

54
export function invariant(
65
condition: unknown,

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ packages:
66
catalog:
77
'@cloudflare/workers-types': ^4.20240919.0
88
'typescript': ^5.6.2
9-
'vite': '6.0.0-beta.2'
9+
'vite': "https://pkg.pr.new/vite@main"
1010
'vitest': '^2.1.2'
1111
'wrangler': '^3.80.4'

0 commit comments

Comments
 (0)