diff --git a/packages/router-plugin/src/hosts/session.ts b/packages/router-plugin/src/hosts/session.ts index 37cda195af..e6a1592c55 100644 --- a/packages/router-plugin/src/hosts/session.ts +++ b/packages/router-plugin/src/hosts/session.ts @@ -180,7 +180,10 @@ export class RouterCompiler { if (d.by === 'parse') { continue } - if (this.warnings.once(normalizePath(path), d)) { + // once per (file, reason and wording) per run: an edit that leaves the + // finding as it is does not repeat it; a new finding is reported + const keyed = d.code ? { ...d, code: `${d.code}\u0000${d.message}` } : d + if (this.warnings.once(normalizePath(path), keyed)) { report(d) } } @@ -191,7 +194,6 @@ export class RouterCompiler { path: string, kind: InvalidateChange['kind'], ): Promise<{ evicted: Array; dependents: Array }> { - this.warnings.reset(normalizePath(path)) return invalidate(this.caches, { path, kind }, this.host) } diff --git a/packages/router-plugin/src/planners/decisions.ts b/packages/router-plugin/src/planners/decisions.ts index d7e7726d2a..e3bf78dac1 100644 --- a/packages/router-plugin/src/planners/decisions.ts +++ b/packages/router-plugin/src/planners/decisions.ts @@ -42,6 +42,8 @@ export interface SplitDecision { region: Range /** The text the chunk declares its export from (the value, or a method's function). */ valueRange: Range + /** The value node (a method's function; an object-form loader's handler). */ + valueNode: Node /** The key as written, for whole-property placeholders (`component: …`). */ keyText: string /** Whether the placeholder replaces the whole property (method, shorthand). */ @@ -209,11 +211,14 @@ function splitTarget( } const value = prop.value! switch (prop.kind) { - case 'init': + case 'init': { + // the chunk declares the bare value (casts and parentheses dropped, so + // React Refresh registers a function); the placeholder replaces it all + const inner = unwrapTs(value) return { shape: 'init', region: { start: value.start, end: value.end }, - valueRange: { start: value.start, end: value.end }, + valueRange: { start: inner.start, end: inner.end }, value, keyText: prop.keyText, wholeProperty: false, @@ -222,6 +227,7 @@ function splitTarget( container, prop, } + } case 'method': return { shape: 'method', @@ -408,6 +414,7 @@ function computeSplitPlan( shape: target.shape, region: target.region, valueRange: target.valueRange, + valueNode: target.value, keyText: target.keyText, wholeProperty: target.wholeProperty, methodPrefix: target.methodPrefix, diff --git a/packages/router-plugin/src/planners/hmr.ts b/packages/router-plugin/src/planners/hmr.ts index acce684456..fc3588c7a3 100644 --- a/packages/router-plugin/src/planners/hmr.ts +++ b/packages/router-plugin/src/planners/hmr.ts @@ -74,6 +74,16 @@ export function routeHmrPlanner(): Planner { const route = r.route const plan = splitPlanOf(ctx, cfg) const split = new Set(plan?.decisions.map((d) => d.key) ?? []) + // the split option values that name a binding: the splitter's own + // edit replaces those occurrences (a ref edit there would collide) + const splitRegions = + plan?.decisions + .filter( + (d) => + d.shape === 'shorthand' || + identifierSymbol(ctx, d.valueNode) !== null, + ) + .map((d) => d.region) ?? [] const round = roundOf(ctx) const { names, symbols } = ctx const code = ctx.analysis.source.code @@ -82,7 +92,6 @@ export function routeHmrPlanner(): Planner { option.key === null || !HMR_COMPONENT_KEYS.includes(option.key) || option.value === null || - split.has(option.key) || option.kind === 'spread' || option.kind === 'computed' || option.kind === 'accessor' @@ -91,6 +100,7 @@ export function routeHmrPlanner(): Planner { } const key = option.key const value = option.value + const isSplit = split.has(key) if ( option.kind !== 'method' && (isFallbackLiteral(value) || isAlreadyLazy(ctx, value)) @@ -100,18 +110,26 @@ export function routeHmrPlanner(): Planner { const sym = option.kind === 'method' ? null : identifierSymbol(ctx, value) if (sym !== null) { + // a module binding: registrable by its own declaration when + // capitalised; a lowercase local one is renamed module-wide, split + // or not (React Refresh registers it wherever it is declared) const name = symbols.name(sym) + const record = (registered: string) => { + if (!isSplit) { + round.unsplit[key] = registered + } + } if (isCapitalised(name)) { - round.unsplit[key] = name + record(name) continue } if (cfg.framework !== 'react' || symbols.isImport(sym)) { - round.unsplit[key] = name + record(name) continue } const already = round.renamed.get(sym) if (already !== undefined) { - round.unsplit[key] = symbols.name(already) + record(symbols.name(already)) continue } const units = ctx.t1.declaringUnits.get(sym) ?? [] @@ -119,16 +137,24 @@ export function routeHmrPlanner(): Planner { (u) => ctx.t1.units[u]!.kind === 'declaration', ) if (!local) { - round.unsplit[key] = name + record(name) continue } const next = names.alloc(`Tsr${pascal(key)}`) - ctx.renameRefs(sym, { sym: next.sym }, 'all-outputs', 'route-hmr') + // the occurrences that are split option values are the splitter's + // (its edit replaces them; the chunk reads the renamed symbol) + ctx.renameRefs( + sym, + { sym: next.sym }, + 'all-outputs', + 'route-hmr', + splitRegions, + ) round.renamed.set(sym, next.sym) - round.unsplit[key] = next.name + record(next.name) continue } - if (cfg.framework !== 'react') { + if (cfg.framework !== 'react' || isSplit) { continue } // inline value or method: hoist into a registrable binding before the options @@ -150,10 +176,13 @@ export function routeHmrPlanner(): Planner { { k: 'text', parts: [`${option.keyText}: `, { sym: hoisted.sym }] }, ] } else { + // casts and parentheses around the value are dropped from the hoist + // (`(() => …) as any`): React Refresh registers a bare function + const inner = unwrapTs(value) body.push({ k: 'text', parts: [`const ${hoisted.name} = `] }) body.push({ k: 'slice', - range: { start: value.start, end: value.end }, + range: { start: inner.start, end: inner.end }, }) body.push({ k: 'text', parts: ['\n'] }) region = { start: value.start, end: value.end } @@ -175,12 +204,17 @@ export function routeHmrPlanner(): Planner { }) round.unsplit[key] = hoisted.name void code - void unwrapTs } }, plan(ctx) { const cfg = configOf(ctx) const r = recognizeRoute(ctx) + if (!cfg.split && cfg.deleteNodes.length === 0) { + // the split planner is not registered: this planner reports recognition + for (const w of r.warnings) { + ctx.diagnostic(w) + } + } if (!r.route || r.route.kind === 'lazy') { return } diff --git a/packages/router-plugin/src/planners/route.ts b/packages/router-plugin/src/planners/route.ts index a77977155e..5072cb2125 100644 --- a/packages/router-plugin/src/planners/route.ts +++ b/packages/router-plugin/src/planners/route.ts @@ -88,7 +88,7 @@ export interface RouteModuleInfo { } export type RouteRecognition = - | { route: RouteModuleInfo } + | { route: RouteModuleInfo; warnings: Array } | { route: null reason: 'no-route-export' | 'unrecognized' @@ -425,6 +425,7 @@ function recognize(ctx: PlanContext): RouteRecognition { warnings, routeId: prefetched?.routeId ?? null, }, + warnings, } } diff --git a/packages/router-plugin/src/planners/split.ts b/packages/router-plugin/src/planners/split.ts index 022448c2a0..4d9f7885f0 100644 --- a/packages/router-plugin/src/planners/split.ts +++ b/packages/router-plugin/src/planners/split.ts @@ -11,7 +11,13 @@ * every component option is split (R42). */ import { stringLiteral } from '@tanstack/router-utils/compiler' -import { pascal, routeEntryDigest, routerRouteId } from './route' +import { + identifierSymbol, + pascal, + recognizeRoute, + routeEntryDigest, + routerRouteId, +} from './route' import { COMPONENT_KEYS, splitPlanOf } from './decisions' import { renamedSymbol } from './round' import type { RouteEntryFacts } from './route' @@ -97,6 +103,10 @@ export function routeSplitPlanner(): Planner { const cfg = configOf(ctx) const plan = splitPlanOf(ctx, cfg) if (!plan) { + // no recognised route: report what recognition found (J2.10, J2.11) + for (const w of recognizeRoute(ctx).warnings) { + ctx.diagnostic(w) + } return } const { route } = plan @@ -214,16 +224,24 @@ export function routeSplitPlanner(): Planner { const split = names.alloc(`TsrSplit${pascal(d.key)}`) facts.chunkExports[d.key] = d.exportName - // the chunk's unit: declares the export from the option's value + // the chunk's unit: declares the export from the option's value. An + // identifier value is read as a symbol (the binding travels by + // placement; a renamed one renders its new name) and its occurrence + // is replaced in the env output only; any other value is claimed by + // the chunk unit and a placeholder renders at its occurrence. + const valueSym = + d.shape === 'shorthand' + ? d.shorthandSym + : d.shape === 'init' + ? identifierSymbol(ctx, d.valueNode) + : null const body: Array = [] - if (d.shape === 'shorthand' && d.shorthandSym !== null) { + if (valueSym !== null) { + // the original symbol: placement follows its declaring unit; a + // rename renders at emit time body.push({ k: 'text', - parts: [ - `const ${split.name} = `, - { sym: renamedSymbol(ctx, d.shorthandSym) }, - '\n', - ], + parts: [`const ${split.name} = `, { sym: valueSym }, '\n'], }) } else { body.push({ @@ -234,10 +252,17 @@ export function routeSplitPlanner(): Planner { body.push({ k: 'text', parts: ['\n'] }) } if (refreshMarkers && isComponent) { + // identity = chunk id + the binding React Refresh registers: the + // user's component when the value names one (a rename is a new + // family), else the chunk's own declaration + const registered = + valueSym !== null + ? symbols.name(renamedSymbol(ctx, valueSym)) + : split.name body.push({ k: 'text', parts: [ - `${split.name}.$$tsrRefreshId = ${stringLiteral(`${g.derivedId} ${split.name}`, '"')}\n`, + `${split.name}.$$tsrRefreshId = ${stringLiteral(`${g.derivedId} ${registered}`, '"')}\n`, ], }) } @@ -268,8 +293,18 @@ export function routeSplitPlanner(): Planner { parts: d.wholeProperty ? [`${d.keyText}: `, ...call] : call, }, ] - const region = ctx.region(d.region, 'option-value') - ctx.rewrites.claim({ region, owner, placeholder, by: ROUTE_SPLIT }) + if (valueSym !== null) { + ctx.rewrites.add({ + range: d.region, + body: placeholder, + scope: ['env'], + by: ROUTE_SPLIT, + reason: 'split', + }) + } else { + const region = ctx.region(d.region, 'option-value') + ctx.rewrites.claim({ region, owner, placeholder, by: ROUTE_SPLIT }) + } } if (helperImports.length > 0) { @@ -346,7 +381,6 @@ export function routeSplitPlanner(): Planner { } ctx.setFacts(ROUTE_SPLIT, facts) - void symbols }, } }