feat: 文件目录名带@转化成动态路由 (#137)

This commit is contained in:
harrywan 2022-07-08 10:19:56 +08:00 committed by GitHub
parent 4e64e090a2
commit 7ef10557d2
2 changed files with 9 additions and 14 deletions

View File

@ -50,14 +50,9 @@ const getRouteName = function (parentRoutePath, fileName) {
.replace(/\*/g, 'FUZZYMATCH'); .replace(/\*/g, 'FUZZYMATCH');
}; };
const getComponentPath = function (parentRoutePath, fileName, config) { const getRoutePath = function (parentRoutePath, fileName, isFile = true) {
const pagesName = config.singular ? 'page' : 'pages';
return posix.join(`@/${pagesName}/`, parentRoutePath, fileName);
};
const getRoutePath = function (parentRoutePath, fileName) {
// /index.vue -> / // /index.vue -> /
if (fileName === 'index') { if (isFile && fileName === 'index') {
fileName = ''; fileName = '';
} }
// /@id.vue -> /:id // /@id.vue -> /:id
@ -87,7 +82,7 @@ function getRouteMeta(content) {
let cacheGenRoutes = {}; let cacheGenRoutes = {};
// TODO 约定 layout 目录作为布局文件夹, // TODO 约定 layout 目录作为布局文件夹,
const genRoutes = function (parentRoutes, path, parentRoutePath, config) { const genRoutes = function (parentRoutes, path, parentRoutePath) {
const dirList = readdirSync(path); const dirList = readdirSync(path);
const hasLayout = checkHasLayout(path); const hasLayout = checkHasLayout(path);
const layoutRoute = { const layoutRoute = {
@ -113,7 +108,7 @@ const genRoutes = function (parentRoutes, path, parentRoutePath, config) {
// 路由名称 // 路由名称
const routeName = getRouteName(parentRoutePath, fileName); const routeName = getRouteName(parentRoutePath, fileName);
const componentPath = getComponentPath(parentRoutePath, fileName, config); const componentPath = posix.join(path, item);
let content = readFileSync(component, 'utf-8'); let content = readFileSync(component, 'utf-8');
let routeMeta = {}; let routeMeta = {};
@ -153,12 +148,12 @@ const genRoutes = function (parentRoutes, path, parentRoutePath, config) {
dirList.forEach((item) => { dirList.forEach((item) => {
if (isProcessDirectory(path, item)) { if (isProcessDirectory(path, item)) {
// 文件或者目录的绝对路径 // 文件或者目录的绝对路径
const component = join(path, item); const nextPath = join(path, item);
const nextParentRouteUrl = posix.join(parentRoutePath, item); const nextParentRouteUrl = getRoutePath(parentRoutePath, item, false);
if (hasLayout) { if (hasLayout) {
genRoutes(layoutRoute.children, component, nextParentRouteUrl, config); genRoutes(layoutRoute.children, nextPath, nextParentRouteUrl);
} else { } else {
genRoutes(parentRoutes, component, nextParentRouteUrl, config); genRoutes(parentRoutes, nextPath, nextParentRouteUrl);
} }
} }
}); });
@ -210,7 +205,7 @@ const getRoutes = function ({ config, absPagesPath }) {
const routes = []; const routes = [];
cacheGenRoutes = {}; cacheGenRoutes = {};
genRoutes(routes, absPagesPath, '/', config); genRoutes(routes, absPagesPath, '/');
rank(routes); rank(routes);
return routes; return routes;
}; };