Skip to content

Commit

Permalink
perf: improve plugin exports
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Aug 11, 2024
1 parent d22fd36 commit df0b7e0
Show file tree
Hide file tree
Showing 5 changed files with 753 additions and 0 deletions.
9 changes: 9 additions & 0 deletions plugin/src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './core/defineMock'
export * from './core/defineMockData'

export type {
MockOptions,
MockHttpItem,
MockWebsocketItem,
MockRequest,
} from './types'
18 changes: 18 additions & 0 deletions plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { mockDevServerPlugin } from './plugin'

export type {
FormidableFile,
MockOptions,
MockHttpItem,
MockWebsocketItem,
MockServerPluginOptions,
MockRequest,
} from './types'

export * from './helper'
export * from './server'

export { mockDevServerPlugin }

/** @deprecated use named export `mockDevServerPlugin` instead */
export default mockDevServerPlugin
90 changes: 90 additions & 0 deletions plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { toArray } from '@pengzhanbo/utils'
import type { Plugin, ResolvedConfig } from 'vite'
import { generateMockServer } from './core/build'
import { mockServerMiddleware } from './core/mockMiddleware'
import { recoverRequest } from './core/requestRecovery'
import type { MockServerPluginOptions } from './types'
import type { ResolvedMockServerPluginOptions } from './core/resolvePluginOptions'
import { resolvePluginOptions } from './core/resolvePluginOptions'

export function mockDevServerPlugin(options: MockServerPluginOptions = {}): Plugin[] {
const plugins: Plugin[] = [serverPlugin(options)]
if (options.build)
plugins.push(buildPlugin(options))

return plugins
}

export function buildPlugin(
options: MockServerPluginOptions,
): Plugin {
let viteConfig = {} as ResolvedConfig
let resolvedOptions!: ResolvedMockServerPluginOptions
return {
name: 'vite-plugin-mock-dev-server-generator',
enforce: 'post',
apply: 'build',
configResolved(config) {
viteConfig = config
resolvedOptions = resolvePluginOptions(options, config)
config.logger.warn('')
},
async buildEnd(error) {
if (error || viteConfig.command !== 'build')
return

await generateMockServer(this, resolvedOptions)
},
}
}

export function serverPlugin(
options: MockServerPluginOptions,
): Plugin {
let resolvedOptions!: ResolvedMockServerPluginOptions
return {
name: 'vite-plugin-mock-dev-server',
enforce: 'pre',
apply: 'serve',

config(config) {
// 如果启用了 websocket mock,根据 wsPrefix 重新配置 server.proxy,
// 可以避免 wss 初始化时的冲突,带来潜在的影响是,可能存在指定了 `wsPrefix`,
// 但在实际的 mock 中没有对该规则进行配置,从而导致默认的 websocket 代理失效。
// 这时候就需要用户自行在 wsPrefix 中注释掉对应的规则。
const wsPrefix = toArray(options.wsPrefix)
if (wsPrefix.length && config.server?.proxy) {
const proxy: ResolvedConfig['server']['proxy'] = {}
Object.keys(config.server.proxy).forEach((key) => {
if (!wsPrefix.includes(key))
proxy[key] = config.server!.proxy![key]!
})
config.server.proxy = proxy
}
// #52 由于请求流被消费,vite http-proxy 无法获取已消费的请求,导致请求流无法继续
// 通过 http-proxy 的 proxyReq 事件,重新写入请求流
recoverRequest(config)
},

configResolved(config) {
resolvedOptions = resolvePluginOptions(options, config)
// This is a hack to prevent Vite from nuking useful logs,
// pending https://github.com/vitejs/vite/issues/9378
config.logger.warn('')
},

configureServer({ middlewares, httpServer, ws }) {
const middlewareList = mockServerMiddleware(resolvedOptions, httpServer, ws)
middlewareList.forEach(middleware => middlewares.use(middleware))
},

configurePreviewServer({ middlewares, httpServer }) {
// viteConfig to be made available as configurePreviewServer
// pending...
// feat: use preview server parameter in preview server hook #11647
// https://github.com/vitejs/vite/pull/11647
const middlewareList = mockServerMiddleware(resolvedOptions, httpServer)
middlewareList.forEach(middleware => middlewares.use(middleware))
},
}
}
4 changes: 4 additions & 0 deletions plugin/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './core/baseMiddleware'
export * from './core/ws'
export * from './core/transform'
export * from './core/logger'
Loading

0 comments on commit df0b7e0

Please sign in to comment.