Skip to content

Commit b8ef497

Browse files
fix: do not rename functions / variables when code splitting (#4960)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent be3931b commit b8ef497

File tree

44 files changed

+479
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+479
-126
lines changed

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

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ export function compileCodeSplitVirtualRoute(
537537
}
538538

539539
let splitNode = splitKey.node
540-
const splitMeta = splitKey.meta
540+
const splitMeta = { ...splitKey.meta, shouldRemoveNode: true }
541541

542542
while (t.isIdentifier(splitNode)) {
543543
const binding = programPath.scope.getBinding(splitNode.name)
@@ -547,21 +547,15 @@ export function compileCodeSplitVirtualRoute(
547547
// Add the node to the program
548548
if (splitNode) {
549549
if (t.isFunctionDeclaration(splitNode)) {
550-
programPath.pushContainer(
551-
'body',
552-
t.variableDeclaration('const', [
553-
t.variableDeclarator(
554-
t.identifier(splitMeta.localExporterIdent),
555-
t.functionExpression(
556-
splitNode.id || null, // Anonymize the function expression
557-
splitNode.params,
558-
splitNode.body,
559-
splitNode.generator,
560-
splitNode.async,
561-
),
562-
),
563-
]),
564-
)
550+
// an anonymous function declaration should only happen for `export default function() {...}`
551+
// so we should never get here
552+
if (!splitNode.id) {
553+
throw new Error(
554+
`Function declaration for "${SPLIT_TYPE}" must have an identifier.`,
555+
)
556+
}
557+
splitMeta.shouldRemoveNode = false
558+
splitMeta.localExporterIdent = splitNode.id.name
565559
} else if (
566560
t.isFunctionExpression(splitNode) ||
567561
t.isArrowFunctionExpression(splitNode)
@@ -589,15 +583,14 @@ export function compileCodeSplitVirtualRoute(
589583
]),
590584
)
591585
} else if (t.isVariableDeclarator(splitNode)) {
592-
programPath.pushContainer(
593-
'body',
594-
t.variableDeclaration('const', [
595-
t.variableDeclarator(
596-
t.identifier(splitMeta.localExporterIdent),
597-
splitNode.init,
598-
),
599-
]),
600-
)
586+
if (t.isIdentifier(splitNode.id)) {
587+
splitMeta.localExporterIdent = splitNode.id.name
588+
splitMeta.shouldRemoveNode = false
589+
} else {
590+
throw new Error(
591+
`Unexpected splitNode type ☝️: ${splitNode.type}`,
592+
)
593+
}
601594
} else if (t.isCallExpression(splitNode)) {
602595
const outputSplitNodeCode = generateFromAst(splitNode).code
603596
const splitNodeAst = babel.parse(outputSplitNodeCode)
@@ -668,11 +661,13 @@ export function compileCodeSplitVirtualRoute(
668661
}
669662
}
670663

671-
// If the splitNode exists at the top of the program
672-
// then we need to remove that copy
673-
programPath.node.body = programPath.node.body.filter((node) => {
674-
return node !== splitNode
675-
})
664+
if (splitMeta.shouldRemoveNode) {
665+
// If the splitNode exists at the top of the program
666+
// then we need to remove that copy
667+
programPath.node.body = programPath.node.body.filter((node) => {
668+
return node !== splitNode
669+
})
670+
}
676671

677672
// Export the node
678673
programPath.pushContainer('body', [

packages/router-plugin/tests/code-splitter/snapshots/react/1-default/[email protected]

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { Link, Outlet } from '@tanstack/react-router';
33
import { Route } from "arrow-function.tsx";
4-
const SplitComponent = () => {
4+
const PostsComponent = () => {
55
const posts = Route.useLoaderData();
66
return <div className="p-2 flex gap-2">
77
<ul className="list-disc pl-4">
@@ -24,4 +24,4 @@ const SplitComponent = () => {
2424
<Outlet />
2525
</div>;
2626
};
27-
export { SplitComponent as component };
27+
export { PostsComponent as component };

packages/router-plugin/tests/code-splitter/snapshots/react/1-default/[email protected]

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import * as React from 'react';
22
import { Route } from "chinese.tsx";
3+
function HomeComponent() {
4+
return <div className="p-2">
5+
<Demo title="标题很好看,谁说不是呢?" />
6+
<Demo title="The title looks great, who can deny that?" />
7+
</div>;
8+
}
39
interface DemoProps {
410
title: string;
511
}
@@ -15,10 +21,4 @@ function Demo({
1521
{title}
1622
</h1>;
1723
}
18-
const SplitComponent = function HomeComponent() {
19-
return <div className="p-2">
20-
<Demo title="标题很好看,谁说不是呢?" />
21-
<Demo title="The title looks great, who can deny that?" />
22-
</div>;
23-
};
24-
export { SplitComponent as component };
24+
export { HomeComponent as component };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const $$splitComponentImporter = () => import('circular-reference-arrow-function.tsx?tsr-split=component');
2+
import { lazyRouteComponent } from '@tanstack/react-router';
3+
import { createFileRoute } from '@tanstack/react-router';
4+
export const Route = createFileRoute('/')({
5+
component: lazyRouteComponent($$splitComponentImporter, 'component')
6+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function getComponentName(obj: Record<string, unknown>): string {
2+
return Object.keys(obj)[0];
3+
}
4+
const App = () => {
5+
const componentName = getComponentName({
6+
App
7+
});
8+
return <div>
9+
Component Name is {componentName}
10+
<OtherComponent />
11+
</div>;
12+
};
13+
function OtherComponent() {
14+
const componentName = getComponentName({
15+
App
16+
});
17+
return <div>App component name is {componentName}</div>;
18+
}
19+
import { Route } from "circular-reference-arrow-function.tsx";
20+
export { App as component };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function getComponentName(obj: Record<string, unknown>): string {
2+
return Object.keys(obj)[0];
3+
}
4+
const App = () => {
5+
const componentName = getComponentName({
6+
App
7+
});
8+
return <div>
9+
Component Name is {componentName}
10+
<OtherComponent />
11+
</div>;
12+
};
13+
function OtherComponent() {
14+
const componentName = getComponentName({
15+
App
16+
});
17+
return <div>App component name is {componentName}</div>;
18+
}
19+
import { Route } from "circular-reference-arrow-function.tsx";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function getComponentName(obj: Record<string, unknown>): string {
2+
return Object.keys(obj)[0];
3+
}
4+
const App = () => {
5+
const componentName = getComponentName({
6+
App
7+
});
8+
return <div>
9+
Component Name is {componentName}
10+
<OtherComponent />
11+
</div>;
12+
};
13+
function OtherComponent() {
14+
const componentName = getComponentName({
15+
App
16+
});
17+
return <div>App component name is {componentName}</div>;
18+
}
19+
import { Route } from "circular-reference-arrow-function.tsx";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const $$splitComponentImporter = () => import('circular-reference-function.tsx?tsr-split=component');
2+
import { lazyRouteComponent } from '@tanstack/react-router';
3+
import { createFileRoute } from '@tanstack/react-router';
4+
export const Route = createFileRoute('/')({
5+
component: lazyRouteComponent($$splitComponentImporter, 'component')
6+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function getComponentName(obj: Record<string, unknown>): string {
2+
return Object.keys(obj)[0];
3+
}
4+
function App() {
5+
const componentName = getComponentName({
6+
App
7+
});
8+
return <div>
9+
Component Name is {componentName}
10+
<OtherComponent />
11+
</div>;
12+
}
13+
function OtherComponent() {
14+
const componentName = getComponentName({
15+
App
16+
});
17+
return <div>App component name is {componentName}</div>;
18+
}
19+
import { Route } from "circular-reference-function.tsx";
20+
export { App as component };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function getComponentName(obj: Record<string, unknown>): string {
2+
return Object.keys(obj)[0];
3+
}
4+
function App() {
5+
const componentName = getComponentName({
6+
App
7+
});
8+
return <div>
9+
Component Name is {componentName}
10+
<OtherComponent />
11+
</div>;
12+
}
13+
function OtherComponent() {
14+
const componentName = getComponentName({
15+
App
16+
});
17+
return <div>App component name is {componentName}</div>;
18+
}
19+
import { Route } from "circular-reference-function.tsx";

0 commit comments

Comments
 (0)