Skip to content

Commit

Permalink
refactor: optimize proxies logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Feb 20, 2023
1 parent 7d190c2 commit 7444193
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = Plugin['buildEnd']> = T extends (
this: infer R,
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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}')
Expand Down
4 changes: 2 additions & 2 deletions src/mockMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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` 以足够
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T = any>(val: unknown): val is T[] => Array.isArray(val)

Expand Down Expand Up @@ -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
}

0 comments on commit 7444193

Please sign in to comment.