@virou/core 0.1.0
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/LICENSE +21 -0
- package/dist/index.cjs +175 -0
- package/dist/index.d.cts +99 -0
- package/dist/index.d.mts +99 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.mjs +170 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tankosin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vue = require('vue');
|
|
4
|
+
const core = require('@vueuse/core');
|
|
5
|
+
const rou3 = require('rou3');
|
|
6
|
+
const ufo = require('ufo');
|
|
7
|
+
|
|
8
|
+
function addRoutes(router, routes, parentPath = "") {
|
|
9
|
+
const { context, routeTree, views, keys } = router;
|
|
10
|
+
routes.forEach(({ key, path, component, meta, children }) => {
|
|
11
|
+
const fullPath = ufo.joinURL(parentPath, path);
|
|
12
|
+
views[fullPath] ||= [];
|
|
13
|
+
views[fullPath].push(vue.defineAsyncComponent(component));
|
|
14
|
+
keys[fullPath] ||= [];
|
|
15
|
+
keys[fullPath].push(key ?? null);
|
|
16
|
+
rou3.addRoute(context, "GET", fullPath, { key, fullPath, meta });
|
|
17
|
+
const node = [fullPath];
|
|
18
|
+
if (children) {
|
|
19
|
+
const childRouteTree = [];
|
|
20
|
+
addRoutes({ ...router, routeTree: childRouteTree }, children, fullPath);
|
|
21
|
+
node[1] = childRouteTree;
|
|
22
|
+
}
|
|
23
|
+
routeTree.push(node);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function resolveRouteTree(routeTree, targetPath, accumulatedPaths = []) {
|
|
27
|
+
for (const [path, children] of routeTree) {
|
|
28
|
+
const paths = [...accumulatedPaths, path];
|
|
29
|
+
if (path === targetPath) {
|
|
30
|
+
return paths;
|
|
31
|
+
}
|
|
32
|
+
if (children) {
|
|
33
|
+
const result = resolveRouteTree(children, targetPath, paths);
|
|
34
|
+
if (result.length) {
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
function createRenderList(routes, targetPath, views, keys) {
|
|
42
|
+
const renderList = [];
|
|
43
|
+
const currentSegments = targetPath.split("/").filter(Boolean);
|
|
44
|
+
routes.forEach((path) => {
|
|
45
|
+
const treeViews = views[path];
|
|
46
|
+
const treeKeys = keys[path];
|
|
47
|
+
if (treeViews === undefined || treeKeys === undefined || treeViews.length !== treeKeys.length) {
|
|
48
|
+
throw new Error(`[virou] Mismatch or missing data for path: ${path}`);
|
|
49
|
+
}
|
|
50
|
+
const resolvedPath = ufo.joinURL("/", ...currentSegments.slice(0, path.split("/").filter(Boolean).length));
|
|
51
|
+
for (let i = 0; i < treeViews.length; i++) {
|
|
52
|
+
const view = treeViews[i];
|
|
53
|
+
const key = treeKeys[i];
|
|
54
|
+
if (view !== undefined && key !== undefined && (targetPath === resolvedPath || i === 0)) {
|
|
55
|
+
renderList.push([key ?? resolvedPath, view]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return renderList;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const virouSymbol = Symbol("virou");
|
|
63
|
+
const useVirouState = core.createGlobalState(() => {
|
|
64
|
+
const routers = vue.shallowRef({});
|
|
65
|
+
const activeRoutePaths = vue.ref({});
|
|
66
|
+
return { routers, activeRoutePaths };
|
|
67
|
+
});
|
|
68
|
+
function createVRouter(key, routes, options) {
|
|
69
|
+
const { routers, activeRoutePaths } = useVirouState();
|
|
70
|
+
if (key in routers.value) {
|
|
71
|
+
throw new Error(`[virou] [createVRouter] key already exists: ${key}`);
|
|
72
|
+
}
|
|
73
|
+
routers.value[key] = {
|
|
74
|
+
context: rou3.createRouter(),
|
|
75
|
+
routeTree: [],
|
|
76
|
+
views: {},
|
|
77
|
+
keys: {}
|
|
78
|
+
};
|
|
79
|
+
addRoutes(routers.value[key], routes);
|
|
80
|
+
const { initialPath } = options ||= {
|
|
81
|
+
initialPath: "/"
|
|
82
|
+
};
|
|
83
|
+
activeRoutePaths.value[key] = initialPath;
|
|
84
|
+
}
|
|
85
|
+
function useVRouter(...args) {
|
|
86
|
+
if (typeof args[0] !== "string") {
|
|
87
|
+
args.unshift(vue.inject(virouSymbol, vue.useId()));
|
|
88
|
+
}
|
|
89
|
+
const [key, routes] = args;
|
|
90
|
+
if (!key || typeof key !== "string") {
|
|
91
|
+
throw new TypeError(`[virou] [useVRouter] key must be a string: ${key}`);
|
|
92
|
+
}
|
|
93
|
+
vue.provide(virouSymbol, key);
|
|
94
|
+
const options = args[2];
|
|
95
|
+
const { routers, activeRoutePaths } = useVirouState();
|
|
96
|
+
if (routers.value[key] === undefined) {
|
|
97
|
+
if (routes === undefined) {
|
|
98
|
+
throw new Error(`[virou] [useVRouter] routes is required for key: ${key}`);
|
|
99
|
+
}
|
|
100
|
+
createVRouter(key, routes, options);
|
|
101
|
+
}
|
|
102
|
+
const route = vue.computed(() => {
|
|
103
|
+
const { context, routeTree, views, keys } = routers.value[key];
|
|
104
|
+
const activeRoutePath = activeRoutePaths.value[key];
|
|
105
|
+
const matchedRoute = rou3.findRoute(
|
|
106
|
+
context,
|
|
107
|
+
"GET",
|
|
108
|
+
activeRoutePath
|
|
109
|
+
);
|
|
110
|
+
const activeRouteTree = matchedRoute && resolveRouteTree(routeTree, matchedRoute.data.fullPath);
|
|
111
|
+
const { search, pathname, hash } = ufo.parseURL(activeRoutePath);
|
|
112
|
+
const renderList = activeRouteTree && createRenderList(activeRouteTree, pathname, views, keys);
|
|
113
|
+
return {
|
|
114
|
+
fullPath: activeRoutePath,
|
|
115
|
+
...matchedRoute,
|
|
116
|
+
search,
|
|
117
|
+
path: pathname,
|
|
118
|
+
hash,
|
|
119
|
+
activeRouteTree,
|
|
120
|
+
renderList
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
route,
|
|
125
|
+
router: {
|
|
126
|
+
addRoute: (route2) => {
|
|
127
|
+
addRoutes(routers.value[key], [route2]);
|
|
128
|
+
},
|
|
129
|
+
replace: (path) => {
|
|
130
|
+
activeRoutePaths.value[key] = path;
|
|
131
|
+
},
|
|
132
|
+
_key: key,
|
|
133
|
+
_depthKey: Symbol.for(key)
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const VRouterView = vue.defineComponent({
|
|
139
|
+
name: "VRouterView",
|
|
140
|
+
props: {
|
|
141
|
+
routerKey: {
|
|
142
|
+
type: String,
|
|
143
|
+
default: undefined
|
|
144
|
+
},
|
|
145
|
+
viewKey: {
|
|
146
|
+
type: String,
|
|
147
|
+
default: undefined
|
|
148
|
+
},
|
|
149
|
+
keepalive: {
|
|
150
|
+
type: Boolean,
|
|
151
|
+
default: false
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
setup(props) {
|
|
155
|
+
const routerKey = props.routerKey ?? vue.inject(virouSymbol);
|
|
156
|
+
if (routerKey === undefined) {
|
|
157
|
+
throw new Error(`[virou] [VRouterView] routerKey is required`);
|
|
158
|
+
}
|
|
159
|
+
const { router, route } = useVRouter(routerKey);
|
|
160
|
+
const depth = vue.inject(router._depthKey, 0);
|
|
161
|
+
const render = vue.computed(() => route.value.renderList?.[depth]);
|
|
162
|
+
vue.provide(router._depthKey, depth + 1);
|
|
163
|
+
return () => {
|
|
164
|
+
const [key, component] = render.value ?? [];
|
|
165
|
+
const _key = key ?? props.viewKey ?? route.value.path;
|
|
166
|
+
const ViewComponent = component !== null && component !== undefined ? vue.h(component, { _key }) : null;
|
|
167
|
+
return props.keepalive ? vue.h(vue.KeepAlive, null, [ViewComponent]) : ViewComponent;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
exports.VRouterView = VRouterView;
|
|
173
|
+
exports.createVRouter = createVRouter;
|
|
174
|
+
exports.useVRouter = useVRouter;
|
|
175
|
+
exports.virouSymbol = virouSymbol;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as vue from 'vue';
|
|
2
|
+
import { Component, AsyncComponentLoader, ComputedRef } from 'vue';
|
|
3
|
+
import { RouterContext } from 'rou3';
|
|
4
|
+
|
|
5
|
+
declare const VRouterView: vue.DefineComponent<vue.ExtractPropTypes<{
|
|
6
|
+
routerKey: {
|
|
7
|
+
type: StringConstructor;
|
|
8
|
+
default: undefined;
|
|
9
|
+
};
|
|
10
|
+
viewKey: {
|
|
11
|
+
type: StringConstructor;
|
|
12
|
+
default: undefined;
|
|
13
|
+
};
|
|
14
|
+
keepalive: {
|
|
15
|
+
type: BooleanConstructor;
|
|
16
|
+
default: boolean;
|
|
17
|
+
};
|
|
18
|
+
}>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}> | null, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
|
|
21
|
+
routerKey: {
|
|
22
|
+
type: StringConstructor;
|
|
23
|
+
default: undefined;
|
|
24
|
+
};
|
|
25
|
+
viewKey: {
|
|
26
|
+
type: StringConstructor;
|
|
27
|
+
default: undefined;
|
|
28
|
+
};
|
|
29
|
+
keepalive: {
|
|
30
|
+
type: BooleanConstructor;
|
|
31
|
+
default: boolean;
|
|
32
|
+
};
|
|
33
|
+
}>> & Readonly<{}>, {
|
|
34
|
+
routerKey: string;
|
|
35
|
+
viewKey: string;
|
|
36
|
+
keepalive: boolean;
|
|
37
|
+
}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
|
|
38
|
+
|
|
39
|
+
type VRouteTreeNode = [key: string, children?: VRouteTreeNode[]];
|
|
40
|
+
type VRouteViews = Record<string, Component[]>;
|
|
41
|
+
type VRouteKeys = Record<string, Array<string | null>>;
|
|
42
|
+
type VRouteRenderView = [key: string, component: Component];
|
|
43
|
+
interface VRouteRecordRaw {
|
|
44
|
+
key?: string;
|
|
45
|
+
path: string;
|
|
46
|
+
component: AsyncComponentLoader<Component>;
|
|
47
|
+
meta?: Record<string, any>;
|
|
48
|
+
children?: VRouteRecordRaw[];
|
|
49
|
+
}
|
|
50
|
+
interface VRouteMatchedData {
|
|
51
|
+
key?: string;
|
|
52
|
+
fullPath: string;
|
|
53
|
+
meta?: Record<string, any>;
|
|
54
|
+
}
|
|
55
|
+
interface VRouterInstance {
|
|
56
|
+
context: RouterContext<VRouteMatchedData>;
|
|
57
|
+
routeTree: VRouteTreeNode[];
|
|
58
|
+
views: VRouteViews;
|
|
59
|
+
keys: VRouteKeys;
|
|
60
|
+
}
|
|
61
|
+
interface VRouterOptions {
|
|
62
|
+
initialPath: string;
|
|
63
|
+
}
|
|
64
|
+
interface VRouter {
|
|
65
|
+
route: ComputedRef<{
|
|
66
|
+
fullPath: string;
|
|
67
|
+
data?: VRouteMatchedData;
|
|
68
|
+
params?: Record<string, string>;
|
|
69
|
+
path: string;
|
|
70
|
+
search: string;
|
|
71
|
+
hash: string;
|
|
72
|
+
activeRouteTree?: string[];
|
|
73
|
+
renderList?: VRouteRenderView[];
|
|
74
|
+
}>;
|
|
75
|
+
router: {
|
|
76
|
+
addRoute: (route: VRouteRecordRaw) => void;
|
|
77
|
+
replace: (path: string) => void;
|
|
78
|
+
_key: string;
|
|
79
|
+
_depthKey: symbol;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare const virouSymbol: unique symbol;
|
|
84
|
+
declare function createVRouter(key: string, routes: VRouteRecordRaw[], options?: VRouterOptions): void;
|
|
85
|
+
/**
|
|
86
|
+
* Creates or retrieves a virtual router
|
|
87
|
+
*
|
|
88
|
+
* @param key - a unique identifier for the router
|
|
89
|
+
* @param routes - a list of routes to add to the router
|
|
90
|
+
* @param options - configuration options for the router
|
|
91
|
+
*
|
|
92
|
+
* @returns an object containing:
|
|
93
|
+
* - `route`: reactive data for the current route (e.g., path, fullPath, query, etc.)
|
|
94
|
+
* - `router`: utilities to manage the router (e.g., `addRoute`, `replace`)
|
|
95
|
+
*/
|
|
96
|
+
declare function useVRouter(key?: string, routes?: VRouteRecordRaw[], options?: VRouterOptions): VRouter;
|
|
97
|
+
declare function useVRouter(routes?: VRouteRecordRaw[], options?: VRouterOptions): VRouter;
|
|
98
|
+
|
|
99
|
+
export { type VRouteKeys, type VRouteMatchedData, type VRouteRecordRaw, type VRouteRenderView, type VRouteTreeNode, type VRouteViews, type VRouter, type VRouterInstance, type VRouterOptions, VRouterView, createVRouter, useVRouter, virouSymbol };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as vue from 'vue';
|
|
2
|
+
import { Component, AsyncComponentLoader, ComputedRef } from 'vue';
|
|
3
|
+
import { RouterContext } from 'rou3';
|
|
4
|
+
|
|
5
|
+
declare const VRouterView: vue.DefineComponent<vue.ExtractPropTypes<{
|
|
6
|
+
routerKey: {
|
|
7
|
+
type: StringConstructor;
|
|
8
|
+
default: undefined;
|
|
9
|
+
};
|
|
10
|
+
viewKey: {
|
|
11
|
+
type: StringConstructor;
|
|
12
|
+
default: undefined;
|
|
13
|
+
};
|
|
14
|
+
keepalive: {
|
|
15
|
+
type: BooleanConstructor;
|
|
16
|
+
default: boolean;
|
|
17
|
+
};
|
|
18
|
+
}>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}> | null, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
|
|
21
|
+
routerKey: {
|
|
22
|
+
type: StringConstructor;
|
|
23
|
+
default: undefined;
|
|
24
|
+
};
|
|
25
|
+
viewKey: {
|
|
26
|
+
type: StringConstructor;
|
|
27
|
+
default: undefined;
|
|
28
|
+
};
|
|
29
|
+
keepalive: {
|
|
30
|
+
type: BooleanConstructor;
|
|
31
|
+
default: boolean;
|
|
32
|
+
};
|
|
33
|
+
}>> & Readonly<{}>, {
|
|
34
|
+
routerKey: string;
|
|
35
|
+
viewKey: string;
|
|
36
|
+
keepalive: boolean;
|
|
37
|
+
}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
|
|
38
|
+
|
|
39
|
+
type VRouteTreeNode = [key: string, children?: VRouteTreeNode[]];
|
|
40
|
+
type VRouteViews = Record<string, Component[]>;
|
|
41
|
+
type VRouteKeys = Record<string, Array<string | null>>;
|
|
42
|
+
type VRouteRenderView = [key: string, component: Component];
|
|
43
|
+
interface VRouteRecordRaw {
|
|
44
|
+
key?: string;
|
|
45
|
+
path: string;
|
|
46
|
+
component: AsyncComponentLoader<Component>;
|
|
47
|
+
meta?: Record<string, any>;
|
|
48
|
+
children?: VRouteRecordRaw[];
|
|
49
|
+
}
|
|
50
|
+
interface VRouteMatchedData {
|
|
51
|
+
key?: string;
|
|
52
|
+
fullPath: string;
|
|
53
|
+
meta?: Record<string, any>;
|
|
54
|
+
}
|
|
55
|
+
interface VRouterInstance {
|
|
56
|
+
context: RouterContext<VRouteMatchedData>;
|
|
57
|
+
routeTree: VRouteTreeNode[];
|
|
58
|
+
views: VRouteViews;
|
|
59
|
+
keys: VRouteKeys;
|
|
60
|
+
}
|
|
61
|
+
interface VRouterOptions {
|
|
62
|
+
initialPath: string;
|
|
63
|
+
}
|
|
64
|
+
interface VRouter {
|
|
65
|
+
route: ComputedRef<{
|
|
66
|
+
fullPath: string;
|
|
67
|
+
data?: VRouteMatchedData;
|
|
68
|
+
params?: Record<string, string>;
|
|
69
|
+
path: string;
|
|
70
|
+
search: string;
|
|
71
|
+
hash: string;
|
|
72
|
+
activeRouteTree?: string[];
|
|
73
|
+
renderList?: VRouteRenderView[];
|
|
74
|
+
}>;
|
|
75
|
+
router: {
|
|
76
|
+
addRoute: (route: VRouteRecordRaw) => void;
|
|
77
|
+
replace: (path: string) => void;
|
|
78
|
+
_key: string;
|
|
79
|
+
_depthKey: symbol;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare const virouSymbol: unique symbol;
|
|
84
|
+
declare function createVRouter(key: string, routes: VRouteRecordRaw[], options?: VRouterOptions): void;
|
|
85
|
+
/**
|
|
86
|
+
* Creates or retrieves a virtual router
|
|
87
|
+
*
|
|
88
|
+
* @param key - a unique identifier for the router
|
|
89
|
+
* @param routes - a list of routes to add to the router
|
|
90
|
+
* @param options - configuration options for the router
|
|
91
|
+
*
|
|
92
|
+
* @returns an object containing:
|
|
93
|
+
* - `route`: reactive data for the current route (e.g., path, fullPath, query, etc.)
|
|
94
|
+
* - `router`: utilities to manage the router (e.g., `addRoute`, `replace`)
|
|
95
|
+
*/
|
|
96
|
+
declare function useVRouter(key?: string, routes?: VRouteRecordRaw[], options?: VRouterOptions): VRouter;
|
|
97
|
+
declare function useVRouter(routes?: VRouteRecordRaw[], options?: VRouterOptions): VRouter;
|
|
98
|
+
|
|
99
|
+
export { type VRouteKeys, type VRouteMatchedData, type VRouteRecordRaw, type VRouteRenderView, type VRouteTreeNode, type VRouteViews, type VRouter, type VRouterInstance, type VRouterOptions, VRouterView, createVRouter, useVRouter, virouSymbol };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as vue from 'vue';
|
|
2
|
+
import { Component, AsyncComponentLoader, ComputedRef } from 'vue';
|
|
3
|
+
import { RouterContext } from 'rou3';
|
|
4
|
+
|
|
5
|
+
declare const VRouterView: vue.DefineComponent<vue.ExtractPropTypes<{
|
|
6
|
+
routerKey: {
|
|
7
|
+
type: StringConstructor;
|
|
8
|
+
default: undefined;
|
|
9
|
+
};
|
|
10
|
+
viewKey: {
|
|
11
|
+
type: StringConstructor;
|
|
12
|
+
default: undefined;
|
|
13
|
+
};
|
|
14
|
+
keepalive: {
|
|
15
|
+
type: BooleanConstructor;
|
|
16
|
+
default: boolean;
|
|
17
|
+
};
|
|
18
|
+
}>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}> | null, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
|
|
21
|
+
routerKey: {
|
|
22
|
+
type: StringConstructor;
|
|
23
|
+
default: undefined;
|
|
24
|
+
};
|
|
25
|
+
viewKey: {
|
|
26
|
+
type: StringConstructor;
|
|
27
|
+
default: undefined;
|
|
28
|
+
};
|
|
29
|
+
keepalive: {
|
|
30
|
+
type: BooleanConstructor;
|
|
31
|
+
default: boolean;
|
|
32
|
+
};
|
|
33
|
+
}>> & Readonly<{}>, {
|
|
34
|
+
routerKey: string;
|
|
35
|
+
viewKey: string;
|
|
36
|
+
keepalive: boolean;
|
|
37
|
+
}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
|
|
38
|
+
|
|
39
|
+
type VRouteTreeNode = [key: string, children?: VRouteTreeNode[]];
|
|
40
|
+
type VRouteViews = Record<string, Component[]>;
|
|
41
|
+
type VRouteKeys = Record<string, Array<string | null>>;
|
|
42
|
+
type VRouteRenderView = [key: string, component: Component];
|
|
43
|
+
interface VRouteRecordRaw {
|
|
44
|
+
key?: string;
|
|
45
|
+
path: string;
|
|
46
|
+
component: AsyncComponentLoader<Component>;
|
|
47
|
+
meta?: Record<string, any>;
|
|
48
|
+
children?: VRouteRecordRaw[];
|
|
49
|
+
}
|
|
50
|
+
interface VRouteMatchedData {
|
|
51
|
+
key?: string;
|
|
52
|
+
fullPath: string;
|
|
53
|
+
meta?: Record<string, any>;
|
|
54
|
+
}
|
|
55
|
+
interface VRouterInstance {
|
|
56
|
+
context: RouterContext<VRouteMatchedData>;
|
|
57
|
+
routeTree: VRouteTreeNode[];
|
|
58
|
+
views: VRouteViews;
|
|
59
|
+
keys: VRouteKeys;
|
|
60
|
+
}
|
|
61
|
+
interface VRouterOptions {
|
|
62
|
+
initialPath: string;
|
|
63
|
+
}
|
|
64
|
+
interface VRouter {
|
|
65
|
+
route: ComputedRef<{
|
|
66
|
+
fullPath: string;
|
|
67
|
+
data?: VRouteMatchedData;
|
|
68
|
+
params?: Record<string, string>;
|
|
69
|
+
path: string;
|
|
70
|
+
search: string;
|
|
71
|
+
hash: string;
|
|
72
|
+
activeRouteTree?: string[];
|
|
73
|
+
renderList?: VRouteRenderView[];
|
|
74
|
+
}>;
|
|
75
|
+
router: {
|
|
76
|
+
addRoute: (route: VRouteRecordRaw) => void;
|
|
77
|
+
replace: (path: string) => void;
|
|
78
|
+
_key: string;
|
|
79
|
+
_depthKey: symbol;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare const virouSymbol: unique symbol;
|
|
84
|
+
declare function createVRouter(key: string, routes: VRouteRecordRaw[], options?: VRouterOptions): void;
|
|
85
|
+
/**
|
|
86
|
+
* Creates or retrieves a virtual router
|
|
87
|
+
*
|
|
88
|
+
* @param key - a unique identifier for the router
|
|
89
|
+
* @param routes - a list of routes to add to the router
|
|
90
|
+
* @param options - configuration options for the router
|
|
91
|
+
*
|
|
92
|
+
* @returns an object containing:
|
|
93
|
+
* - `route`: reactive data for the current route (e.g., path, fullPath, query, etc.)
|
|
94
|
+
* - `router`: utilities to manage the router (e.g., `addRoute`, `replace`)
|
|
95
|
+
*/
|
|
96
|
+
declare function useVRouter(key?: string, routes?: VRouteRecordRaw[], options?: VRouterOptions): VRouter;
|
|
97
|
+
declare function useVRouter(routes?: VRouteRecordRaw[], options?: VRouterOptions): VRouter;
|
|
98
|
+
|
|
99
|
+
export { type VRouteKeys, type VRouteMatchedData, type VRouteRecordRaw, type VRouteRenderView, type VRouteTreeNode, type VRouteViews, type VRouter, type VRouterInstance, type VRouterOptions, VRouterView, createVRouter, useVRouter, virouSymbol };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { defineAsyncComponent, shallowRef, ref, inject, useId, provide, computed, defineComponent, h, KeepAlive } from 'vue';
|
|
2
|
+
import { createGlobalState } from '@vueuse/core';
|
|
3
|
+
import { addRoute, createRouter, findRoute } from 'rou3';
|
|
4
|
+
import { joinURL, parseURL } from 'ufo';
|
|
5
|
+
|
|
6
|
+
function addRoutes(router, routes, parentPath = "") {
|
|
7
|
+
const { context, routeTree, views, keys } = router;
|
|
8
|
+
routes.forEach(({ key, path, component, meta, children }) => {
|
|
9
|
+
const fullPath = joinURL(parentPath, path);
|
|
10
|
+
views[fullPath] ||= [];
|
|
11
|
+
views[fullPath].push(defineAsyncComponent(component));
|
|
12
|
+
keys[fullPath] ||= [];
|
|
13
|
+
keys[fullPath].push(key ?? null);
|
|
14
|
+
addRoute(context, "GET", fullPath, { key, fullPath, meta });
|
|
15
|
+
const node = [fullPath];
|
|
16
|
+
if (children) {
|
|
17
|
+
const childRouteTree = [];
|
|
18
|
+
addRoutes({ ...router, routeTree: childRouteTree }, children, fullPath);
|
|
19
|
+
node[1] = childRouteTree;
|
|
20
|
+
}
|
|
21
|
+
routeTree.push(node);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function resolveRouteTree(routeTree, targetPath, accumulatedPaths = []) {
|
|
25
|
+
for (const [path, children] of routeTree) {
|
|
26
|
+
const paths = [...accumulatedPaths, path];
|
|
27
|
+
if (path === targetPath) {
|
|
28
|
+
return paths;
|
|
29
|
+
}
|
|
30
|
+
if (children) {
|
|
31
|
+
const result = resolveRouteTree(children, targetPath, paths);
|
|
32
|
+
if (result.length) {
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
function createRenderList(routes, targetPath, views, keys) {
|
|
40
|
+
const renderList = [];
|
|
41
|
+
const currentSegments = targetPath.split("/").filter(Boolean);
|
|
42
|
+
routes.forEach((path) => {
|
|
43
|
+
const treeViews = views[path];
|
|
44
|
+
const treeKeys = keys[path];
|
|
45
|
+
if (treeViews === undefined || treeKeys === undefined || treeViews.length !== treeKeys.length) {
|
|
46
|
+
throw new Error(`[virou] Mismatch or missing data for path: ${path}`);
|
|
47
|
+
}
|
|
48
|
+
const resolvedPath = joinURL("/", ...currentSegments.slice(0, path.split("/").filter(Boolean).length));
|
|
49
|
+
for (let i = 0; i < treeViews.length; i++) {
|
|
50
|
+
const view = treeViews[i];
|
|
51
|
+
const key = treeKeys[i];
|
|
52
|
+
if (view !== undefined && key !== undefined && (targetPath === resolvedPath || i === 0)) {
|
|
53
|
+
renderList.push([key ?? resolvedPath, view]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return renderList;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const virouSymbol = Symbol("virou");
|
|
61
|
+
const useVirouState = createGlobalState(() => {
|
|
62
|
+
const routers = shallowRef({});
|
|
63
|
+
const activeRoutePaths = ref({});
|
|
64
|
+
return { routers, activeRoutePaths };
|
|
65
|
+
});
|
|
66
|
+
function createVRouter(key, routes, options) {
|
|
67
|
+
const { routers, activeRoutePaths } = useVirouState();
|
|
68
|
+
if (key in routers.value) {
|
|
69
|
+
throw new Error(`[virou] [createVRouter] key already exists: ${key}`);
|
|
70
|
+
}
|
|
71
|
+
routers.value[key] = {
|
|
72
|
+
context: createRouter(),
|
|
73
|
+
routeTree: [],
|
|
74
|
+
views: {},
|
|
75
|
+
keys: {}
|
|
76
|
+
};
|
|
77
|
+
addRoutes(routers.value[key], routes);
|
|
78
|
+
const { initialPath } = options ||= {
|
|
79
|
+
initialPath: "/"
|
|
80
|
+
};
|
|
81
|
+
activeRoutePaths.value[key] = initialPath;
|
|
82
|
+
}
|
|
83
|
+
function useVRouter(...args) {
|
|
84
|
+
if (typeof args[0] !== "string") {
|
|
85
|
+
args.unshift(inject(virouSymbol, useId()));
|
|
86
|
+
}
|
|
87
|
+
const [key, routes] = args;
|
|
88
|
+
if (!key || typeof key !== "string") {
|
|
89
|
+
throw new TypeError(`[virou] [useVRouter] key must be a string: ${key}`);
|
|
90
|
+
}
|
|
91
|
+
provide(virouSymbol, key);
|
|
92
|
+
const options = args[2];
|
|
93
|
+
const { routers, activeRoutePaths } = useVirouState();
|
|
94
|
+
if (routers.value[key] === undefined) {
|
|
95
|
+
if (routes === undefined) {
|
|
96
|
+
throw new Error(`[virou] [useVRouter] routes is required for key: ${key}`);
|
|
97
|
+
}
|
|
98
|
+
createVRouter(key, routes, options);
|
|
99
|
+
}
|
|
100
|
+
const route = computed(() => {
|
|
101
|
+
const { context, routeTree, views, keys } = routers.value[key];
|
|
102
|
+
const activeRoutePath = activeRoutePaths.value[key];
|
|
103
|
+
const matchedRoute = findRoute(
|
|
104
|
+
context,
|
|
105
|
+
"GET",
|
|
106
|
+
activeRoutePath
|
|
107
|
+
);
|
|
108
|
+
const activeRouteTree = matchedRoute && resolveRouteTree(routeTree, matchedRoute.data.fullPath);
|
|
109
|
+
const { search, pathname, hash } = parseURL(activeRoutePath);
|
|
110
|
+
const renderList = activeRouteTree && createRenderList(activeRouteTree, pathname, views, keys);
|
|
111
|
+
return {
|
|
112
|
+
fullPath: activeRoutePath,
|
|
113
|
+
...matchedRoute,
|
|
114
|
+
search,
|
|
115
|
+
path: pathname,
|
|
116
|
+
hash,
|
|
117
|
+
activeRouteTree,
|
|
118
|
+
renderList
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
route,
|
|
123
|
+
router: {
|
|
124
|
+
addRoute: (route2) => {
|
|
125
|
+
addRoutes(routers.value[key], [route2]);
|
|
126
|
+
},
|
|
127
|
+
replace: (path) => {
|
|
128
|
+
activeRoutePaths.value[key] = path;
|
|
129
|
+
},
|
|
130
|
+
_key: key,
|
|
131
|
+
_depthKey: Symbol.for(key)
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const VRouterView = defineComponent({
|
|
137
|
+
name: "VRouterView",
|
|
138
|
+
props: {
|
|
139
|
+
routerKey: {
|
|
140
|
+
type: String,
|
|
141
|
+
default: undefined
|
|
142
|
+
},
|
|
143
|
+
viewKey: {
|
|
144
|
+
type: String,
|
|
145
|
+
default: undefined
|
|
146
|
+
},
|
|
147
|
+
keepalive: {
|
|
148
|
+
type: Boolean,
|
|
149
|
+
default: false
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
setup(props) {
|
|
153
|
+
const routerKey = props.routerKey ?? inject(virouSymbol);
|
|
154
|
+
if (routerKey === undefined) {
|
|
155
|
+
throw new Error(`[virou] [VRouterView] routerKey is required`);
|
|
156
|
+
}
|
|
157
|
+
const { router, route } = useVRouter(routerKey);
|
|
158
|
+
const depth = inject(router._depthKey, 0);
|
|
159
|
+
const render = computed(() => route.value.renderList?.[depth]);
|
|
160
|
+
provide(router._depthKey, depth + 1);
|
|
161
|
+
return () => {
|
|
162
|
+
const [key, component] = render.value ?? [];
|
|
163
|
+
const _key = key ?? props.viewKey ?? route.value.path;
|
|
164
|
+
const ViewComponent = component !== null && component !== undefined ? h(component, { _key }) : null;
|
|
165
|
+
return props.keepalive ? h(KeepAlive, null, [ViewComponent]) : ViewComponent;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
export { VRouterView, createVRouter, useVRouter, virouSymbol };
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@virou/core",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Virtual router with multiple instance support for Vue",
|
|
6
|
+
"author": "Tankosin<https://github.com/tankosinn>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/tankosinn/virou#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/tankosinn/virou.git",
|
|
12
|
+
"directory": "packages/core"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/tankosinn/virou/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"vue",
|
|
19
|
+
"vue-router",
|
|
20
|
+
"virtual-router",
|
|
21
|
+
"router"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"default": "./dist/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/index.d.cts",
|
|
33
|
+
"default": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"main": "./dist/index.cjs",
|
|
38
|
+
"module": "./dist/index.mjs",
|
|
39
|
+
"types": "./dist/index.d.cts",
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@vueuse/core": "^12.4.0",
|
|
45
|
+
"rou3": "^0.5.1",
|
|
46
|
+
"ufo": "^1.5.4",
|
|
47
|
+
"vue": "^3.5.13"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"unbuild": "3.3.1"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "unbuild"
|
|
54
|
+
}
|
|
55
|
+
}
|