UNPKG

@aniyajs/plugin-router

Version:

基于 react-routerV6 封装的路由插件服务于aniyajs自动化脚手架

55 lines (47 loc) 1.64 kB
import { flatArr } from "./index"; export default (api) => { api.describe({ key: "routes", config: { default: [], }, }); if (api.configition.routes && api.configition.routes.length) { api.generate(async (args) => { const { pluginTempPath, ...paths } = args; const routers = flatArr({ initArr: api.configition.routes ? api.configition.routes : [], childrenField: "routes", }).filter((item) => item.component); const content = routers .map((route, i) => { const routeName = `Demo${i + 1}`; const routePath = api.join(paths.appPagesPath, route.component); // 转为 POSIX 路径(正斜杠) const posixMockPath = routePath.replace(/\\/g, "/"); return `export { default as ${routeName} } from "${posixMockPath}";`; }) .join("\n"); api.writeFile({ file: api.resolve(pluginTempPath, "imports.ts"), content, }); const templatePaths = await api.readFilterdir({ rootPath: api.join( paths.appNodeModules, "@aniyajs/plugin-router/templates" ), filterHandle: Boolean, withSuffix: true, }); templatePaths.forEach((templatePath) => { const basename = api.basename(templatePath); const filename = basename.slice(0, basename.length - 4); api.copyTpl(templatePath, api.resolve(pluginTempPath, filename), { haveRedux: api.configition.toolTik ? api.configition.toolTik : false, configFile: paths.appConfigJs.replace(/\\/g, "/"), }); }); }); } };