Skip to content

Commit 5f8a5a7

Browse files
committed
refactor: remove /auto as much as possible
1 parent 6044b94 commit 5f8a5a7

File tree

18 files changed

+78
-83
lines changed

18 files changed

+78
-83
lines changed

client.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ declare module 'vue-router/auto-routes' {
99
/**
1010
* Setups hot module replacement for routes.
1111
* @param router - The router instance
12+
* @example
13+
* ```ts
14+
* import { createRouter, createWebHistory } from 'vue-router'
15+
* import { routes, handleHotUpdate } from 'vue-router/auto-routes'
16+
* const router = createRouter({
17+
* history: createWebHistory(),
18+
* routes,
19+
* })
20+
* if (import.meta.hot) {
21+
* handleHotUpdate(router)
22+
* }
23+
* ```
1224
*/
1325
export function handleHotUpdate(router: Router): void
1426
}

docs/guide/eslint.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# ESlint
22

3-
If you are not using auto imports, you will need to tell ESlint about `vue-router/auto` and `vue-router/auto-routes`. Add these lines to your eslint configuration:
3+
If you are not using auto imports, you will need to tell ESlint about `vue-router/auto-routes`. Add these lines to your eslint configuration:
44

55
```json{3}
66
{
77
"settings": {
8-
"import/core-modules": ["vue-router/auto", "vue-router/auto-routes"]
8+
"import/core-modules": ["vue-router/auto-routes"]
99
}
1010
}
1111
```

docs/guide/extending-routes.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ It's possible to override the route configuration directly in the page component
4242

4343
### `definePage()`
4444

45-
You can modify and extend any page component with the `definePage()` macro from `vue-router/auto`. This is useful for adding meta information, or modifying the route object. If you have configured auto imports, you won't need to import `definePage` from `vue-router/auto` as it is already available.
45+
You can modify and extend any page component with the `definePage()` macro. This is useful for adding meta information, or modifying the route object. It's globally available in Vue components but you can import it from `unplugin-vue-router/runtime` if needed.
4646

