Skip to content

Commit 373c5b6

Browse files
committed
fix(router-plugin): handle explicit undefined component in code splitter
1 parent 9b93a05 commit 373c5b6

22 files changed

+119
-0
lines changed

packages/router-plugin/src/core/code-splitter/compilers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,16 @@ export function compileCodeSplitVirtualRoute(
620620
console.info('Unexpected splitNode type:', splitNode)
621621
throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`)
622622
}
623+
} else if (splitNode === undefined) {
624+
programPath.pushContainer(
625+
'body',
626+
t.variableDeclaration('const', [
627+
t.variableDeclarator(
628+
t.identifier(splitMeta.localExporterIdent),
629+
t.identifier('undefined'),
630+
),
631+
]),
632+
)
623633
}
624634

625635
// If the splitNode exists at the top of the program
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const $$splitComponentImporter = () => import('explicit-undefined-component.tsx?tsr-split=component');
2+
import { lazyRouteComponent } from '@tanstack/react-router';
3+
import React from 'react';
4+
import { createFileRoute } from '@tanstack/react-router';
5+
export const Route = createFileRoute('/')({
6+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)
7+
});
8+
if (import.meta.hot) {
9+
import.meta.hot.accept(newModule => {
10+
if (newModule && newModule.Route && typeof newModule.Route.clone === 'function') {
11+
newModule.Route.clone(Route);
12+
}
13+
});
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
3+
const SplitComponent = undefined;
4+
export { SplitComponent as component };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const $$splitComponentImporter = () => import('explicit-undefined-component.tsx?tsr-split=component');
2+
import { lazyRouteComponent } from '@tanstack/react-router';
3+
import React from 'react';
4+
import { createFileRoute } from '@tanstack/react-router';
5+
export const Route = createFileRoute('/')({
6+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
3+
const SplitComponent = undefined;
4+
export { SplitComponent as component };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const $$splitComponentImporter = () => import('explicit-undefined-component.tsx?tsr-split=component---errorComponent---notFoundComponent---pendingComponent');
2+
import { lazyRouteComponent } from '@tanstack/react-router';
3+
import React from 'react';
4+
import { createFileRoute } from '@tanstack/react-router';
5+
export const Route = createFileRoute('/')({
6+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)
7+
});
8+
if (import.meta.hot) {
9+
import.meta.hot.accept(newModule => {
10+
if (newModule && newModule.Route && typeof newModule.Route.clone === 'function') {
11+
newModule.Route.clone(Route);
12+
}
13+
});
14+
}

0 commit comments

Comments
 (0)