Skip to content

Commit 63788f6

Browse files
committed
fix: require explicit routes import to avoid cyclic imports
Fix #132 BREAKING CHANGE: `createRouter()` now requires the explicit `router` property to be set and imported: ```diff import { createRouter, createWebHistory } from 'vue-router/auto' +import { routes } from 'vue-router/auto-routes' createRouter({ history: createWebHistory(), + routes }) ``` This also means that runtime `extendRoutes()` option is not needed. It has been deprecated and will be removed in the next major release.
1 parent 85d8472 commit 63788f6

File tree

10 files changed

+43
-40
lines changed

10 files changed

+43
-40
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ If you don't have an `env.d.ts` file, you can create one and add the unplugin-vu
149149
{
150150
"compilerOptions": {
151151
// ...
152-
"types": [
153-
"unplugin-vue-router/client"
154-
]
152+
"types": ["unplugin-vue-router/client"]
155153
}
156154
}
157155
```
@@ -161,11 +159,12 @@ Finally, you should replace your imports from `vue-router` to `vue-router/auto`:
161159
```diff
162160
-import { createRouter, createWebHistory } from 'vue-router'
163161
+import { createRouter, createWebHistory } from 'vue-router/auto'
162+
+import { routes } from 'vue-router/auto-routes'
164163

165164
createRouter({
166165
history: createWebHistory(),
167-
// You don't need to pass the routes anymore,
168-
// the plugin writes it for you 🤖
166+
// pass the generated routes written by the plugin 🤖
167+
+ routes,
169168
})
170169
```
171170

docs/guide/extending-routes.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,37 +89,38 @@ The `<route>` custom block is a way to extend existing routes. It can be used to
8989

9090
Note you can specify the language to use with `<route lang="yaml">`. By default, the language is JSON5 (more flexible version of JSON) but yaml and JSON are also supported.
9191

92-
## `extendRoutes()`
92+
## Extending routes at runtime
9393

94-
As an escape-hatch, it's possible to extend the routes **at runtime** with the `extendRoutes` option in `createRouter()`. Since these changes are made at runtime, they are not reflected in the generated `typed-router.d.ts` file.
94+
As an escape-hatch, it's possible to extend the routes **at runtime** by simply changing the `routes` array before passing it to `createRouter()`. Since these changes are made at runtime, they are not reflected in the generated `typed-router.d.ts` file.
9595

96-
```js{4-12}
96+
```js{4-9}
9797
import { createWebHistory, createRouter } from 'vue-router/auto'
98+
import { routes } from 'vue-router/auto-routes'
99+
100+
for (const route of routes) {
101+
if (route.name === '/admin') {
102+
adminRoute.meta ??= {}
103+
adminRoute.meta.requiresAuth = true
104+
}
105+
}
98106
99107
const router = createRouter({
100-
extendRoutes: (routes) => {
101-
const adminRoute = routes.find((r) => r.name === '/admin')
102-
if (adminRoute) {
103-
adminRoute.meta ??= {}
104-
adminRoute.meta.requiresAuth = true
105-
}
106-
// the return is completely optional since we are modifying the routes in place
107-
return routes
108-
},
109108
history: createWebHistory(),
109+
routes,
110110
})
111111
```
112112

113-
As this plugin evolves, this function should be used less and less and only become necessary in unique edge cases.
113+
As this plugin evolves, this should be used less and less and only become necessary in unique edge cases.
114114

115-
One example of this is using [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts) which can only be used alongside `extendRoutes()`:
115+
One example of this is using [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts) which can only be used this way:
116116

117117
```ts
118118
import { createRouter } from 'vue-router/auto'
119+
import { routes } from 'vue-router/auto-routes'
119120
import { setupLayouts } from 'virtual:generated-layouts'
120121

121122
const router = createRouter({
122123
// ...
123-
extendRoutes: (routes) => setupLayouts(routes),
124+
routes: setupLayouts(routes),
124125
})
125126
```

docs/introduction.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Given the following route configuration:
179179
```ts [src/router.ts]
180180
import { createRouter, createWebHistory } from 'vue-router' // [!code --]
181181
import { createRouter, createWebHistory } from 'vue-router/auto' // [!code ++]
182+
import { routes } from 'vue-router/auto-routes' // [!code ++]
182183
183184
export const router = createRouter({
184185
history: createWebHistory(),
@@ -196,6 +197,7 @@ export const router = createRouter({
196197
component: () => import('src/pages/About.vue'), // [!code --]
197198
}, // [!code --]
198199
] // [!code --]
200+
routes, // [!code ++]
199201
})
200202
```
201203

@@ -222,14 +224,15 @@ Check the [file-based routing](/guide/file-based-routing) guide for more informa
222224

223225
::: code-group
224226

