UNPKG

@kfl-ui/utils

Version:

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

93 lines (90 loc) 3.62 kB
// create.ts var _bem = (prefixName, blockSuffix, element, modifier) => { if (blockSuffix) prefixName += `-${blockSuffix}`; if (element) prefixName += `__${element}`; if (modifier) prefixName += `--${modifier}`; return prefixName; }; var createBEM = (prefixName) => { const b = (blockSuffix = "") => _bem(prefixName, blockSuffix, "", ""); const e = (element = "") => element ? _bem(prefixName, "", element, "") : ""; const m = (modifier = "") => modifier ? _bem(prefixName, "", "", modifier) : ""; const be = (blockSuffix = "", element = "") => blockSuffix && element ? _bem(prefixName, blockSuffix, element, "") : ""; const bm = (blockSuffix = "", modifier = "") => blockSuffix && modifier ? _bem(prefixName, blockSuffix, "", modifier) : ""; const em = (element = "", modifier = "") => element && modifier ? _bem(prefixName, "", element, modifier) : ""; const bem = (blockSuffix = "", element = "", modifier = "") => blockSuffix && element && modifier ? _bem(prefixName, blockSuffix, element, modifier) : ""; return { b, e, m, be, bm, em, bem }; }; var createNamespace = (name) => { const prefixName = `kfl-${name}`; return createBEM(prefixName); }; // with-install.ts var withInstall = (comp) => { comp.install = (app) => { const { name } = comp; app.component(name, comp); }; return comp; }; // autoRouter.ts import { createRouter, createWebHistory } from "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 createRouter({ history: createWebHistory(base), routes, scrollBehavior: () => ({ top: 0 }) }); } export { createNamespace, createRouterFromGlobs, withInstall };