Repository navigation
fix(router-plugin, router-utils): preserve route exports under code splitting and keep re-export aliases apart #8541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hungateJoseph
wants to merge
4
commits into
TanStack:main
Choose a base branch
from
hungateJoseph:fix/code-split-exports-and-reexport-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+866
−93
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
09a92a6
fix(router-plugin, router-utils): preserve route exports under code s…
hungateJoseph e5f02a0
fix(router-utils): keep overload signatures with a function moved int…
hungateJoseph fabf56f
Merge remote-tracking branch 'origin/main' into fix/code-split-export…
hungateJoseph b890f65
fix(router-plugin): keep split code working when it refers to an expo…
hungateJoseph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@tanstack/router-utils': patch | ||
| '@tanstack/router-plugin': patch | ||
| --- | ||
|
|
||
| Keep route exports intact under automatic code splitting: exported classes and `export { name as alias }` specifiers are retained like other exported route options, and the overload signatures of a function moved into the shared module no longer stay behind to shadow its import. Re-exports of the same name from different sources, such as `export { wrap as allowed } from './server'` next to `export { wrap as denied } from './client'`, now resolve to their own sources in the Start compiler. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/router-plugin/tests/code-splitter-exports.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import { createServer } from 'vite' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { tanstackRouter } from '../src/vite' | ||
|
|
||
| // Loads a route file through the Vite dev pipeline with automatic code | ||
| // splitting on, then hands the module to the assertions while the server is | ||
| // still up, so lazy route options can import their split chunks. | ||
| async function withSplitRoute( | ||
| routeSource: string, | ||
| assert: (routeModule: Record<string, any>) => Promise<void> | void, | ||
| ) { | ||
| const root = await mkdtemp(path.join(__dirname, '.split-exports-')) | ||
| let server: Awaited<ReturnType<typeof createServer>> | undefined | ||
| try { | ||
| await mkdir(path.join(root, 'routes')) | ||
| await writeFile( | ||
| path.join(root, 'routes/__root.tsx'), | ||
| `import { createRootRoute } from '@tanstack/react-router' | ||
| export const Route = createRootRoute({})`, | ||
| ) | ||
| await writeFile(path.join(root, 'routes/page.tsx'), routeSource) | ||
| server = await createServer({ | ||
| root, | ||
| configFile: false, | ||
| logLevel: 'silent', | ||
| plugins: [ | ||
| tanstackRouter({ | ||
| target: 'react', | ||
| autoCodeSplitting: true, | ||
| codeSplittingOptions: { | ||
| defaultBehavior: [['component'], ['loader']], | ||
| }, | ||
| routesDirectory: './routes', | ||
| generatedRouteTree: './routeTree.gen.ts', | ||
| }), | ||
| ], | ||
| server: { middlewareMode: true }, | ||
| appType: 'custom', | ||
| }) | ||
| await assert(await server.ssrLoadModule('/routes/page.tsx')) | ||
| } finally { | ||
| await server?.close() | ||
| await rm(root, { recursive: true, force: true }) | ||
| } | ||
| } | ||
|
|
||
| describe('route exports under automatic code splitting', () => { | ||
| it('keeps a shared function with overload signatures importable and aliased', async () => { | ||
| await withSplitRoute( | ||
| ` | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| export function helper(value: string): string | ||
| export function helper(value: number): number | ||
| export function helper(value: string | number) { return value } | ||
| export { helper as helperAlias } | ||
| export const Route = createFileRoute('/page')({ | ||
| component: () => null, | ||
| beforeLoad: () => helper('before'), | ||
| loader: () => helper(42), | ||
| })`, | ||
| async (routeModule) => { | ||
| expect(routeModule.helper).toBeTypeOf('function') | ||
| expect(routeModule.helperAlias).toBe(routeModule.helper) | ||
| expect(routeModule.Route.options.beforeLoad()).toBe('before') | ||
| expect(await routeModule.Route.options.loader()).toBe(42) | ||
| }, | ||
| ) | ||
| }, 30_000) | ||
|
|
||
| it('retains an exported class component', async () => { | ||
| await withSplitRoute( | ||
| ` | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| export class Layout { render() { return null } } | ||
| export const Route = createFileRoute('/page')({ | ||
| component: Layout, | ||
| })`, | ||
| async (routeModule) => { | ||
| expect(routeModule.Layout).toBeTypeOf('function') | ||
| expect(routeModule.Route.options.component).toBe(routeModule.Layout) | ||
| }, | ||
| ) | ||
| }, 30_000) | ||
|
|
||
| it('retains route options exported under an alias', async () => { | ||
| await withSplitRoute( | ||
| ` | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| function Layout() { return null } | ||
| function loadLayout() { return 'loaded' } | ||
| export { Layout as View, loadLayout as load } | ||
| export const Route = createFileRoute('/page')({ | ||
| component: Layout, | ||
| loader: loadLayout, | ||
| })`, | ||
| async (routeModule) => { | ||
| expect(routeModule.View).toBeTypeOf('function') | ||
| expect(routeModule.Route.options.component).toBe(routeModule.View) | ||
| expect(routeModule.load).toBeTypeOf('function') | ||
| expect(routeModule.Route.options.loader).toBe(routeModule.load) | ||
| expect(routeModule.load()).toBe('loaded') | ||
| }, | ||
| ) | ||
| }, 30_000) | ||
| }) |
17 changes: 17 additions & 0 deletions
17
...ages/router-plugin/tests/code-splitter/snapshots/react/1-default/retain-exports-alias.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import * as React from 'react' | ||
| import { createFileRoute, Outlet } from '@tanstack/react-router' | ||
| function Layout() { | ||
| return ( | ||
| <main> | ||
| <Outlet /> | ||
| </main> | ||
| ) | ||
| } | ||
| function loadLayout() { | ||
| return { title: 'layout' } | ||
| } | ||
| export { Layout as View, loadLayout as load } | ||
| export const Route = createFileRoute('/_layout')({ | ||
| component: Layout, | ||
| loader: loadLayout, | ||
| }) |
1 change: 1 addition & 0 deletions
1
...r-plugin/tests/code-splitter/snapshots/react/1-default/retain-exports-alias@component.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import * as React from 'react' |
1 change: 1 addition & 0 deletions
1
...gin/tests/code-splitter/snapshots/react/1-default/retain-exports-alias@errorComponent.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import * as React from 'react' |
1 change: 1 addition & 0 deletions
1
.../tests/code-splitter/snapshots/react/1-default/retain-exports-alias@notFoundComponent.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import * as React from 'react' |
Empty file.
15 changes: 15 additions & 0 deletions
15
...lugin/tests/code-splitter/snapshots/react/1-default/retain-exports-class-shared-group.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import * as React from 'react' | ||
| import { createFileRoute, Outlet } from '@tanstack/react-router' | ||
| export class Layout extends React.Component { | ||
| render() { | ||
| return ( | ||
| <main> | ||
| <Outlet /> | ||
| </main> | ||
| ) | ||
| } | ||
| } | ||
| export const Route = createFileRoute('/_layout')({ | ||
| component: Layout, | ||
| loader: () => Layout.name, | ||
| }) |
2 changes: 2 additions & 0 deletions
2
...s/code-splitter/snapshots/react/1-default/retain-exports-class-shared-group@component.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import { Layout } from 'retain-exports-class-shared-group.tsx?tsr-shared=1' | ||
| export { Layout as component } |
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions
12
...ests/code-splitter/snapshots/react/1-default/retain-exports-class-shared-group@shared.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import * as React from 'react' | ||
| import { Outlet } from '@tanstack/react-router' | ||
| class Layout extends React.Component { | ||
| render() { | ||
| return ( | ||
| <main> | ||
| <Outlet /> | ||
| </main> | ||
| ) | ||
| } | ||
| } | ||
| export { Layout } |
12 changes: 12 additions & 0 deletions
12
...ages/router-plugin/tests/code-splitter/snapshots/react/1-default/retain-exports-class.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import * as React from 'react' | ||
| import { createFileRoute, Outlet } from '@tanstack/react-router' | ||
| export class Layout extends React.Component { | ||
| render() { | ||
| return ( | ||
| <main> | ||
| <Outlet /> | ||
| </main> | ||
| ) | ||
| } | ||
| } | ||
| export const Route = createFileRoute('/_layout')({ component: Layout }) |
Empty file.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.