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

host.ts

138 lines3.8 KB

1import { StartCompiler, getLookupKindsForEnv } from './compiler'
2import { getLookupConfigurationsForEnv } from './config'
3import type {
4 CompileStartFrameworkOptions,
5 StartCompilerImportTransform,
6 StartCompilerPlugin,
7 StartCompilerTransformResult,
8 StartCompilerVirtualModuleContext,
9} from '../types'
10import type {
11 DevServerFnModuleSpecifierEncoder,
12 GenerateFunctionIdFnOptional,
13 ServerFn,
14} from './types'
15​
16export interface CreateStartCompilerOptions {
17 env: 'client' | 'server'
18 envName: string
19 root: string
20 framework: CompileStartFrameworkOptions
21 providerEnvName: string
22 mode: 'dev' | 'build'
23 generateFunctionId?: GenerateFunctionIdFnOptional
24 compilerTransforms?: Array<StartCompilerImportTransform> | undefined
25 compilerPlugins?: Array<StartCompilerPlugin> | undefined
26 serverFnProviderModuleDirectives?: ReadonlyArray<string> | undefined
27 warn?: (message: string) => void
28 onServerFnsById?: (d: Record<string, ServerFn>) => void
29 getKnownServerFns: () => Record<string, ServerFn>
30 encodeModuleSpecifierInDev?: DevServerFnModuleSpecifierEncoder
31 loadModule: (id: string) => Promise<void>
32 resolveId: (id: string, importer?: string) => Promise<string | null>
34​
35export function createStartCompiler(
36 options: CreateStartCompilerOptions,
37): StartCompiler {
38 return new StartCompiler({
39 env: options.env,
40 envName: options.envName,
41 root: options.root,
42 lookupKinds: getLookupKindsForEnv(options.env, {
43 compilerTransforms: options.compilerTransforms,
44 }),
45 lookupConfigurations: getLookupConfigurationsForEnv(
46 options.env,
47 options.framework,
48 { compilerTransforms: options.compilerTransforms },
49 ),
50 mode: options.mode,
51 framework: options.framework,
52 providerEnvName: options.providerEnvName,
53 generateFunctionId: options.generateFunctionId,
54 onServerFnsById: options.onServerFnsById,
55 compilerTransforms: options.compilerTransforms,
56 compilerPlugins: options.compilerPlugins,
57 serverFnProviderModuleDirectives: options.serverFnProviderModuleDirectives,
58 warn: options.warn,
59 getKnownServerFns: options.getKnownServerFns,
60 devServerFnModuleSpecifierEncoder: options.encodeModuleSpecifierInDev,
61 loadModule: options.loadModule,
62 resolveId: options.resolveId,
63 })
65​
66export function mergeServerFnsById(
67 current: Record<string, ServerFn>,
68 next: Record<string, ServerFn>,
69): void {
70 for (const [id, fn] of Object.entries(next)) {
71 const existing = current[id]
72​
73 if (existing) {
74 current[id] = {
75 ...fn,
76 isClientReferenced:
77 existing.isClientReferenced || fn.isClientReferenced,
78 }
79 continue
80 }
81​
82 current[id] = fn
83 }
85​
86export function matchesCodeFilters(
87 code: string,
88 filters: ReadonlyArray<RegExp>,
89): boolean {
90 for (const pattern of filters) {
91 pattern.lastIndex = 0
92 if (pattern.test(code)) {
93 return true
94 }
95 }
96​
97 return false
99​
100export function createCompilerVirtualModuleIdPattern(
101 compilerPlugins: ReadonlyArray<StartCompilerPlugin>,
102) {
103 const patterns = compilerPlugins
104 .map((plugin) => plugin.virtualModuleIdPattern)
105 .filter((pattern): pattern is RegExp => !!pattern)
106​
107 if (patterns.length === 0) {
108 return undefined
109 }
110​
111 return new RegExp(
112 patterns.map((pattern) => `(?:${pattern.source})`).join('|'),
113 )
115​
116export function loadCompilerVirtualModule(
117 compilerPlugins: ReadonlyArray<StartCompilerPlugin>,
118 context: StartCompilerVirtualModuleContext,
119): StartCompilerTransformResult | null {
120 for (const compilerPlugin of compilerPlugins) {
121 const pattern = compilerPlugin.virtualModuleIdPattern
122 if (!pattern) {
123 continue
124 }
125​
126 pattern.lastIndex = 0
127 if (!pattern.test(context.id)) {
128 continue
129 }
130​
131 const result = compilerPlugin.loadVirtualModule?.(context)
132 if (result) {
133 return result
134 }
135 }
136​
137 return null