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.

31 lines
807 B

7 months ago
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';