@virou/core 1.1.1 → 1.1.2

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
@@ -186,7 +186,7 @@ const { router, route } = useVRouter('my-wizard', routes)
186
186
  hash: string
187
187
  meta?: Record<PropertyKey, unknown>
188
188
  params?: Record<string, string>
189
- _renderList: Component[] | null
189
+ '~renderList': Component[] | null
190
190
  }
191
191
  ```
192
192
  - `router`:
@@ -312,10 +312,10 @@ const { router, route } = useVRouter('embedded-widget-app')
312
312
 
313
313
  #### Runtime-Registered Globals
314
314
 
315
- You may also mark a router as global at runtime by passing the `_isGlobal` option:
315
+ You may also mark a router as global at runtime by passing the `isGlobal` option:
316
316
 
317
317
  ```ts
318
- useVRouter(routes, { _isGlobal: true })
318
+ useVRouter(routes, { isGlobal: true })
319
319
  ```
320
320
 
321
321
  That router will stay registered even after components that use it unmount.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { RouterContext } from "rou3";
2
2
  import * as vue0 from "vue";
3
- import { Component, DefineComponent, Plugin, PropType, Ref, ShallowRef, SlotsType, VNodeChild } from "vue";
3
+ import { Component, DefineComponent, Plugin, PropType, ShallowRef, SlotsType, VNodeChild } from "vue";
4
4
 
5
5
  //#region src/types.d.ts
6
6
  type Lazy<T> = () => Promise<T>;
@@ -30,37 +30,37 @@ interface VRoute {
30
30
  path: string;
31
31
  search: string;
32
32
  hash: string;
33
- _renderList: Component[] | null;
33
+ '~renderList': Component[] | null;
34
34
  }
35
35
  interface VRouterData {
36
36
  context: RouterContext<VRouteMatchedData>;
37
37
  routes: VRoutesMap;
38
- activePath: Ref<string>;
38
+ activePath: ShallowRef<string>;
39
39
  route: ShallowRef<VRoute>;
40
- _isGlobal: boolean;
41
- _deps: number;
42
- _dispose: () => void;
40
+ isGlobal: boolean;
41
+ '~deps': number;
42
+ '~dispose': () => void;
43
43
  }
44
44
  interface VRouterOptions {
45
45
  initialPath?: string;
46
- _isGlobal?: boolean;
46
+ isGlobal?: boolean;
47
47
  }
48
48
  interface VRouter {
49
49
  route: VRouterData['route'];
50
50
  router: {
51
51
  addRoute: (route: VRouteRaw) => void;
52
52
  replace: (path: string) => void;
53
- _depthKey: symbol;
53
+ '~depthKey': symbol;
54
54
  };
55
55
  }
56
56
  interface VirouPluginOptions {
57
57
  routers?: Record<string, {
58
58
  routes: VRouteRaw[];
59
- options?: Omit<VRouterOptions, '_isGlobal'>;
59
+ options?: Omit<VRouterOptions, 'isGlobal'>;
60
60
  }>;
61
61
  }
62
62
  //#endregion
63
- //#region src/component.d.ts
63
+ //#region src/components/VRouterView.d.ts
64
64
  declare const VRouterView: vue0.DefineComponent<vue0.ExtractPropTypes<{
65
65
  routerKey: StringConstructor;
66
66
  keepAlive: {
@@ -93,14 +93,24 @@ declare const VRouterView: vue0.DefineComponent<vue0.ExtractPropTypes<{
93
93
  }) => VNodeChild;
94
94
  }>, {}, {}, string, vue0.ComponentProvideOptions, true, {}, any>;
95
95
  //#endregion
96
+ //#region src/composables/useVRouter.d.ts
97
+ declare function useVRouter(routes?: VRouteRaw[], options?: VRouterOptions): VRouter;
98
+ declare function useVRouter(key?: string, routes?: VRouteRaw[], options?: VRouterOptions): VRouter;
99
+ //#endregion
100
+ //#region src/constants.d.ts
101
+ declare const virouSymbol: unique symbol;
102
+ //#endregion
96
103
  //#region src/plugin.d.ts
97
104
  declare const virou: Plugin<[VirouPluginOptions?]>;
98
105
  //#endregion
99
- //#region src/router.d.ts
100
- declare const virouSymbol: unique symbol;
106
+ //#region src/router/create.d.ts
101
107
  declare function createVRouter(routes: VRouteRaw[], options?: VRouterOptions): VRouterData;
102
- declare function useVRouter(routes?: VRouteRaw[], options?: VRouterOptions): VRouter;
103
- declare function useVRouter(key?: string, routes?: VRouteRaw[], options?: VRouterOptions): VRouter;
104
108
  //#endregion
105
- export { VRoute, VRouteComponent, VRouteId, VRouteLazyComponent, VRouteMatchedData, VRouteMeta, VRouteNormalized, VRouteRaw, VRouteRenderComponent, VRouter, VRouterData, VRouterOptions, VRouterView, VRoutesMap, VirouPluginOptions, createVRouter, useVRouter, virou, virouSymbol };
109
+ //#region src/router/register.d.ts
110
+ declare function registerRoutes(ctx: RouterContext<VRouteMatchedData>, routes: VRouteRaw[], registry: VRoutesMap): void;
111
+ //#endregion
112
+ //#region src/router/render.d.ts
113
+ declare function createRenderList(data: VRouteMatchedData, routes: VRoutesMap): VRouteRenderComponent[];
114
+ //#endregion
115
+ export { VRoute, VRouteComponent, VRouteId, VRouteLazyComponent, VRouteMatchedData, VRouteMeta, VRouteNormalized, VRouteRaw, VRouteRenderComponent, VRouter, VRouterData, VRouterOptions, VRouterView, VRoutesMap, VirouPluginOptions, createRenderList, createVRouter, registerRoutes, useVRouter, virou, virouSymbol };
106
116
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/component.ts","../src/plugin.ts","../src/router.ts"],"sourcesContent":[],"mappings":";;;;;KAGK,gBAAgB,QAAQ;KAEjB,eAAA,GAAkB,YAAY;KAC9B,mBAAA,GAAsB,KAAK;AAHlC,KAIO,qBAAA,GAAwB,eAJR,GAI0B,mBAJ1B;AAEhB,KAIA,UAAA,GAAa,MAJE,CAIK,WAJF,EAAY,OAAA,CAAA;AAC9B,UAKK,SAAA,CALc;EACnB,IAAA,EAAA,MAAA;EAEA,SAAA,EAIC,qBAJmB;EAEf,IAAA,CAAA,EAGR,UAHiB;EAEb,QAAA,CAAA,EAEA,SAFA,EAAA;;AAEA,KAGD,QAAA,GAAW,QAHV,CAAA,CAAA,IAAA,EAAA,MAAA,EAAA,KAAA,EAAA,MAAA,CAAA,CAAA;AAAS,UAKL,iBAAA,CALK;EAGV,EAAA,EAGN,QAHc;EAEH,IAAA,CAAA,EAER,UAFQ;AAKjB;AAA+C,UAA9B,gBAAA,SAAyB,IAAK,CAAA,SAAA,EAAA,MAAA,GAAA,UAAA,CAAA,CA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Router.ts","../src/constants.ts","../src/plugin.ts","../src/router/create.ts","../src/router/register.ts","../src/router/render.ts"],"sourcesContent":[],"mappings":";;;;;KAGK,gBAAgB,QAAQ;KAEjB,eAAA,GAAkB,YAAY;KAC9B,mBAAA,GAAsB,KAAK;AAHlC,KAIO,qBAAA,GAAwB,eAJR,GAI0B,mBAJ1B;AAEhB,KAIA,UAAA,GAAa,MAJE,CAIK,WAJF,EAAY,OAAA,CAAA;AAC9B,UAKK,SAAA,CALc;EACnB,IAAA,EAAA,MAAA;EAEA,SAAA,EAIC,qBAJmB;EAEf,IAAA,CAAA,EAGR,UAHiB;EAEb,QAAA,CAAA,EAEA,SAFA,EAAA;;AAEA,KAGD,QAAA,GAAW,QAHV,CAAA,CAAA,IAAA,EAAA,MAAA,EAAA,KAAA,EAAA,MAAA,CAAA,CAAA;AAAS,UAKL,iBAAA,CALK;EAGV,EAAA,EAGN,QAHc;EAEH,IAAA,CAAA,EAER,UAFQ;AAKjB;AAA+C,UAA9B,gBAAA,SAAyB,IAAK,CAAA,SAAA,EAAA,MAAA,GAAA,UAAA,CAAA,CAAA;EAClC,QAAA,CAAA,EAAA,QAAA;;AADiC,KAIlC,UAAA,GAAa,GAJqB,CAIjB,QAJiB,EAIP,gBAJO,CAAA;AAIlC,UAEK,MAAA,CAFK;EAAO,QAAA,EAAA,MAAA;EAAU,IAAA,CAAA,EAI9B,UAJ8B;EAAd,MAAA,CAAA,EAKd,MALc,CAAA,MAAA,EAAA,MAAA,CAAA;EAAG,IAAA,EAAA,MAAA;EAEX,MAAA,EAAA,MAAM;EAEd,IAAA,EAAA,MAAA;EACE,aAAA,EAIM,SAJN,EAAA,GAAA,IAAA;;AAIe,UAGT,WAAA,CAHS;EAGT,OAAA,EACN,aADiB,CACH,iBADG,CAAA;EACH,MAAA,EACf,UADe;EAAd,UAAA,EAEG,UAFH,CAAA,MAAA,CAAA;EACD,KAAA,EAED,UAFC,CAEU,MAFV,CAAA;EACI,QAAA,EAAA,OAAA;E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AA,CAAA,CAAA;EAAI,CAAA;AAI9C,CAAA,CAAA,CAAA,WAAY,CAAA,CAAA,CAAU,CAAA,EAAA;EAAO,SAAA,EAAA,OAAA;CAAU,WAAA,CAAA;EAAd,OAAA,EAAA,CAAA,OAAA,EAAA;IAAG,SAAA,ECPQ,UDOR;IAEX,KAAM,ECTgC,MDShC;EAEd,CAAA,EAAA,GCX2D,UDW3D;EACE,QAAA,EAAA,CAAA,OAAA,EAAA;IAIM,KAAA,ECfgB,MDehB;EAAS,CAAA,EAAA,GCfoB,UDepB;AAG1B,CAAA,CAAA,EAAiB,CAAA,CAAA,EAAA,CAAA,CAAA,EAAA,MAAW,gCAAA,IAAA,EAAA,CAAA,CAAA,EAAA,GAAA,CAAA;;;iBEpCZ,UAAA,UAAoB,uBAAuB,iBAAiB;iBAC5D,UAAA,wBAAkC,uBAAuB,iBAAiB;;;cCN7E;;;cCIA,OAAO,QAAQ;;;iBCYZ,aAAA,SAAsB,uBAAuB,iBAAiB;;;iBCL9D,cAAA,MACT,cAAc,4BACX,uBACE;;;iBCTI,gBAAA,OACR,2BACE,aACP"}
package/dist/index.js CHANGED
@@ -1,15 +1,25 @@
1
1
  import { addRoute, createRouter, findRoute } from "rou3";
2
- import { KeepAlive, Suspense, defineAsyncComponent, defineComponent, getCurrentInstance, h, inject, onScopeDispose, provide, ref, shallowRef, useId, watchEffect } from "vue";
2
+ import { KeepAlive, Suspense, defineAsyncComponent, defineComponent, getCurrentInstance, h, inject, onScopeDispose, provide, shallowRef, useId, watchEffect } from "vue";
3
3
  import { joinURL, parseURL } from "ufo";
4
4
 
5
+ //#region src/constants.ts
6
+ const virouSymbol = Symbol("virou");
7
+
8
+ //#endregion
5
9
  //#region src/utils.ts
6
- const renderListCache = /* @__PURE__ */ new WeakMap();
7
10
  function normalizeComponent(component) {
8
11
  if (typeof component === "function" && component.length === 0) return defineAsyncComponent(component);
9
12
  return component;
10
13
  }
11
- function registerRoutes(ctx, routes, registry, parentId) {
12
- for (const { path, meta, component, children } of routes) {
14
+
15
+ //#endregion
16
+ //#region src/router/register.ts
17
+ function registerRoutes(ctx, routes, registry) {
18
+ const stack = [];
19
+ for (let i = routes.length - 1; i >= 0; i--) stack.push({ route: routes[i] });
20
+ while (stack.length > 0) {
21
+ const { route, parentId } = stack.pop();
22
+ const { path, meta, component, children } = route;
13
23
  const fullPath = joinURL(parentId?.[0] ?? "/", path);
14
24
  const depth = (parentId?.[1] ?? -1) + 1;
15
25
  const id = Object.freeze([fullPath, depth]);
@@ -18,13 +28,24 @@ function registerRoutes(ctx, routes, registry, parentId) {
18
28
  component: normalizeComponent(component),
19
29
  parentId
20
30
  });
21
- if (children && children.length) registerRoutes(ctx, children, registry, id);
22
- addRoute(ctx, "GET", fullPath, {
31
+ let isShadowed = false;
32
+ if (children && children.length > 0) {
33
+ isShadowed = children.some((c) => c.path === "" || c.path === "/");
34
+ for (let i = children.length - 1; i >= 0; i--) stack.push({
35
+ route: children[i],
36
+ parentId: id
37
+ });
38
+ }
39
+ if (!isShadowed) addRoute(ctx, "GET", fullPath, {
23
40
  id,
24
41
  meta
25
42
  });
26
43
  }
27
44
  }
45
+
46
+ //#endregion
47
+ //#region src/router/render.ts
48
+ const renderListCache = /* @__PURE__ */ new WeakMap();
28
49
  function createRenderList(data, routes) {
29
50
  let cacheForRoutes = renderListCache.get(routes);
30
51
  if (!cacheForRoutes) {
@@ -46,16 +67,25 @@ function createRenderList(data, routes) {
46
67
  }
47
68
 
48
69
  //#endregion
49
- //#region src/router.ts
50
- const virouSymbol = Symbol("virou");
70
+ //#region src/router/create.ts
51
71
  function createVRouter(routes, options) {
52
72
  const context = createRouter();
53
73
  const routeRegistry = /* @__PURE__ */ new Map();
54
74
  registerRoutes(context, routes, routeRegistry);
55
- const activePath = ref(options?.initialPath ?? "/");
75
+ let lastMatchedId;
76
+ let lastRenderList = null;
77
+ const activePath = shallowRef(options?.initialPath ?? "/");
56
78
  const snapshot = () => {
57
79
  const matchedRoute = findRoute(context, "GET", activePath.value);
58
- const _renderList = matchedRoute ? createRenderList(matchedRoute.data, routeRegistry) : null;
80
+ if (matchedRoute) {
81
+ if (matchedRoute.data.id !== lastMatchedId) {
82
+ lastRenderList = createRenderList(matchedRoute.data, routeRegistry);
83
+ lastMatchedId = matchedRoute.data.id;
84
+ }
85
+ } else {
86
+ lastMatchedId = void 0;
87
+ lastRenderList = null;
88
+ }
59
89
  const { pathname, hash, search } = parseURL(activePath.value);
60
90
  return {
61
91
  fullPath: activePath.value,
@@ -64,7 +94,7 @@ function createVRouter(routes, options) {
64
94
  hash,
65
95
  meta: matchedRoute?.data.meta,
66
96
  params: matchedRoute?.params,
67
- _renderList
97
+ "~renderList": lastRenderList
68
98
  };
69
99
  };
70
100
  const route = shallowRef(snapshot());
@@ -76,18 +106,21 @@ function createVRouter(routes, options) {
76
106
  routes: routeRegistry,
77
107
  activePath,
78
108
  route,
79
- _isGlobal: options?._isGlobal ?? false,
80
- _deps: 0,
81
- _dispose: unwatch
109
+ isGlobal: options?.isGlobal ?? false,
110
+ "~deps": 0,
111
+ "~dispose": unwatch
82
112
  };
83
113
  }
114
+
115
+ //#endregion
116
+ //#region src/composables/useVRouter.ts
84
117
  function useVRouter(...args) {
85
118
  if (typeof args[0] !== "string") args.unshift(inject(virouSymbol, useId()));
86
119
  const [key, routes = [], options = {}] = args;
87
120
  if (!key || typeof key !== "string") throw new TypeError(`[virou] [useVRouter] key must be a string: ${key}`);
88
121
  provide(virouSymbol, key);
89
122
  const vm = getCurrentInstance();
90
- if (!vm) throw new TypeError("[virou] [useVRouter] useVRouter must be called in setup()");
123
+ if (!vm) throw new Error("[virou] [useVRouter] useVRouter must be called in setup()");
91
124
  const virou$1 = vm.proxy?.$virou;
92
125
  if (!virou$1) throw new Error("[virou] [useVRouter] virou plugin not installed");
93
126
  if (routes.length) {
@@ -96,11 +129,11 @@ function useVRouter(...args) {
96
129
  }
97
130
  const router = virou$1.get(key);
98
131
  if (!router) throw new Error(`[virou] [useVRouter] router with key "${key}" not found`);
99
- router._deps++;
132
+ router["~deps"]++;
100
133
  onScopeDispose(() => {
101
- router._deps--;
102
- if (router._deps === 0 && !router._isGlobal) {
103
- router._dispose();
134
+ router["~deps"]--;
135
+ if (router["~deps"] === 0 && !router.isGlobal) {
136
+ router["~dispose"]();
104
137
  virou$1.delete(key);
105
138
  }
106
139
  });
@@ -113,13 +146,13 @@ function useVRouter(...args) {
113
146
  replace: (path) => {
114
147
  router.activePath.value = path;
115
148
  },
116
- _depthKey: Symbol.for(key)
149
+ "~depthKey": Symbol.for(key)
117
150
  }
118
151
  };
119
152
  }
120
153
 
121
154
  //#endregion
122
- //#region src/component.ts
155
+ //#region src/components/VRouterView.ts
123
156
  const VRouterView = defineComponent({
124
157
  name: "VRouterView",
125
158
  inheritAttrs: false,
@@ -136,10 +169,10 @@ const VRouterView = defineComponent({
136
169
  const key = props.routerKey ?? inject(virouSymbol);
137
170
  if (key === void 0) throw new Error("[virou] [VRouterView] routerKey is required");
138
171
  const { route, router } = useVRouter(key);
139
- const depth = inject(router._depthKey, 0);
140
- provide(router._depthKey, depth + 1);
172
+ const depth = inject(router["~depthKey"], 0);
173
+ provide(router["~depthKey"], depth + 1);
141
174
  return () => {
142
- const component = route.value._renderList?.[depth];
175
+ const component = route.value["~renderList"]?.[depth];
143
176
  if (!component) return slots.default?.({
144
177
  Component: null,
145
178
  route: route.value
@@ -169,10 +202,10 @@ const virou = (app, options = {}) => {
169
202
  app.config.globalProperties.$virou = map;
170
203
  if (routers) for (const [key, router] of Object.entries(routers)) map.set(key, createVRouter(router.routes, {
171
204
  ...router.options,
172
- _isGlobal: true
205
+ isGlobal: true
173
206
  }));
174
207
  };
175
208
 
176
209
  //#endregion
177
- export { VRouterView, createVRouter, useVRouter, virou, virouSymbol };
210
+ export { VRouterView, createRenderList, createVRouter, registerRoutes, useVRouter, virou, virouSymbol };
178
211
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["id: VRouteId","routeRegistry: VRoutesMap","virou","virou: Plugin<[VirouPluginOptions?]>"],"sources":["../src/utils.ts","../src/router.ts","../src/component.ts","../src/plugin.ts"],"sourcesContent":["import type { RouterContext } from 'rou3'\nimport type { Component } from 'vue'\nimport type { VRouteId, VRouteLazyComponent, VRouteMatchedData, VRouteRaw, VRouteRenderComponent, VRoutesMap } from './types'\nimport { addRoute } from 'rou3'\nimport { joinURL } from 'ufo'\nimport { defineAsyncComponent } from 'vue'\n\nconst renderListCache = new WeakMap<VRoutesMap, Map<VRouteId, Component[]>>()\n\nfunction normalizeComponent(component: VRouteRenderComponent): Component {\n if (typeof component === 'function' && (component as VRouteLazyComponent).length === 0) {\n return defineAsyncComponent(component as VRouteLazyComponent)\n }\n return component as Component\n}\n\nexport function registerRoutes(\n ctx: RouterContext<VRouteMatchedData>,\n routes: VRouteRaw[],\n registry: VRoutesMap,\n parentId?: VRouteId,\n): void {\n for (const { path, meta, component, children } of routes) {\n const fullPath = joinURL(parentId?.[0] ?? '/', path)\n const depth = (parentId?.[1] ?? -1) + 1\n\n const id: VRouteId = Object.freeze([fullPath, depth])\n\n registry.set(id, {\n meta,\n component: normalizeComponent(component),\n parentId,\n })\n\n if (children && children.length) {\n registerRoutes(ctx, children, registry, id)\n }\n\n addRoute(ctx, 'GET', fullPath, { id, meta })\n }\n}\n\nexport function createRenderList(\n data: VRouteMatchedData,\n routes: VRoutesMap,\n): VRouteRenderComponent[] {\n let cacheForRoutes = renderListCache.get(routes)\n if (!cacheForRoutes) {\n cacheForRoutes = new Map<VRouteId, Component[]>()\n renderListCache.set(routes, cacheForRoutes)\n }\n\n const cached = cacheForRoutes.get(data.id)\n if (cached) {\n return cached\n }\n\n const depth = data.id[1]\n const list = Array.from<VRouteRenderComponent>({ length: depth })\n let idx = depth\n let cursor = routes.get(data.id)\n while (cursor) {\n list[idx--] = cursor.component\n cursor = cursor.parentId !== undefined ? routes.get(cursor.parentId) : undefined\n }\n\n cacheForRoutes.set(data.id, list)\n return list\n}\n","import type { VRoute, VRouteMatchedData, VRouter, VRouteRaw, VRouterData, VRouterOptions, VRoutesMap } from './types'\nimport { createRouter, findRoute } from 'rou3'\nimport { parseURL } from 'ufo'\nimport { getCurrentInstance, inject, onScopeDispose, provide, ref, shallowRef, useId, watchEffect } from 'vue'\nimport { createRenderList, registerRoutes } from './utils'\n\nexport const virouSymbol = Symbol('virou')\n\nexport function createVRouter(routes: VRouteRaw[], options?: VRouterOptions): VRouterData {\n const context = createRouter<VRouteMatchedData>()\n const routeRegistry: VRoutesMap = new Map()\n registerRoutes(context, routes, routeRegistry)\n\n const activePath = ref(options?.initialPath ?? '/')\n\n const snapshot = (): VRoute => {\n const matchedRoute = findRoute(context, 'GET', activePath.value)\n const _renderList = matchedRoute ? createRenderList(matchedRoute.data, routeRegistry) : null\n const { pathname, hash, search } = parseURL(activePath.value)\n return {\n fullPath: activePath.value,\n path: pathname,\n search,\n hash,\n meta: matchedRoute?.data.meta,\n params: matchedRoute?.params,\n _renderList,\n }\n }\n\n const route = shallowRef<VRoute>(snapshot())\n\n const unwatch = watchEffect(() => {\n route.value = snapshot()\n })\n\n return {\n context,\n routes: routeRegistry,\n activePath,\n route,\n _isGlobal: options?._isGlobal ?? false,\n _deps: 0,\n _dispose: unwatch,\n }\n}\n\nexport function useVRouter(routes?: VRouteRaw[], options?: VRouterOptions): VRouter\nexport function useVRouter(key?: string, routes?: VRouteRaw[], options?: VRouterOptions): VRouter\nexport function useVRouter(...args: any[]): VRouter {\n if (typeof args[0] !== 'string') {\n args.unshift(inject<string>(virouSymbol, useId()))\n }\n\n const [key, routes = [], options = {}] = args as [string, VRouteRaw[], VRouterOptions]\n\n if (!key || typeof key !== 'string') {\n throw new TypeError(`[virou] [useVRouter] key must be a string: ${key}`)\n }\n\n provide(virouSymbol, key)\n\n const vm = getCurrentInstance()\n if (!vm) {\n throw new TypeError('[virou] [useVRouter] useVRouter must be called in setup()')\n }\n\n const virou = vm.proxy?.$virou\n if (!virou) {\n throw new Error('[virou] [useVRouter] virou plugin not installed')\n }\n\n if (routes.length) {\n if (virou.get(key)) {\n throw new Error(`[virou] [useVRouter] router with key \"${key}\" already exists`)\n }\n\n virou.set(key, createVRouter(routes, options))\n }\n\n const router = virou.get(key)\n if (!router) {\n throw new Error(`[virou] [useVRouter] router with key \"${key}\" not found`)\n }\n\n router._deps++\n onScopeDispose(() => {\n router._deps--\n\n if (router._deps === 0 && !router._isGlobal) {\n router._dispose()\n virou.delete(key)\n }\n })\n\n return {\n route: router.route,\n router: {\n addRoute: (route: VRouteRaw) => {\n registerRoutes(router.context, [route], router.routes)\n },\n replace: (path: string) => {\n router.activePath.value = path\n },\n _depthKey: Symbol.for(key),\n },\n }\n}\n","import type { PropType, SlotsType, VNodeChild } from 'vue'\nimport type { VRoute } from './types'\nimport {\n defineComponent,\n h,\n inject,\n KeepAlive,\n provide,\n Suspense,\n} from 'vue'\nimport { useVRouter, virouSymbol } from './router'\n\nexport const VRouterView = defineComponent({\n name: 'VRouterView',\n inheritAttrs: false,\n props: {\n routerKey: String,\n keepAlive: { type: Boolean, default: false },\n viewKey: { type: [String, Function] as PropType<string | ((route: VRoute, key: string) => string)> },\n },\n slots: Object as SlotsType<{\n default: (payload: { Component: VNodeChild, route: VRoute }) => VNodeChild\n fallback: (payload: { route: VRoute }) => VNodeChild\n }>,\n setup(props, { slots, attrs }) {\n const key = props.routerKey ?? inject<string>(virouSymbol)\n if (key === undefined) {\n throw new Error('[virou] [VRouterView] routerKey is required')\n }\n\n const { route, router } = useVRouter(key)\n\n const depth = inject<number>(router._depthKey, 0)\n provide(router._depthKey, depth + 1)\n\n return () => {\n const component = route.value._renderList?.[depth]\n if (!component) {\n return slots.default?.({ Component: null, route: route.value }) ?? null\n }\n\n const vnodeKey = typeof props.viewKey === 'function'\n ? props.viewKey(route.value, key)\n : props.viewKey ?? `${key}-${depth}-${route.value.path}`\n\n const suspenseVNode = h(\n Suspense,\n null,\n {\n default: () => h(component, { key: vnodeKey, ...attrs }),\n fallback: () => slots.fallback?.({ route: route.value }) ?? null,\n },\n )\n\n const vnode = props.keepAlive\n ? h(KeepAlive, null, { default: () => suspenseVNode })\n : suspenseVNode\n\n return slots.default?.({ Component: vnode, route: route.value }) ?? vnode\n }\n },\n})\n","import type { Plugin } from 'vue'\nimport type { VirouPluginOptions, VRouterData } from './types'\nimport { createVRouter } from './router'\n\nexport const virou: Plugin<[VirouPluginOptions?]> = (app, options = {}) => {\n const { routers } = options\n\n const map = new Map<string, VRouterData>()\n app.config.globalProperties.$virou = map\n\n if (routers) {\n for (const [key, router] of Object.entries(routers)) {\n map.set(key, createVRouter(router.routes, { ...router.options, _isGlobal: true }))\n }\n }\n}\n"],"mappings":";;;;;AAOA,MAAM,kCAAkB,IAAI,SAAiD;AAE7E,SAAS,mBAAmB,WAA6C;AACvE,KAAI,OAAO,cAAc,cAAe,UAAkC,WAAW,EACnF,QAAO,qBAAqB,UAAiC;AAE/D,QAAO;;AAGT,SAAgB,eACd,KACA,QACA,UACA,UACM;AACN,MAAK,MAAM,EAAE,MAAM,MAAM,WAAW,cAAc,QAAQ;EACxD,MAAM,WAAW,QAAQ,WAAW,MAAM,KAAK,KAAK;EACpD,MAAM,SAAS,WAAW,MAAM,MAAM;EAEtC,MAAMA,KAAe,OAAO,OAAO,CAAC,UAAU,MAAM,CAAC;AAErD,WAAS,IAAI,IAAI;GACf;GACA,WAAW,mBAAmB,UAAU;GACxC;GACD,CAAC;AAEF,MAAI,YAAY,SAAS,OACvB,gBAAe,KAAK,UAAU,UAAU,GAAG;AAG7C,WAAS,KAAK,OAAO,UAAU;GAAE;GAAI;GAAM,CAAC;;;AAIhD,SAAgB,iBACd,MACA,QACyB;CACzB,IAAI,iBAAiB,gBAAgB,IAAI,OAAO;AAChD,KAAI,CAAC,gBAAgB;AACnB,mCAAiB,IAAI,KAA4B;AACjD,kBAAgB,IAAI,QAAQ,eAAe;;CAG7C,MAAM,SAAS,eAAe,IAAI,KAAK,GAAG;AAC1C,KA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AAW,QAAQ,EAAE;AAEpC,eAAa;GACX,MAAM,YAAY,MAAM,MAAM,cAAc;AAC5C,OAAI,CAAC,UACH,QAAO,MAAM,UAAU;IAAE,WAAW;IAAM,OAAO,MAAM;IAAO,CAAC,IAAI;GAGrE,MAAM,WAAW,OAAO,MAAM,YAAY,aACtC,MAAM,QAAQ,MAAM,OAAO,IAAI,GAC/B,MAAM,WAAW,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,MAAM;GAEpD,MAAM,gBAAgB,EACpB,UACA,MACA;IACE,eAAe,EAAE,WAAW;KAAE,KAAK;KAAU,GAAG;KAAO,CAAC;IACxD,gBAAgB,MAAM,WAAW,EAAE,OAAO,MAAM,OAAO,CAAC,IAAI;IAC7D,CACF;GAED,MAAM,QAAQ,MAAM,YAChB,EAAE,WAAW,MAAM,EAAE,eAAe,eAAe,CAAC,GACpD;AAEJ,UAAO,MAAM,UAAU;IAAE,WAAW;IAAO,OAAO,MAAM;IAAO,CAAC,IAAI;;;CAGzE,CAAC;;;;ACzDF,MAAaC,SAAwC,KAAK,UAAU,EAAE,KAAK;CACzE,MAAM,EAAE,YAAY;CAEpB,MAAM,sBAAM,IAAI,KAA0B;AAC1C,KAAI,OAAO,iBAAiB,SAAS;AAErC,KAAI,QACF,MAAK,MAAM,CAAC,KAAK,WAAW,OAAO,QAAQ,QAAQ,CACjD,KAAI,IAAI,KAAK,cAAc,OAAO,QAAQ;EAAE,GAAG,OAAO;EAAS,WAAW;EAAM,CAAC,CAAC"}
1
+ {"version":3,"file":"index.js","names":["stack: StackItem[]","id: VRouteId","routeRegistry: VRoutesMap","lastMatchedId: VRouteId | undefined","lastRenderList: VRouteRenderComponent[] | null","virou","virou: Plugin<[VirouPluginOptions?]>"],"sources":["../src/constants.ts","../src/utils.ts","../src/router/register.ts","../src/router/render.ts","../src/router/create.ts","../src/composables/useVRouter.ts","../src/components/VRouterView.ts","../src/plugin.ts"],"sourcesContent":["export const virouSymbol = Symbol('virou')\n","import type { Component } from 'vue'\nimport type { VRouteLazyComponent, VRouteRenderComponent } from './types'\nimport { defineAsyncComponent } from 'vue'\n\nexport function normalizeComponent(component: VRouteRenderComponent): Component {\n if (typeof component === 'function' && (component as VRouteLazyComponent).length === 0) {\n return defineAsyncComponent(component as VRouteLazyComponent)\n }\n return component as Component\n}\n","import type { RouterContext } from 'rou3'\nimport type { VRouteId, VRouteMatchedData, VRouteRaw, VRoutesMap } from '../types'\nimport { addRoute } from 'rou3'\nimport { joinURL } from 'ufo'\nimport { normalizeComponent } from '../utils'\n\ninterface StackItem {\n route: VRouteRaw\n parentId?: VRouteId\n}\n\nexport function registerRoutes(\n ctx: RouterContext<VRouteMatchedData>,\n routes: VRouteRaw[],\n registry: VRoutesMap,\n): void {\n const stack: StackItem[] = []\n for (let i = routes.length - 1; i >= 0; i--) {\n stack.push({ route: routes[i] })\n }\n\n while (stack.length > 0) {\n const { route, parentId } = stack.pop()!\n const { path, meta, component, children } = route\n\n const parentPath = parentId?.[0] ?? '/'\n const fullPath = joinURL(parentPath, path)\n const depth = (parentId?.[1] ?? -1) + 1\n const id: VRouteId = Object.freeze([fullPath, depth])\n\n registry.set(id, {\n meta,\n component: normalizeComponent(component),\n parentId,\n })\n\n let isShadowed = false\n if (children && children.length > 0) {\n isShadowed = children.some(c => c.path === '' || c.path === '/')\n\n for (let i = children.length - 1; i >= 0; i--) {\n stack.push({ route: children[i], parentId: id })\n }\n }\n\n if (!isShadowed) {\n addRoute(ctx, 'GET', fullPath, { id, meta })\n }\n }\n}\n","import type { Component } from 'vue'\nimport type { VRouteId, VRouteMatchedData, VRouteRenderComponent, VRoutesMap } from '../types'\n\nconst renderListCache = new WeakMap<VRoutesMap, Map<VRouteId, Component[]>>()\n\nexport function createRenderList(\n data: VRouteMatchedData,\n routes: VRoutesMap,\n): VRouteRenderComponent[] {\n let cacheForRoutes = renderListCache.get(routes)\n if (!cacheForRoutes) {\n cacheForRoutes = new Map<VRouteId, Component[]>()\n renderListCache.set(routes, cacheForRoutes)\n }\n\n const cached = cacheForRoutes.get(data.id)\n if (cached) {\n return cached\n }\n\n const depth = data.id[1]\n const list = Array.from<VRouteRenderComponent>({ length: depth })\n let idx = depth\n let cursor = routes.get(data.id)\n while (cursor) {\n list[idx--] = cursor.component\n cursor = cursor.parentId !== undefined ? routes.get(cursor.parentId) : undefined\n }\n\n cacheForRoutes.set(data.id, list)\n return list\n}\n","import type {\n VRoute,\n VRouteId,\n VRouteMatchedData,\n VRouteRaw,\n VRouterData,\n VRouteRenderComponent,\n VRouterOptions,\n VRoutesMap,\n} from '../types'\nimport { createRouter, findRoute } from 'rou3'\nimport { parseURL } from 'ufo'\nimport { shallowRef, watchEffect } from 'vue'\nimport { registerRoutes } from './register'\nimport { createRenderList } from './render'\n\nexport function createVRouter(routes: VRouteRaw[], options?: VRouterOptions): VRouterData {\n const context = createRouter<VRouteMatchedData>()\n const routeRegistry: VRoutesMap = new Map()\n\n registerRoutes(context, routes, routeRegistry)\n\n let lastMatchedId: VRouteId | undefined\n let lastRenderList: VRouteRenderComponent[] | null = null\n\n const activePath = shallowRef(options?.initialPath ?? '/')\n\n const snapshot = (): VRoute => {\n const matchedRoute = findRoute(context, 'GET', activePath.value)\n if (matchedRoute) {\n if (matchedRoute.data.id !== lastMatchedId) {\n lastRenderList = createRenderList(matchedRoute.data, routeRegistry)\n lastMatchedId = matchedRoute.data.id\n }\n }\n else {\n lastMatchedId = undefined\n lastRenderList = null\n }\n\n const { pathname, hash, search } = parseURL(activePath.value)\n\n return {\n fullPath: activePath.value,\n path: pathname,\n search,\n hash,\n meta: matchedRoute?.data.meta,\n params: matchedRoute?.params,\n '~renderList': lastRenderList,\n }\n }\n\n const route = shallowRef<VRoute>(snapshot())\n\n const unwatch = watchEffect(() => {\n route.value = snapshot()\n })\n\n return {\n context,\n routes: routeRegistry,\n activePath,\n route,\n isGlobal: options?.isGlobal ?? false,\n '~deps': 0,\n '~dispose': unwatch,\n }\n}\n","import type { VRouter, VRouteRaw, VRouterOptions } from '../types'\nimport { getCurrentInstance, inject, onScopeDispose, provide, useId } from 'vue'\nimport { virouSymbol } from '../constants'\nimport { createVRouter, registerRoutes } from '../router'\n\nexport function useVRouter(routes?: VRouteRaw[], options?: VRouterOptions): VRouter\nexport function useVRouter(key?: string, routes?: VRouteRaw[], options?: VRouterOptions): VRouter\nexport function useVRouter(...args: any[]): VRouter {\n if (typeof args[0] !== 'string') {\n args.unshift(inject<string>(virouSymbol, useId()))\n }\n\n const [key, routes = [], options = {}] = args as [string, VRouteRaw[], VRouterOptions]\n\n if (!key || typeof key !== 'string') {\n throw new TypeError(`[virou] [useVRouter] key must be a string: ${key}`)\n }\n\n provide(virouSymbol, key)\n\n const vm = getCurrentInstance()\n if (!vm) {\n throw new Error('[virou] [useVRouter] useVRouter must be called in setup()')\n }\n\n const virou = vm.proxy?.$virou\n if (!virou) {\n throw new Error('[virou] [useVRouter] virou plugin not installed')\n }\n\n if (routes.length) {\n if (virou.get(key)) {\n throw new Error(`[virou] [useVRouter] router with key \"${key}\" already exists`)\n }\n\n virou.set(key, createVRouter(routes, options))\n }\n\n const router = virou.get(key)\n if (!router) {\n throw new Error(`[virou] [useVRouter] router with key \"${key}\" not found`)\n }\n\n router['~deps']++\n onScopeDispose(() => {\n router['~deps']--\n\n if (router['~deps'] === 0 && !router.isGlobal) {\n router['~dispose']()\n virou.delete(key)\n }\n })\n\n return {\n route: router.route,\n router: {\n addRoute: (route: VRouteRaw) => {\n registerRoutes(router.context, [route], router.routes)\n },\n replace: (path: string) => {\n router.activePath.value = path\n },\n '~depthKey': Symbol.for(key),\n },\n }\n}\n","import type { PropType, SlotsType, VNodeChild } from 'vue'\nimport type { VRoute } from '../types'\nimport {\n defineComponent,\n h,\n inject,\n KeepAlive,\n provide,\n Suspense,\n} from 'vue'\nimport { useVRouter } from '../composables'\nimport { virouSymbol } from '../constants'\n\nexport const VRouterView = defineComponent({\n name: 'VRouterView',\n inheritAttrs: false,\n props: {\n routerKey: String,\n keepAlive: { type: Boolean, default: false },\n viewKey: { type: [String, Function] as PropType<string | ((route: VRoute, key: string) => string)> },\n },\n slots: Object as SlotsType<{\n default: (payload: { Component: VNodeChild, route: VRoute }) => VNodeChild\n fallback: (payload: { route: VRoute }) => VNodeChild\n }>,\n setup(props, { slots, attrs }) {\n const key = props.routerKey ?? inject<string>(virouSymbol)\n if (key === undefined) {\n throw new Error('[virou] [VRouterView] routerKey is required')\n }\n\n const { route, router } = useVRouter(key)\n\n const depth = inject<number>(router['~depthKey'], 0)\n provide(router['~depthKey'], depth + 1)\n\n return () => {\n const component = route.value['~renderList']?.[depth]\n if (!component) {\n return slots.default?.({ Component: null, route: route.value }) ?? null\n }\n\n const vnodeKey = typeof props.viewKey === 'function'\n ? props.viewKey(route.value, key)\n : props.viewKey ?? `${key}-${depth}-${route.value.path}`\n\n const suspenseVNode = h(\n Suspense,\n null,\n {\n default: () => h(component, { key: vnodeKey, ...attrs }),\n fallback: () => slots.fallback?.({ route: route.value }) ?? null,\n },\n )\n\n const vnode = props.keepAlive\n ? h(KeepAlive, null, { default: () => suspenseVNode })\n : suspenseVNode\n\n return slots.default?.({ Component: vnode, route: route.value }) ?? vnode\n }\n },\n})\n","import type { Plugin } from 'vue'\nimport type { VirouPluginOptions, VRouterData } from './types'\nimport { createVRouter } from './router'\n\nexport const virou: Plugin<[VirouPluginOptions?]> = (app, options = {}) => {\n const { routers } = options\n\n const map = new Map<string, VRouterData>()\n app.config.globalProperties.$virou = map\n\n if (routers) {\n for (const [key, router] of Object.entries(routers)) {\n map.set(key, createVRouter(router.routes, { ...router.options, isGlobal: true }))\n }\n }\n}\n"],"mappings":";;;;;AAAA,MAAa,cAAc,OAAO,QAAQ;;;;ACI1C,SAAgB,mBAAmB,WAA6C;AAC9E,KAAI,OAAO,cAAc,cAAe,UAAkC,WAAW,EACnF,QAAO,qBAAqB,UAAiC;AAE/D,QAAO;;;;;ACGT,SAAgB,eACd,KACA,QACA,UACM;CACN,MAAMA,QAAqB,EAAE;AAC7B,MAA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MAAM;IAAO,CAAC,IAAI;GAGrE,MAAM,WAAW,OAAO,MAAM,YAAY,aACtC,MAAM,QAAQ,MAAM,OAAO,IAAI,GAC/B,MAAM,WAAW,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,MAAM;GAEpD,MAAM,gBAAgB,EACpB,UACA,MACA;IACE,eAAe,EAAE,WAAW;KAAE,KAAK;KAAU,GAAG;KAAO,CAAC;IACxD,gBAAgB,MAAM,WAAW,EAAE,OAAO,MAAM,OAAO,CAAC,IAAI;IAC7D,CACF;GAED,MAAM,QAAQ,MAAM,YAChB,EAAE,WAAW,MAAM,EAAE,eAAe,eAAe,CAAC,GACpD;AAEJ,UAAO,MAAM,UAAU;IAAE,WAAW;IAAO,OAAO,MAAM;IAAO,CAAC,IAAI;;;CAGzE,CAAC;;;;AC1DF,MAAaC,SAAwC,KAAK,UAAU,EAAE,KAAK;CACzE,MAAM,EAAE,YAAY;CAEpB,MAAM,sBAAM,IAAI,KAA0B;AAC1C,KAAI,OAAO,iBAAiB,SAAS;AAErC,KAAI,QACF,MAAK,MAAM,CAAC,KAAK,WAAW,OAAO,QAAQ,QAAQ,CACjD,KAAI,IAAI,KAAK,cAAc,OAAO,QAAQ;EAAE,GAAG,OAAO;EAAS,UAAU;EAAM,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@virou/core",
3
3
  "type": "module",
4
- "version": "1.1.1",
4
+ "version": "1.1.2",
5
5
  "description": "Virtual router with multiple instance support for Vue",
6
6
  "author": "Tankosin<https://github.com/tankosinn>",
7
7
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "vue": "^3.5.0"
35
35
  },
36
36
  "dependencies": {
37
- "rou3": "^0.7.11",
37
+ "rou3": "^0.7.12",
38
38
  "ufo": "^1.6.1"
39
39
  },
40
40
  "scripts": {