_createProductionServer.ts 979 B

12345678910111213141516171819202122232425262728293031323334
  1. import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer';
  2. // 问题描述
  3. // 1. `import.meta.globEager` 已被弃用, 需要升级vite版本,有兼容问题
  4. // 2. `vite-plugin-mock` 插件问题 https://github.com/vbenjs/vite-plugin-mock/issues/56
  5. // const modules: Record<string, any> = import.meta.glob("./**/*.ts", {
  6. // import: "default",
  7. // eager: true,
  8. // });
  9. // const mockModules = Object.keys(modules).reduce((pre, key) => {
  10. // if (!key.includes("/_")) {
  11. // pre.push(...modules[key]);
  12. // }
  13. // return pre;
  14. // }, [] as any[]);
  15. const modules = import.meta.glob('./**/*.ts', { eager: true });
  16. const mockModules: any[] = [];
  17. Object.keys(modules).forEach((key) => {
  18. if (key.includes('/_')) {
  19. return;
  20. }
  21. mockModules.push(...(modules as Recordable)[key].default);
  22. });
  23. /**
  24. * Used in a production environment. Need to manually import all modules
  25. */
  26. export function setupProdMockServer() {
  27. createProdMockServer(mockModules);
  28. }