vitarx-router 0.0.1 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # VitarxRouter
2
2
 
3
+ Vitarx前端框架的配套路由器
3
4
  ________________________________________________________________________
4
5
 
5
6
  ## 安装
@@ -8,7 +9,41 @@ ________________________________________________________________________
8
9
  npm install vitarx-router
9
10
  ```
10
11
 
11
- ## 使用
12
- ```javascript
13
- import { Router } from 'vitarx-router'
12
+ ## 简单示例
13
+
14
+ ```ts
15
+ // main.ts
16
+ import { createRouter } from 'vitarx-router'
17
+ import Page1 from './Page/Page1.js'
18
+ import App from './App.js'
19
+
20
+ createRouter({
21
+ mode: 'path', // 路由模式,可选值:hash、path、memory
22
+ suffix: '*', // 允许任何后缀,例如 /x.html
23
+ routes: [
24
+ {
25
+ name: 'home', // 命名路由
26
+ path: '/',
27
+ widget: lazy(() => import('./Pages/home.js')),// 代码分块懒加载
28
+ children: [ // 嵌套路由
29
+ {
30
+ path: '/workbench',
31
+ widget: lazy(() => import('./Pages/home/workbench.js')) // 直接使用小部件
32
+ }
33
+ ]
34
+ },
35
+ { // 动态路由
36
+ path: '/page1/{name?}',// {name?}为可选参数,没有?为必填参数
37
+ widget: Page1
38
+ },
39
+ { // 命名视图
40
+ path: '/page3',
41
+ widget: { // 命名视图
42
+ 'default': lazy(() => import('./Page/xxx.js')),
43
+ 'right': lazy(() => import('./Page/xxx.js'))
44
+ }
45
+ }
46
+ ]
47
+ })
48
+ createApp('#root').render(App)
14
49
  ```
@@ -0,0 +1,19 @@
1
+ /**
2
+ * 路由路线配置
3
+ */
4
+ declare interface Route {
5
+ path: string;
6
+ name?: string;
7
+ children?: Route[];
8
+ widget?: any;
9
+ }
10
+ /**
11
+ * 生成路由索引类型声明
12
+ *
13
+ * @param {Route[]} routes - 路由表
14
+ * @param {string[]} custom - 自定义的名称或路径,例如['/home', 'home']
15
+ * @param {string} [writePath] - 要写入的路径,完整的路径!例如'/Users/Vitarx/vitarx/route.type.d.ts',默认为当前工作目录下的route.type.d.ts
16
+ * @return {void}
17
+ */
18
+ export default function make(routes: Route[], custom?: string[], writePath?: string): void;
19
+ export {};
@@ -0,0 +1,66 @@
1
+ import * as fs from 'node:fs';
2
+ /**
3
+ * 格式化path
4
+ *
5
+ * @param {string} path - 路径字符串
6
+ * @return {string} - 格式化后的路径字符串
7
+ */
8
+ function formatPath(path) {
9
+ // 去除所有空格 处理重复//
10
+ path = `/${path}`.replace(/\s+/g, '').replace(/\/+/g, '/');
11
+ if (!path.length)
12
+ return '/';
13
+ if (path === '/' || path === '/#/')
14
+ return path;
15
+ return path.replace(/\/$/, '');
16
+ }
17
+ /**
18
+ * 根据路由表生成路由索引
19
+ *
20
+ * @param {Route[]} routes - 路由表
21
+ * @return {{ paths: string[], names: string[] }} - 路由索引对象,包含所有路由路径和名称
22
+ */
23
+ function buildRouteIndex(routes) {
24
+ const paths = [];
25
+ const names = [];
26
+ // 递归遍历路由,拼接路径
27
+ function traverse(route, parentPath = '') {
28
+ // 如果是路由组,拼接路径并继续遍历子路由
29
+ const fullPath = formatPath(parentPath ? `${parentPath}/${route.path}` : route.path);
30
+ // 如果有widget,记录路径
31
+ if (route.widget) {
32
+ paths.push(fullPath);
33
+ }
34
+ // 如果有name,记录name
35
+ if (route.name) {
36
+ names.push(route.name);
37
+ }
38
+ // 如果有子路由,递归遍历
39
+ if (route.children && route.children.length > 0) {
40
+ route.children.forEach(childRoute => traverse(childRoute, fullPath));
41
+ }
42
+ }
43
+ // 遍历所有的根路由
44
+ routes.forEach(route => traverse(route));
45
+ return {
46
+ paths,
47
+ names
48
+ };
49
+ }
50
+ /**
51
+ * 生成路由索引类型声明
52
+ *
53
+ * @param {Route[]} routes - 路由表
54
+ * @param {string[]} custom - 自定义的名称或路径,例如['/home', 'home']
55
+ * @param {string} [writePath] - 要写入的路径,完整的路径!例如'/Users/Vitarx/vitarx/route.type.d.ts',默认为当前工作目录下的route.type.d.ts
56
+ * @return {void}
57
+ */
58
+ export default function make(routes, custom = [], writePath = '') {
59
+ const { paths, names } = buildRouteIndex(routes);
60
+ const all = [...paths, ...names, ...custom];
61
+ // 构造类型声明字符串
62
+ const typeDeclaration = `type VitarxRouterRouteIndexTyped = ${all.map(route => `'${route}'`).join(' | ')};`;
63
+ if (!writePath)
64
+ writePath = `${process.cwd()}/route.type.d.ts`;
65
+ fs.writeFileSync(writePath, typeDeclaration, { encoding: 'utf-8' });
66
+ }