@kfl-ui/utils
Version:
Vue 工具集合,包含自动路由生成器
122 lines (117 loc) • 4.77 kB
JavaScript
;
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);
// index.ts
var index_exports = {};
__export(index_exports, {
createNamespace: () => createNamespace,
createRouterFromGlobs: () => createRouterFromGlobs,
withInstall: () => withInstall
});
module.exports = __toCommonJS(index_exports);
// 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
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 = {
createNamespace,
createRouterFromGlobs,
withInstall
});