From 1a40c3cc24f053427ed32154443e8725efcfbb80 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Wed, 17 Jan 2024 11:19:11 +0800 Subject: [PATCH 1/3] `shouldServeWithHono` option --- packages/dev-server/src/dev-server.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/dev-server/src/dev-server.ts b/packages/dev-server/src/dev-server.ts index 22f1de78..581de2ac 100644 --- a/packages/dev-server/src/dev-server.ts +++ b/packages/dev-server/src/dev-server.ts @@ -10,6 +10,7 @@ export type DevServerOptions = { injectClientScript?: boolean exclude?: (string | RegExp)[] env?: Env | EnvFunc + shouldServeWithHono?: (url: string) => boolean } & { /** * @deprecated @@ -19,7 +20,7 @@ export type DevServerOptions = { cf?: Parameters[0] } -export const defaultOptions: Required> = { +export const defaultOptions: Required> = { entry: './src/index.ts', injectClientScript: true, exclude: [ @@ -43,6 +44,11 @@ export function devServer(options?: DevServerOptions): Plugin { res: http.ServerResponse, next: Connect.NextFunction ): Promise { + if ( + options?.shouldServeWithHono && + !options.shouldServeWithHono(req.url!) + ) return next() + const exclude = options?.exclude ?? defaultOptions.exclude for (const pattern of exclude) { From ca2777f663ba11ad17ae93dddaab3aa87ddfb952 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Wed, 17 Jan 2024 11:26:12 +0800 Subject: [PATCH 2/3] `include` + `middleware` properties --- packages/dev-server/src/dev-server.ts | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/dev-server/src/dev-server.ts b/packages/dev-server/src/dev-server.ts index 581de2ac..ca3b39a6 100644 --- a/packages/dev-server/src/dev-server.ts +++ b/packages/dev-server/src/dev-server.ts @@ -8,9 +8,11 @@ import type { Env, Fetch, EnvFunc } from './types.js' export type DevServerOptions = { entry?: string injectClientScript?: boolean + include?: (string | RegExp)[] exclude?: (string | RegExp)[] env?: Env | EnvFunc - shouldServeWithHono?: (url: string) => boolean + // Returning `undefined` will skip the middleware and pass the request to Vite. + middleware?: (req: Request) => Response | undefined } & { /** * @deprecated @@ -20,7 +22,7 @@ export type DevServerOptions = { cf?: Parameters[0] } -export const defaultOptions: Required> = { +export const defaultOptions: Required> = { entry: './src/index.ts', injectClientScript: true, exclude: [ @@ -44,10 +46,27 @@ export function devServer(options?: DevServerOptions): Plugin { res: http.ServerResponse, next: Connect.NextFunction ): Promise { - if ( - options?.shouldServeWithHono && - !options.shouldServeWithHono(req.url!) - ) return next() + if (options?.include) { + let matched = false + + for (const pattern of options.include) { + if (req.url) { + if (pattern instanceof RegExp) { + if (pattern.test(req.url)) { + matched = true + break + } + } else if (minimatch(req.url?.toString(), pattern)) { + matched = true + break + } + } + } + + if (!matched) { + return next() + } + } const exclude = options?.exclude ?? defaultOptions.exclude @@ -90,6 +109,9 @@ export function devServer(options?: DevServerOptions): Plugin { env = await cloudflarePagesGetEnv(options.cf)() } + const result = options?.middleware?.(request) + if (result instanceof Response) return result + let response = await app.fetch(request, env, { waitUntil: async (fn) => fn, passThroughOnException: () => { From f850a06f753ee9ad95c9ca293683621a63e11fe5 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Wed, 17 Jan 2024 13:03:29 +0800 Subject: [PATCH 3/3] Check original URL --- packages/dev-server/src/dev-server.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/dev-server/src/dev-server.ts b/packages/dev-server/src/dev-server.ts index ca3b39a6..5e346b57 100644 --- a/packages/dev-server/src/dev-server.ts +++ b/packages/dev-server/src/dev-server.ts @@ -22,7 +22,9 @@ export type DevServerOptions = { cf?: Parameters[0] } -export const defaultOptions: Required> = { +export const defaultOptions: Required< + Omit +> = { entry: './src/index.ts', injectClientScript: true, exclude: [ @@ -42,7 +44,7 @@ export function devServer(options?: DevServerOptions): Plugin { configureServer: async (server) => { async function createMiddleware(server: ViteDevServer): Promise { return async function ( - req: http.IncomingMessage, + req: Connect.IncomingMessage, res: http.ServerResponse, next: Connect.NextFunction ): Promise { @@ -52,7 +54,7 @@ export function devServer(options?: DevServerOptions): Plugin { for (const pattern of options.include) { if (req.url) { if (pattern instanceof RegExp) { - if (pattern.test(req.url)) { + if (pattern.test(req.originalUrl || req.url)) { matched = true break }