版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

130 строки
3.2 KiB

  1. 'use strict';
  2. const path = require('path');
  3. const localPkg = require('local-pkg');
  4. const normalizePath = (path$1) => {
  5. path$1 = path$1.startsWith("/") ? path$1 : "/" + path$1;
  6. return path.posix.normalize(path$1);
  7. };
  8. const isVite2 = async () => {
  9. const info = await localPkg.getPackageInfo("vite");
  10. if (info) {
  11. return /.?2/.test(info.version);
  12. }
  13. return false;
  14. };
  15. const createPluginName = (reusable = false) => {
  16. let i = 0;
  17. return (name) => {
  18. const base = `vite-plugin-${name}`;
  19. return reusable ? `${base}:${i++}` : base;
  20. };
  21. };
  22. const createVirtualModuleID = (name) => {
  23. const virtualModuleId = `virtual:${name}`;
  24. const resolvedVirtualModuleId = "\0" + virtualModuleId;
  25. return {
  26. virtualModuleId,
  27. resolvedVirtualModuleId
  28. };
  29. };
  30. const createVirtualGlob = async (target, isSync) => {
  31. const g = `"${target}/**/*.vue"`;
  32. if (await isVite2()) {
  33. return isSync ? `import.meta.globEager(${g})` : `import.meta.glob(${g})`;
  34. }
  35. return `import.meta.glob(${g}, { eager: ${isSync} })`;
  36. };
  37. const createVirtualModuleCode = async (options) => {
  38. const { target, defaultLayout, importMode } = options;
  39. const normalizedTarget = normalizePath(target);
  40. const isSync = importMode === "sync";
  41. return `
  42. export const createGetRoutes = (router, withLayout = false) => {
  43. const routes = router.getRoutes()
  44. if (withLayout) {
  45. return routes
  46. }
  47. return () => routes.filter(route => !route.meta.isLayout)
  48. }
  49. export const setupLayouts = routes => {
  50. const layouts = {}
  51. const modules = ${await createVirtualGlob(
  52. normalizedTarget,
  53. isSync
  54. )}
  55. Object.entries(modules).forEach(([name, module]) => {
  56. let key = name.replace("${normalizedTarget}/", '').replace('.vue', '')
  57. layouts[key] = ${isSync ? "module.default" : "module"}
  58. })
  59. function deepSetupLayout(routes, top = true) {
  60. return routes.map(route => {
  61. if (route.children?.length > 0) {
  62. route.children = deepSetupLayout(route.children, false)
  63. }
  64. if (top) {
  65. return {
  66. path: route.path,
  67. component: layouts[route.meta?.layout || '${defaultLayout}'],
  68. children: [ {...route, path: ''} ],
  69. meta: {
  70. isLayout: true
  71. }
  72. }
  73. }
  74. if (route.meta?.layout) {
  75. return {
  76. path: route.path,
  77. component: layouts[route.meta?.layout],
  78. children: [ {...route, path: ''} ],
  79. meta: {
  80. isLayout: true
  81. }
  82. }
  83. }
  84. return route
  85. })
  86. }
  87. return deepSetupLayout(routes)
  88. }`;
  89. };
  90. const useName = createPluginName(false);
  91. const usePlugin = (options) => {
  92. const {
  93. target = "src/layouts",
  94. defaultLayout = "default",
  95. importMode = process.env.VITE_SSG ? "sync" : "async"
  96. } = options || {};
  97. const { virtualModuleId, resolvedVirtualModuleId } = createVirtualModuleID("meta-layouts");
  98. return {
  99. name: useName("vue-meta-layouts"),
  100. resolveId(id) {
  101. if (id === virtualModuleId) {
  102. return resolvedVirtualModuleId;
  103. }
  104. },
  105. load(id) {
  106. if (id === resolvedVirtualModuleId) {
  107. return createVirtualModuleCode({
  108. target,
  109. importMode,
  110. defaultLayout
  111. });
  112. }
  113. }
  114. };
  115. };
  116. module.exports = usePlugin;