111
This commit is contained in:
1 parent
64a74564bb
commit
3105fdb07b
8 files changed
+4957
-3861
No files matched your search
@@ -1 +1 @@
|
||||
export {useRouter} from "./use-router/vite-router-plug"
|
||||
export { useRouter } from "./use-router/vite-router-plug";
|
||||
@@ -1,99 +1,99 @@
|
||||
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);
|
||||
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}`;
|
||||
}
|
||||
return routes;
|
||||
}
|
||||
|
||||
getSubPackagesRoutes() {
|
||||
if (!(this.uniPagesJSON && this.uniPagesJSON.subPackages)) {
|
||||
return [];
|
||||
if (key === "aliasPath" && i == 0 && rootPath == null) {
|
||||
route[key] = route[key] || "/";
|
||||
} else if (value !== undefined) {
|
||||
route[key] = value;
|
||||
}
|
||||
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;
|
||||
}
|
||||
routes.push(route);
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export const useRouter = ({
|
||||
pagesJsonDir,
|
||||
includes = []
|
||||
}: any) => {
|
||||
let defaultIncludes = ['path', 'aliasPath', 'name'];
|
||||
includes = [...defaultIncludes, ...includes];
|
||||
let pages = new TransformPages({
|
||||
pagesJsonDir,
|
||||
includes,
|
||||
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);
|
||||
});
|
||||
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)})`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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),
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user