225-
```ts{2,5-8,11} [src/main.ts]
227+
```ts{2-3,6-9,12} [src/main.ts]
226228
import { createApp } from 'vue'
227229
import { createRouter, createWebHistory } from 'vue-router/auto'
230+
import { routes } from 'vue-router/auto-routes'
228231
import App from './App.vue'
229232
230233
const router = createRouter({
231234
history: createWebHistory(),
232-
// the routes property is handled by the plugin
235+
routes,
233236
})
234237
235238
createApp(App)
@@ -247,9 +250,9 @@ createApp(App)
247250

248251
Check the [file-based routing](/guide/file-based-routing) guide for more information about the naming conventions.
249252

250-
### Passing the routes manually
253+
### Manipulating the routes
251254

252-
Alternatively, **you can also import the `routes` array** and create the router manually or pass it to some plugin. Here is an example with [Vitesse starter](https://github.com/antfu/vitesse/blob/main/src/main.ts):
255+
You can pass the `routes` to any plugin that needs to add changes to them but note that **these changes will not be reflected in types**. Use [build-time routes instead](./guide//extending-routes.md) if you want to have types support. Here is an example with [Vitesse starter](https://github.com/antfu/vitesse/blob/main/src/main.ts):
253256

254257
```ts
255258
import { ViteSSG } from 'vite-ssg'

docs/rfcs/data-loaders/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ import { DataLoaderPlugin, createRouter, createWebHistory } from 'vue-router/aut
566566
const app = createApp({})
567567
const router = createRouter({
568568
history: createWebHistory(),
569+
routes: [],
569570
})
570571
// ---cut---
571572
// @moduleResolution: bundler

playground/src/main.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ import {
55
createWebHistory,
66
DataLoaderPlugin,
77
} from 'vue-router/auto'
8+
import { routes } from 'vue-router/auto-routes'
89
import { MutationCache, QueryCache, VueQueryPlugin } from '@tanstack/vue-query'
910
import { createPinia } from 'pinia'
1011
import { QueryPlugin } from '@pinia/colada'
1112

1213
const router = createRouter({
1314
history: createWebHistory(),
14-
extendRoutes: (routes) => {
15-
// routes.find((r) => r.name === '/')!.meta = {}
16-
return routes
17-
},
15+
routes,
1816
})
1917

2018
const app = createApp(App)

src/codegen/vueRouterModule.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export function generateVueRouterProxy(
99
{ addPiniaColada }: { addPiniaColada: boolean }
1010
) {
1111
return `
12-
import { routes } from '${routesModule}'
1312
import { createRouter as _createRouter } from 'vue-router'
1413
1514
export * from 'vue-router'
@@ -24,14 +23,19 @@ export * from 'unplugin-vue-router/data-loaders/basic'
2423
${addPiniaColada ? "export * from 'unplugin-vue-router/data-loaders/pinia-colada'" : ''}
2524
2625
export function createRouter(options) {
27-
const { extendRoutes } = options
26+
const { extendRoutes, routes } = options
2827
// use Object.assign for better browser support
28+
if (extendRoutes) {
29+
console.warn('"extendRoutes()" is deprecated, please modify the routes directly. See')
30+
}
2931
const router = _createRouter(Object.assign(
3032
options,
31-
{ routes: typeof extendRoutes === 'function' ? extendRoutes(routes) : routes },
33+
{ routes: typeof extendRoutes === 'function' ? (extendRoutes(routes) || routes) : routes },
3234
))
3335
3436
return router
3537
}
3638
`.trimStart()
3739
}
40+
41+
// FIXME: remove `extendRoutes()` in the next major version

src/core/vite/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ViteDevServer } from 'vite'
1+
import { type ViteDevServer } from 'vite'
22
import { ServerContext } from '../../options'
33
import { asVirtualId } from '../moduleConstants'
44

src/data-loaders/navigation-guard.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,8 @@ export class NavigationResult {
325325
*
326326
* @example
327327
* ```ts
328-
* import { createApp } from 'vue'
329-
* import {
330-
* createRouter,
331-
* DataLoaderPlugin,
332-
* createWebHistory,
333-
* } from 'vue-router/auto'
334-
*
335328
* const router = createRouter({
329+
* routes,
336330
* history: createWebHistory(),
337331
* })
338332
*

src/type-extensions/router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,14 @@ export type _Router =
152152

153153
/**
154154
* unplugin-vue-router version of `RouterOptions`.
155+
* @deprecated use `RouterOptions` instead. This type is no longer needed.
155156
* @see {@link RouterOptions}
156157
*/
157-
export interface _RouterOptions extends Omit<RouterOptions, 'routes'> {
158+
export interface _RouterOptions extends RouterOptions {
158159
/**
159160
* Modify the routes before they are passed to the router. You can modify the existing array or return a
160161
* new one.
162+
* @deprecated `routes` is now required, so you can just modify a copy of the array directly.
161163
*
162164
* @param routes - The routes generated by this plugin and exposed by `vue-router/auto-routes`
163165
*/

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type {
4949
// TODO: deprecate and remove
5050
_RouterTyped,
5151
_Router as Router,
52+
// FIXME: remove in the next major version
5253
_RouterOptions,
5354
} from './type-extensions/router'
5455
export type {

0 commit comments

Comments
 (0)