Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { builtinModules } from 'node:module';
import * as path from 'node:path';
import * as vite from 'vite';
import { INIT_PATH, invariant, UNKNOWN_HOST } from './shared';
import type { NormalizedPluginConfig, WorkerOptions } from './plugin-config';
Expand Down Expand Up @@ -118,7 +119,8 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment {
}
}

export function createCloudflareEnvironment(
export function createCloudflareEnvironmentOptions(
name: string,
options: WorkerOptions,
): vite.EnvironmentOptions {
return vite.mergeConfig(
Expand Down Expand Up @@ -158,6 +160,7 @@ export function createCloudflareEnvironment(
createEnvironment(name, config) {
return new vite.BuildEnvironment(name, config);
},
outDir: path.join('dist', name),
ssr: true,
rollupOptions: {
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
Expand Down
17 changes: 6 additions & 11 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as path from 'node:path';
import { createMiddleware } from '@hattip/adapter-node';
import { Miniflare } from 'miniflare';
import * as vite from 'vite';
import {
createCloudflareEnvironment,
createCloudflareEnvironmentOptions,
initRunners,
} from './cloudflare-environment';
import { getMiniflareOptions } from './miniflare-options';
Expand Down Expand Up @@ -38,19 +37,15 @@ export function cloudflare<T extends Record<string, WorkerOptions>>(
);
},
},
// Ensure there is an environment for each worker
environments: Object.fromEntries(
Object.entries(pluginConfig.workers).map(([name, options]) => {
return [name, createCloudflareEnvironment(options)];
}),
Object.entries(pluginConfig.workers).map(([name, workerOptions]) => [
name,
createCloudflareEnvironmentOptions(name, workerOptions),
]),
),
};
},
configEnvironment(name, options) {
options.build = {
outDir: path.join('dist', name),
...options.build,
};
},
configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
},
Expand Down
9 changes: 1 addition & 8 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';
import { Log, LogLevel, Response as MiniflareResponse } from 'miniflare';
import * as vite from 'vite';
import { unstable_getMiniflareWorkerOptions } from 'wrangler';
import { invariant, WORKERD_CUSTOM_IMPORT_PATH } from './shared';
import { invariant } from './shared';
import type { CloudflareDevEnvironment } from './cloudflare-environment';
import type { NormalizedPluginConfig } from './plugin-config';
import type { MiniflareOptions, SharedOptions, WorkerOptions } from 'miniflare';
Expand Down Expand Up @@ -191,7 +191,6 @@ export function getMiniflareOptions(

return {
...workerOptions,
unsafeUseModuleFallbackService: true,
modules: [
{
type: 'ESModule',
Expand All @@ -205,12 +204,6 @@ export function getMiniflareOptions(
fileURLToPath(new URL(RUNNER_PATH, import.meta.url)),
),
},
{
// Declared as a CommonJS module so that `require` is made available and we are able to handle cjs imports
type: 'CommonJS',
path: path.join(miniflareModulesRoot, WORKERD_CUSTOM_IMPORT_PATH),
contents: 'module.exports = path => import(path)',
},
],
serviceBindings: {
...workerOptions.serviceBindings,
Expand Down
13 changes: 2 additions & 11 deletions packages/vite-plugin-cloudflare/src/runner/module-runner.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { ModuleRunner } from 'vite/module-runner';
import { UNKNOWN_HOST, WORKERD_CUSTOM_IMPORT_PATH } from '../shared';
import { UNKNOWN_HOST } from '../shared';
import type { WrapperEnv } from './env';
import type { FetchResult } from 'vite/module-runner';

let moduleRunner: ModuleRunner;

// TODO: node modules using process.env don't find `process` in the global scope for some reason
// for now we just create a `process` in the global scope but a proper solution needs to be
// implemented (see: https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/22)
(globalThis as Record<string, unknown>).process = { env: {} };

export async function createModuleRunner(
env: WrapperEnv,
webSocket: WebSocket,
Expand All @@ -18,10 +13,6 @@ export async function createModuleRunner(
throw new Error('Runner already initialized');
}

const { default: workerdCustomImport } = await (import(
`/${WORKERD_CUSTOM_IMPORT_PATH}`
) as Promise<{ default: (...args: unknown[]) => Promise<unknown> }>);

moduleRunner = new ModuleRunner(
{
root: env.__VITE_ROOT__,
Expand Down Expand Up @@ -70,7 +61,7 @@ export async function createModuleRunner(
},
async runExternalModule(filepath) {
filepath = filepath.replace(/^file:\/\//, '');
return workerdCustomImport(filepath);
return import(filepath);
},
},
);
Expand Down
1 change: 0 additions & 1 deletion packages/vite-plugin-cloudflare/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const UNKNOWN_HOST = 'http://localhost';
export const INIT_PATH = '/__vite_plugin_cloudflare_init__';
export const WORKERD_CUSTOM_IMPORT_PATH = '__WORKERD_CUSTOM_IMPORT_PATH__';

export function invariant(
condition: unknown,
Expand Down