This commit is contained in:
weiwei committed 2025-03-25 21:54:20 +08:00
1 parent 40a5288b0e
commit a41dad0a99
4 files changed
+105 -111

No files matched your search

+82 -90
View File
@@ -1,99 +1,91 @@
import fs from "fs";
import stripJsonComments from "./strip-json-comments";
import fs from "fs"
import stripJsonComments from "./strip-json-comments"
class TransformPages {
routes: any = [];
tabbar: any = [];
includes = [];
uniPagesJSON: any = "";
routes: any = []
tabbar: any = []
includes = []
uniPagesJSON: any = ""
constructor({ includes, pagesJsonDir }: any) {
this.includes = includes;
this.uniPagesJSON = JSON.parse(
stripJsonComments(fs.readFileSync(pagesJsonDir, "utf-8"))
);
this.routes = this.getPagesRoutes().concat(this.getSubPackagesRoutes());
this.tabbar = this.getTabbarRoutes();
}
constructor({ includes, pagesJsonDir }: any) {
this.includes = includes
this.uniPagesJSON = JSON.parse(stripJsonComments(fs.readFileSync(pagesJsonDir, "utf-8")))
this.routes = this.getPagesRoutes().concat(this.getSubPackagesRoutes())
this.tabbar = this.getTabbarRoutes()
}
getPagesRoutes(pages = this.uniPagesJSON.pages, rootPath = null) {
let routes = [];
for (let i = 0; i < pages.length; i++) {
const item = pages[i];
let route: any = {};
for (let j = 0; j < this.includes.length; j++) {
const key = this.includes[j];
let value = item[key];
if (key === "path") {
value = rootPath ? `/${rootPath}/${value}` : `/${value}`;
}
if (key === "aliasPath" && i == 0 && rootPath == null) {
route[key] = route[key] || "/";
} else if (value !== undefined) {
route[key] = value;
}
}
routes.push(route);
}
return routes;
}
getPagesRoutes(pages = this.uniPagesJSON.pages, rootPath = null) {
let routes = []
for (let i = 0; i < pages.length; i++) {
const item = pages[i]
let route: any = {}
for (let j = 0; j < this.includes.length; j++) {
const key = this.includes[j]
let value = item[key]
if (key === "path") {
value = rootPath ? `/${rootPath}/${value}` : `/${value}`
}
if (key === "aliasPath" && i == 0 && rootPath == null) {
route[key] = route[key] || "/"
} else if (value !== undefined) {
route[key] = value
}
}
routes.push(route)
}
return routes
}
getSubPackagesRoutes() {
if (!(this.uniPagesJSON && this.uniPagesJSON.subPackages)) {
return [];
}
const subPackages = this.uniPagesJSON.subPackages;
let routes: any = [];
for (let i = 0; i < subPackages.length; i++) {
const subPages = subPackages[i].pages;
const root = subPackages[i].root;
const subRoutes = this.getPagesRoutes(subPages, root);
routes = routes.concat(subRoutes);
}
return routes;
}
getSubPackagesRoutes() {
if (!(this.uniPagesJSON && this.uniPagesJSON.subPackages)) {
return []
}
const subPackages = this.uniPagesJSON.subPackages
let routes: any = []
for (let i = 0; i < subPackages.length; i++) {
const subPages = subPackages[i].pages
const root = subPackages[i].root
const subRoutes = this.getPagesRoutes(subPages, root)
routes = routes.concat(subRoutes)
}
return routes
}
getTabbarRoutes() {
if (
!(
this.uniPagesJSON &&
this.uniPagesJSON.tabBar &&
this.uniPagesJSON.tabBar.list
)
) {
return [];
}
const tabbar = this.uniPagesJSON.tabBar.list;
let tabbarMap: any = [];
tabbar.forEach((bar: { pagePath: string }) => {
tabbarMap.push("/" + bar.pagePath);
});
return tabbarMap;
}
getTabbarRoutes() {
if (!(this.uniPagesJSON && this.uniPagesJSON.tabBar && this.uniPagesJSON.tabBar.list)) {
return []
}
const tabbar = this.uniPagesJSON.tabBar.list
let tabbarMap: any = []
tabbar.forEach((bar: { pagePath: string }) => {
tabbarMap.push("/" + bar.pagePath)
})
return tabbarMap
}
}
export const useRouter = ({ pagesJsonDir, includes = [] }: any) => {
let defaultIncludes = ["path", "aliasPath", "name"];
includes = [...defaultIncludes, ...includes];
let pages = new TransformPages({
pagesJsonDir,
includes,
});
if (pages.tabbar && pages.tabbar.length) {
console.table(pages.tabbar);
}
if (pages.routes && pages.routes.length) {
console.table(pages.routes);
}
return {
name: "use-router",
config() {
return {
define: {
__ROUTES__: JSON.stringify(pages.routes),
__TABBAR__: JSON.stringify(pages.tabbar),
},
};
},
};
};
let defaultIncludes = ["path", "aliasPath", "name"]
includes = [...defaultIncludes, ...includes]
let pages = new TransformPages({
pagesJsonDir,
includes,
})
if (pages.tabbar && pages.tabbar.length) {
console.table(pages.tabbar)
}
if (pages.routes && pages.routes.length) {
console.table(pages.routes)
}
return {
name: "use-router",
config() {
return {
define: {
__ROUTES__: JSON.stringify(pages.routes),
__TABBAR__: JSON.stringify(pages.tabbar),
},
}
},
}
}