Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

handleCreateIsomorphicFn.ts

23 lines749 Bytes

1import { is } from 'yuku-ast'
2import { parseExpression } from '@tanstack/router-utils'
3import type { CompilationContext, RewriteCandidate } from './types'
4​
5export function handleCreateIsomorphicFn(
6 candidates: Array<RewriteCandidate>,
7 context: CompilationContext,
8): void {
9 for (const { node, methodChain } of candidates) {
10 const selected =
11 context.env === 'client' ? methodChain.client : methodChain.server
12 if (!selected) {
13 context.replaceNode(node, parseExpression('() => {}'))
14 continue
15 }
16 if (!is.Expression(selected.firstArg)) {
17 throw new Error(
18 `createIsomorphicFn().${context.env}(func) must be called with a function!`,
19 )
20 }
21 context.replaceNode(node, selected.firstArg)
22 }