Skip to content

Commit 48d58a4

Browse files
Sheraffclaude
andcommitted
fix(compiler): compile modules under project paths with a #
A `#` in the project path (`C#`, `project #2`) was read as a URL fragment by the compiler's id parsing (`identify()`, the route split planner's sibling specifier, import protection's path stripping), so every module of such an app was passed through untouched: no route split, no HMR and, in Start, no server-function, environment or import-protection transform. A `#` is now a fragment only after the query. Rspack and webpack escape a `#` of the path in the loader's `resource` (`​#`, `\0#`); both loaders now unescape it, so the path matches the route registry and the file system. The Vite hosts load their own derived ids (`?tsr-split=`, `?tss-hydrate=`, `?tss-serverfn-split`) and resolve absolute ones verbatim, since Vite's load fallback and file resolution cut an id at the `#`. Tests: identity unit test; router builds under `#`, `%`, spaces and non-ASCII directories on Vite, Rspack and webpack assert the split; a Start build under a `#` directory asserts the handler is not in the client bundle and the route is split, on Vite and Rsbuild. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 468ecbd commit 48d58a4

13 files changed

Lines changed: 300 additions & 39 deletions

File tree

‎packages/router-plugin/src/hosts/loader.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import remapping from '@jridgewell/remapping'
99
import { SourceText, identify } from '@tanstack/router-utils/compiler'
10-
import { normalizePath } from './session'
10+
import { normalizePath, resourceId } from './session'
1111
import type { RouterCompiler } from './session'
1212
import type { Diagnostic } from '@tanstack/router-utils/compiler'
1313

