Skip to content

Commit 60a0dda

Browse files
fix: enable dead code elimination in split-route to remove unused imports
1 parent 943d616 commit 60a0dda

File tree

196 files changed

+70
-311
lines changed

Some content is hidden

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

196 files changed

+70
-311
lines changed

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -727,19 +727,33 @@ export function compileCodeSplitVirtualRoute(
727727

728728
if (path.node.declaration) {
729729
if (t.isVariableDeclaration(path.node.declaration)) {
730-
path.replaceWith(
731-
t.importDeclaration(
732-
path.node.declaration.declarations.map((decl) =>
733-
t.importSpecifier(
734-
t.identifier((decl.id as any).name),
735-
t.identifier((decl.id as any).name),
736-
),
737-
),
738-
t.stringLiteral(
739-
removeSplitSearchParamFromFilename(opts.filename),
730+
const importDecl = t.importDeclaration(
731+
path.node.declaration.declarations.map((decl) =>
732+
t.importSpecifier(
733+
t.identifier((decl.id as any).name),
734+
t.identifier((decl.id as any).name),
740735
),
741736
),
737+
t.stringLiteral(
738+
removeSplitSearchParamFromFilename(opts.filename),
739+
),
742740
)
741+
742+
path.replaceWith(importDecl)
743+
744+
// Track the imported identifier paths so deadCodeElimination can remove them if unused
745+
// We need to traverse the newly created import to get the identifier paths
746+
path.traverse({
747+
Identifier(identPath) {
748+
// Only track the local binding identifiers (the imported names)
749+
if (
750+
identPath.parentPath.isImportSpecifier() &&
751+
identPath.key === 'local'
752+
) {
753+
refIdents.add(identPath)
754+
}
755+
},
756+
})
743757
}
744758
}
745759
},
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import * as React from 'react';
2-
import { Route } from "arrow-function.tsx";
1+
import * as React from 'react';
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import * as React from 'react';
2-
import { Route } from "arrow-function.tsx";
1+
import * as React from 'react';
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
// Test errorComponent with false literal
2-
import { Route } from "boolean-null-literals.tsx";
32
const SplitComponent = () => <div>Test Component</div>;
43
export { SplitComponent as component };
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
// Test errorComponent with false literal
2-
import { Route } from "boolean-null-literals.tsx";
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
// Test errorComponent with false literal
2-
import { Route } from "boolean-null-literals.tsx";

packages/router-plugin/tests/code-splitter/snapshots/react/1-default/chinese@component.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import { Route } from "chinese.tsx";
32
function HomeComponent() {
43
return <div className="p-2">
54
<Demo title="标题很好看,谁说不是呢?" />
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import { Route } from "chinese.tsx";
32
interface DemoProps {
43
title: string;
54
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import { Route } from "chinese.tsx";
32
interface DemoProps {
43
title: string;
54
}

packages/router-plugin/tests/code-splitter/snapshots/react/1-default/circular-reference-arrow-function@component.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ function OtherComponent() {
1616
});
1717
return <div>App component name is {componentName}</div>;
1818
}
19-
import { Route } from "circular-reference-arrow-function.tsx";
2019
export { App as component };

0 commit comments

Comments
 (0)