2021-01-09 23:28:52 +08:00
|
|
|
import { viteMockServe } from 'vite-plugin-mock';
|
|
|
|
import { ViteEnv } from '../../utils';
|
2020-12-13 21:17:37 +08:00
|
|
|
|
2021-01-09 23:28:52 +08:00
|
|
|
export function configMockPlugin(env: ViteEnv, isBuild: boolean) {
|
2020-12-13 21:17:37 +08:00
|
|
|
const { VITE_USE_MOCK } = env;
|
2020-12-22 22:13:03 +08:00
|
|
|
|
2021-01-22 22:42:27 +08:00
|
|
|
const useLocalMock = !isBuild && VITE_USE_MOCK;
|
|
|
|
const useProdMock = isBuild && VITE_USE_MOCK;
|
2020-12-22 22:13:03 +08:00
|
|
|
|
2021-01-22 22:42:27 +08:00
|
|
|
if (useLocalMock || useProdMock) {
|
2021-01-09 23:28:52 +08:00
|
|
|
const mockPlugin = viteMockServe({
|
2020-12-22 22:13:03 +08:00
|
|
|
ignore: /^\_/,
|
|
|
|
mockPath: 'mock',
|
|
|
|
showTime: true,
|
2021-01-22 22:42:27 +08:00
|
|
|
localEnabled: useLocalMock,
|
|
|
|
prodEnabled: useProdMock,
|
|
|
|
injectCode: `
|
|
|
|
import { setupProdMockServer } from '../mock/_createProductionServer';
|
|
|
|
|
|
|
|
setupProdMockServer();
|
|
|
|
`,
|
2020-12-22 22:13:03 +08:00
|
|
|
});
|
2021-01-09 23:28:52 +08:00
|
|
|
return mockPlugin;
|
2020-12-13 21:17:37 +08:00
|
|
|
}
|
2021-01-09 23:28:52 +08:00
|
|
|
return [];
|
2020-12-13 21:17:37 +08:00
|
|
|
}
|