Skip to content
Open
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
34 changes: 32 additions & 2 deletions packages/dev-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +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
// Returning `undefined` will skip the middleware and pass the request to Vite.
middleware?: (req: Request) => Response | undefined
} & {
/**
* @deprecated
Expand All @@ -19,7 +22,9 @@ export type DevServerOptions = {
cf?: Parameters<typeof cloudflarePagesGetEnv>[0]
}

export const defaultOptions: Required<Omit<DevServerOptions, 'env' | 'cf'>> = {
export const defaultOptions: Required<
Omit<DevServerOptions, 'env' | 'cf' | 'include' | 'middleware'>
> = {
entry: './src/index.ts',
injectClientScript: true,
exclude: [
Expand All @@ -39,10 +44,32 @@ export function devServer(options?: DevServerOptions): Plugin {
configureServer: async (server) => {
async function createMiddleware(server: ViteDevServer): Promise<Connect.HandleFunction> {
return async function (
req: http.IncomingMessage,
req: Connect.IncomingMessage,
res: http.ServerResponse,
next: Connect.NextFunction
): Promise<void> {
if (options?.include) {
let matched = false

for (const pattern of options.include) {
if (req.url) {
if (pattern instanceof RegExp) {
if (pattern.test(req.originalUrl || 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

for (const pattern of exclude) {
Expand Down Expand Up @@ -84,6 +111,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: () => {
Expand Down