You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
807 B
30 lines
807 B
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';
|
|
|