From 522cffa0a2d23f40b0a526c24f7db8e37b0b4db9 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 26 Jan 2025 13:51:05 +0100 Subject: [PATCH 1/6] chore: remove unused playground file --- .../pages/(test-group)/test-group-child(treated-as-static).vue | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 playground/src/pages/(test-group)/test-group-child(treated-as-static).vue diff --git a/playground/src/pages/(test-group)/test-group-child(treated-as-static).vue b/playground/src/pages/(test-group)/test-group-child(treated-as-static).vue deleted file mode 100644 index 7a9dff7e3..000000000 --- a/playground/src/pages/(test-group)/test-group-child(treated-as-static).vue +++ /dev/null @@ -1,3 +0,0 @@ - From 8c5ad57c2dd746fac3dea425879887de5f95220c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 26 Jan 2025 13:51:42 +0100 Subject: [PATCH 2/6] chore: playground group --- playground/src/pages/(test-group).vue | 15 +++++++++++++++ .../src/pages/(test-group)/test-group-child.vue | 11 ++++++++++- playground/src/pages/index.vue | 1 + playground/typed-router.d.ts | 4 ++-- 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 playground/src/pages/(test-group).vue diff --git a/playground/src/pages/(test-group).vue b/playground/src/pages/(test-group).vue new file mode 100644 index 000000000..233a27e8a --- /dev/null +++ b/playground/src/pages/(test-group).vue @@ -0,0 +1,15 @@ + + + diff --git a/playground/src/pages/(test-group)/test-group-child.vue b/playground/src/pages/(test-group)/test-group-child.vue index fa79d0f72..b61198c5b 100644 --- a/playground/src/pages/(test-group)/test-group-child.vue +++ b/playground/src/pages/(test-group)/test-group-child.vue @@ -1,3 +1,12 @@ + + diff --git a/playground/src/pages/index.vue b/playground/src/pages/index.vue index 236d8c3d6..03d7cd877 100644 --- a/playground/src/pages/index.vue +++ b/playground/src/pages/index.vue @@ -2,6 +2,7 @@ definePage({ meta: { title: 'hello', + n: 8, }, }) diff --git a/playground/typed-router.d.ts b/playground/typed-router.d.ts index 70354262a..1cce685e1 100644 --- a/playground/typed-router.d.ts +++ b/playground/typed-router.d.ts @@ -18,9 +18,9 @@ declare module 'vue-router/auto-routes' { * Route name map generated by unplugin-vue-router */ export interface RouteNamedMap { - 'home': RouteRecordInfo<'home', '/', Record, Record>, + '/(test-group)': RouteRecordInfo<'/(test-group)', '/', Record, Record>, '/(test-group)/test-group-child': RouteRecordInfo<'/(test-group)/test-group-child', '/test-group-child', Record, Record>, - '/(test-group)/test-group-child(treated-as-static)': RouteRecordInfo<'/(test-group)/test-group-child(treated-as-static)', '/test-group-child(treated-as-static)', Record, Record>, + 'home': RouteRecordInfo<'home', '/', Record, Record>, '/[name]': RouteRecordInfo<'/[name]', '/:name', { name: ParamValue }, { name: ParamValue }>, '/[...path]': RouteRecordInfo<'/[...path]', '/:path(.*)', { path: ParamValue }, { path: ParamValue }>, '/[...path]+': RouteRecordInfo<'/[...path]+', '/:path(.*)+', { path: ParamValueOneOrMore }, { path: ParamValueOneOrMore }>, From 0af47d21b43d63294dc1a22603d71e1626d3b5bc Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 26 Jan 2025 13:54:10 +0100 Subject: [PATCH 3/6] chore: playground tests hmr --- playground/src/pages/about.vue | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/playground/src/pages/about.vue b/playground/src/pages/about.vue index fc23e04ab..e0f4d8e2c 100644 --- a/playground/src/pages/about.vue +++ b/playground/src/pages/about.vue @@ -1,5 +1,24 @@ + + From b734d9a1883eef472ca38514f5d44f7f99e4f20b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 26 Jan 2025 13:56:28 +0100 Subject: [PATCH 4/6] feat: fix indent in generated js for auto-routes --- src/codegen/generateRouteRecords.ts | 44 +++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/codegen/generateRouteRecords.ts b/src/codegen/generateRouteRecords.ts index 2b064d3a1..a46180ee9 100644 --- a/src/codegen/generateRouteRecords.ts +++ b/src/codegen/generateRouteRecords.ts @@ -26,6 +26,25 @@ ${node ]` } + const definePageDataList: string[] = [] + + if (node.hasDefinePage) { + for (const [name, filePath] of node.value.components) { + const pageDataImport = `_definePage_${name}_${importsMap.size}` + definePageDataList.push(pageDataImport) + importsMap.addDefault( + // TODO: apply the language used in the sfc + `${filePath}?definePage&vue&lang.tsx`, + pageDataImport + ) + } + + // extra indent to add `_mergeRouteRecord()` + if (definePageDataList.length > 0) { + indent++ + } + } + const startIndent = ' '.repeat(indent * 2) const indentStr = ' '.repeat((indent + 1) * 2) @@ -75,25 +94,14 @@ ${indentStr}],` }${formatMeta(node, indentStr)} ${startIndent}}` - if (node.hasDefinePage) { - const definePageDataList: string[] = [] - for (const [name, filePath] of node.value.components) { - const pageDataImport = `_definePage_${name}_${importsMap.size}` - definePageDataList.push(pageDataImport) - importsMap.addDefault( - // TODO: apply the language used in the sfc - `${filePath}?definePage&vue&lang.tsx`, - pageDataImport - ) - } - - if (definePageDataList.length) { - importsMap.add('unplugin-vue-router/runtime', '_mergeRouteRecord') - return ` _mergeRouteRecord( + if (definePageDataList.length > 0) { + // remove one tab + const mergeCallIndent = startIndent.slice(2) + importsMap.add('unplugin-vue-router/runtime', '_mergeRouteRecord') + return `${mergeCallIndent}_mergeRouteRecord( ${routeRecord}, - ${definePageDataList.join(',\n')} - )` - } +${definePageDataList.map((s) => startIndent + s).join(',\n')} +${mergeCallIndent})` } return routeRecord From 170df1187fd488b8d4eceef4fa6895a54bc711dc Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 26 Jan 2025 14:11:08 +0100 Subject: [PATCH 5/6] feat: allow HMR Callback Close #503 --- client.d.ts | 6 +++++- playground/src/router.ts | 15 ++++++++++++++- src/core/context.ts | 19 ++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/client.d.ts b/client.d.ts index 01d734ef3..e6afb9f3b 100644 --- a/client.d.ts +++ b/client.d.ts @@ -9,6 +9,7 @@ declare module 'vue-router/auto-routes' { /** * Setups hot module replacement for routes. * @param router - The router instance + * @param hotUpdateCallback - Callback to be called after replacing the routes and before the navigation * @example * ```ts * import { createRouter, createWebHistory } from 'vue-router' @@ -22,7 +23,10 @@ declare module 'vue-router/auto-routes' { * } * ``` */ - export function handleHotUpdate(router: Router): void + export function handleHotUpdate( + router: Router, + hotUpdateCallback?: (newRoutes: RouteRecordRaw[]) => void + ): void } declare module 'vue-router' { diff --git a/playground/src/router.ts b/playground/src/router.ts index 30423e82b..69e44b1c9 100644 --- a/playground/src/router.ts +++ b/playground/src/router.ts @@ -7,8 +7,21 @@ export const router = createRouter({ routes, }) +function addRedirects() { + router.addRoute({ + path: '/new-about', + redirect: '/about?from=hoho', + }) +} + if (import.meta.hot) { - handleHotUpdate(router) + handleHotUpdate(router, (routes) => { + console.log('🔥 HMR with', routes) + addRedirects() + }) +} else { + // production + addRedirects() } // manual extension of route types diff --git a/src/core/context.ts b/src/core/context.ts index 50798b577..5d9de314e 100644 --- a/src/core/context.ts +++ b/src/core/context.ts @@ -19,6 +19,7 @@ import { generateVueRouterProxy as _generateVueRouterProxy } from '../codegen/vu import { definePageTransform, extractDefinePageNameAndPath } from './definePage' import { EditableTreeNode } from './extendRoutes' import { isPackageExists as isPackageInstalled } from 'local-pkg' +import { ts } from '../utils' export function createRoutesContext(options: ResolvedOptions) { const { dts: preferDTS, root, routesFolder } = options @@ -186,10 +187,11 @@ export function createRoutesContext(options: ResolvedOptions) { importsMap )}\n` - let hmr = ` -export function handleHotUpdate(_router) { + let hmr = ts` +export function handleHotUpdate(_router, _hotUpdateCallback) { if (import.meta.hot) { import.meta.hot.data.router = _router + import.meta.hot.data.router_hotUpdateCallback = _hotUpdateCallback } } @@ -204,7 +206,18 @@ if (import.meta.hot) { for (const route of mod.routes) { router.addRoute(route) } - router.replace('') + // call the hotUpdateCallback for custom updates + import.meta.hot.data.router_hotUpdateCallback?.(mod.routes) + const route = router.currentRoute.value + router.replace({ + ...route, + // NOTE: we should be able to just do ...route but the router + // currently skips resolving and can give errors with renamed routes + // so we explicitly set remove matched and name + name: undefined, + matched: undefined, + force: true + }) }) } ` From 9da054060b6e451bf3137141afa685d43e5fdede Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 26 Jan 2025 14:13:56 +0100 Subject: [PATCH 6/6] release: unplugin-vue-router@0.11.2 --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1712a12..f91560a20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.11.2](https://github.com/posva/unplugin-vue-router/compare/v0.11.1...v0.11.2) (2025-01-26) + +### Features + +- allow HMR Callback ([170df11](https://github.com/posva/unplugin-vue-router/commit/170df1187fd488b8d4eceef4fa6895a54bc711dc)), closes [#503](https://github.com/posva/unplugin-vue-router/issues/503) +- fix indent in generated js for auto-routes ([b734d9a](https://github.com/posva/unplugin-vue-router/commit/b734d9a1883eef472ca38514f5d44f7f99e4f20b)) + ## [0.11.1](https://github.com/posva/unplugin-vue-router/compare/v0.11.0...v0.11.1) (2025-01-21) ### Bug Fixes diff --git a/package.json b/package.json index 0391d5340..f723183f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unplugin-vue-router", - "version": "0.11.1", + "version": "0.11.2", "type": "module", "packageManager": "pnpm@9.15.3", "description": "File based typed routing for Vue Router",