diff --git a/packages/vite-plugin-cloudflare/src/assets.ts b/packages/vite-plugin-cloudflare/src/constants.ts similarity index 52% rename from packages/vite-plugin-cloudflare/src/assets.ts rename to packages/vite-plugin-cloudflare/src/constants.ts index 669b239ec17c..402ec783fe52 100644 --- a/packages/vite-plugin-cloudflare/src/assets.ts +++ b/packages/vite-plugin-cloudflare/src/constants.ts @@ -1,9 +1,3 @@ -import type { Miniflare } from 'miniflare'; - export const ROUTER_WORKER_NAME = '__router-worker__'; export const ASSET_WORKER_NAME = '__asset-worker__'; export const ASSET_WORKERS_COMPATIBILITY_DATE = '2024-10-04'; - -export function getRouterWorker(miniflare: Miniflare) { - return miniflare.getWorker(ROUTER_WORKER_NAME); -} diff --git a/packages/vite-plugin-cloudflare/src/dev.ts b/packages/vite-plugin-cloudflare/src/dev.ts new file mode 100644 index 000000000000..895acabcb25e --- /dev/null +++ b/packages/vite-plugin-cloudflare/src/dev.ts @@ -0,0 +1,22 @@ +import assert from 'node:assert'; +import { ROUTER_WORKER_NAME } from './constants'; +import type { ResolvedPluginConfig } from './plugin-config'; +import type { Miniflare } from 'miniflare'; + +export function getDevEntryWorker( + resolvedPluginConfig: ResolvedPluginConfig, + miniflare: Miniflare, +) { + const entryWorkerConfig = + resolvedPluginConfig.type === 'assets-only' + ? resolvedPluginConfig.config + : resolvedPluginConfig.workers[ + resolvedPluginConfig.entryWorkerEnvironmentName + ]; + + assert(entryWorkerConfig, 'Unexpected error: No entry worker configuration'); + + return entryWorkerConfig.assets + ? miniflare.getWorker(ROUTER_WORKER_NAME) + : miniflare.getWorker(entryWorkerConfig.name); +} diff --git a/packages/vite-plugin-cloudflare/src/index.ts b/packages/vite-plugin-cloudflare/src/index.ts index 80366a24fc22..136eec326f77 100644 --- a/packages/vite-plugin-cloudflare/src/index.ts +++ b/packages/vite-plugin-cloudflare/src/index.ts @@ -1,14 +1,14 @@ import assert from 'node:assert'; import * as fs from 'node:fs'; -import path from 'node:path'; +import * as path from 'node:path'; import { createMiddleware } from '@hattip/adapter-node'; import { Miniflare } from 'miniflare'; import * as vite from 'vite'; -import { getRouterWorker } from './assets'; import { createCloudflareEnvironmentOptions, initRunners, } from './cloudflare-environment'; +import { getDevEntryWorker } from './dev'; import { getDevMiniflareOptions, getPreviewMiniflareOptions, @@ -209,21 +209,19 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin { ); await initRunners(resolvedPluginConfig, viteDevServer, miniflare); - const routerWorker = await getRouterWorker(miniflare); + const entryWorker = await getDevEntryWorker( + resolvedPluginConfig, + miniflare, + ); - const middleware = createMiddleware(async ({ request }) => { - return routerWorker.fetch(toMiniflareRequest(request), { + const middleware = createMiddleware(({ request }) => { + return entryWorker.fetch(toMiniflareRequest(request), { redirect: 'manual', }) as any; }); return () => { viteDevServer.middlewares.use((req, res, next) => { - if (!middleware) { - next(); - return; - } - middleware(req, res, next); }); }; @@ -241,11 +239,6 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin { return () => { vitePreviewServer.middlewares.use((req, res, next) => { - if (!middleware) { - next(); - return; - } - middleware(req, res, next); }); }; diff --git a/packages/vite-plugin-cloudflare/src/miniflare-options.ts b/packages/vite-plugin-cloudflare/src/miniflare-options.ts index 3a94120410e6..b1112bcd6fd0 100644 --- a/packages/vite-plugin-cloudflare/src/miniflare-options.ts +++ b/packages/vite-plugin-cloudflare/src/miniflare-options.ts @@ -13,7 +13,7 @@ import { ASSET_WORKER_NAME, ASSET_WORKERS_COMPATIBILITY_DATE, ROUTER_WORKER_NAME, -} from './assets'; +} from './constants'; import type { CloudflareDevEnvironment } from './cloudflare-environment'; import type { PersistState,