Skip to content

Commit 559f751

Browse files
committed
refactor: fix local playground
1 parent dbadb46 commit 559f751

File tree

11 files changed

+53
-35
lines changed

11 files changed

+53
-35
lines changed

docs/guide/extending-routes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Note you can specify the language to use with `<route lang="yaml">`. By default,
8989

9090
## Extending routes at runtime
9191

92-
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.
92+
As an escape-hatch, it's possible to extend the routes **at runtime** by simply changing or cloning 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.
9393

9494
```js{4-9}
9595
import { createWebHistory, createRouter } from 'vue-router'

playground/auto-imports.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// Generated by unplugin-auto-import
66
export {}
77
declare global {
8-
const defineBasicLoader: typeof import('unplugin-vue-router/data-loaders/basic')['defineBasicLoader']
8+
const defineBasicLoader: typeof import('../src/data-loaders/entries/basic')['defineBasicLoader']
9+
const defineColadaLoader: typeof import('../src/data-loaders/entries/pinia-colada')['defineColadaLoader']
910
const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave']
1011
const onBeforeRouteUpdate: typeof import('vue-router')['onBeforeRouteUpdate']
1112
const useRoute: typeof import('vue-router')['useRoute']

playground/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createApp } from 'vue'
22
import App from './App.vue'
3-
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
43
import { MutationCache, QueryCache, VueQueryPlugin } from '@tanstack/vue-query'
54
import { createPinia } from 'pinia'
65
import { PiniaColada } from '@pinia/colada'
76
import { router } from './router'
7+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
88

99
const app = createApp(App)
1010

playground/src/pages/users/colada-loader.[id].vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts" setup>
22
import { simulateError, useUserData } from '@/loaders/colada-loaders'
3-
import { serialize } from '@pinia/colada'
3+
import { serializeTreeMap } from '@pinia/colada'
44
import { getActivePinia } from 'pinia'
55
66
definePage({
@@ -15,7 +15,7 @@ const pinia = getActivePinia()!
1515
function copy() {
1616
console.log(
1717
JSON.parse(
18-
JSON.stringify(serialize(pinia.state.value._pc_query.entryRegistry))
18+
JSON.stringify(serializeTreeMap(pinia.state.value._pc_query.entryRegistry))
1919
)
2020
)
2121
}

playground/tsconfig.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"./src/**/*.ts",
66
"./src/**/*.vue",
77
"./typed-router.d.ts",
8-
"./auto-imports.d.ts"
8+
"./auto-imports.d.ts",
9+
"../src"
910
],
1011
"compilerOptions": {
1112
"baseUrl": ".",
@@ -16,19 +17,19 @@
1617
"./src/*"
1718
],
1819
"unplugin-vue-router/runtime": [
19-
"../src/runtime"
20+
"../src/runtime.ts"
2021
],
2122
"unplugin-vue-router/types": [
22-
"../src/types"
23+
"../src/types.ts"
2324
],
2425
"unplugin-vue-router/data-loaders": [
25-
"../src/data-loaders/index"
26+
"../src/data-loaders/entries/index.ts"
2627
],
2728
"unplugin-vue-router/data-loaders/basic": [
28-
"../src/data-loaders/basic"
29+
"../src/data-loaders/entries/basic.ts"
2930
],
3031
"unplugin-vue-router/data-loaders/pinia-colada": [
31-
"../src/data-loaders/pinia-colada"
32+
"../src/data-loaders/entries/pinia-colada.ts"
3233
],
3334
},
3435
},

playground/vite.config.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ import VueDevtools from 'vite-plugin-vue-devtools'
1616

1717
export default defineConfig({
1818
clearScreen: false,
19+
resolve: {
20+
alias: {
21+
'@': fileURLToPath(new URL('./src', import.meta.url)),
22+
'~': fileURLToPath(new URL('./src', import.meta.url)),
23+
'unplugin-vue-router/runtime': fileURLToPath(
24+
new URL('../src/runtime.ts', import.meta.url)
25+
),
26+
'unplugin-vue-router/types': fileURLToPath(
27+
new URL('../src/types.ts', import.meta.url)
28+
),
29+
'unplugin-vue-router/data-loaders/basic': fileURLToPath(
30+
new URL('../src/data-loaders/entries/basic.ts', import.meta.url)
31+
),
32+
'unplugin-vue-router/data-loaders/pinia-colada': fileURLToPath(
33+
new URL('../src/data-loaders/entries/pinia-colada.ts', import.meta.url)
34+
),
35+
'unplugin-vue-router/data-loaders': fileURLToPath(
36+
new URL('../src/data-loaders/entries/index.ts', import.meta.url)
37+
),
38+
},
39+
},
1940
build: {
2041
sourcemap: true,
2142
},
@@ -130,23 +151,18 @@ export default defineConfig({
130151
imports: [
131152
VueRouterAutoImports,
132153
{
133-
'unplugin-vue-router/data-loaders/basic': ['defineBasicLoader'],
154+
// NOTE: we need to match the resolved paths to local files for development
155+
// instead of just 'unplugin-vue-router/data-loaders/basic': ['defineBasicLoader'],
156+
[fileURLToPath(
157+
new URL('../src/data-loaders/entries/basic.ts', import.meta.url)
158+
)]: ['defineBasicLoader'],
159+
// [fileURLToPath(
160+
// new URL('../src/data-loaders/entries/pinia-colada.ts', import.meta.url)
161+
// )]: ['defineColadaLoader'],
134162
},
135163
],
136164
}),
137165
VueDevtools(),
138166
Inspect(),
139167
],
140-
resolve: {
141-
alias: {
142-
'@': fileURLToPath(new URL('./src', import.meta.url)),
143-
'~': fileURLToPath(new URL('./src', import.meta.url)),
144-
'unplugin-vue-router/runtime': fileURLToPath(
145-
new URL('../src/runtime.ts', import.meta.url)
146-
),
147-
'unplugin-vue-router/types': fileURLToPath(
148-
new URL('../src/types.ts', import.meta.url)
149-
),
150-
},
151-
},
152168
})

src/data-loaders/defineColadaLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
UseDataLoader,
1515
UseDataLoaderResult,
1616
_DefineLoaderEntryMap,
17+
_PromiseMerged,
1718
} from 'unplugin-vue-router/runtime'
1819
import {
1920
ABORT_CONTROLLER_KEY,
@@ -24,7 +25,6 @@ import {
2425
PENDING_LOCATION_KEY,
2526
STAGED_NO_VALUE,
2627
NavigationResult,
27-
type _PromiseMerged,
2828
assign,
2929
getCurrentContext,
3030
isSubsetOf,

src/data-loaders/defineLoader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { App, defineComponent } from 'vue'
55
import {
6-
DefineDataLoaderOptions,
6+
type DefineDataLoaderOptions,
77
INITIAL_DATA_KEY,
88
SERVER_INITIAL_DATA_KEY,
99
defineBasicLoader,

src/data-loaders/defineLoader.test-d.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ describe('defineBasicLoader', () => {
4242
expectTypeOf<Ref<UserData | undefined>>(
4343
defineBasicLoader(loaderUser, { lazy: true })().data
4444
)
45-
})
46-
47-
it('removes undefined from non lazy loaders', () => {
48-
expectTypeOf<Ref<UserData>>(defineBasicLoader(loaderUser, {})().data)
49-
expectTypeOf<Ref<UserData>>(
50-
defineBasicLoader(loaderUser, { lazy: false })().data
45+
expectTypeOf<Ref<UserData | undefined>>(
46+
defineBasicLoader(loaderUser, { lazy: () => false })().data
5147
)
5248
})
5349

@@ -58,12 +54,12 @@ describe('defineBasicLoader', () => {
5854
expectTypeOf<Promise<UserData>>(defineBasicLoader(loaderUser, {})())
5955
})
6056

61-
expectTypeOf<{ data: Ref<UserData> }>(
57+
expectTypeOf<{ data: Ref<UserData | undefined> }>(
6258
defineBasicLoader(loaderUser, { lazy: false })()
6359
)
6460

6561
it('allows returning a Navigation Result without a type error', () => {
66-
expectTypeOf<{ data: Ref<UserData> }>(
62+
expectTypeOf<{ data: Ref<UserData | undefined> }>(
6763
defineBasicLoader(
6864
async () => {
6965
if (Math.random()) {

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
],
5151
"unplugin-vue-router/runtime": [
5252
"./src/runtime.ts"
53-
]
53+
],
54+
// "unplugin-vue-router/data-loaders": [
55+
// "./src/data-loaders/entries/index.ts"
56+
// ]
5457
},
5558
"types": [
5659
"node",

0 commit comments

Comments
 (0)