@@ -58,7 +58,7 @@ export function createLoaderImpl(
5858
env: { name: string; type: 'client' | 'server' },
5959
): LoaderImpl {
6060
return async (ctx, source, inputMap) => {
61-
const id = normalizePath(ctx.resource)
61+
const id = resourceId(ctx.resource)
6262
const identity = identify(id)
6363
if (identity.lang === null || !compiler.wants(id)) {
6464
return { code: source, map: inputMap }

‎packages/router-plugin/src/hosts/session.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ export { normalizePath }
6161
/** JS module ids the compiler may transform (query allowed). */
6262
export const JS_ID_RE = /\.[cm]?[jt]sx?(\?|$)/
6363

64+
/**
65+
* The module id of a webpack / Rspack `resource`, slashes normalized. The
66+
* bundlers escape a `#` of the file path in `resource` (webpack as `\0#`,
67+
* Rspack as `\u200b#`; `resourcePath` is unescaped), so that their request
68+
* parsing reads it as a fragment only when meant as one: a project path with
69+
* a `#` (`C#`, `project #2`) must match the registry's file paths.
70+
*/
71+
export function resourceId(resource: string): string {
72+
return normalizePath(resource.replace(/[\0\u200b]#/g, '#'))
73+
}
74+
6475
/** A query carrying one of the compiler-owned keys (derived module ids). */
6576
export const OWNED_QUERY_RE =
6677
/(?:^|[?&])(?:tsr-split|tss-hydrate|tss-serverfn-split)(?:=|&|$)/

‎packages/router-plugin/src/hosts/vite.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* ledger to `this.warn`. Dev or build is the bundler's command, never
99
* `process.env.NODE_ENV` (D8).
1010
*/
11+
import { readFile } from 'node:fs/promises'
1112
import path from 'node:path'
1213
import { SourceText, identify } from '@tanstack/router-utils/compiler'
1314
import { getConfig } from '../core/config'
@@ -306,6 +307,11 @@ export function createRouterCompilerVitePlugin(
306307
resolveId: {
307308
filter: { id: OWNED_QUERY_RE },
308309
handler(source, importer) {
310+
// an absolute derived id of the compiler's own making is resolved
311+
// (Vite's file resolution would read a `#` of the path as a fragment)
312+
if (identify(source).derived !== null && path.isAbsolute(source)) {
313+
return source
314+
}
309315
if (!importer || !source.startsWith('./')) {
310316
return null
311317
}
@@ -322,6 +328,32 @@ export function createRouterCompilerVitePlugin(
322328
},
323329
},
324330

331+
load: {
332+
filter: { id: OWNED_QUERY_RE },
333+
async handler(id) {
334+
// a derived id of a file of the project (`route.tsx?tsr-split=…`) is
335+
// the file's own text, read here: Vite's load fallback cleans the id
336+
// of its query and fragment, and a `#` of a project path (`C#`,
337+
// `project #2`) would end the path there. A derived id with a foreign
338+
// query (`.vue?vue&…`) or of a virtual module is its owner's to load.
339+
const identity = identify(id)
340+
if (
341+
identity.derived === null ||
342+
identity.foreignQuery !== '' ||
343+
identity.path.startsWith('\0') ||
344+
!compiler.wants(id)
345+
) {
346+
return null
347+
}
348+
const code = await readFile(identity.path, 'utf8').catch(() => null)
349+
if (code === null) {
350+
return null
351+
}
352+
this.addWatchFile(identity.path)
353+
return code
354+
},
355+
},
356+
325357
transform: {
326358
filter: { id: { include: JS_ID_RE } },
327359
async handler(code, id, opts) {

‎packages/router-plugin/src/hosts/webpack.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
changedRoutePaths,
1414
onRoutesChanged,
1515
} from '../core/router-plugin-context'
16-
import { RouterCompiler, normalizePath } from './session'
16+
import { RouterCompiler, normalizePath, resourceId } from './session'
1717
import { LOADER_REGISTRY, createLoaderImpl } from './loader'
1818
import type { LoaderImpl } from './loader'
1919
import type { RouterPluginContext } from '../core/router-plugin-context'
@@ -90,7 +90,7 @@ export function warnEagerExports(
9090
modules.push(m, ...(m.modules ?? []))
9191
}
9292
for (const m of modules) {
93-
const id = m.resource ?? m.identifier?.() ?? ''
93+
const id = resourceId(m.resource ?? m.identifier?.() ?? '')
9494
const match = EXTRACTED_EXPORT_RE.exec(id)
9595
if (!match) {
9696
continue

‎packages/router-plugin/src/planners/split.ts‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
* properties in the env output only; route CSS moves into the chunks when
1111
* every component option is split (R42).
1212
*/
13-
import { stringLiteral } from '@tanstack/router-utils/compiler'
13+
import {
14+
siblingSpecifier,
15+
stringLiteral,
16+
} from '@tanstack/router-utils/compiler'
1417
import {
1518
identifierSymbol,
1619
pascal,
@@ -231,7 +234,7 @@ export function routeSplitPlanner(): Planner {
231234
const importer = names.alloc(
232235
`tsrImport${d.groupKey.split('~').map(pascal).join('')}`,
233236
)
234-
const spec = siblingSpecifierOf(derivedId)
237+
const spec = siblingSpecifier(derivedId)
235238
ctx.rewrites.addGenerated({
236239
body: [
237240
{ k: 'text', parts: [`const ${importer.name} = () => import(`] },
@@ -485,16 +488,4 @@ export function routeSplitPlanner(): Planner {
485488
}
486489
}
487490

488-
/** `./<basename>?<query>` of a derived id (same directory as the route module). */
489-
function siblingSpecifierOf(targetId: string): string {
490-
const hashAt = targetId.indexOf('#')
491-
const noHash = hashAt === -1 ? targetId : targetId.slice(0, hashAt)
492-
const q = noHash.indexOf('?')
493-
const base = q === -1 ? noHash : noHash.slice(0, q)
494-
const query = q === -1 ? '' : noHash.slice(q)
495-
const slash = Math.max(base.lastIndexOf('/'), base.lastIndexOf('\\'))
496-
const basename = slash === -1 ? base : base.slice(slash + 1)
497-
return `./${basename}${query}`
498-
}
499-
500491
export { routerRouteId }
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* The project's path (SPEC §2.1, J2.15, G1): an app under a directory whose
3+
* name carries characters a project path may (`#` as in `C#` or `project
4+
* #2`, `%`, spaces, parentheses, non-ASCII) is code-split like any other
5+
* app, in every bundler's build. A `#` matters most: read as a URL fragment,
6+
* every route module of the app silently stays unsplit (nothing fails, the
7+
* app only ships everything eagerly).
8+
*/
9+
import { expect, test } from 'vitest'
10+
import { build, head, rootRoute, ssrRenderEntry } from '../build-harness'
11+
12+
type BundlerName = 'vite' | 'rspack' | 'webpack'
13+
14+
const route = `${head}function Page() {
15+
return <p>MARK_COMPONENT</p>
16+
}
17+
export const Route = createFileRoute('/')({
18+
loader: () => 'MARK_LOADER',
19+
component: Page,
20+
})
21+
`
22+
23+
/** Builds the app under a directory named with `prefix` and returns what the build placed where, and the render. */
24+
async function buildUnder(bundler: BundlerName, prefix: string) {
25+
const app = await build({
26+
bundler,
27+
env: 'ssr',
28+
minify: false,
29+
prefix,
30+
files: {
31+
'routes/__root.tsx': rootRoute,
32+
'routes/index.tsx': route,
33+
'entry.ts': ssrRenderEntry,
34+
},
35+
})
36+
const run = await app.run(
37+
`return [await entry.render(entry.Route.options.component), await entry.Route.options.loader({})]`,
38+
)
39+
return {
40+
root: app.root,
41+
component: app.where('MARK_COMPONENT'),
42+
rendered: run.result,
43+
}
44+
}
45+
46+
const directories = [
47+
{ name: 'a # (C#, project #2)', prefix: '.project-path-C# project #2-' },
48+
{ name: 'a %', prefix: '.project-path-100%-' },
49+
{
50+
name: 'spaces, parentheses and non-ASCII',
51+
prefix: '.project-path-café 日本語 (2)+-',
52+
},
53+
]
54+
55+
test.each(
56+
directories.flatMap(({ name, prefix }) =>
57+
(['vite', 'rspack', 'webpack'] as const).map((bundler) => ({
58+
name,
59+
prefix,
60+
bundler,
61+
})),
62+
),
63+
)(
64+
'an app under a directory with $name is code-split and renders ($bundler)',
65+
async ({ prefix, bundler }) => {
66+
const result = await buildUnder(bundler, prefix)
67+
expect(result.root).toContain(prefix.slice(1, -1))
68+
expect(result).toMatchObject({
69+
component: 'lazy',
70+
rendered: ['<p>MARK_COMPONENT</p>', 'MARK_LOADER'],
71+
})
72+
},
73+
120_000,
74+
)

‎packages/router-utils/src/compiler/identity.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,28 @@ export const OWNED_QUERY_KEYS = {
4646

4747
const OWNED_KEY_SET = new Set<string>(Object.values(OWNED_QUERY_KEYS))
4848

49+
/**
50+
* Splits a host id into path, query and fragment. A project path may carry
51+
* `#` (`C#`, `project #2`), and Vite and Rspack hand such ids with the `#`
52+
* as it is: a `#` is a URL fragment only after the query. (`?` cannot be
53+
* part of a Windows path; on other systems a `?` in the project path is not
54+
* supported by the bundlers either.)
55+
*/
4956
function splitQuery(id: string): { base: string; query: string; hash: string } {
50-
const hashAt = id.indexOf('#')
51-
const hash = hashAt === -1 ? '' : id.slice(hashAt)
52-
const noHash = hashAt === -1 ? id : id.slice(0, hashAt)
53-
const q = noHash.indexOf('?')
57+
const q = id.indexOf('?')
5458
if (q === -1) {
55-
return { base: noHash, query: '', hash }
59+
return { base: id, query: '', hash: '' }
60+
}
61+
const rest = id.slice(q + 1)
62+
const hashAt = rest.indexOf('#')
63+
if (hashAt === -1) {
64+
return { base: id.slice(0, q), query: rest, hash: '' }
65+
}
66+
return {
67+
base: id.slice(0, q),
68+
query: rest.slice(0, hashAt),
69+
hash: rest.slice(hashAt),
5670
}
57-
return { base: noHash.slice(0, q), query: noHash.slice(q + 1), hash }
5871
}
5972

6073
function parseDerived(key: string, value: string): DerivedQuery | null {

‎packages/router-utils/tests/identity.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,52 @@ describe('identity (R35)', () => {
7575
expect(langFromId('/a/comp.vue')).toBeNull()
7676
})
7777

78+
// A project path may carry `#`, spaces, `%` and non-ASCII characters;
79+
// Vite and Rspack hand such ids with the characters as they are. A `#` is
80+
// a URL fragment only after the query.
81+
it('keeps a # of the path and reads a fragment only after the query', () => {
82+
const plain = identify('/home/me/C#/project #2/src/routes/a.tsx')
83+
expect(plain.path).toBe('/home/me/C#/project #2/src/routes/a.tsx')
84+
expect(plain.canonicalId).toBe('/home/me/C#/project #2/src/routes/a.tsx')
85+
expect(plain.lang).toBe('tsx')
86+
expect(plain.derived).toBeNull()
87+
88+
const split = identify('/home/me/C#/src/a.tsx?tsr-split=component')
89+
expect(split.path).toBe('/home/me/C#/src/a.tsx')
90+
expect(split.canonicalId).toBe('/home/me/C#/src/a.tsx')
91+
expect(split.derived).toEqual({ kind: 'split', value: 'component' })
92+
expect(split.lang).toBe('tsx')
93+
expect(
94+
deriveId('/home/me/C#/src/a.tsx', { kind: 'split', value: 'component' }),
95+
).toBe('/home/me/C#/src/a.tsx?tsr-split=component')
96+
expect(
97+
siblingSpecifier('/home/me/C#/src/a#b.tsx?tsr-split=component'),
98+
).toBe('./a#b.tsx?tsr-split=component')
99+
100+
const fragment = identify(
101+
'/app/page.tsx?flavour=b&tsr-split=component#frag',
102+
)
103+
expect(fragment.path).toBe('/app/page.tsx')
104+
expect(fragment.canonicalId).toBe('/app/page.tsx?flavour=b#frag')
105+
expect(fragment.derived).toEqual({ kind: 'split', value: 'component' })
106+
expect(
107+
deriveId('/app/page.tsx?flavour=b#frag', {
108+
kind: 'split',
109+
value: 'component',
110+
}),
111+
).toBe('/app/page.tsx?flavour=b&tsr-split=component#frag')
112+
113+
for (const dir of [
114+
'/home/me/my app (2)+',
115+
'/home/me/caf\u00e9 \u65e5\u672c\u8a9e',
116+
'/home/me/100%',
117+
]) {
118+
const id = identify(`${dir}/src/a.tsx`)
119+
expect(id.path).toBe(`${dir}/src/a.tsx`)
120+
expect(id.lang).toBe('tsx')
121+
}
122+
})
123+
78124
it('revision is stable and content-only', () => {
79125
expect(revision('a')).toBe(revision('a'))
80126
expect(revision('a')).not.toBe(revision('b'))

‎packages/start-plugin-core/src/import-protection/utils.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ export function dedupeViolationKey(info: {
6161
return `${info.type}:${info.importer}:${info.specifier}:${info.resolved ?? ''}`
6262
}
6363

64-
/** Strip both `?query` and `#hash` from a module ID. */
64+
/**
65+
* Strip the `?query` and `#hash` of a module ID. A project path may carry a
66+
* `#` (`C#`, `project #2`): a `#` is a fragment only after the query.
67+
*/
6568
export function stripQueryAndHash(id: string): string {
6669
const q = id.indexOf('?')
67-
const h = id.indexOf('#')
68-
if (q === -1 && h === -1) return id
69-
if (q === -1) return id.slice(0, h)
70-
if (h === -1) return id.slice(0, q)
71-
return id.slice(0, Math.min(q, h))
70+
if (q === -1) {
71+
return id
72+
}
73+
return id.slice(0, q)
7274
}
7375

7476
/**

‎packages/start-plugin-core/src/rsbuild/compiler-session.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ export class StartRsbuildCompilerSession {
628628
ctx.addDependency(dep)
629629
}
630630
}
631-
this.reportDiagnostics(ctx, out, identity.path, source)
631+
this.reportDiagnostics(ctx, out, identity.path, source, env.type)
632632
const ipEnv = ip?.env(env.name)
633633
env.compiled.add(ctx.resource)
634634
if (ipEnv) {
@@ -830,7 +830,10 @@ export class StartRsbuildCompilerSession {
830830
* dropped.
831831
*/
832832
export function canonicalResource(resource: string): string {
833-
const normalized = normalizePath(resource)
833+
// Rspack and webpack escape a `#` of the file path in `resource` (`\u200b#`
834+
// and `\0#`; `resourcePath` is unescaped): a project path with a `#` (`C#`,
835+
// `project #2`) must give the file's path
836+
const normalized = normalizePath(resource.replace(/[\0\u200b]#/g, '#'))
834837
const q = normalized.indexOf('?')
835838
if (q === -1) {
836839
return normalized

0 commit comments

Comments
 (0)