registerPackages.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import type { App } from 'vue';
  2. import { warn } from '/@/utils/log';
  3. import { registerDynamicRouter } from '/@/utils/monorepo/dynamicRouter';
  4. // 引入模块
  5. // import PACKAGE_TEST_JEECG_ONLINE from '@jeecg/online';
  6. // export function registerPackages(app: App) {
  7. // use(app, PACKAGE_TEST_JEECG_ONLINE);
  8. // }
  9. // noinspection JSUnusedGlobalSymbols
  10. const installOptions = {
  11. baseImport,
  12. };
  13. /** 注册模块 */
  14. function use(app: App, pkg) {
  15. app.use(pkg, installOptions);
  16. registerDynamicRouter(pkg.getViews);
  17. }
  18. // 模块里可使用的import
  19. const importGlobs = [import.meta.glob('../../utils/**/*.{ts,js,tsx}'), import.meta.glob('../../hooks/**/*.{ts,js,tsx}')];
  20. /**
  21. * 基础项目导包
  22. * 目前支持导入如下
  23. * /@/utils/**
  24. * /@/hooks/**
  25. *
  26. * @param path 文件路径,ts无需输入后缀名。如:/@/utils/common/compUtils
  27. */
  28. async function baseImport(path: string) {
  29. if (path) {
  30. // 将 /@/ 替换成 ../../
  31. path = path.replace(/^\/@\//, '../../');
  32. for (const glob of importGlobs) {
  33. for (const key of Object.keys(glob)) {
  34. if (path === key || `${path}.ts` === key || `${path}.tsx` === key) {
  35. return glob[key]();
  36. }
  37. }
  38. }
  39. warn(`引入失败:${path} 不存在`);
  40. }
  41. return null;
  42. }