2020-09-28 20:19:10 +08:00
|
|
|
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer';
|
2021-01-17 23:47:54 +08:00
|
|
|
|
2023-02-22 18:32:53 +08:00
|
|
|
// 问题描述
|
|
|
|
// 1. `import.meta.globEager` 已被弃用, 需要升级vite版本,有兼容问题
|
|
|
|
// 2. `vite-plugin-mock` 插件问题 https://github.com/vbenjs/vite-plugin-mock/issues/56
|
|
|
|
|
|
|
|
// const modules: Record<string, any> = import.meta.glob("./**/*.ts", {
|
|
|
|
// import: "default",
|
|
|
|
// eager: true,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// const mockModules = Object.keys(modules).reduce((pre, key) => {
|
|
|
|
// if (!key.includes("/_")) {
|
|
|
|
// pre.push(...modules[key]);
|
|
|
|
// }
|
|
|
|
// return pre;
|
|
|
|
// }, [] as any[]);
|
|
|
|
|
2023-07-06 15:00:38 +08:00
|
|
|
const modules = import.meta.glob('./**/*.ts', { eager: true });
|
2021-01-17 23:47:54 +08:00
|
|
|
|
|
|
|
const mockModules: any[] = [];
|
|
|
|
Object.keys(modules).forEach((key) => {
|
|
|
|
if (key.includes('/_')) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-06 15:00:38 +08:00
|
|
|
mockModules.push(...(modules as Recordable)[key].default);
|
2021-01-17 23:47:54 +08:00
|
|
|
});
|
2020-09-28 20:19:10 +08:00
|
|
|
|
2020-10-29 23:01:11 +08:00
|
|
|
/**
|
|
|
|
* Used in a production environment. Need to manually import all modules
|
|
|
|
*/
|
2020-09-28 20:19:10 +08:00
|
|
|
export function setupProdMockServer() {
|
2021-01-17 23:47:54 +08:00
|
|
|
createProdMockServer(mockModules);
|
2020-09-28 20:19:10 +08:00
|
|
|
}
|