Skip to content

Commit

Permalink
feat(projects): route const
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jul 9, 2023
1 parent 9e470d0 commit 477a53e
Show file tree
Hide file tree
Showing 26 changed files with 2,454 additions and 4,383 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["soybeanjs"],
"rules": {
"max-params": ["error", 4]
}
}
3 changes: 0 additions & 3 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cSpell.words": ["consola", "kolorist", "unplugin"],
"cSpell.words": ["consola", "kolorist", "magicast", "unplugin"],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
Expand Down
10 changes: 5 additions & 5 deletions examples/template-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
"@elegant-router/vue": "workspace:*",
"pinia": "^2.1.4",
"vue": "^3.3.4",
"vue-router": "^4.2.2"
"vue-router": "^4.2.4"
},
"devDependencies": {
"@soybeanjs/cli": "^0.6.2",
"@types/node": "^20.3.3",
"@types/node": "^20.4.1",
"@vitejs/plugin-vue": "^4.2.3",
"@vitejs/plugin-vue-jsx": "^3.0.1",
"eslint": "^8.44.0",
"eslint-config-soybeanjs": "^0.4.9",
"eslint-config-soybeanjs": "^0.5.1",
"npm-run-all": "^4.1.5",
"typescript": "5.1.6",
"vite": "^4.3.9",
"vue-tsc": "^1.8.3"
"vite": "^4.4.2",
"vue-tsc": "^1.8.4"
}
}
15 changes: 11 additions & 4 deletions examples/template-vue/src/router/auto-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
import type { RouteComponent } from "vue-router";
import type { LastLevelRoute } from "@elegant-router/types";

import _403 from "@/views/403/index.vue";
import demoA_child1 from "@/views/demo-a/child1/index.vue";
import demoA_child2_child3 from "@/views/demo-a/child2/child3/index.vue";
import demoA_child3 from "@/views/demo-a/child3/index.vue";
import demo3 from "@/views/demo3/[id].vue";

