From 744419329955956ec88ee670d8f4b6ba33385f5c Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Tue, 21 Feb 2023 00:11:48 +0800 Subject: [PATCH] refactor: optimize `proxies` logic --- src/build.ts | 6 +++--- src/mockMiddleware.ts | 4 ++-- src/utils.ts | 13 +++++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/build.ts b/src/build.ts index 6ed0bcb..e485b09 100644 --- a/src/build.ts +++ b/src/build.ts @@ -10,7 +10,7 @@ import { createFilter } from 'vite' import { name, version } from '../package.json' import { externalizeDeps, json5Loader, jsonLoader } from './esbuildPlugin' import type { MockServerPluginOptions, ServerBuildOption } from './types' -import { ensureArray, lookupFile } from './utils' +import { ensureArray, ensureProxies, lookupFile } from './utils' type PluginContext = T extends ( this: infer R, @@ -33,7 +33,7 @@ export async function generateMockServer( define[key] = typeof val === 'string' ? val : JSON.stringify(val) } } - const proxies: string[] = Object.keys(config.server.proxy || {}) + const proxies: string[] = ensureProxies(config.server.proxy || {}) const prefix = ensureArray(options.prefix) let pkg = {} @@ -133,7 +133,7 @@ import mockData from './mock-data.js' const app = connect() app.use(baseMiddleware({ mockData }, { formidableOptions: { multiples: true }, - proxies: ${JSON.stringify(proxies)} + proxies: ${JSON.stringify(proxies)} })) app.listen(${port}) console.log('listen: http://localhost:${port}') diff --git a/src/mockMiddleware.ts b/src/mockMiddleware.ts index b95f691..d240a48 100644 --- a/src/mockMiddleware.ts +++ b/src/mockMiddleware.ts @@ -3,7 +3,7 @@ import type { Connect, ResolvedConfig } from 'vite' import { baseMiddleware } from './baseMiddleware' import { MockLoader } from './MockLoader' import type { MockServerPluginOptions } from './types' -import { ensureArray } from './utils' +import { ensureArray, ensureProxies } from './utils' export async function mockServerMiddleware( httpServer: http.Server | null, @@ -36,7 +36,7 @@ export async function mockServerMiddleware( * * 在一般开发场景中,我们也只需要对通过 vite server 进行代理的请求 进行 mock */ - const proxies: string[] = Object.keys(config.server.proxy || {}) + const proxies: string[] = ensureProxies(config.server.proxy || {}) /** * 保留直接通过 plugin option 直接配置 路径匹配规则, * 但在大多数场景下,共用 `server.proxy` 以足够 diff --git a/src/utils.ts b/src/utils.ts index 1d6e70c..6610896 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,7 @@ import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import Debug from 'debug' +import type { ResolvedConfig } from 'vite' export const isArray = (val: unknown): val is T[] => Array.isArray(val) @@ -54,3 +55,15 @@ export function lookupFile( return lookupFile(parentDir, formats, options) } } + +export const ensureProxies = ( + serverProxy: ResolvedConfig['server']['proxy'] = {}, +): string[] => { + const proxies: string[] = Object.keys(serverProxy) + .map((key) => { + const value = serverProxy[key] + return typeof value === 'string' ? key : value.ws === true ? '' : key + }) + .filter(Boolean) + return proxies +}