47-
```vue{2,4-9} twoslash
47+
```vue{2-7} twoslash
4848
<script setup lang="ts">
4949
// ---cut-start---
5050
import 'unplugin-vue-router/client'
@@ -53,8 +53,6 @@ export {}
5353
// ---cut-end---
5454
// @errors: 2322 2339
5555
// @moduleResolution: bundler
56-
import { definePage } from 'unplugin-vue-router/runtime'
57-
5856
definePage({
5957
alias: ['/n/:name'],
6058
meta: {

docs/guide/typescript.md

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This plugin generates a `d.ts` file with all the typing overrides when the dev o
1313
}
1414
```
1515

16-
Then, you will be able to import from `vue-router/auto` (instead of `vue-router`) to get access to the typed APIs.
16+
The generated _Route Map_ is picked up by `unplugin-vue-router/client` types and configures the `vue-router` types to be aware of the routes in your application. Making everything type safe!
1717

1818
::: tip
1919
You can commit the newly added `.d.ts` files to your repository to make your life easier.
@@ -33,17 +33,13 @@ router.push('')
3333

3434
## Extra types
3535

36-
You can always take a look at the generated `typed-router.d.ts` file to inspect what are the generated types. `unplugin-vue-router` improves upon many of the existing types in `vue-router` and adds a few ones as well:
37-
38-
### `RouteNamedMap`
39-
40-
The `RouteNamedMap` interface gives you access to all the metadata associated with a route. It can also be extended to enable types for **dynamic routes** that are added during runtime.
36+
You can always take a look at the generated `typed-router.d.ts` file to inspect what are the generated types. `unplugin-vue-router` creates a `RouteNamedMap` interface and exports it from `'vue-router/auto-routes'`.
4137

4238
```ts
4339
import type { RouteNamedMap } from 'vue-router/auto-routes'
4440
```
4541

46-
Extending types with dynamically added routes:
42+
This interface contains all the routes in your application along with their metadata. Augment it to add types for **dynamic routes** that are added during runtime:
4743

4844
```ts
4945
export {} // needed in .d.ts files
@@ -70,27 +66,7 @@ declare module 'vue-router/auto-routes' {
7066
}
7167
```
7268

73-
### `Router`
74-
75-
The `Router` type gives you access to the typed version of the router instance. It's also the _ReturnType_ of the `useRouter()` function.
76-
77-
```ts
78-
import type { Router } from 'vue-router'
79-
```
80-
81-
### `RouteLocationResolved`
82-
83-
The `RouteLocationResolved` type exposed by `vue-router/auto` allows passing a generic (which autocomplete) to type a route **whenever checking the name doesn't makes sense because you know the type**. This is useful for cases like `<RouterLink v-slot="{ route }">`:
84-
85-
```vue
86-
<RouterLink v-slot="{ route }">
87-
User {{ (route as RouteLocationResolved<'/users/[id]'>).params.id }}
88-
</RouterLink>
89-
```
90-
91-
This type is also the return type of `router.resolve()`.
92-
93-
You have the same equivalents for `RouteLocation`, `RouteLocationNormalized`, and `RouteLocationNormalizedLoaded`. All of them exist in `vue-router` but `vue-router/auto` override them to provide a type safe version of them. In addition to that, you can pass the name of the route as a generic:
69+
You can now pass a _type param_ to the generic route location types to narrow down the type of the route:
9470

9571
```ts twoslash
9672
// ---cut-start---

docs/introduction.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,19 @@ declare module 'vue-router/auto-routes' {
115115
* Route name map generated by unplugin-vue-router
116116
*/
117117
export interface RouteNamedMap {
118-
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>,
119-
'/about': RouteRecordInfo<'/about', '/about', Record<never, never>, Record<never, never>>,
120-
'/users/[id]': RouteRecordInfo<'/[id]', '/:id', { id: ParamValue<true> }, { id: ParamValue<false> }>,
118+
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>
119+
'/about': RouteRecordInfo<
120+
'/about',
121+
'/about',
122+
Record<never, never>,
123+
Record<never, never>
124+
>
125+
'/users/[id]': RouteRecordInfo<
126+
'/[id]',
127+
'/:id',
128+
{ id: ParamValue<true> },
129+
{ id: ParamValue<false> }
130+
>
121131
}
122132
}
123133
```
@@ -152,21 +162,22 @@ If you don't have an `env.d.ts` file, you can create one and add the unplugin-vu
152162

153163
:::
154164

155-
Instead of importing from `vue-router`, you should import from `vue-router/auto`.
165+
::: warning
166+
167+
unplugin-vue-router will add a virtual `vue-router/auto` module that exports everything from `vue-router` with some extra features from `unplugin-vue-router/runtime`. It's recommended to avoid using `vue-router/auto` in new projects. It's kept for compatibility with existing projects that use it and will likely be removed in the future.
156168

157169
::: tip
158-
You can exclude `vue-router` from VSCode import suggestions by adding this setting to your `.vscode/settings.json`:
170+
You can exclude `vue-router/auto` from VSCode import suggestions by adding this setting to your `.vscode/settings.json`:
159171

160172
```json
161173
{
162174
"typescript.tsdk": "node_modules/typescript/lib",
163-
"typescript.preferences.autoImportFileExcludePatterns": ["vue-router$"]
175+
"typescript.preferences.autoImportFileExcludePatterns": ["vue-router/auto$"]
164176
}
165177
```
166178

167-
This will ensure VSCode only suggests `vue-router/auto` for imports. Alternatively, you can also configure [auto imports](#auto-imports).
179+
This will ensure VSCode does not suggest `vue-router/auto` for imports. Alternatively, you can also configure [auto imports](#auto-imports).
168180

169-
The reason we need to import from `vue-router/auto` rather than `vue-router` is because we override all the types with typed version. It is **not possible** to override the types from `vue-router` directly as the typed ones are all generic.
170181
:::
171182

172183
### Migrating an existing project
@@ -219,7 +230,7 @@ Check the [file-based routing](/guide/file-based-routing) guide for more informa
219230
### From scratch
220231

221232
- Create a `src/pages` folder and add an `index.vue` component to it. This will render your home page at `/`.
222-
- Add Vue Router using `vue-router/auto` instead of `vue-router`. These types are augmented to be fully typed.
233+
- Import the `routes` from `vue-router/auto-routes` and pass them to the `createRouter` function.
223234

224235
::: code-group
225236

@@ -308,21 +319,5 @@ export default defineConfig({
308319
})
309320
```
310321

311-
FIXME: DELETE
312-
313-
Note that the `vue-router` preset might export less things than the one exported by `unplugin-vue-router` so you might need to add any other imports you were relying on manually:
314-
315-
```ts
316-
AutoImport({
317-
imports: [
318-
VueRouterAutoImports,
319-
{ // [!code ++]
320-
// add any other imports you were relying on // [!code ++]
321-
'vue-router/auto': ['useLink'] // [!code ++]
322-
}, // [!code ++]
323-
],
324-
}),
325-
```
326-
327322
If you use ESlint, check [the ESlint section](/guide/eslint).
328323
If you use TypeScript or have a `jsconfig.json` file, check [the TypeScript section](/guide/typescript).

docs/rfcs/data-loaders/index.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ You might only be interested in trying out Data Loaders. In that case, check out
203203
```ts{2,9}
204204
import { createApp } from 'vue'
205205
import { createRouter } from 'vue-router'
206-
import { DataLoaderPlugin } from 'vue-router/auto'
206+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
207207
208208
const router = createRouter({
209209
// ...
@@ -564,7 +564,7 @@ Since navigation loaders can run in parallel, they can return different navigati
564564
import 'unplugin-vue-router/client'
565565
import { createApp } from 'vue'
566566
import { createRouter, createWebHistory } from 'vue-router'
567-
import { DataLoaderPlugin } from 'vue-router/auto'
567+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
568568
const app = createApp({})
569569
const router = createRouter({
570570
history: createWebHistory(),
@@ -598,7 +598,7 @@ By default, `selectNavigation` returns the first value of the array.
598598
If a loader wants to eagerly alter the navigation, it can `throw` the `NavigationResult` instead of returning it. This skips the `selectNavigationResult()` and take precedence without triggering `router.onError()`.
599599

600600
```ts{10-15}
601-
import { NavigationResult } from 'vue-router/auto'
601+
import { NavigationResult } from 'unplugin-vue-router/data-loaders'
602602
603603
export const useUserData = defineLoader(
604604
async (to) => {
@@ -830,10 +830,6 @@ This aligns with the future [Navigation API](https://github.com/WICG/navigation-
830830

831831
### Implementations
832832

833-
::: info
834-
Ideally, we would import from `vue-router/auto`. This should be added by [unplugin-vue-router][uvr] but given the current state, we need to import from `unplugin-vue-router/data-loaders/...`.
835-
:::
836-
837833
### Interfaces
838834

839835
Defining a minimal set of information and options for Data Loaders is what enables external libraries to implement their own data loaders. They are meant to extend these interfaces to add more features that are specific to them. You can see a practical example with the [Pinia Colada](colada.md) implementation.

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@
9393
"require": "./dist/types.cjs",
9494
"import": "./dist/types.js"
9595
},
96+
"./data-loaders": {
97+
"types": {
98+
"require": "./dist/data-loaders/index.d.cts",
99+
"import": "./dist/data-loaders/index.d.ts"
100+
},
101+
"require": "./dist/data-loaders/index.cjs",
102+
"import": "./dist/data-loaders/index.js"
103+
},
96104
"./data-loaders/basic": {
97105
"types": {
98106
"require": "./dist/data-loaders/basic.d.cts",

playground/auto-imports.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
export {}
77
declare global {
88
const defineBasicLoader: typeof import('unplugin-vue-router/data-loaders/basic')['defineBasicLoader']
9-
const onBeforeRouteLeave: typeof import('vue-router/auto')['onBeforeRouteLeave']
10-
const onBeforeRouteUpdate: typeof import('vue-router/auto')['onBeforeRouteUpdate']
11-
const useRoute: typeof import('vue-router/auto')['useRoute']
12-
const useRouter: typeof import('vue-router/auto')['useRouter']
9+
const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave']
10+
const onBeforeRouteUpdate: typeof import('vue-router')['onBeforeRouteUpdate']
11+
const useRoute: typeof import('vue-router')['useRoute']
12+
const useRouter: typeof import('vue-router')['useRouter']
1313
}

playground/src/components/TestSetup.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { onBeforeRouteLeave } from 'vue-router/auto'
2+
import { onBeforeRouteLeave } from 'vue-router'
33
44
onBeforeRouteLeave((to) => {
55
if (to.name === '/[name]') {

playground/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createApp } from 'vue'
22
import App from './App.vue'
33
// FIXME:
4-
import { DataLoaderPlugin } from 'vue-router/auto'
4+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
55
// import { DataLoaderPlugin } from 'unplugin-vue-router/runtime'
66
import { MutationCache, QueryCache, VueQueryPlugin } from '@tanstack/vue-query'
77
import { createPinia } from 'pinia'

0 commit comments

Comments
 (0)