Skip to content

Commit 804bf39

Browse files
KyleAMathewsclaude
andcommitted
fix(router-plugin): don't prepend ./ to absolute paths in imports
Fixed an issue where absolute paths (used internally by bundlers like Vite and Rspack) were incorrectly having './' prepended, creating invalid import paths like './/home/workspace/...'. The fix now checks if a path is absolute before adding the './' prefix: - Unix absolute paths (starting with '/') - Windows absolute paths (starting with drive letter like 'C:') - Already relative paths (starting with './' or '../') These are all left unchanged, while bare relative paths get './' prepended for proper ES module resolution. This maintains the correct behavior for both: - User-facing relative imports (need './' for bundlers) - Internal absolute paths (used by bundler virtual file systems) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 0f5c9d7 commit 804bf39

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ function addSplitSearchParamToFilename(
8181
) {
8282
const [bareFilename] = filename.split('?')
8383

84-
// Ensure relative import specifier for proper bundler resolution
84+
// Don't modify absolute paths - they're used internally by bundlers
85+
// Only ensure relative import specifier for relative paths
8586
const relativeFilename =
86-
bareFilename!.startsWith('./') || bareFilename!.startsWith('../')
87+
bareFilename!.startsWith('/') || // Unix absolute path
88+
/^[a-zA-Z]:/.test(bareFilename!) || // Windows absolute path (C:\ or C:/)
89+
bareFilename!.startsWith('./') ||
90+
bareFilename!.startsWith('../')
8791
? bareFilename!
8892
: `./${bareFilename!}`
8993

@@ -96,8 +100,13 @@ function addSplitSearchParamToFilename(
96100

97101
function removeSplitSearchParamFromFilename(filename: string) {
98102
const [bareFilename] = filename.split('?')
99-
// Ensure relative import specifier for proper bundler resolution
100-
return bareFilename!.startsWith('./') || bareFilename!.startsWith('../')
103+
104+
// Don't modify absolute paths - they're used internally by bundlers
105+
// Only ensure relative import specifier for relative paths
106+
return bareFilename!.startsWith('/') || // Unix absolute path
107+
/^[a-zA-Z]:/.test(bareFilename!) || // Windows absolute path (C:\ or C:/)
108+
bareFilename!.startsWith('./') ||
109+
bareFilename!.startsWith('../')
101110
? bareFilename!
102111
: `./${bareFilename!}`
103112
}

packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$splitComponentImporter = () => import('arrow-function.tsx?tsr-split=component');
1+
const $$splitComponentImporter = () => import('./arrow-function.tsx?tsr-split=component');
22
import { lazyRouteComponent } from '@tanstack/react-router';
33
import * as React from 'react';
44
import { createFileRoute } from '@tanstack/react-router';

packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$splitComponentImporter = () => import('arrow-function.tsx?tsr-split=component');
1+
const $$splitComponentImporter = () => import('./arrow-function.tsx?tsr-split=component');
22
import { lazyRouteComponent } from '@tanstack/react-router';
33
import * as React from 'react';
44
import { createFileRoute } from '@tanstack/react-router';

packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$splitComponentImporter = () => import('function-declaration.tsx?tsr-split=component');
1+
const $$splitComponentImporter = () => import('./function-declaration.tsx?tsr-split=component');
22
import { lazyRouteComponent } from '@tanstack/react-router';
33
import * as React from 'react';
44
import { createFileRoute } from '@tanstack/react-router';

packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$splitComponentImporter = () => import('function-declaration.tsx?tsr-split=component');
1+
const $$splitComponentImporter = () => import('./function-declaration.tsx?tsr-split=component');
22
import { lazyRouteComponent } from '@tanstack/react-router';
33
import * as React from 'react';
44
import { createFileRoute } from '@tanstack/react-router';

packages/router-plugin/tests/add-hmr/snapshots/solid/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$splitComponentImporter = () => import('arrow-function.tsx?tsr-split=component');
1+
const $$splitComponentImporter = () => import('./arrow-function.tsx?tsr-split=component');
22
import { lazyRouteComponent } from '@tanstack/solid-router';
33
import { createFileRoute } from '@tanstack/solid-router';
44
import { fetchPosts } from '../posts';

packages/router-plugin/tests/add-hmr/snapshots/solid/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$splitComponentImporter = () => import('arrow-function.tsx?tsr-split=component');
1+
const $$splitComponentImporter = () => import('./arrow-function.tsx?tsr-split=component');
22
import { lazyRouteComponent } from '@tanstack/solid-router';
33
import { createFileRoute } from '@tanstack/solid-router';
44
import { fetchPosts } from '../posts';

0 commit comments

Comments
 (0)