UNPKG

@kfl-ui/utils

Version:

Vue 工具集合,包含自动路由生成器

85 lines (83 loc) 3.33 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // autoRouter.ts var autoRouter_exports = {}; __export(autoRouter_exports, { createRouterFromGlobs: () => createRouterFromGlobs }); module.exports = __toCommonJS(autoRouter_exports); var import_vue_router = require("vue-router"); function createRouterFromGlobs(pages, components, base = "/") { var _a; const normalizePath = (path) => path.replace("/src/views", "").replace(/\/page\.(ts|js)$/, "") || "/"; const getRouteName = (path) => path.split("/").filter(Boolean).join("-") || "index"; const routeMap = {}; const childrenPaths = /* @__PURE__ */ new Set(); for (const [pagePath, config] of Object.entries(pages)) { const routePath = normalizePath(pagePath); const compPath = pagePath.replace(/page\.(ts|js)$/, "index.vue"); const route = { path: routePath, name: getRouteName(routePath), component: components[compPath], meta: config.meta || {}, children: [] }; if (config.redirect) { route.children.push({ path: "", redirect: config.redirect }); } routeMap[routePath] = route; } for (const [pagePath, config] of Object.entries(pages)) { const parentPath = normalizePath(pagePath); if ((_a = config == null ? void 0 : config.children) == null ? void 0 : _a.length) { config.children.forEach((child) => { var _a2, _b; const fullPath = `${parentPath}/${child.path}`.replace(/\/+/g, "/"); const compPath = `/src/views${fullPath}/index.vue`; const childConfig = pages[`/src/views${fullPath}/page.ts`] || pages[`/src/views${fullPath}/page.js`] || {}; const childRoute = { path: child.path, name: child.name, component: components[compPath], meta: child.meta || childConfig.meta || {} }; (_b = (_a2 = routeMap[parentPath]) == null ? void 0 : _a2.children) == null ? void 0 : _b.push(childRoute); childrenPaths.add(`/src/views${fullPath}/page.ts`); childrenPaths.add(`/src/views${fullPath}/page.js`); }); } } const routes = Object.entries(pages).filter(([pagePath]) => !childrenPaths.has(pagePath)).map(([pagePath]) => { const routePath = normalizePath(pagePath); return routeMap[routePath]; }); return (0, import_vue_router.createRouter)({ history: (0, import_vue_router.createWebHistory)(base), routes, scrollBehavior: () => ({ top: 0 }) }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createRouterFromGlobs });