diff --git a/.changeset/router-plugin-split-source-maps.md b/.changeset/router-plugin-split-source-maps.md new file mode 100644 index 00000000000..82a0a6faaf1 --- /dev/null +++ b/.changeset/router-plugin-split-source-maps.md @@ -0,0 +1,5 @@ +--- +'@tanstack/router-plugin': patch +--- + +Source maps of split routes now point at the route file. On webpack and Rspack (including Rsbuild), a split component or shared code mapped to the wrong line, because unplugin's loader dropped the router plugin's map; `unplugin` now requires `^3.5.0`, which keeps it. The maps of split and shared modules also name the route file itself instead of its `?tsr-split` / `?tsr-shared` module id, so debuggers bind breakpoints set in the route file. diff --git a/packages/router-plugin/package.json b/packages/router-plugin/package.json index 5f72aeb8e2b..c57562a33d8 100644 --- a/packages/router-plugin/package.json +++ b/packages/router-plugin/package.json @@ -124,7 +124,7 @@ "@tanstack/router-generator": "workspace:*", "@tanstack/router-utils": "workspace:*", "chokidar": "catalog:", - "unplugin": "^3.3.0", + "unplugin": "^3.5.0", "zod": "catalog:" }, "devDependencies": { diff --git a/packages/router-plugin/src/core/code-splitter/compilers.ts b/packages/router-plugin/src/core/code-splitter/compilers.ts index 89cdae41add..0a6b31cf5bb 100644 --- a/packages/router-plugin/src/core/code-splitter/compilers.ts +++ b/packages/router-plugin/src/core/code-splitter/compilers.ts @@ -1285,7 +1285,8 @@ export function compileCodeSplitVirtualRoute( const result = generateFromAst(ast, { sourceMaps: true, - sourceFileName: opts.filename, + // Name the route file, not this module's `?tsr-*` id. + sourceFileName: removeSplitSearchParamFromFilename(opts.filename), filename: opts.filename, }) @@ -1376,7 +1377,8 @@ export function compileCodeSplitSharedRoute( const result = generateFromAst(ast, { sourceMaps: true, - sourceFileName: opts.filename, + // Name the route file, not this module's `?tsr-*` id. + sourceFileName: removeSplitSearchParamFromFilename(opts.filename), filename: opts.filename, }) diff --git a/packages/router-plugin/tests/source-maps.test.ts b/packages/router-plugin/tests/source-maps.test.ts new file mode 100644 index 00000000000..3217dd9aa33 --- /dev/null +++ b/packages/router-plugin/tests/source-maps.test.ts @@ -0,0 +1,322 @@ +/** + * Source maps of split routes point at the route file the user wrote: a throw + * in a split component, or in code the route module and a split chunk share, + * maps to its line in the route file, in real webpack and Rspack builds and + * in Vite's dev server, and the map names the route file itself, not the + * `?tsr-split` / `?tsr-shared` module id. Issues #3828 and #6667. + */ +import { + mkdir, + mkdtemp, + readFile, + readdir, + rm, + writeFile, +} from 'node:fs/promises' +import { SourceMap } from 'node:module' +import path from 'node:path' +import { rspack } from '@rsbuild/core' +import { createServer } from 'vite' +import webpack from 'webpack' +import { describe, expect, test } from 'vitest' +import type { SourceMapPayload } from 'node:module' + +const timeout = 60_000 + +/** + * A route whose split component throws, and whose loader and component share + * a function that throws, each on its own line. + */ +function routeSource({ types }: { types: boolean }) { + const never = types ? ': never' : '' + return `import { createFileRoute } from '@tanstack/react-router' + +function fail()${never} { + throw new Error('MARK_SHARED_THROW') +} + +function Page()${never} { + throw new Error('MARK_PAGE_THROW ' + typeof fail) +} + +export const Route = createFileRoute('/')({ + loader: () => fail(), + component: Page, +}) +` +} + +const rootSource = `import { createRootRoute } from '@tanstack/react-router' +export const Route = createRootRoute({}) +` + +/** The 1-based line of `marker` in `code`. */ +function lineOf(code: string, marker: string) { + return code.slice(0, code.indexOf(marker)).split('\n').length +} + +/** + * Where `code`'s source map sends the opening quote of the string `marker`: + * the source as `source` gives it, and the 1-based line. + */ +function originalPositionOf( + code: string, + map: SourceMapPayload, + marker: string, + source: (name: string) => string, +) { + const lines = code.slice(0, code.indexOf(marker) - 1).split('\n') + const entry = new SourceMap(map).findEntry( + lines.length - 1, + lines.at(-1)!.length, + ) + if (!('originalSource' in entry)) { + return undefined + } + return { + source: source(entry.originalSource), + line: entry.originalLine + 1, + } +} + +/** Writes `files` into a new app directory inside the package. */ +async function createApp(files: Record) { + // Keep the app inside the package so runtime imports resolve. + const root = await mkdtemp(path.join(__dirname, '.source-maps-')) + await mkdir(path.join(root, 'routes')) + for (const [file, code] of Object.entries(files)) { + await writeFile(path.join(root, file), code) + } + return root +} + +function routerOptions(root: string) { + return { + target: 'react', + routesDirectory: path.join(root, 'routes'), + generatedRouteTree: path.join(root, 'routeTree.gen.ts'), + autoCodeSplitting: true, + codeSplittingOptions: { addHmr: false }, + } as const +} + +/** A client production build of `entry`, unminified, with source maps. */ +function compilerConfig(root: string, entry: string) { + return { + context: root, + mode: 'production', + target: 'web', + devtool: 'source-map', + entry: `./${entry}`, + output: { path: path.join(root, 'dist') }, + optimization: { minimize: false }, + } as const +} + +interface Compiler { + run: ( + callback: ( + error: Error | null, + stats?: { + hasErrors: () => boolean + toString: (options: object) => string + }, + ) => void, + ) => void + close: (callback: (error?: Error | null) => void) => void +} + +/** Runs one build and settles once the compiler has closed. */ +function runCompiler(compiler: Compiler) { + return new Promise((resolve, reject) => { + compiler.run((error, stats) => { + // The test removes the app afterwards: wait for the compiler to finish. + compiler.close((closeError) => { + if (error ?? closeError) { + reject(error ?? closeError) + } else if (stats?.hasErrors()) { + reject(new Error(stats.toString({ all: false, errors: true }))) + } else { + resolve() + } + }) + }) + }) +} + +const bundlers = [ + { + name: 'webpack', + // webpack parses no TypeScript: the router plugin is the only loader. + extension: 'js', + async build(root: string, entry: string) { + const { tanstackRouter } = await import('../src/webpack') + await runCompiler( + webpack({ + ...compilerConfig(root, entry), + // The test app is not part of the package's TypeScript project. + resolve: { tsconfig: false }, + plugins: [tanstackRouter(routerOptions(root))], + }), + ) + }, + }, + { + name: 'rspack', + // As in Rsbuild: SWC compiles the router plugin's output. + extension: 'tsx', + async build(root: string, entry: string) { + const { tanstackRouter } = await import('../src/rspack') + await runCompiler( + rspack({ + ...compilerConfig(root, entry), + module: { + rules: [ + { + test: /\.tsx?$/, + loader: 'builtin:swc-loader', + options: { + jsc: { parser: { syntax: 'typescript', tsx: true } }, + }, + type: 'javascript/auto', + }, + ], + }, + plugins: [tanstackRouter(routerOptions(root))], + }), + ) + }, + }, +] as const + +describe.each(bundlers)('$name build', (bundler) => { + test( + 'a throw in a split component, and in code it shares, maps to its line in the route file', + async () => { + const route = routeSource({ types: bundler.extension === 'tsx' }) + const routeFile = `routes/index.${bundler.extension}` + const entry = `entry.${bundler.extension}` + const root = await createApp({ + [`routes/__root.${bundler.extension}`]: rootSource, + [routeFile]: route, + [entry]: `import { Route } from './${routeFile}'\nglobalThis.route = Route\n`, + }) + try { + await bundler.build(root, entry) + const dist = path.join(root, 'dist') + const chunks = new Map() + for (const file of await readdir(dist)) { + if (file.endsWith('.js')) { + chunks.set(file, await readFile(path.join(dist, file), 'utf8')) + } + } + for (const marker of ['MARK_PAGE_THROW', 'MARK_SHARED_THROW']) { + const [file, code] = + [...chunks].find(([, chunk]) => chunk.includes(marker)) ?? [] + expect(file, `the chunk holding ${marker}`).toBeDefined() + if (marker === 'MARK_PAGE_THROW') { + expect(file, 'the component is split').not.toBe('main.js') + } + const map = JSON.parse( + await readFile(path.join(dist, `${file}.map`), 'utf8'), + ) as SourceMapPayload + expect( + originalPositionOf(code!, map, marker, (source) => + source.replace(/^webpack:\/\/[^/]*\/\.\//, ''), + ), + `${marker} in ${file}`, + ).toEqual({ source: routeFile, line: lineOf(route, marker) }) + } + } finally { + await rm(root, { recursive: true, force: true }) + } + }, + timeout, + ) +}) + +function inlineSourceMap(code: string) { + const data = + /\/\/# sourceMappingURL=data:application\/json;(?:charset=utf-8;)?base64,(\S+)\s*$/.exec( + code, + )?.[1] + expect(data, 'the module has an inline source map').toBeDefined() + return JSON.parse( + Buffer.from(data!, 'base64').toString('utf8'), + ) as SourceMapPayload +} + +/** + * The route module served at `routeUrl` and every module derived from it + * (imports of the route file's path with a query), by URL. + */ +async function routeModules(routeUrl: URL) { + const modules = new Map() + const pending = [routeUrl.href] + while (pending.length > 0) { + const url = pending.pop()! + if (modules.has(url)) { + continue + } + const code = await (await fetch(url)).text() + modules.set(url, code) + for (const [, specifier] of code.matchAll(/["']([^"'\s]+)["']/g)) { + const derived = new URL(specifier!, url) + if (derived.pathname === routeUrl.pathname && derived.search) { + pending.push(derived.href) + } + } + } + return modules +} + +describe('vite dev', () => { + test( + 'the modules of a split route map a throw to its line in the route file, which their maps name as their source', + async () => { + const route = routeSource({ types: true }) + const root = await createApp({ + 'routes/__root.tsx': rootSource, + 'routes/index.tsx': route, + }) + const { tanstackRouter } = await import('../src/vite') + const server = await createServer({ + root, + configFile: false, + logLevel: 'silent', + plugins: [tanstackRouter(routerOptions(root))], + optimizeDeps: { noDiscovery: true, include: [] }, + server: { port: 0, hmr: false, ws: false }, + }) + try { + await server.listen() + const routeUrl = new URL( + '/routes/index.tsx', + server.resolvedUrls!.local[0], + ) + const modules = await routeModules(routeUrl) + for (const marker of ['MARK_PAGE_THROW', 'MARK_SHARED_THROW']) { + const [url, code] = + [...modules].find(([, module]) => module.includes(marker)) ?? [] + expect(url, `the module holding ${marker}`).toBeDefined() + if (marker === 'MARK_PAGE_THROW') { + expect(url, 'the component is split').not.toBe(routeUrl.href) + } + expect( + originalPositionOf( + code!, + inlineSourceMap(code!), + marker, + (source) => new URL(source, url).href, + ), + `${marker} in ${url}`, + ).toEqual({ source: routeUrl.href, line: lineOf(route, marker) }) + } + } finally { + await server.close() + await rm(root, { recursive: true, force: true }) + } + }, + timeout, + ) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b20d885861f..d384daac508 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14748,8 +14748,8 @@ importers: specifier: 'catalog:' version: 5.0.0 unplugin: - specifier: ^3.3.0 - version: 3.3.0(@rspack/core@2.2.7(@swc/helpers@0.5.23))(esbuild@0.28.2)(rolldown@1.2.13)(rollup@4.63.1)(vite@8.3.4(@types/node@25.0.9)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.105.1)(sass@1.97.2)(terser@5.51.2)(tsx@4.20.3)(yaml@2.9.0))(webpack@5.110.3) + specifier: ^3.5.0 + version: 3.7.0(@rsbuild/core@2.2.5)(@rspack/core@2.2.7(@swc/helpers@0.5.23))(esbuild@0.28.2)(rolldown@1.2.13)(rollup@4.63.1)(vite@8.3.4(@types/node@25.0.9)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.105.1)(sass@1.97.2)(terser@5.51.2)(tsx@4.20.3)(yaml@2.9.0))(webpack@5.110.3) vite: specifier: ^8.3.4 version: 8.3.4(@types/node@25.0.9)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.105.1)(sass@1.97.2)(terser@5.51.2)(tsx@4.20.3)(yaml@2.9.0) @@ -28640,11 +28640,12 @@ packages: resolution: {integrity: sha512-5uKD0nqiYVzlmCRs01Fhs2BdkEgBS3SAVP6ndrBsuK42iC2+JHyxM05Rm9G8+5mkmRtzMZGY8Ct5+mliZxU/Ww==} engines: {node: '>=18.12.0'} - unplugin@3.3.0: - resolution: {integrity: sha512-qa66K+crbfyE6JK10GjvbJeRrOsuC/JpbnHctfyp/i4oBTxWOzJfRZyDiOk1PtErMFRu8JhsU/wPvOdBNWe5Rg==} + unplugin@3.7.0: + resolution: {integrity: sha512-GrM2uq3DQo3hvLU2FjeYhooJ8BY9N6j1Fbzgx7Pta3VVJevsD3VP4dPjZkZOS0mLvN8w9zzr9OlP+31bSMyuLw==} engines: {node: ^20.19.0 || >=22.12.0} peerDependencies: '@farmfe/core': '*' + '@rsbuild/core': '*' '@rspack/core': '*' bun-types-no-globals: '*' esbuild: '*' @@ -28656,6 +28657,8 @@ packages: peerDependenciesMeta: '@farmfe/core': optional: true + '@rsbuild/core': + optional: true '@rspack/core': optional: true bun-types-no-globals: @@ -35894,7 +35897,7 @@ snapshots: '@typescript-eslint/utils': 8.57.1(@typescript/typescript6@6.0.2)(eslint@9.22.0(jiti@2.7.0)(supports-color@7.2.0))(supports-color@10.2.2) '@typescript-eslint/visitor-keys': 8.57.1 eslint: 9.22.0(jiti@2.7.0)(supports-color@7.2.0) - ignore: 7.0.5 + ignore: 7.0.12 natural-compare: 1.4.0 ts-api-utils: 2.4.0(@typescript/typescript6@6.0.2) typescript: '@typescript/typescript6@6.0.2' @@ -43635,12 +43638,14 @@ snapshots: picomatch: 4.0.7 webpack-virtual-modules: 0.6.2 - unplugin@3.3.0(@rspack/core@2.2.7(@swc/helpers@0.5.23))(esbuild@0.28.2)(rolldown@1.2.13)(rollup@4.63.1)(vite@8.3.4(@types/node@25.0.9)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.105.1)(sass@1.97.2)(terser@5.51.2)(tsx@4.20.3)(yaml@2.9.0))(webpack@5.110.3): + unplugin@3.7.0(@rsbuild/core@2.2.5)(@rspack/core@2.2.7(@swc/helpers@0.5.23))(esbuild@0.28.2)(rolldown@1.2.13)(rollup@4.63.1)(vite@8.3.4(@types/node@25.0.9)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.105.1)(sass@1.97.2)(terser@5.51.2)(tsx@4.20.3)(yaml@2.9.0))(webpack@5.110.3): dependencies: '@jridgewell/remapping': 2.3.5 + '@rolldown/pluginutils': 1.0.1 picomatch: 4.0.7 webpack-virtual-modules: 0.6.2 optionalDependencies: + '@rsbuild/core': 2.2.5 '@rspack/core': 2.2.7(@swc/helpers@0.5.23) esbuild: 0.28.2 rolldown: 1.2.13