export const views: Record<LastLevelRoute, RouteComponent | (() => Promise<{ default: RouteComponent }>)> = {
"demo-a_child1": () => import("@/views/demo-a/child1/index.vue"),
"demo-a_child2_child3": () => import("@/views/demo-a/child2/child3/index.vue"),
"demo-a_child3": () => import("@/views/demo-a/child3/index.vue"),
demo3: () => import("@/views/demo3/[id].vue"),
403: _403,
"demo-a_child1": demoA_child1,
"demo-a_child2_child3": demoA_child2_child3,
"demo-a_child3": demoA_child3,
demo3,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,13 @@
// Read more: https://github.com/soybeanjs/elegant-router

declare module "@elegant-router/types" {
/**
* the root route key
*/
export type RootRouteKey = "root";

/**
* the root path
*/
export type RootRoutePath = "/";

/**
* the not found route, which catch the invalid route path
*/
export type NotFoundRouteKey = "not-found";

/**
* the not found route path, which catch the invalid route path
*/
export type NotFoundRoutePath = "/:pathMatch(.*)*";

/**
* route map
*/
export type RouteMap = {
"root": "/";
"not-found": "/:pathMatch(.*)*";
"notFound": "/:pathMatch(.*)*";
"403": "/403";
"demo-a": "/demo-a";
"demo-a_child1": "/demo-a/child1";
"demo-a_child2": "/demo-a/child2";
Expand All @@ -43,11 +24,21 @@ declare module "@elegant-router/types" {
*/
export type RouteKey = keyof RouteMap;

/**
* custom route key
*/
export type CustomRouteKey = Extract<
RouteKey,
| "root"
| "notFound"
>;

/**
* the last level route, which has the page file
*/
export type LastLevelRoute = Extract<
RouteKey,
| "403"
| "demo-a_child1"
| "demo-a_child2_child3"
| "demo-a_child3"
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion examples/template-vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vueJsx from '@vitejs/plugin-vue-jsx';
import ElegantVueRouter from '@elegant-router/vue/vite';

export default defineConfig({
plugins: [vue(), vueJsx(), ElegantVueRouter({})],
plugins: [vue(), vueJsx(), ElegantVueRouter({ lazyImport: () => false })],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
"format": "soy prettier-format",
"commit": "soy git-commit",
"cleanup": "soy cleanup",
"update-pkg": "soy update-pkg",
"ncu": "soy ncu",
"publish-pkg": "pnpm -r publish --access public",
"release": "pnpm soy release && pnpm build && pnpm publish-pkg",
"prepare": "pnpm -r --filter='./packages/core' run dev"
},
"devDependencies": {
"@soybeanjs/cli": "0.6.2",
"eslint": "8.44.0",
"eslint-config-soybeanjs": "0.4.9",
"eslint-config-soybeanjs": "0.5.1",
"simple-git-hooks": "2.8.1",
"tsx": "^3.12.7"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
},
"dependencies": {
"chokidar": "3.5.3",
"consola": "3.2.2",
"consola": "3.2.3",
"fast-glob": "3.3.0",
"kolorist": "1.8.0",
"micromatch": "4.0.5"
},
"devDependencies": {
"@types/micromatch": "^4.0.2",
"@types/node": "20.3.3",
"@types/node": "20.4.1",
"typescript": "5.1.6",
"unbuild": "1.2.1"
}
Expand Down
31 changes: 20 additions & 11 deletions packages/core/src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,45 @@ export function transformRouterMapsToEntries(maps: ElegantRouterNamePathMap) {
* @param options
*/
export function transformRouterEntriesToTrees(entries: ElegantRouterNamePathEntry[], maps: ElegantRouterNamePathMap) {
const treeWithClassify: Record<string, string[][]> = {};
const treeWithClassify = new Map<string, string[][]>();

entries.forEach(([routeName]) => {
const isFirstLevel = routeName.includes(PAGE_DEGREE_SPLITTER);
const isFirstLevel = !routeName.includes(PAGE_DEGREE_SPLITTER);

if (isFirstLevel) {
treeWithClassify[routeName] = [];
treeWithClassify.set(routeName, []);
} else {
const firstLevelName = routeName.split(PAGE_DEGREE_SPLITTER)[0];

const levels = routeName.split(PAGE_DEGREE_SPLITTER).length;
const levelNames = treeWithClassify[routeName][levels - 2];

treeWithClassify[routeName][levels - 2] = [...(levelNames || []), routeName];
const currentLevelChildren = treeWithClassify.get(firstLevelName) || [];

const child = currentLevelChildren[levels - 2] || [];

child.push(routeName);

currentLevelChildren[levels - 2] = child;

treeWithClassify.set(firstLevelName, currentLevelChildren);
}
});

const trees: ElegantRouterTree[] = [];

Object.keys(treeWithClassify).forEach(moduleName => {
treeWithClassify.forEach((children, key) => {
const firstLevelRoute: ElegantRouterTree = {
routeName: moduleName,
routePath: maps.get(moduleName) || ''
routeName: key,
routePath: maps.get(key) || ''
};

const children = treeWithClassify[moduleName];

const treeChildren = recursiveGetRouteTreeChildren(moduleName, children, maps);
const treeChildren = recursiveGetRouteTreeChildren(key, children, maps);

if (treeChildren.length > 0) {
firstLevelRoute.children = treeChildren;
}

trees.push(firstLevelRoute);
});

return trees;
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/core/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import chokidar from 'chokidar';
import { watch } from 'chokidar';
import { log } from './log';

// eslint-disable-next-line max-params
export function setupWatcher(
watchDir: string,
ignored: string[],
callback: (glob: string[]) => Promise<void> | void,
showLog = true
) {
const watcher = chokidar.watch('.', {
const watcher = watch('.', {
ignoreInitial: true,
cwd: watchDir,
ignored
Expand Down
6 changes: 3 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"consola": "3.2.2",
"consola": "3.2.3",
"fs-extra": "11.1.1",
"magicast": "0.2.9",
"unplugin": "1.3.1"
"unplugin": "1.3.2"
},
"devDependencies": {
"@elegant-router/core": "workspace:*",
"@types/fs-extra": "11.0.1",
"@types/node": "20.3.3",
"@types/node": "20.4.1",
"typescript": "5.1.6",
"unbuild": "1.2.1"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"consola": "3.2.2",
"consola": "3.2.3",
"fs-extra": "11.1.1",
"magicast": "0.2.9",
"unplugin": "1.3.1"
"unplugin": "1.3.2"
},
"devDependencies": {
"@elegant-router/core": "workspace:*",
"@types/fs-extra": "11.0.1",
"@types/node": "20.3.3",
"@types/node": "20.4.1",
"typescript": "5.1.6",
"unbuild": "1.2.1"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"consola": "3.2.2",
"consola": "3.2.3",
"fs-extra": "11.1.1",
"magicast": "0.2.9",
"unplugin": "1.3.1"
"unplugin": "1.3.2"
},
"devDependencies": {
"@elegant-router/core": "workspace:*",
"@types/fs-extra": "11.0.1",
"@types/node": "20.3.3",
"@types/node": "20.4.1",
"typescript": "5.1.6",
"unbuild": "1.2.1"
}
Expand Down
17 changes: 17 additions & 0 deletions packages/vue/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ElegantRouter from '@elegant-router/core';

import { getRouteConstExport } from './src/context/const';
import { createPluginOptions } from './src/context/options';
import treeJson from './tree.json';

function start() {
const er = new ElegantRouter();

const options = createPluginOptions(er.options, {});

const { autoRoutes } = getRouteConstExport(treeJson as any, options);

console.log('autoRoutes: ', autoRoutes);
}

start();
10 changes: 5 additions & 5 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"consola": "3.2.2",
"consola": "3.2.3",
"fs-extra": "11.1.1",
"kolorist": "1.8.0",
"magicast": "0.2.9",
"unplugin": "1.3.1"
"unplugin": "1.3.2"
},
"devDependencies": {
"@elegant-router/core": "workspace:*",
"@types/fs-extra": "11.0.1",
"@types/node": "20.3.3",
"@types/node": "20.4.1",
"typescript": "5.1.6",
"unbuild": "1.2.1",
"vite": "^4.3.9",
"vue-router": "^4.2.2"
"vite": "^4.4.2",
"vue-router": "^4.2.4"
}
}
7 changes: 0 additions & 7 deletions packages/vue/src/constants/index.ts

This file was deleted.

Loading

0 comments on commit 477a53e

Please sign in to comment.