push
This commit is contained in:
30
build/plugin/mock/index.ts
Normal file
30
build/plugin/mock/index.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { ViteMockOptions } from './types';
|
||||
import type { Plugin } from 'vite';
|
||||
import { ResolvedConfig } from 'vite';
|
||||
import { createMockServer, requestMiddleware } from './createMockServer';
|
||||
|
||||
export function viteMockServe(opt: ViteMockOptions = {}): Plugin {
|
||||
let isDev = false;
|
||||
let config: ResolvedConfig;
|
||||
|
||||
return {
|
||||
name: 'vite:mock',
|
||||
enforce: 'pre' as const,
|
||||
configResolved(resolvedConfig) {
|
||||
config = resolvedConfig;
|
||||
isDev = config.command === 'serve';
|
||||
isDev && createMockServer(opt, config);
|
||||
},
|
||||
|
||||
configureServer: async ({ middlewares }) => {
|
||||
const { enable = isDev } = opt;
|
||||
if (!enable) {
|
||||
return;
|
||||
}
|
||||
const middleware = await requestMiddleware(opt);
|
||||
middlewares.use(middleware);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export * from './types';
|
||||
Reference in New Issue
Block a user