diff --git a/.changeset/router-plugin-vite-hot-accept.md b/.changeset/router-plugin-vite-hot-accept.md new file mode 100644 index 00000000000..014dc955401 --- /dev/null +++ b/.changeset/router-plugin-vite-hot-accept.md @@ -0,0 +1,5 @@ +--- +'@tanstack/router-plugin': patch +--- + +Vite route HMR now writes `import.meta.hot.accept(` out instead of calling it through a `hot` alias. Vite only treats a module as a hot boundary when the call is spelled out, so an edit of a route file no framework plugin accepts (a `.ts` route file, or an app without `@vitejs/plugin-react`) reloaded the page instead of updating the route in place. With React HMR, a route file that declares its own top-level `hot` binding no longer fails to compile with `Duplicate declaration "hot"`. diff --git a/packages/router-plugin/src/core/code-splitter/plugins/react-refresh-ignored-route-exports.ts b/packages/router-plugin/src/core/code-splitter/plugins/react-refresh-ignored-route-exports.ts index cc2be62034f..f951d863b32 100644 --- a/packages/router-plugin/src/core/code-splitter/plugins/react-refresh-ignored-route-exports.ts +++ b/packages/router-plugin/src/core/code-splitter/plugins/react-refresh-ignored-route-exports.ts @@ -5,9 +5,8 @@ import type { ReferenceRouteCompilerPlugin } from '../plugins' const buildReactRefreshIgnoredRouteExportsStatements = template.statements( ` -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports diff --git a/packages/router-plugin/src/core/hmr/vite-adapter.ts b/packages/router-plugin/src/core/hmr/vite-adapter.ts index ba8d42a80ad..5357c45acfa 100644 --- a/packages/router-plugin/src/core/hmr/vite-adapter.ts +++ b/packages/router-plugin/src/core/hmr/vite-adapter.ts @@ -4,7 +4,9 @@ import type * as t from '@babel/types' /** * Emits HMR accept code for Vite / native ESM HMR: `import.meta.hot.accept` - * with a callback that receives the freshly re-imported module. + * with a callback that receives the freshly re-imported module. Vite makes the + * module a hot boundary only when the code spells `import.meta.hot.accept(` + * out, so the call must not go through an alias. * * `targetFramework` is currently unused — Vite's framework-specific fast-refresh * plugins handle component body patching via their own accept boundaries — but @@ -26,8 +28,7 @@ export function createViteHmrStatement( template.statement( ` if (import.meta.hot) { - const hot = import.meta.hot - const hotData = hot.data ??= {} + const hotData = import.meta.hot.data ??= {} const handleRouteUpdate = ${handleRouteUpdateCode} const initialRouteId = ${routeIdFallback} ?? hotData['tsr-route-id'] if (initialRouteId) { @@ -41,7 +42,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? ${routeIdFallback} if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr.test.ts b/packages/router-plugin/tests/add-hmr.test.ts index 810f8e9b8d1..0d89832f3b0 100644 --- a/packages/router-plugin/tests/add-hmr.test.ts +++ b/packages/router-plugin/tests/add-hmr.test.ts @@ -1,5 +1,13 @@ -import { readFile, readdir } from 'node:fs/promises' +import { + mkdir, + mkdtemp, + readFile, + readdir, + rm, + writeFile, +} from 'node:fs/promises' import path from 'node:path' +import { createServer } from 'vite' import { describe, expect, it } from 'vitest' import { compileCodeSplitReferenceRoute } from '../src/core/code-splitter/compilers' @@ -196,3 +204,174 @@ describe('add-hmr works', () => { expect(output).not.toContain('/__root') }) }) + +/** + * Starts a Vite dev server with the router plugin (and no framework plugin) + * for an app made of `files`, connects an HMR client to it the way the + * browser's `/@vite/client` does, and requests the app's modules from + * `main.ts` on, as a page would. + */ +async function startDevApp( + files: Record, + options: { autoCodeSplitting: boolean }, +) { + // Keep the app inside the package so runtime imports resolve. + const root = await mkdtemp(path.join(__dirname, '.add-hmr-')) + await mkdir(path.join(root, 'routes')) + const allFiles: Record = { + 'index.html': '', + 'main.ts': `import { createRouter } from '@tanstack/react-router' +import { routeTree } from './routeTree.gen' +export const router = createRouter({ routeTree }) +`, + 'routes/__root.tsx': `import { createRootRoute } from '@tanstack/react-router' +export const Route = createRootRoute({}) +`, + ...files, + } + for (const [file, code] of Object.entries(allFiles)) { + await writeFile(path.join(root, file), code) + } + const { tanstackRouter } = await import('../src/vite') + const server = await createServer({ + root, + configFile: false, + logLevel: 'silent', + server: { port: 0, strictPort: false }, + // A dependency optimisation would reload the page on its own. + optimizeDeps: { noDiscovery: true, include: [] }, + plugins: [ + tanstackRouter({ + target: 'react', + routesDirectory: './routes', + generatedRouteTree: './routeTree.gen.ts', + autoCodeSplitting: options.autoCodeSplitting, + }), + ], + }) + const close = async () => { + await server.close() + await rm(root, { recursive: true, force: true }) + } + try { + await server.listen() + const origin = server.resolvedUrls!.local[0]!.replace(/\/$/, '') + const messages: Array<{ type: string }> = [] + const socket = new WebSocket( + `${origin.replace('http', 'ws')}/?token=${server.config.webSocketToken}`, + 'vite-hmr', + ) + socket.addEventListener('message', (event) => { + messages.push(JSON.parse(String(event.data))) + }) + await new Promise((resolve, reject) => { + socket.addEventListener('open', resolve) + socket.addEventListener('error', reject) + }) + const statuses: Record = {} + const served: Record = {} + const queue = ['/main.ts'] + while (queue.length > 0) { + const url = queue.shift()! + if (url in statuses) { + continue + } + const response = await fetch(origin + url) + statuses[url] = response.status + const code = await response.text() + served[url] = code + // Static imports of the app's own modules + for (const [, imported] of code.matchAll( + /^import\s[^'"]*?["'](\/(?!@|node_modules)[^"'?]+)["']/gm, + )) { + queue.push(imported!) + } + } + return { + statuses, + served, + /** Writes `code` to `file`; resolves with Vite's first reaction. */ + async edit(file: string, code: string) { + const seen = messages.length + await writeFile(path.join(root, file), code) + const deadline = Date.now() + 10_000 + while (Date.now() < deadline) { + const reaction = messages + .slice(seen) + .find( + (message) => + message.type === 'update' || message.type === 'full-reload', + ) + if (reaction) { + return reaction.type + } + await new Promise((resolve) => setTimeout(resolve, 50)) + } + return 'nothing' + }, + async close() { + socket.close() + await close() + }, + } + } catch (error) { + await close() + throw error + } +} + +describe('route HMR in a Vite dev server', () => { + // Vite makes a module a hot boundary only when its code spells + // `import.meta.hot.accept(` out. Route modules no framework plugin accepts + // (a `.ts` route file, or an app without `@vitejs/plugin-react`) reloaded + // the page on every edit. + it.each([ + { + name: 'a .ts route file', + file: 'routes/index.ts', + code: `import { createFileRoute } from '@tanstack/react-router' +export const Route = createFileRoute('/')({ loader: () => 'data v1' }) +`, + autoCodeSplitting: true, + }, + { + name: 'a route component, without a framework plugin', + file: 'routes/index.tsx', + code: `import { createFileRoute } from '@tanstack/react-router' +export const Route = createFileRoute('/')({ component: () =>

page v1

}) +`, + autoCodeSplitting: false, + }, + ])( + 'an edit of $name is a hot update, not a page reload', + async ({ file, code, autoCodeSplitting }) => { + const app = await startDevApp({ [file]: code }, { autoCodeSplitting }) + try { + expect(app.statuses[`/${file}`]).toBe(200) + expect(await app.edit(file, code.replace('v1', 'v2'))).toBe('update') + } finally { + await app.close() + } + }, + 30_000, + ) + + it('serves a route file with a binding named hot', async () => { + const app = await startDevApp( + { + 'routes/index.tsx': `import { createFileRoute } from '@tanstack/react-router' +const hot = 'hot' +export const Route = createFileRoute('/')({ component: () =>

{hot}

}) +`, + }, + // Unsplit, so the binding stays in the module the HMR code goes into + { autoCodeSplitting: false }, + ) + try { + expect(app.statuses['/routes/index.tsx']).toBe(200) + expect(app.served['/routes/index.tsx']).toMatch(/const hot = ["']hot["']/) + } finally { + await app.close() + } + }, 30_000) +}) diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/arrow-function@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/arrow-function@true.tsx index c771483895a..e45f8b634a4 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/arrow-function@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/arrow-function@true.tsx @@ -20,9 +20,8 @@ export const Route = createFileRoute('/posts')({ loader: fetchPosts, component: TSRSplitComponent, }) -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -39,8 +38,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -101,7 +99,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/createFileRoute-lowercase-components@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/createFileRoute-lowercase-components@true.tsx index bf94d5d3fd2..441852d7621 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/createFileRoute-lowercase-components@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/createFileRoute-lowercase-components@true.tsx @@ -36,9 +36,8 @@ export const Route = createFileRoute('/lowercase-components')({ function TSRPendingComponent() { return
Pending
} -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -58,8 +57,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -120,7 +118,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-inline-component@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-inline-component@true.tsx index f12187ccd1a..f45186bdd98 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-inline-component@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-inline-component@true.tsx @@ -2,9 +2,8 @@ import * as React from 'react' import { createRootRoute } from '@tanstack/react-router' const TSRComponent = () =>
root hmr
export const Route = createRootRoute({ component: TSRComponent }) -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -24,8 +23,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -86,7 +84,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-lowercase-components@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-lowercase-components@true.tsx index 9f8df0d43f5..f9a6875fe7c 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-lowercase-components@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-lowercase-components@true.tsx @@ -18,9 +18,8 @@ function TSRErrorComponent() { function TSRComponent() { return
Hello
} -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -40,8 +39,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -102,7 +100,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute@true.tsx index 859380ef535..3bde5adf5c2 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute@true.tsx @@ -4,9 +4,8 @@ export const Route = createRootRoute({ component: RootComponent }) function RootComponent() { return
Hello
} -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -23,8 +22,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -85,7 +83,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRouteWithContext-type-args@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRouteWithContext-type-args@true.tsx index 9d942f65a2b..c96d6f01a58 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRouteWithContext-type-args@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/createRootRouteWithContext-type-args@true.tsx @@ -9,9 +9,8 @@ export const Route = createRootRouteWithContext()({ function RootComponent() { return
Hello
} -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -31,8 +30,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -93,7 +91,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/explicit-undefined-component@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/explicit-undefined-component@true.tsx index 555c8fdd6cd..99674604faf 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/explicit-undefined-component@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/explicit-undefined-component@true.tsx @@ -1,9 +1,8 @@ import React from 'react' import { createFileRoute } from '@tanstack/react-router' export const Route = createFileRoute('/')({ component: undefined }) -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -22,8 +21,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -84,7 +82,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/function-declaration@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/function-declaration@true.tsx index 9e27af7d699..614c38c954c 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/function-declaration@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/function-declaration@true.tsx @@ -20,9 +20,8 @@ export const Route = createFileRoute('/posts')({ loader: fetchPosts, component: TSRSplitComponent, }) -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -39,8 +38,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -101,7 +99,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/multi-component@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/multi-component@true.tsx index 1a134b88da2..871bf352e01 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/multi-component@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/multi-component@true.tsx @@ -35,9 +35,8 @@ export const Route = createFileRoute('/posts')({ component: TSRSplitComponent, errorComponent: TSRSplitErrorComponent, }) -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -54,8 +53,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -116,7 +114,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/react/string-literal-keys@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/react/string-literal-keys@true.tsx index b3bc82d69ed..6d5b2a1fd31 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/react/string-literal-keys@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/react/string-literal-keys@true.tsx @@ -35,9 +35,8 @@ export const Route = createFileRoute('/posts')({ component: TSRSplitComponent, errorComponent: TSRSplitErrorComponent, }) -const hot = import.meta.hot -if (hot && typeof window !== 'undefined') { - hot.data ??= {} +if (import.meta.hot && typeof window !== 'undefined') { + import.meta.hot.data ??= {} const tsrReactRefresh = (window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports @@ -54,8 +53,7 @@ export function TSRFastRefreshAnchor() { return null } if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -116,7 +114,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/add-hmr/snapshots/solid/arrow-function@true.tsx b/packages/router-plugin/tests/add-hmr/snapshots/solid/arrow-function@true.tsx index 05fa745c4b5..bc531f9000e 100644 --- a/packages/router-plugin/tests/add-hmr/snapshots/solid/arrow-function@true.tsx +++ b/packages/router-plugin/tests/add-hmr/snapshots/solid/arrow-function@true.tsx @@ -8,8 +8,7 @@ export const Route = createFileRoute('/posts')({ component: lazyRouteComponent($$splitComponentImporter, 'component'), }) if (import.meta.hot) { - const hot = import.meta.hot - const hotData = (hot.data ??= {}) + const hotData = (import.meta.hot.data ??= {}) const handleRouteUpdate = function handleRouteUpdate(routeId, newRoute) { const router = window.__TSR_ROUTER__ const oldRoute = router.routesById[routeId] @@ -64,7 +63,7 @@ if (import.meta.hot) { handleRouteUpdate(initialRouteId, Route) hotData['tsr-route-update-handled'] = Route } - hot.accept((newModule) => { + import.meta.hot.accept((newModule) => { if (Route && newModule && newModule.Route) { const routeId = hotData['tsr-route-id'] ?? Route.id if (routeId) { diff --git a/packages/router-plugin/tests/router-plugin-context.test.ts b/packages/router-plugin/tests/router-plugin-context.test.ts index d3fb0563e60..b03e5f6701c 100644 --- a/packages/router-plugin/tests/router-plugin-context.test.ts +++ b/packages/router-plugin/tests/router-plugin-context.test.ts @@ -77,16 +77,9 @@ function getCode(result: TransformResult | null | undefined) { return result.code } -function countProgramHotDeclarations(code: string) { - const ast = parseAst({ code }) - return ast.program.body.filter((statement) => { - return ( - t.isVariableDeclaration(statement) && - statement.declarations.some((declaration) => { - return t.isIdentifier(declaration.id) && declaration.id.name === 'hot' - }) - ) - }).length +/** How many times the route module accepts its own hot updates. */ +function countHotAccepts(code: string) { + return code.split('import.meta.hot.accept(').length - 1 } describe('router plugin context', () => { @@ -367,7 +360,7 @@ export const Route = createFileRoute('/owned')({ const firstCode = getCode(firstResult) expect(firstCode).not.toBeNull() - expect(countProgramHotDeclarations(firstCode!)).toBe(1) + expect(countHotAccepts(firstCode!)).toBe(1) const secondResult = await transformReferenceRoute( splitterB,