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

config.ts

119 lines3.0 KB

1import {
2 KindDetectionPatterns,
3 getExternalLookupKind,
4 getLookupKindsForEnv,
5 isCompilerTransformEnabledForEnv,
6 isStartCompilerPluginEnabledForEnv,
7} from './compiler'
8import type { BuiltInLookupKind, LookupConfig } from './compiler'
9import type {
10 CompileStartFrameworkOptions,
11 StartCompilerImportTransform,
12 StartCompilerPlugin,
13} from '../types'
14​
15export function getTransformCodeFilterForEnv(
16 env: 'client' | 'server',
17 opts?: {
18 compilerTransforms?: Array<StartCompilerImportTransform> | undefined
19 compilerPlugins?: Array<StartCompilerPlugin> | undefined
20 },
21): Array<RegExp> {
22 const validKinds = getLookupKindsForEnv(env, opts)
23 const patterns: Array<RegExp> = []
24​
25 for (const [kind, pattern] of Object.entries(KindDetectionPatterns) as Array<
26 [BuiltInLookupKind, RegExp]
27 >) {
28 if (validKinds.has(kind)) {
29 patterns.push(pattern)
30 }
31 }
32​
33 for (const transform of opts?.compilerTransforms ?? []) {
34 if (isCompilerTransformEnabledForEnv(transform, env)) {
35 patterns.push(transform.detect)
36 }
37 }
38​
39 for (const plugin of opts?.compilerPlugins ?? []) {
40 if (plugin.detect && isStartCompilerPluginEnabledForEnv(plugin, env)) {
41 patterns.push(plugin.detect)
42 }
43 }
44​
45 return patterns
47​
48export function getLookupConfigurationsForEnv(
49 env: 'client' | 'server',
50 framework: CompileStartFrameworkOptions,
51 opts?: {
52 compilerTransforms?: Array<StartCompilerImportTransform> | undefined
53 },
54): Array<LookupConfig> {
55 const commonConfigs: Array<LookupConfig> = [
56 {
57 libName: `@tanstack/${framework}-start`,
58 rootExport: 'createServerFn',
59 kind: 'Root',
60 },
61 {
62 libName: `@tanstack/${framework}-start`,
63 rootExport: 'createIsomorphicFn',
64 kind: 'IsomorphicFn',
65 },
66 {
67 libName: `@tanstack/${framework}-start`,
68 rootExport: 'createServerOnlyFn',
69 kind: 'ServerOnlyFn',
70 },
71 {
72 libName: `@tanstack/${framework}-start`,
73 rootExport: 'createClientOnlyFn',
74 kind: 'ClientOnlyFn',
75 },
76 ]
77​
78 const externalConfigs: Array<LookupConfig> = []
79 for (const transform of opts?.compilerTransforms ?? []) {
80 if (!isCompilerTransformEnabledForEnv(transform, env)) continue
81​
82 const kind = getExternalLookupKind(transform)
83 for (const imported of transform.imports) {
84 externalConfigs.push({
85 libName: imported.libName,
86 rootExport: imported.rootExport,
87 kind,
88 })
89 }
90 }
91​
92 if (env === 'client') {
93 return [
94 {
95 libName: `@tanstack/${framework}-start`,
96 rootExport: 'createMiddleware',
97 kind: 'Root',
98 },
99 {
100 libName: `@tanstack/${framework}-start`,
101 rootExport: 'createStart',
102 kind: 'Root',
103 },
104 ...commonConfigs,
105 ...externalConfigs,
106 ]
107 }
108​
109 const serverConfigs: Array<LookupConfig> = [
110 ...commonConfigs,
111 {
112 libName: `@tanstack/${framework}-router`,
113 rootExport: 'ClientOnly',
114 kind: 'ClientOnlyJSX',
115 },
116 ]
117​
118 return [...serverConfigs, ...externalConfigs]