Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import {
3 StartCompiler,
4 detectKindsInCode,
5 getLookupKindsForEnv,
6} from '../src/start-compiler/compiler'
7import { getLookupConfigurationsForEnv } from '../src/start-compiler/config'
8import type { LookupConfig, LookupKind } from '../src/start-compiler/compiler'
9import type { StartCompilerImportTransform } from '../src/types'
10β
11// Default test options for StartCompiler
12function getDefaultTestOptions(env: 'client' | 'server') {
13 const envName = env === 'client' ? 'client' : 'ssr'
14 return {
15 envName,
16 root: '/test',
17 framework: 'react' as const,
18 providerEnvName: 'ssr',
19 }
20}
21β
22// Helper to create a compiler with all kinds enabled
23function createFullCompiler(
24 env: 'client' | 'server',
25 opts?: {
26 serverFnProviderModuleDirectives?: ReadonlyArray<string> | undefined
27 },
28) {
29 const lookupKinds: Set<LookupKind> =
30 env === 'client'
31 ? new Set([
32 'ServerFn',
33 'Middleware',
34 'IsomorphicFn',
35 'ServerOnlyFn',
36 'ClientOnlyFn',
37 ])
38 : new Set(['ServerFn', 'IsomorphicFn', 'ServerOnlyFn', 'ClientOnlyFn'])
39β
40 const lookupConfigurations: Array<LookupConfig> = [
41 {
42 libName: '@tanstack/react-start',
43 rootExport: 'createServerFn',
44 kind: 'Root',
45 },
46 {
47 libName: '@tanstack/react-start',
48 rootExport: 'createMiddleware',
49 kind: 'Root',
50 },
51 {
52 libName: '@tanstack/react-start',
53 rootExport: 'createIsomorphicFn',
54 kind: 'IsomorphicFn',
55 },
56 {
57 libName: '@tanstack/react-start',
58 rootExport: 'createServerOnlyFn',
59 kind: 'ServerOnlyFn',
60 },
61 {
62 libName: '@tanstack/react-start',
63 rootExport: 'createClientOnlyFn',
64 kind: 'ClientOnlyFn',
65 },
66 ]
67β
68 return new StartCompiler({
69 env,
70 ...getDefaultTestOptions(env),
71 lookupKinds,
72 lookupConfigurations,
73 getKnownServerFns: () => ({}),
74 loadModule: async () => {},
75 resolveId: async (id) => id,
76 mode: 'build',
77 serverFnProviderModuleDirectives: opts?.serverFnProviderModuleDirectives,
78 })
79}
80β
81function createExternalTransformCompiler() {
82 const compilerTransforms: Array<StartCompilerImportTransform> = [
83 {
84 name: 'test-render-option-injection',
85 environment: 'server',
86 imports: [
87 {
88 libName: '@example/runtime',
89 rootExport: 'renderThing',
90 },
91 ],
92 detect: /\brenderThing\b/,
93 transform: (candidates, context) => {
94 const t = context.types
95 for (const candidate of candidates) {
96 const args = candidate.path.node.arguments
97 if (args.length !== 1) continue
98 args.push(
99 t.objectExpression([
100 t.objectProperty(
101 t.identifier('injected'),
102 context.parseExpression('loadThing()'),
103 ),
104 ]),
105 )
106 }
107 },
108 },
109 ]
110β
111 return new StartCompiler({
112 env: 'server',
113 envName: 'server',
114 root: '/test',
115 framework: 'react',
116 providerEnvName: 'server',
117 lookupKinds: getLookupKindsForEnv('server', { compilerTransforms }),
118 lookupConfigurations: getLookupConfigurationsForEnv('server', 'react', {
119 compilerTransforms,
120 }),
121 getKnownServerFns: () => ({}),
122 loadModule: async () => {},
123 resolveId: async (id) => id,
124 mode: 'build',
125 compilerTransforms,
126 })
127}
128β
129describe('detectKindsInCode', () => {
130 describe('detects individual kinds correctly', () => {
131 test('detects ServerFn via .handler(', () => {
132 const code = `
133 import { createServerFn } from '@tanstack/react-start'
134 const fn = createServerFn().handler(() => 'hello')
135 `
136 expect(detectKindsInCode(code, 'client')).toEqual(new Set(['ServerFn']))
137 expect(detectKindsInCode(code, 'server')).toEqual(new Set(['ServerFn']))
138 })
139β
140 test('detects Middleware', () => {
141 const code = `
142 import { createMiddleware } from '@tanstack/react-start'
143 const mw = createMiddleware().server(({ next }) => next())
144 `
145 // Middleware is only valid on client
146 expect(detectKindsInCode(code, 'client')).toEqual(new Set(['Middleware']))
147 expect(detectKindsInCode(code, 'server')).toEqual(new Set())
148 })
149β
150 test('detects IsomorphicFn', () => {
151 const code = `
152 import { createIsomorphicFn } from '@tanstack/react-start'
153 const fn = createIsomorphicFn().client(() => 'c').server(() => 's')
154 `
155 expect(detectKindsInCode(code, 'client')).toEqual(
156 new Set(['IsomorphicFn']),
157 )
158 expect(detectKindsInCode(code, 'server')).toEqual(
159 new Set(['IsomorphicFn']),
160 )
161 })
162β
163 test('detects ServerOnlyFn', () => {
164 const code = `
165 import { createServerOnlyFn } from '@tanstack/react-start'
166 const fn = createServerOnlyFn(() => 'server only')
167 `
168 expect(detectKindsInCode(code, 'client')).toEqual(
169 new Set(['ServerOnlyFn']),
170 )
171 expect(detectKindsInCode(code, 'server')).toEqual(
172 new Set(['ServerOnlyFn']),
173 )
174 })
175β
176 test('detects ClientOnlyFn', () => {
177 const code = `
178 import { createClientOnlyFn } from '@tanstack/react-start'
179 const fn = createClientOnlyFn(() => 'client only')
180 `
181 expect(detectKindsInCode(code, 'client')).toEqual(
182 new Set(['ClientOnlyFn']),
183 )
184 expect(detectKindsInCode(code, 'server')).toEqual(
185 new Set(['ClientOnlyFn']),
186 )
187 })
188β
189 test('resets external transform regex state between detections', () => {
190 const compilerTransforms: Array<StartCompilerImportTransform> = [
191 {
192 name: 'global-detect',
193 environment: 'server',
194 imports: [{ libName: '@example/runtime', rootExport: 'renderThing' }],
195 detect: /\brenderThing\b/g,
196 transform: () => {},
197 },
198 ]
199 const code = `
200 import { renderThing } from '@example/runtime'
201 renderThing()
202 `
203β
204 expect(detectKindsInCode(code, 'server', { compilerTransforms })).toEqual(
205 new Set(['External:global-detect']),
206 )
207 expect(detectKindsInCode(code, 'server', { compilerTransforms })).toEqual(
208 new Set(['External:global-detect']),
209 )
210 })
211 })
212β
213 describe('detects multiple kinds in same file', () => {
214 test('detects ServerFn and IsomorphicFn', () => {
215 const code = `
216 import { createServerFn, createIsomorphicFn } from '@tanstack/react-start'
217 const serverFn = createServerFn().handler(() => 'hello')
218 const isoFn = createIsomorphicFn().client(() => 'client')
219 `
220 expect(detectKindsInCode(code, 'client')).toEqual(
221 new Set(['ServerFn', 'IsomorphicFn']),
222 )
223 })
224β
225 test('detects all kinds on client', () => {
226 const code = `
227 import { createServerFn, createMiddleware, createIsomorphicFn, createServerOnlyFn, createClientOnlyFn } from '@tanstack/react-start'
228 const a = createServerFn().handler(() => {})
229 const b = createMiddleware().server(({ next }) => next())
230 const c = createIsomorphicFn()
231 const d = createServerOnlyFn(() => {})
232 const e = createClientOnlyFn(() => {})
233 `
234 expect(detectKindsInCode(code, 'client')).toEqual(
235 new Set([
236 'ServerFn',
237 'Middleware',
238 'IsomorphicFn',
239 'ServerOnlyFn',
240 'ClientOnlyFn',
241 ]),
242 )
243 })
244β
245 test('detects all valid kinds on server (excludes Middleware)', () => {
246 const code = `
247 import { createServerFn, createMiddleware, createIsomorphicFn, createServerOnlyFn, createClientOnlyFn } from '@tanstack/react-start'
248 const a = createServerFn().handler(() => {})
249 const b = createMiddleware().server(({ next }) => next())
250 const c = createIsomorphicFn()
251 const d = createServerOnlyFn(() => {})
252 const e = createClientOnlyFn(() => {})
253 `
254 // Middleware should NOT be detected on server
255 expect(detectKindsInCode(code, 'server')).toEqual(
256 new Set(['ServerFn', 'IsomorphicFn', 'ServerOnlyFn', 'ClientOnlyFn']),
257 )
258 })
259 })
260β
261 describe('handles edge cases', () => {
262 test('returns empty set for code with no matching patterns', () => {
263 const code = `
264 const foo = 'bar'
265 function hello() { return 'world' }
266 `
267 expect(detectKindsInCode(code, 'client')).toEqual(new Set())
268 expect(detectKindsInCode(code, 'server')).toEqual(new Set())
269 })
270β
271 test('handles .handler with whitespace variations', () => {
272 const code1 = `fn.handler(() => {})`
273 const code2 = `fn.handler (() => {})`
274 const code3 = `fn.handler\n(() => {})`
275 const code4 = `fn.handler\t(() => {})`
276β
277 expect(detectKindsInCode(code1, 'client')).toEqual(new Set(['ServerFn']))
278 expect(detectKindsInCode(code2, 'client')).toEqual(new Set(['ServerFn']))
279 expect(detectKindsInCode(code3, 'client')).toEqual(new Set(['ServerFn']))
280 expect(detectKindsInCode(code4, 'client')).toEqual(new Set(['ServerFn']))
281 })
282β
283 test('handles whitespace between . and handler (reformatted code)', () => {
284 // When Vite/Babel reformats code, the dot can end up on a previous line
285 const code1 = `fn.\nhandler(() => {})`
286 const code2 = `fn.\n handler(() => {})`
287 const code3 = `fn. \n handler(() => {})`
288β
289 expect(detectKindsInCode(code1, 'client')).toEqual(new Set(['ServerFn']))
290 expect(detectKindsInCode(code2, 'client')).toEqual(new Set(['ServerFn']))
291 expect(detectKindsInCode(code3, 'client')).toEqual(new Set(['ServerFn']))
292 })
293β
294 test('detects createServerFn() call directly', () => {
295 // The pattern should match createServerFn() calls, not just .handler()
296 const code = `
297 import { createServerFn } from '@tanstack/react-start'
298 const fn = createServerFn()
299 `
300 expect(detectKindsInCode(code, 'client')).toEqual(new Set(['ServerFn']))
301 })
302β
303 test('does not false positive on similar function names', () => {
304 // Only exact createServerFn( should match, not variations
305 const code = `
306 const fn = createServerFnExample()
307 const fn2 = createServerFnLike()
308 const fn3 = mycreateServerFn()
309 const fn4 = _createServerFn()
310 `
311 expect(detectKindsInCode(code, 'client')).toEqual(new Set())
312 })
313β
314 test('does not false positive on similar names', () => {
315 const code = `
316 const myCreateServerFn = () => {}
317 const createServerFnLike = () => {}
318 // But this should match:
319 createServerFn().handler(() => {})
320 `
321 // Should only match because of .handler(, not because of variable names
322 expect(detectKindsInCode(code, 'client')).toEqual(new Set(['ServerFn']))
323 })
324 })
325})
326β
327describe('compiler handles multiple files with different kinds', () => {
328 test('single compiler instance correctly processes files with different kinds in succession', async () => {
329 const compiler = createFullCompiler('client')
330β
331 // File 1: ServerFn only
332 const result1 = await compiler.compile({
333 code: `
334 import { createServerFn } from '@tanstack/react-start'
335 export const fn = createServerFn().handler(() => 'hello')
336 `,
337 id: 'file1.ts',
338β
339 detectedKinds: new Set(['ServerFn']),
340 })
341 expect(result1).not.toBeNull()
342 expect(result1!.code).toContain('createClientRpc') // Client should have RPC stub
343 expect(result1!.code).not.toContain('createMiddleware')
344 expect(result1!.code).not.toContain('createIsomorphicFn')
345β
346 // File 2: Middleware only
347 const result2 = await compiler.compile({
348 code: `
349 import { createMiddleware } from '@tanstack/react-start'
350 export const mw = createMiddleware().server(({ next }) => {
351 console.log('server only')
352 return next()
353 })
354 `,
355 id: 'file2.ts',
356β
357 detectedKinds: new Set(['Middleware']),
358 })
359 expect(result2).not.toBeNull()
360 // .server() should be stripped on client
361 expect(result2!.code).not.toContain('console.log')
362 expect(result2!.code).not.toContain('createServerFn')
363β
364 // File 3: IsomorphicFn only
365 const result3 = await compiler.compile({
366 code: `
367 import { createIsomorphicFn } from '@tanstack/react-start'
368 export const fn = createIsomorphicFn()
369 .client(() => 'client-value')
370 .server(() => 'server-value')
371 `,
372 id: 'file3.ts',
373β
374 detectedKinds: new Set(['IsomorphicFn']),
375 })
376 expect(result3).not.toBeNull()
377 // Client should have client implementation
378 expect(result3!.code).toContain('client-value')
379 expect(result3!.code).not.toContain('server-value')
380β
381 // File 4: ServerOnlyFn only
382 const result4 = await compiler.compile({
383 code: `
384 import { createServerOnlyFn } from '@tanstack/react-start'
385 export const fn = createServerOnlyFn(() => 'server only value')
386 `,
387 id: 'file4.ts',
388β
389 detectedKinds: new Set(['ServerOnlyFn']),
390 })
391 expect(result4).not.toBeNull()
392 // Client should have error throw
393 expect(result4!.code).toContain('throw new Error')
394 expect(result4!.code).not.toContain('server only value')
395β
396 // File 5: Mix of ServerFn and IsomorphicFn
397 const result5 = await compiler.compile({
398 code: `
399 import { createServerFn, createIsomorphicFn } from '@tanstack/react-start'
400 export const serverFn = createServerFn().handler(() => 'hello')
401 export const isoFn = createIsomorphicFn().client(() => 'client-iso')
402 `,
403 id: 'file5.ts',
404β
405 detectedKinds: new Set(['ServerFn', 'IsomorphicFn']),
406 })
407 expect(result5).not.toBeNull()
408 expect(result5!.code).toContain('createClientRpc') // ServerFn RPC
409 expect(result5!.code).toContain('client-iso') // IsomorphicFn client impl
410 })
411β
412 test('compiler works correctly when processing same kind multiple times', async () => {
413 const compiler = createFullCompiler('client')
414β
415 // First file with IsomorphicFn
416 const result1 = await compiler.compile({
417 code: `
418 import { createIsomorphicFn } from '@tanstack/react-start'
419 export const fn1 = createIsomorphicFn().client(() => 'first')
420 `,
421 id: 'first.ts',
422β
423 detectedKinds: new Set(['IsomorphicFn']),
424 })
425 expect(result1!.code).toContain('first')
426β
427 // Second file with different IsomorphicFn
428 const result2 = await compiler.compile({
429 code: `
430 import { createIsomorphicFn } from '@tanstack/react-start'
431 export const fn2 = createIsomorphicFn().client(() => 'second')
432 `,
433 id: 'second.ts',
434β
435 detectedKinds: new Set(['IsomorphicFn']),
436 })
437 expect(result2!.code).toContain('second')
438 expect(result2!.code).not.toContain('first') // Should not leak from previous file
439 })
440β
441 test('server environment excludes Middleware from detected kinds', async () => {
442 const compiler = createFullCompiler('server')
443β
444 // Even if Middleware is in detectedKinds, server env should ignore it
445 const result = await compiler.compile({
446 code: `
447 import { createMiddleware } from '@tanstack/react-start'
448 export const mw = createMiddleware().server(({ next }) => next())
449 `,
450 id: 'middleware.ts',
451β
452 // Intentionally including Middleware even though it's server env
453 detectedKinds: new Set(['Middleware']),
454 })
455 // Should return null since Middleware is not valid on server
456 expect(result).toBeNull()
457 })
458})
459β
460describe('compiler handles external import transforms', () => {
461 test('runs configured direct-call transforms before server function extraction', async () => {
462 const compiler = createExternalTransformCompiler()
463β
464 const result = await compiler.compile({
465 code: `
466 import { createServerFn } from '@tanstack/react-start'
467 import { renderThing as renderAlias } from '@example/runtime'
468β
469 export const getComponent = createServerFn({ method: 'GET' }).handler(async () => {
470 return renderAlias(<ServerCard />)
471 })
472 `,
473 id: '/test/src/routes/card.tsx?tss-serverfn-split',
474 })
475β
476 expect(result).not.toBeNull()
477 expect(result!.code).toContain('injected: loadThing()')
478 expect(result!.code).toContain('renderAlias(<ServerCard />,')
479 })
480β
481 test('runs configured direct-call transforms for namespace imports', async () => {
482 const compiler = createExternalTransformCompiler()
483β
484 const result = await compiler.compile({
485 code: `
486 import * as runtime from '@example/runtime'
487β
488 export const component = runtime.renderThing(<ServerCard />)
489 `,
490 id: '/test/src/routes/card.tsx',
491 })
492β
493 expect(result).not.toBeNull()
494 expect(result!.code).toContain('injected: loadThing()')
495 expect(result!.code).toContain('runtime.renderThing(<ServerCard />,')
496 })
497β
498 test('does not run external transforms for shadowed import names', async () => {
499 const compiler = createExternalTransformCompiler()
500β
501 const result = await compiler.compile({
502 code: `
503 import { renderThing } from '@example/runtime'
504β
505 export function component() {
506 const renderThing = (node: React.ReactNode) => node
507 return renderThing(<LocalCard />)
508 }
509 `,
510 id: '/test/src/routes/card.tsx',
511 })
512β
513 expect(result).toBeNull()
514 })
515β
516 // Source: @vitejs/plugin-rsc transform fixtures (per row)
517 test.each([
518 {
519 // hoist/function-hoist-block.js: block-level functions are block scoped
520 name: 'past a function declared in a nested block',
521 body: `{
522 function renderThing() {}
523 }
524 return renderThing(<Card />)`,
525 transformed: true,
526 },
527 {
528 // scope/param-default-var-hoisting.js
529 name: 'in a parameter default next to a var of the same name',
530 params: `element = renderThing(<Card />)`,
531 body: `var renderThing = null
532 return element`,
533 transformed: true,
534 },
535 {
536 // hoist/catch-binding-shadow.js
537 name: 'in a try block whose catch parameter shadows it',
538 body: `try {
539 return renderThing(<Card />)
540 } catch (renderThing) {
541 return renderThing
542 }`,
543 transformed: true,
544 },
545 {
546 // hoist/var-hoist-block.js
547 name: 'after a var in a nested block shadows it',
548 body: `if (Math.random() < 2) {
549 var renderThing = (element) => element
550 }
551 return renderThing(<Card />)`,
552 transformed: false,
553 },
554 {
555 // scope/fn-decl-hoisting.js
556 name: 'before a hoisted function declaration shadows it',
557 body: `return renderThing(<Card />)
558 function renderThing(element) {
559 return element
560 }`,
561 transformed: false,
562 },
563 {
564 // scope/catch-param.js
565 name: 'in a catch block whose parameter shadows it',
566 body: `try {
567 throw (element) => element
568 } catch (renderThing) {
569 return renderThing(<Card />)
570 }`,
571 transformed: false,
572 },
573 ])(
574 'runs external transforms on a call $name only if it reads the import',
575 async ({ params = '', body, transformed }) => {
576 const compiler = createExternalTransformCompiler()
577β
578 // The top-level call keeps the module transformed in every row.
579 const result = await compiler.compile({
580 code: `
581 import { renderThing } from '@example/runtime'
582 export const transformed = renderThing(<Card />)
583 export function render(${params}) {
584 ${body}
585 }
586 `,
587 id: '/test/src/routes/card.tsx',
588 })
589β
590 expect(result).not.toBeNull()
591 const renderStart = result!.code.indexOf('function render(')
592 expect(renderStart).toBeGreaterThanOrEqual(0)
593 const render = result!.code.slice(renderStart)
594 expect(render.includes('injected: loadThing()')).toBe(transformed)
595 },
596 )
597})
598β
599describe('server function provider module directives', () => {
600 test.each([
601 'export const Route = { component: Page }',
602 'export function Route() { return Page() }',
603 'export default function Route() { return Page() }',
604 ])(
605 'removes browser-only dependencies of stripped exports: %s',
606 async (route) => {
607 const compiler = createFullCompiler('server')
608 const result = await compiler.compile({
609 code: `
610 import { createServerFn } from '@tanstack/react-start'
611 export const getMessage = createServerFn().handler(() => 'server message')
612 const browserState = { window }
613 function Page() { return browserState }
614 ${route}
615 `,
616 id: '/test/src/routes/message.tsx?tss-serverfn-split',
617 })
618β
619 expect(result).not.toBeNull()
620 expect(result!.code).toContain('server message')
621 expect(result!.code).toContain('createServerRpc')
622 expect(result!.code).not.toContain('window')
623 expect(result!.code).not.toContain('browserState')
624 expect(result!.code).not.toContain('Page')
625 expect(result!.code).not.toContain('Route')
626 },
627 )
628β
629 const code = `
630 import { createServerFn } from '@tanstack/react-start'
631β
632 export const getMessage = createServerFn({ method: 'GET' }).handler(() => {
633 return 'hello'
634 })
635 `
636β
637 test('does not add module directives without compiler configuration', async () => {
638 const compiler = createFullCompiler('server')
639β
640 const result = await compiler.compile({
641 code,
642 id: '/test/src/routes/message.tsx?tss-serverfn-split',
643 })
644β
645 expect(result).not.toBeNull()
646 expect(result!.code).not.toContain('"use server-entry"')
647 })
648β
649 test('adds configured module directives to provider files only', async () => {
650 const compiler = createFullCompiler('server', {
651 serverFnProviderModuleDirectives: ['use server-entry'],
652 })
653β
654 const providerResult = await compiler.compile({
655 code,
656 id: '/test/src/routes/message.tsx?tss-serverfn-split',
657 })
658 const callerResult = await compiler.compile({
659 code,
660 id: '/test/src/routes/message.tsx',
661 })
662β
663 expect(providerResult).not.toBeNull()
664 expect(providerResult!.code).toContain('"use server-entry";')
665 expect(callerResult).not.toBeNull()
666 expect(callerResult!.code).not.toContain('"use server-entry"')
667 })
668})
669β
670describe('edge cases for detectedKinds', () => {
671 test('empty detectedKinds returns null (no candidates to process)', async () => {
672 const compiler = createFullCompiler('client')
673β
674 const result = await compiler.compile({
675 code: `
676 // This file somehow passed Vite filter but has no actual kinds
677 const foo = 'bar'
678 // Maybe a false positive match like "handler(" in a comment
679 // .handler( in string
680 `,
681 id: 'empty.ts',
682β
683 detectedKinds: new Set(), // Empty set
684 })
685β
686 expect(result).toBeNull()
687 })
688β
689 test('detectedKinds not provided falls back to all valid kinds', async () => {
690 const compiler = createFullCompiler('client')
691β
692 // When detectedKinds is not provided, should check all valid kinds
693 const result = await compiler.compile({
694 code: `
695 import { createIsomorphicFn } from '@tanstack/react-start'
696 export const fn = createIsomorphicFn().client(() => 'works')
697 `,
698 id: 'no-detected.ts',
699β
700 // No detectedKinds provided
701 })
702β
703 expect(result).not.toBeNull()
704 expect(result!.code).toContain('works')
705 })
706β
707 test('detectedKinds with invalid kinds for env are filtered out', async () => {
708 const compiler = createFullCompiler('server')
709β
710 // Passing Middleware (invalid for server) along with valid kind
711 const result = await compiler.compile({
712 code: `
713 import { createIsomorphicFn } from '@tanstack/react-start'
714 export const fn = createIsomorphicFn().server(() => 'server-impl')
715 `,
716 id: 'filtered.ts',
717β
718 detectedKinds: new Set(['Middleware', 'IsomorphicFn']), // Middleware should be filtered
719 })
720β
721 expect(result).not.toBeNull()
722 expect(result!.code).toContain('server-impl')
723 })
724})
725β
726test('ingestModule handles empty code gracefully', () => {
727 const compiler = new StartCompiler({
728 env: 'client',
729 ...getDefaultTestOptions('client'),
730 lookupKinds: new Set(['ServerFn']),
731 lookupConfigurations: [],
732 getKnownServerFns: () => ({}),
733 loadModule: async () => {},
734 resolveId: async (id) => id,
735 })
736β
737 // Should not throw when ingesting empty module
738 expect(() => {
739 compiler.ingestModule({ code: '', id: 'empty-types.ts' })
740 }).not.toThrow()
741β
742 // Should also handle whitespace-only modules
743 expect(() => {
744 compiler.ingestModule({ code: ' \n\t ', id: 'whitespace.ts' })
745 }).not.toThrow()
746})
747β
748test('compile caches modules without detected candidates for transitive importer traversal', async () => {
749 const compiler = new StartCompiler({
750 env: 'client',
751 ...getDefaultTestOptions('client'),
752 lookupKinds: new Set(['ServerFn']),
753 lookupConfigurations: [],
754 getKnownServerFns: () => ({}),
755 loadModule: async () => {},
756 resolveId: async (source, importer) => {
757 if (source === './leaf' && importer === '/src/plain.ts') {
758 return '/src/leaf.ts'
759 }
760β
761 if (source === './plain' && importer === '/src/parent.ts') {
762 return '/src/plain.ts'
763 }
764β
765 return null
766 },
767 })
768β
769 await compiler.compile({
770 id: '/src/plain.ts',
771 code: `
772 import { value } from './leaf'
773 export const plain = value
774 `,
775 detectedKinds: new Set(),
776 })
777β
778 await compiler.compile({
779 id: '/src/parent.ts',
780 code: `
781 import { plain } from './plain'
782 export const parent = plain
783 `,
784 detectedKinds: new Set(),
785 })
786β
787 expect(await compiler.getTransitiveImporters('/src/leaf.ts')).toEqual(
788 new Set(['/src/plain.ts', '/src/parent.ts']),
789 )
790 expect(await compiler.getTransitiveImporters(['/src/leaf.ts'])).toEqual(
791 new Set(['/src/plain.ts', '/src/parent.ts']),
792 )
793})
794β
795describe('calling result of createServerOnlyFn/createClientOnlyFn', () => {
796 // This tests the fix for https://github.com/TanStack/router/issues/6643
797 // When a file has both createServerFn and createServerOnlyFn, and the result
798 // of createServerOnlyFn is invoked (e.g., `const result = myServerOnlyFn()`),
799 // the compiler should NOT treat that invocation as a ServerOnlyFn candidate.
800 // The var binding holds the RESULT of the factory, not the factory itself.
801β
802 test('invocation of createServerOnlyFn result is not treated as a candidate', async () => {
803 const compiler = createFullCompiler('server')
804β
805 const code = `
806 import { createServerFn, createServerOnlyFn } from '@tanstack/react-start'
807β
808 const createRandomNumber = createServerOnlyFn(() => Math.floor(Math.random() * 100))
809 const randomNumber = createRandomNumber()
810β
811 export const personServerFn = createServerFn({ method: 'GET' })
812 .handler(({ data: name }) => {
813 return { name, randomNumber }
814 })
815 `
816β
817 // Should NOT throw "createServerOnlyFn() must be called with a function!"
818 // The main file output is the split-file version (createServerFn splits the handler),
819 // so we just verify it compiles without error and produces valid output.
820 const result = await compiler.compile({ code, id: 'test-6643.ts' })
821 expect(result).not.toBeNull()
822 // The output should contain the personServerFn export (with SSR RPC reference)
823 expect(result!.code).toContain('personServerFn')
824 })
825β
826 test('invocation of createClientOnlyFn result is not treated as a candidate', async () => {
827 const compiler = createFullCompiler('client')
828β
829 const code = `
830 import { createClientOnlyFn, createServerFn } from '@tanstack/react-start'
831β
832 const getWindowWidth = createClientOnlyFn(() => window.innerWidth)
833 const width = getWindowWidth()
834β
835 export const personServerFn = createServerFn({ method: 'GET' })
836 .handler(({ data: name }) => {
837 return { name }
838 })
839 `
840β
841 const result = await compiler.compile({ code, id: 'test-6643-client.ts' })
842 expect(result).not.toBeNull()
843 expect(result!.code).toContain('window.innerWidth')
844 expect(result!.code).toContain('getWindowWidth()')
845 })
846β
847 test('re-exported createServerOnlyFn still resolves correctly', async () => {
848 // Ensure the fix doesn't break re-export chain resolution (issue #6583)
849 const virtualModules: Record<string, string> = {
850 '@tanstack/start-client-core': `
851 export { createServerOnlyFn } from '@tanstack/start-fn-stubs'
852 `,
853 }
854β
855 const compiler: StartCompiler = new StartCompiler({
856 env: 'server',
857 envName: 'ssr',
858 root: '/test',
859 framework: 'react' as const,
860 providerEnvName: 'ssr',
861 lookupKinds: new Set(['ServerOnlyFn', 'ClientOnlyFn']),
862 lookupConfigurations: [],
863 getKnownServerFns: () => ({}),
864 loadModule: async (id) => {
865 const code = virtualModules[id]
866 if (code) {
867 compiler.ingestModule({ code, id })
868 }
869 },
870 resolveId: async (id) => {
871 return virtualModules[id] ? id : null
872 },
873 })
874β
875 const code = `
876 import { createServerOnlyFn } from '@tanstack/start-client-core'
877 const myFn = createServerOnlyFn(() => 'server-only-value')
878 `
879β
880 const result = await compiler.compile({ code, id: 'reexport-test.ts' })
881 expect(result).not.toBeNull()
882 expect(result!.code).toContain('server-only-value')
883 })
884β
885 test('aliased factory is still detected as a candidate', async () => {
886 // `const createSO = createServerOnlyFn` is an alias, not an invocation.
887 // `createSO(() => ...)` should still be treated as ServerOnlyFn.
888 const compiler = createFullCompiler('server')
889β
890 const code = `
891 import { createServerOnlyFn } from '@tanstack/react-start'
892β
893 const createSO = createServerOnlyFn
894 const myFn = createSO(() => 'aliased-server-only')
895 `
896β
897 const result = await compiler.compile({ code, id: 'alias-test.ts' })
898 expect(result).not.toBeNull()
899 // The factory should be unwrapped on server env
900 expect(result!.code).toContain('aliased-server-only')
901 })
902})
903β
904describe('re-export chain resolution', () => {
905 // This tests the fix for https://github.com/TanStack/router/issues/6583
906 // Third-party packages may import from @tanstack/start-client-core (which re-exports
907 // from @tanstack/start-fn-stubs) rather than directly from @tanstack/react-start.
908 // The compiler should correctly resolve these imports via the slow path.
909β
910 // Virtual module contents for simulating the re-export chain:
911 // @third-party/lib -> @tanstack/start-client-core -> @tanstack/start-fn-stubs
912 //
913 // Note: We don't need a virtual module for @tanstack/start-fn-stubs because
914 // init() hardcodes it in knownRootImports. When the slow path resolution reaches
915 // that package, it uses the fast path lookup and never needs to parse the module.
916 const virtualModules: Record<string, string> = {
917 // The client-core package that re-exports from stubs
918 '@tanstack/start-client-core': `
919 export { createIsomorphicFn, createServerOnlyFn, createClientOnlyFn } from '@tanstack/start-fn-stubs'
920 `,
921 // A third-party package that imports from start-client-core
922 '@third-party/lib': `
923 import { createIsomorphicFn, createClientOnlyFn } from '@tanstack/start-client-core'
924β
925 export const getThemeData = createIsomorphicFn()
926 .client(() => 'client-theme')
927 .server(() => 'server-theme')
928β
929 export const initClientFeature = createClientOnlyFn(() => {
930 console.log('client only initialization')
931 return 'initialized'
932 })
933 `,
934 }
935β
936 function createCompilerWithVirtualModules(env: 'client' | 'server') {
937 const envName = env === 'client' ? 'client' : 'ssr'
938 const lookupKinds: Set<LookupKind> = new Set([
939 'IsomorphicFn',
940 'ServerOnlyFn',
941 'ClientOnlyFn',
942 ])
943β
944 // Note: We use empty lookupConfigurations because this test is specifically
945 // testing the slow path resolution through re-export chains. The compiler
946 // should still work because @tanstack/start-fn-stubs is hardcoded in
947 // knownRootImports during init().
948 const compiler: StartCompiler = new StartCompiler({
949 env,
950 envName,
951 root: '/test',
952 framework: 'react' as const,
953 providerEnvName: 'ssr',
954 lookupKinds,
955 lookupConfigurations: [],
956 getKnownServerFns: () => ({}),
957 loadModule: async (id) => {
958 const code = virtualModules[id]
959 if (code) {
960 compiler.ingestModule({ code, id })
961 }
962 },
963 resolveId: async (id) => {
964 return virtualModules[id] ? id : null
965 },
966 })
967β
968 return compiler
969 }
970β
971 const testCases: Array<{
972 env: 'client' | 'server'
973 fn: string
974 shouldContain: Array<string>
975 shouldNotContain: Array<string>
976 }> = [
977 {
978 env: 'client',
979 fn: 'createIsomorphicFn',
980 shouldContain: ['client-theme'],
981 shouldNotContain: ['server-theme'],
982 },
983 {
984 env: 'server',
985 fn: 'createIsomorphicFn',
986 shouldContain: ['server-theme'],
987 shouldNotContain: ['client-theme'],
988 },
989 {
990 env: 'client',
991 fn: 'createClientOnlyFn',
992 shouldContain: ['client only initialization'],
993 shouldNotContain: [],
994 },
995 {
996 env: 'server',
997 fn: 'createClientOnlyFn',
998 shouldContain: ['throw new Error'],
999 shouldNotContain: ['client only initialization'],
1000 },
1001 ]
1002β
1003 test.each(testCases)(
1004 'resolves $fn via re-export chain on $env',
1005 async ({ env, shouldContain, shouldNotContain }) => {
1006 const compiler = createCompilerWithVirtualModules(env)
1007 const code = virtualModules['@third-party/lib']!
1008β
1009 const result = await compiler.compile({
1010 code,
1011 id: '@third-party/lib',
1012 })
1013β
1014 expect(result).not.toBeNull()
1015 for (const str of shouldContain) {
1016 expect(result!.code).toContain(str)
1017 }
1018 for (const str of shouldNotContain) {
1019 expect(result!.code).not.toContain(str)
1020 }
1021 },
1022 )
1023β
1024 test('handles deeper re-export chains', async () => {
1025 // Add another level to the chain
1026 const deeperVirtualModules: Record<string, string> = {
1027 ...virtualModules,
1028 // Another intermediate package
1029 '@another-intermediate/pkg': `
1030 export { createIsomorphicFn } from '@tanstack/start-client-core'
1031 `,
1032 '@deep-third-party/lib': `
1033 import { createIsomorphicFn } from '@another-intermediate/pkg'
1034 export const deepFn = createIsomorphicFn()
1035 .client(() => 'deep-client')
1036 .server(() => 'deep-server')
1037 `,
1038 }
1039β
1040 const compiler: StartCompiler = new StartCompiler({
1041 env: 'client',
1042 envName: 'client',
1043 root: '/test',
1044 framework: 'react' as const,
1045 providerEnvName: 'ssr',
1046 lookupKinds: new Set(['IsomorphicFn', 'ServerOnlyFn', 'ClientOnlyFn']),
1047 lookupConfigurations: [],
1048 getKnownServerFns: () => ({}),
1049 loadModule: async (id) => {
1050 const code = deeperVirtualModules[id]
1051 if (code) {
1052 compiler.ingestModule({ code, id })
1053 }
1054 },
1055 resolveId: async (id) => {
1056 return deeperVirtualModules[id] ? id : null
1057 },
1058 })
1059β
1060 const result = await compiler.compile({
1061 code: deeperVirtualModules['@deep-third-party/lib']!,
1062 id: '@deep-third-party/lib',
1063 })
1064β
1065 expect(result).not.toBeNull()
1066 expect(result!.code).toContain('deep-client')
1067 expect(result!.code).not.toContain('deep-server')
1068 })
1069β
1070 test.each([
1071 {
1072 name: 'named re-export cycle',
1073 virtualModules: {
1074 './factory-a': `
1075 export { createServerOnlyFn } from './factory-b'
1076 `,
1077 './factory-b': `
1078 export { createServerOnlyFn } from './factory-a'
1079 `,
1080 },
1081 },
1082 {
1083 name: 'import alias cycle',
1084 virtualModules: {
1085 './factory-a': `
1086 import { createServerOnlyFn } from './factory-b'
1087 export { createServerOnlyFn }
1088 `,
1089 './factory-b': `
1090 import { createServerOnlyFn } from './factory-a'
1091 export { createServerOnlyFn }
1092 `,
1093 },
1094 },
1095 {
1096 name: 'export-star cycle',
1097 virtualModules: {
1098 './factory-a': `
1099 export * from './factory-b'
1100 `,
1101 './factory-b': `
1102 export * from './factory-a'
1103 `,
1104 },
1105 },
1106 ])('handles circular import chain: $name', async ({ virtualModules }) => {
1107 const compiler: StartCompiler = new StartCompiler({
1108 env: 'server',
1109 envName: 'ssr',
1110 root: '/test',
1111 framework: 'react' as const,
1112 providerEnvName: 'ssr',
1113 lookupKinds: new Set(['ServerOnlyFn']),
1114 lookupConfigurations: [],
1115 getKnownServerFns: () => ({}),
1116 loadModule: async (id) => {
1117 const code = virtualModules[id as keyof typeof virtualModules]
1118 if (code) {
1119 compiler.ingestModule({ code, id })
1120 }
1121 },
1122 resolveId: async (id) => {
1123 return id in virtualModules ? id : null
1124 },
1125 mode: 'build',
1126 })
1127β
1128 const result = await compiler.compile({
1129 id: 'circular-import-test.ts',
1130 code: `
1131 import { createServerOnlyFn } from './factory-a'
1132 const myFn = createServerOnlyFn(() => 'server-only-value')
1133 `,
1134 })
1135β
1136 expect(result).toBeNull()
1137 })
1138β
1139 test('ingestModule populates module metadata for later resolution', async () => {
1140 const compiler: StartCompiler = new StartCompiler({
1141 env: 'server',
1142 envName: 'ssr',
1143 root: '/test',
1144 framework: 'react' as const,
1145 providerEnvName: 'ssr',
1146 lookupKinds: new Set(['ServerOnlyFn']),
1147 lookupConfigurations: [],
1148 getKnownServerFns: () => ({}),
1149 loadModule: async () => {},
1150 resolveId: async (id) => {
1151 return id === '@tanstack/start-client-core' ? id : null
1152 },
1153 mode: 'build',
1154 })
1155β
1156 compiler.ingestModule({
1157 id: '@tanstack/start-client-core',
1158 code: `
1159 export { createServerOnlyFn } from '@tanstack/start-fn-stubs'
1160 `,
1161 })
1162β
1163 const result = await compiler.compile({
1164 id: 'cached-module-test.ts',
1165 code: `
1166 import { createServerOnlyFn } from '@tanstack/start-client-core'
1167 const myFn = createServerOnlyFn(() => 'cached-server-only-value')
1168 `,
1169 })
1170β
1171 expect(result).not.toBeNull()
1172 expect(result!.code).toContain('cached-server-only-value')
1173 })
1174β
1175 test('invalidateModule clears cached module resolution state', async () => {
1176 const virtualModules: Record<string, string> = {
1177 './factory': `
1178 export { createServerOnlyFn } from './factory-inner'
1179 `,
1180 './factory-inner': `
1181 export { createServerOnlyFn } from '@tanstack/start-fn-stubs'
1182 `,
1183 }
1184β
1185 const compiler: StartCompiler = new StartCompiler({
1186 env: 'server',
1187 envName: 'ssr',
1188 root: '/test',
1189 framework: 'react' as const,
1190 providerEnvName: 'ssr',
1191 lookupKinds: new Set(['ServerOnlyFn', 'ClientOnlyFn']),
1192 lookupConfigurations: [],
1193 getKnownServerFns: () => ({}),
1194 loadModule: async (id) => {
1195 const code = virtualModules[id]
1196 if (code) {
1197 compiler.ingestModule({ code, id })
1198 }
1199 },
1200 resolveId: async (id) => {
1201 return virtualModules[id] ? id : null
1202 },
1203 mode: 'build',
1204 })
1205β
1206 const source = `
1207 import { createServerOnlyFn } from './factory'
1208 const myFn = createServerOnlyFn(() => 'server-only-value')
1209 `
1210β
1211 const initialResult = await compiler.compile({
1212 id: 'transitive-invalidation-test.ts',
1213 code: source,
1214 })
1215β
1216 expect(initialResult).not.toBeNull()
1217 expect(initialResult!.code).toContain('server-only-value')
1218 expect(initialResult!.code).not.toContain('throw new Error')
1219β
1220 virtualModules['./factory-inner'] = `
1221 export { createClientOnlyFn as createServerOnlyFn } from '@tanstack/start-fn-stubs'
1222 `
1223β
1224 expect(compiler.invalidateModule('./factory-inner')).toBe(true)
1225β
1226 const updatedResult = await compiler.compile({
1227 id: 'transitive-invalidation-test.ts',
1228 code: source,
1229 })
1230β
1231 expect(updatedResult).not.toBeNull()
1232 expect(updatedResult!.code).toContain('throw new Error')
1233 expect(updatedResult!.code).not.toContain('server-only-value')
1234 })
1235β
1236 test('invalidateModule preserves root lookup metadata while clearing importer memoization', async () => {
1237 const virtualModules: Record<string, string> = {
1238 './factory': `
1239 export { createServerOnlyFn } from './factory-inner'
1240 `,
1241 './factory-inner': `
1242 export { createServerOnlyFn } from '@tanstack/start-fn-stubs'
1243 `,
1244 }
1245β
1246 const compiler: StartCompiler = new StartCompiler({
1247 env: 'server',
1248 envName: 'ssr',
1249 root: '/test',
1250 framework: 'react' as const,
1251 providerEnvName: 'ssr',
1252 lookupKinds: new Set(['ServerOnlyFn', 'ClientOnlyFn']),
1253 lookupConfigurations: [
1254 {
1255 libName: '@tanstack/react-start',
1256 rootExport: 'createServerOnlyFn',
1257 kind: 'ServerOnlyFn',
1258 },
1259 {
1260 libName: '@tanstack/react-start',
1261 rootExport: 'createClientOnlyFn',
1262 kind: 'ClientOnlyFn',
1263 },
1264 ],
1265 getKnownServerFns: () => ({}),
1266 loadModule: async (id) => {
1267 const code = virtualModules[id]
1268 if (code) {
1269 compiler.ingestModule({ code, id })
1270 }
1271 },
1272 resolveId: async (id) => {
1273 return virtualModules[id] ? id : null
1274 },
1275 })
1276β
1277 const source = `
1278 import { createServerOnlyFn } from './factory'
1279 const myFn = createServerOnlyFn(() => 'server-only-value')
1280 `
1281β
1282 const initialResult = await compiler.compile({
1283 id: 'memoized-invalidation-test.ts',
1284 code: source,
1285 })
1286β
1287 expect(initialResult).not.toBeNull()
1288 expect(initialResult!.code).toContain('server-only-value')
1289β
1290 virtualModules['./factory-inner'] = `
1291 export { createClientOnlyFn as createServerOnlyFn } from '@tanstack/start-fn-stubs'
1292 `
1293β
1294 expect(compiler.invalidateModule('./factory-inner')).toBe(true)
1295β
1296 const updatedResult = await compiler.compile({
1297 id: 'memoized-invalidation-test.ts',
1298 code: source,
1299 })
1300β
1301 expect(updatedResult).not.toBeNull()
1302 expect(updatedResult!.code).toContain('throw new Error')
1303 expect(updatedResult!.code).not.toContain('server-only-value')
1304 })
1305β
1306 test('recompiles aliased env-only direct calls when only server function syntax is detected', async () => {
1307 const virtualModules: Record<string, string> = {
1308 './factory': `
1309 export { createServerOnlyFn } from '@tanstack/start-fn-stubs'
1310 `,
1311 }
1312β
1313 const compiler: StartCompiler = new StartCompiler({
1314 env: 'server',
1315 envName: 'ssr',
1316 root: '/test',
1317 framework: 'react' as const,
1318 providerEnvName: 'ssr',
1319 mode: 'build',
1320 lookupKinds: new Set(['ServerFn', 'ServerOnlyFn', 'ClientOnlyFn']),
1321 lookupConfigurations: [
1322 {
1323 libName: '@tanstack/react-start',
1324 rootExport: 'createServerFn',
1325 kind: 'Root',
1326 },
1327 ],
1328 getKnownServerFns: () => ({}),
1329 loadModule: async (id) => {
1330 const code = virtualModules[id]
1331 if (code) {
1332 compiler.ingestModule({ code, id })
1333 }
1334 },
1335 resolveId: async (id) => {
1336 return virtualModules[id] ? id : null
1337 },
1338 })
1339β
1340 const source = `
1341 import { createServerFn } from '@tanstack/react-start'
1342 import { createServerOnlyFn as createServerFnHmrFactory } from './factory'
1343β
1344 const serverOnlyImpl = createServerFnHmrFactory(() => 'server-only-value')
1345β
1346 export const serverFn = createServerFn().handler(async () => {
1347 return serverOnlyImpl()
1348 })
1349 `
1350β
1351 const providerId = 'aliased-env-only-server-fn.ts?tss-serverfn-split'
1352β
1353 const initialResult = await compiler.compile({
1354 id: providerId,
1355 code: source,
1356 detectedKinds: new Set(['ServerFn']),
1357 })
1358β
1359 expect(initialResult).not.toBeNull()
1360 expect(initialResult!.code).toContain('server-only-value')
1361 expect(initialResult!.code).not.toContain('throw new Error')
1362β
1363 virtualModules['./factory'] = `
1364 export { createClientOnlyFn as createServerOnlyFn } from '@tanstack/start-fn-stubs'
1365 `
1366β
1367 expect(compiler.invalidateModule('./factory')).toBe(true)
1368β
1369 const updatedResult = await compiler.compile({
1370 id: providerId,
1371 code: source,
1372 detectedKinds: new Set(['ServerFn']),
1373 })
1374β
1375 expect(updatedResult).not.toBeNull()
1376 expect(updatedResult!.code).toContain('throw new Error')
1377 expect(updatedResult!.code).not.toContain('server-only-value')
1378 })
1379})
1380β
1381test('leaves unrelated SSR middleware chains to the bundler when a server function factory is present', async () => {
1382 const compiler = new StartCompiler({
1383 ...getDefaultTestOptions('server'),
1384 env: 'server',
1385 mode: 'build',
1386 lookupKinds: getLookupKindsForEnv('server'),
1387 lookupConfigurations: getLookupConfigurationsForEnv('server', 'react'),
1388 getKnownServerFns: () => ({}),
1389 resolveId: async () => {
1390 throw new Error(
1391 'SSR host has no package import conditions for this unrelated binding',
1392 )
1393 },
1394 loadModule: async () => {
1395 throw new Error('Unexpected compiler dependency load')
1396 },
1397 })
1398 const code = `import { createMiddleware, createServerFn } from '@tanstack/react-start';
1399const middleware = createMiddleware({ type: 'function' }).server(({ next }) => next({ context: { role: 'admin' } }));
1400export const factory = createServerFn().middleware([middleware]);`
1401 await expect(
1402 compiler.compile({
1403 id: '/test/auth-wrapper.ts',
1404 code,
1405 detectedKinds: detectKindsInCode(code, 'server'),
1406 }),
1407 ).resolves.toBeNull()
1408})
1409β
1410test.each([
1411 `(createServerOnlyFn)(() => 'server-value')`,
1412 `(createIsomorphicFn)().server(() => 'server-value').client(() => 'client-value')`,
1413 `(createIsomorphicFn().server(() => 'server-value')).client(() => 'client-value')`,
1414])(
1415 'compiles transparent parentheses around native call chains: %s',
1416 async (expression) => {
1417 const compiler = createFullCompiler('server')
1418 const result = await compiler.compile({
1419 id: '/test/parenthesized.ts',
1420 code: `import { createServerOnlyFn, createIsomorphicFn } from '@tanstack/react-start'; export const fn = ${expression}`,
1421 })
1422β
1423 expect(result).not.toBeNull()
1424 const evaluate = new Function(
1425 `${result!.code.replace(/^import .+;$/gm, '').replace(/\bexport (?=const)/g, '')}\nreturn fn`,
1426 )
1427 expect(evaluate()()).toBe('server-value')
1428 },
1429)
1430β
1431test('compiles a server function through a parenthesized namespace receiver', async () => {
1432 const compiler = createFullCompiler('client')
1433 const result = await compiler.compile({
1434 id: '/test/namespace-parenthesized.ts',
1435 code: `import * as Start from '@tanstack/react-start'; export const fn = (Start).createServerFn().handler(() => 'private-server-body')`,
1436 })
1437 expect(result).not.toBeNull()
1438 expect(result!.code).not.toContain('private-server-body')
1439 expect(result!.code).toContain('createClientRpc')
1440})
1441β
1442// The compiler currently mistakes the local parameter for the namespace import.
1443// Keep the intended behavior executable until binding resolution is corrected.
1444test.fails(
1445 'preserves a shadowed parenthesized namespace receiver',
1446 async () => {
1447 const compiler = createFullCompiler('client')
1448 const code = `import * as Start from '@tanstack/react-start'; export function fn(Start) { return (Start).createServerFn().handler(() => 'local-runtime-body') }`
1449 const result = await compiler.compile({
1450 id: '/test/shadowed-namespace.ts',
1451 code,
1452 })
1453 const output = result?.code ?? code
1454 expect(output).toContain('local-runtime-body')
1455 expect(output).not.toContain('createClientRpc')
1456 },
1457)
1458β
1459test.each([
1460 `(Start).createServerOnlyFn(() => 'private-server-body')`,
1461 `(Start).createIsomorphicFn().server(() => 'private-server-body').client(() => 'client-value')`,
1462 `(Start).createMiddleware().server(() => 'private-server-body')`,
1463])(
1464 'removes server-only code through a parenthesized namespace: %s',
1465 async (expression) => {
1466 const compiler = createFullCompiler('client')
1467 const result = await compiler.compile({
1468 id: '/test/namespace-builtins.ts',
1469 code: `import * as Start from '@tanstack/react-start'; export const fn = ${expression}`,
1470 })
1471 expect(result).not.toBeNull()
1472 expect(result!.code).not.toContain('private-server-body')
1473 },
1474)