@virou/nuxt 0.1.0 → 1.0.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/README.md +335 -0
- package/dist/module.d.mts +12 -3
- package/dist/module.json +4 -4
- package/dist/module.mjs +9 -4
- package/dist/runtime/plugin.d.ts +2 -0
- package/dist/runtime/plugin.js +6 -0
- package/dist/types.d.mts +1 -7
- package/package.json +11 -14
- package/dist/module.cjs +0 -5
- package/dist/module.d.ts +0 -5
- package/dist/types.d.ts +0 -7
package/README.md
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
# 🧭 Virou
|
|
2
|
+
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://www.npmjs.com/package/@virou/core"><img src="https://img.shields.io/npm/v/@virou/core.svg?style=flat&colorA=18181B&colorB=39737d" alt="Version"></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/@virou/core"><img src="https://img.shields.io/npm/dm/@virou/core.svg?style=flat&colorA=18181B&colorB=39737d" alt="Downloads"></a>
|
|
6
|
+
<a href="https://bundlephobia.com/package/@virou/core"><img src="https://img.shields.io/bundlephobia/min/@virou/core?style=flat&colorA=18181B&colorB=39737d" alt="Bundle Size"></a>
|
|
7
|
+
<a href="https://github.com/tankosinn/virou/tree/main/LICENSE"><img src="https://img.shields.io/github/license/tankosinn/virou.svg?style=flat&colorA=18181B&colorB=39737d" alt="License"></a>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
Virou is a high-performance, lightweight virtual router for Vue with dynamic routing capabilities.
|
|
11
|
+
|
|
12
|
+
> Perfect for modals, wizards, embeddable widgets, or any scenario requiring routing without altering the browser's URL or history.
|
|
13
|
+
|
|
14
|
+
## ✨ Features
|
|
15
|
+
|
|
16
|
+
- 🪄 **Dynamic Virtual Routing**: Navigate without altering the browser's URL or history
|
|
17
|
+
- 🍂 **Multiple Router Instances**: Manage independent routing contexts within the same app
|
|
18
|
+
- 🪆 **Nested Routing**: Seamlessly handle complex, nested routes
|
|
19
|
+
- 🦾 **Type-Safe**: Written in [TypeScript](https://www.typescriptlang.org/)
|
|
20
|
+
- ⚡ **SSR-Friendly**: Compatible with server-side rendering
|
|
21
|
+
|
|
22
|
+
## 🐣 Usage
|
|
23
|
+
```vue
|
|
24
|
+
<script setup lang="ts">
|
|
25
|
+
import { useVRouter } from '@virou/core'
|
|
26
|
+
|
|
27
|
+
// Define your routes
|
|
28
|
+
const routes = [
|
|
29
|
+
{
|
|
30
|
+
path: '/',
|
|
31
|
+
component: () => import('./views/Home.vue'),
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
path: '/about',
|
|
35
|
+
component: () => import('./views/About.vue'),
|
|
36
|
+
},
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
// Create a virtual router instance
|
|
40
|
+
const { router, route } = useVRouter(routes)
|
|
41
|
+
</script>
|
|
42
|
+
|
|
43
|
+
<template>
|
|
44
|
+
<!-- Renders the current route's component -->
|
|
45
|
+
<VRouterView />
|
|
46
|
+
</template>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 📦 Installation
|
|
50
|
+
|
|
51
|
+
Install Virou with your package manager:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pnpm add @virou/core
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Register the `virou` plugin in your Vue app:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { virou } from '@virou/core'
|
|
61
|
+
import { createApp } from 'vue'
|
|
62
|
+
import App from './App.vue'
|
|
63
|
+
|
|
64
|
+
createApp(App)
|
|
65
|
+
.use(virou)
|
|
66
|
+
.mount('#app')
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 🧩 Using with Nuxt
|
|
70
|
+
|
|
71
|
+
Install Virou Nuxt module with your package manager:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
pnpm add @virou/nuxt
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Add the module to your Nuxt configuration:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// nuxt.config.ts
|
|
81
|
+
export default defineNuxtConfig({
|
|
82
|
+
modules: [
|
|
83
|
+
'@virou/nuxt',
|
|
84
|
+
],
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 🧱 Essentials
|
|
89
|
+
|
|
90
|
+
### 🌿 Routes
|
|
91
|
+
|
|
92
|
+
Declare your routes as an array of objects with required `path` and `component`, and optional `meta` and `children` properties.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
const routes: VRouterRaw[] = [
|
|
96
|
+
{
|
|
97
|
+
path: '/', // static path
|
|
98
|
+
component: Home,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
path: '/user/:id', // dynamic path with parameter
|
|
102
|
+
component: () => import('./views/User.vue'),
|
|
103
|
+
meta: {
|
|
104
|
+
foo: 'bar',
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
path: '/**:notFound', // Named wildcard path
|
|
109
|
+
component: defineAsyncComponent(() => import('./views/NotFound.vue')),
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Props**:
|
|
115
|
+
- `path`: the URL pattern to match. Supports:
|
|
116
|
+
- Static ("/about") for exact matches
|
|
117
|
+
- Dynamic ("/user/:id") for named parameters
|
|
118
|
+
- Wildcard ("/**") for catch-all segments
|
|
119
|
+
- Named wildcard ("/**:notFound") for catch-all with a name
|
|
120
|
+
- `component`: the Vue component to render when this route matches. Can be synchronous or an async loader.
|
|
121
|
+
- `meta`: metadata for the route.
|
|
122
|
+
- `children`: an array of child routes.
|
|
123
|
+
|
|
124
|
+
> Virou uses [rou3](https://github.com/h3js/rou3) under the hood for create router context and route matching.
|
|
125
|
+
|
|
126
|
+
### 🪆 Nested Routes
|
|
127
|
+
|
|
128
|
+
To define nested routes, add a children array to a route record. Child path values are relative to their parent (leading `/` is ignored).
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const routes: VRouterRaw[] = [
|
|
132
|
+
// ...
|
|
133
|
+
{
|
|
134
|
+
path: '/user/:id',
|
|
135
|
+
component: User,
|
|
136
|
+
children: [
|
|
137
|
+
{
|
|
138
|
+
path: '', // /user/:id -> default child route
|
|
139
|
+
component: UserProfile,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
path: '/settings', // /user/:id/settings
|
|
143
|
+
component: UserSettings,
|
|
144
|
+
children: [
|
|
145
|
+
{
|
|
146
|
+
path: '/', // /user/:id/settings -> deep default child route
|
|
147
|
+
component: UserSettingsGeneral,
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
path: '/notifications', // /user/:id/settings/notifications
|
|
151
|
+
component: UserSettingsNotifications,
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
]
|
|
156
|
+
},
|
|
157
|
+
// ...
|
|
158
|
+
]
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 🌳 Router
|
|
162
|
+
|
|
163
|
+
Create (or access) a virtual router instance with `useVRouter` composable.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const { router, route } = useVRouter('my-wizard', routes)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
> `useVRouter` must be called inside `setup()`.
|
|
170
|
+
|
|
171
|
+
**Params:**
|
|
172
|
+
- `key`: a unique key for the router instance. If you do not provide a key, Virou will generate one via `useId()`.
|
|
173
|
+
- `routes`: an array of route objects.
|
|
174
|
+
- `options`:
|
|
175
|
+
- `initialPath`: the path to render on initialization (defaults to `/`).
|
|
176
|
+
|
|
177
|
+
**Returns:**
|
|
178
|
+
- `route`: a Vue shallow ref that contains the current route object.
|
|
179
|
+
```typescript
|
|
180
|
+
export interface VRoute {
|
|
181
|
+
fullPath: string
|
|
182
|
+
path: string
|
|
183
|
+
search: string
|
|
184
|
+
hash: string
|
|
185
|
+
meta?: Record<PropertyKey, unknown>
|
|
186
|
+
params?: Record<string, string>
|
|
187
|
+
_renderList: Component[] | null
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
- `router`:
|
|
191
|
+
- `replace(path: string): void`: navigate to a new path.
|
|
192
|
+
- `addRoute(route: VRouteRaw): void`: add a route at runtime.
|
|
193
|
+
|
|
194
|
+
#### 🍃 Multiple Router Instances
|
|
195
|
+
|
|
196
|
+
Virou allows you to create multiple independent router instances within the same app.
|
|
197
|
+
|
|
198
|
+
```vue
|
|
199
|
+
<script setup lang="ts">
|
|
200
|
+
// Settings modal router
|
|
201
|
+
useVRouter('settings-modal', [
|
|
202
|
+
{
|
|
203
|
+
path: '/profile',
|
|
204
|
+
component: UserProfile,
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
path: '/preferences',
|
|
208
|
+
component: UserPreferences,
|
|
209
|
+
},
|
|
210
|
+
// ...
|
|
211
|
+
], { initialPath: '/profile' })
|
|
212
|
+
|
|
213
|
+
// Onboarding wizard router
|
|
214
|
+
useVRouter('onboarding-wizard', [
|
|
215
|
+
{
|
|
216
|
+
path: '/profile',
|
|
217
|
+
component: OnboardingProfile,
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
path: '/teamspace',
|
|
221
|
+
component: OnboardingTeamspace,
|
|
222
|
+
},
|
|
223
|
+
// ...
|
|
224
|
+
], { initialPath: '/profile' })
|
|
225
|
+
</script>
|
|
226
|
+
|
|
227
|
+
<template>
|
|
228
|
+
<SettingsModal>
|
|
229
|
+
<VRouterView router-key="settings-modal" />
|
|
230
|
+
</SettingsModal>
|
|
231
|
+
|
|
232
|
+
<Wizard>
|
|
233
|
+
<VRouterView router-key="onboarding-wizard" />
|
|
234
|
+
</Wizard>
|
|
235
|
+
</template>
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## 📺 Rendering
|
|
239
|
+
|
|
240
|
+
`<VRouterView>` mounts the matched component at its current nesting depth.
|
|
241
|
+
|
|
242
|
+
```vue
|
|
243
|
+
<template>
|
|
244
|
+
<VRouterView router-key="my-router">
|
|
245
|
+
:keep-alive="true" <!-- preserve component state -->
|
|
246
|
+
:view-key="(route, key) => `${key}|${route.fullPath}`" <!-- custom vnode key -->
|
|
247
|
+
>
|
|
248
|
+
<!-- default slot receives { Component, route } -->
|
|
249
|
+
<template #default="{ Component, route }">
|
|
250
|
+
<Transition name="fade" mode="out-in">
|
|
251
|
+
<component :is="Component" v-bind="route.params" />
|
|
252
|
+
</Transition>
|
|
253
|
+
</template>
|
|
254
|
+
|
|
255
|
+
<!-- fallback slot shown while async loader resolves -->
|
|
256
|
+
<template #fallback>
|
|
257
|
+
<div class="spinner">
|
|
258
|
+
Loading...
|
|
259
|
+
</div>
|
|
260
|
+
</template>
|
|
261
|
+
</VRouterView>
|
|
262
|
+
</template>
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Props:**
|
|
266
|
+
- `routerKey`: key of the router instance to render. If not provided, uses the nearest router instance.
|
|
267
|
+
- `keepAlive`: wraps the rendered component in `<KeepAlive>` when set to true, preserving its state across navigations.
|
|
268
|
+
- `viewKey`: accepts either a string or a function `(route, key) => string` to compute the vnode key for the rendered component.
|
|
269
|
+
|
|
270
|
+
**Slots:**
|
|
271
|
+
- `default`: slot receives `{ Component, route }` so you can wrap or decorate the active component.
|
|
272
|
+
- `fallback`: receives `{ route }` and is displayed inside `<Suspense>` while an async component is resolving.
|
|
273
|
+
|
|
274
|
+
> Virou wraps components in `<Suspense>` by default. To combine `<Suspense>` with other components, see the [Vue docs](https://vuejs.org/guide/built-ins/suspense#combining-with-other-components).
|
|
275
|
+
|
|
276
|
+
## 🛠️ Advanced
|
|
277
|
+
|
|
278
|
+
### 🌐 Global Routers
|
|
279
|
+
|
|
280
|
+
By default, routers created with `useVRouter(key, routes)` are disposable—they unregister themselves automatically once no components reference them.
|
|
281
|
+
|
|
282
|
+
To keep a router alive for your app’s entire lifecycle, register it as a global router.
|
|
283
|
+
|
|
284
|
+
#### Plugin-Registered Globals
|
|
285
|
+
|
|
286
|
+
Defined routers in the `virou` plugin options are registered as global routers:
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
createApp(App)
|
|
290
|
+
.use(virou, {
|
|
291
|
+
routers: {
|
|
292
|
+
'embedded-widget-app': {
|
|
293
|
+
routes: [
|
|
294
|
+
{ path: '/chat', component: () => import('./views/Chat.vue') },
|
|
295
|
+
{ path: '/settings', component: () => import('./views/Settings.vue') },
|
|
296
|
+
],
|
|
297
|
+
options: { initialPath: '/chat' }
|
|
298
|
+
},
|
|
299
|
+
// add more global routers here...
|
|
300
|
+
}
|
|
301
|
+
})
|
|
302
|
+
.mount('#app')
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Later:
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
const { router, route } = useVRouter('embedded-widget-app')
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
#### Runtime-Registered Globals
|
|
312
|
+
|
|
313
|
+
You may also mark a router as global at runtime by passing the `_isGlobal` option:
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
useVRouter(routes, { _isGlobal: true })
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
That router will stay registered even after components that use it unmount.
|
|
320
|
+
|
|
321
|
+
### 🧪 Create Standalone Virtual Router
|
|
322
|
+
|
|
323
|
+
You can create a standalone virtual router with `createVRouter`:
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
export function useCustomRouter() {
|
|
327
|
+
const { router, route } = createVRouter(routes)
|
|
328
|
+
|
|
329
|
+
// Custom logic here...
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## 📝 License
|
|
334
|
+
|
|
335
|
+
[MIT License](https://github.com/tankosinn/virou/blob/main/LICENSE)
|
package/dist/module.d.mts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { VirouPluginOptions } from '@virou/core';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
interface ModuleOptions extends VirouPluginOptions {
|
|
5
|
+
}
|
|
6
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
declare module '@nuxt/schema' {
|
|
9
|
+
interface PublicRuntimeConfig {
|
|
10
|
+
virou?: ModuleOptions;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { type ModuleOptions, _default as default };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { defineNuxtModule, addImports, addComponent } from '@nuxt/kit';
|
|
1
|
+
import { defineNuxtModule, createResolver, addPlugin, addImports, addComponent } from '@nuxt/kit';
|
|
2
|
+
import { defu } from 'defu';
|
|
2
3
|
|
|
3
4
|
const functions = [
|
|
4
5
|
"useVRouter"
|
|
@@ -8,14 +9,18 @@ const components = [
|
|
|
8
9
|
];
|
|
9
10
|
const module = defineNuxtModule({
|
|
10
11
|
meta: {
|
|
11
|
-
name: "virou"
|
|
12
|
+
name: "@virou/nuxt",
|
|
13
|
+
configKey: "virou"
|
|
12
14
|
},
|
|
13
|
-
setup(
|
|
15
|
+
setup(options, nuxt) {
|
|
16
|
+
const resolver = createResolver(import.meta.url);
|
|
17
|
+
nuxt.options.runtimeConfig.public.virou = defu(nuxt.options.runtimeConfig.public.virou, options);
|
|
18
|
+
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
14
19
|
functions.forEach((name) => {
|
|
15
20
|
addImports({ name, as: name, from: "@virou/core", priority: -1 });
|
|
16
21
|
});
|
|
17
22
|
components.forEach((name) => {
|
|
18
|
-
|
|
23
|
+
addComponent({ name, export: name, filePath: "@virou/core", priority: -1 });
|
|
19
24
|
});
|
|
20
25
|
}
|
|
21
26
|
});
|
package/dist/types.d.mts
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type { default as Module } from './module.js'
|
|
4
|
-
|
|
5
|
-
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
-
|
|
7
|
-
export { default } from './module.js'
|
|
1
|
+
export { type ModuleOptions, default } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virou/nuxt",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"description": "Virou Nuxt Module",
|
|
6
6
|
"author": "Tankosin<https://github.com/tankosinn>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -25,28 +25,25 @@
|
|
|
25
25
|
],
|
|
26
26
|
"sideEffects": false,
|
|
27
27
|
"exports": {
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
"import": "./dist/module.mjs",
|
|
31
|
-
"require": "./dist/module.cjs"
|
|
32
|
-
}
|
|
28
|
+
"types": "./dist/types.d.mts",
|
|
29
|
+
"default": "./dist/module.mjs"
|
|
33
30
|
},
|
|
34
|
-
"main": "./dist/module.
|
|
35
|
-
"types": "./dist/types.d.ts",
|
|
31
|
+
"main": "./dist/module.mjs",
|
|
36
32
|
"files": [
|
|
37
33
|
"dist"
|
|
38
34
|
],
|
|
39
35
|
"engines": {
|
|
40
|
-
"node": ">=18.
|
|
36
|
+
"node": ">=18.12.0"
|
|
41
37
|
},
|
|
42
38
|
"dependencies": {
|
|
43
|
-
"@nuxt/kit": "^3.
|
|
44
|
-
"
|
|
39
|
+
"@nuxt/kit": "^3.17.2",
|
|
40
|
+
"defu": "^6.1.4",
|
|
41
|
+
"@virou/core": "1.0.0"
|
|
45
42
|
},
|
|
46
43
|
"devDependencies": {
|
|
47
|
-
"@nuxt/module-builder": "0.
|
|
48
|
-
"@nuxt/schema": "3.
|
|
49
|
-
"nuxt": "3.
|
|
44
|
+
"@nuxt/module-builder": "1.0.1",
|
|
45
|
+
"@nuxt/schema": "3.17.2",
|
|
46
|
+
"nuxt": "3.17.2"
|
|
50
47
|
},
|
|
51
48
|
"scripts": {
|
|
52
49
|
"build": "nuxt-module-build build"
|
package/dist/module.cjs
DELETED
package/dist/module.d.ts
DELETED
package/dist/types.d.ts
DELETED