Open History to see the last commit
1import { fileURLToPath, pathToFileURL } from 'node:url'
2β
3import { logDiff } from '@tanstack/router-utils'
4import { compileDirectives } from './compilers'
5import type {
6 CompileDirectivesOpts,
7 DirectiveFn,
8 GenerateFunctionIdFn,
9} from './compilers'
10import type { Plugin } from 'vite'
11β
12const debug =
13 process.env.TSR_VITE_DEBUG &&
14 ['true', 'directives-functions-plugin'].includes(process.env.TSR_VITE_DEBUG)
15β
16export type {
17 DirectiveFn,
18 CompileDirectivesOpts,
19 ReplacerFn,
20 GenerateFunctionIdFn,
21} from './compilers'
22β
23export type DirectiveFunctionsViteEnvOptions = Pick<
24 CompileDirectivesOpts,
25 'getRuntimeCode' | 'replacer'
26> & {
27 envLabel: string
28}
29β
30export type DirectiveFunctionsViteOptions = Pick<
31 CompileDirectivesOpts,
32 'directive' | 'directiveLabel'
33> &
34 DirectiveFunctionsViteEnvOptions & {
35 onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
36 generateFunctionId: GenerateFunctionIdFn
37 }
38β
39const createDirectiveRx = (directive: string) =>
40 new RegExp(`"${directive}"|'${directive}'`, 'gm')
41β
42export type DirectiveFunctionsVitePluginEnvOptions = Pick<
43 CompileDirectivesOpts,
44 'directive' | 'directiveLabel'
45> & {
46 environments: {
47 client: DirectiveFunctionsViteEnvOptions & { envName?: string }
48 server: DirectiveFunctionsViteEnvOptions & { envName?: string }
49 }
50 onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
51 generateFunctionId: GenerateFunctionIdFn
52}
53β
54export function TanStackDirectiveFunctionsPluginEnv(
55 opts: DirectiveFunctionsVitePluginEnvOptions,
56): Plugin {
57 opts = {
58 ...opts,
59 environments: {
60 client: {
61 envName: 'client',
62 ...opts.environments.client,
63 },
64 server: {
65 envName: 'server',
66 ...opts.environments.server,
67 },
68 },
69 }
70β
71 let root: string = process.cwd()
72β
73 const directiveRx = createDirectiveRx(opts.directive)
74β
75 return {
76 name: 'tanstack-start-directive-vite-plugin',
77 enforce: 'pre',
78 buildStart() {
79 root = this.environment.config.root
80 },
81 applyToEnvironment(env) {
82 return [
83 opts.environments.client.envName,
84 opts.environments.server.envName,
85 ].includes(env.name)
86 },
87 transform: {
88 filter: {
89 code: directiveRx,
90 },
91 handler(code, id) {
92 const envOptions = [
93 opts.environments.client,
94 opts.environments.server,
95 ].find((e) => e.envName === this.environment.name)
96β
97 if (!envOptions) {
98 throw new Error(`Environment ${this.environment.name} not found`)
99 }
100β
101 return transformCode({
102 ...opts,
103 ...envOptions,
104 code,
105 id,
106 root,
107 })
108 },
109 },
110 }
111}
112β
113function transformCode({
114 code,
115 id,
116 envLabel,
117 directive,
118 directiveLabel,
119 getRuntimeCode,
120 generateFunctionId,
121 replacer,
122 onDirectiveFnsById,
123 root,
124}: DirectiveFunctionsViteOptions & {
125 code: string
126 id: string
127 root: string
128}) {
129 const url = pathToFileURL(id)
130 url.searchParams.delete('v')
131 id = fileURLToPath(url).replace(/\\/g, '/')
132β
133 if (debug) console.info(`${envLabel}: Compiling Directives: `, id)
134β
135 const { compiledResult, directiveFnsById, isDirectiveSplitParam } =
136 compileDirectives({
137 directive,
138 directiveLabel,
139 getRuntimeCode,
140 generateFunctionId,
141 replacer,
142 code,
143 root,
144 filename: id,
145 })
146 // when we process a file with a directive split param, we have already encountered the directives in that file
147 // (otherwise we wouldn't have gotten here)
148 if (!isDirectiveSplitParam) {
149 onDirectiveFnsById?.(directiveFnsById)
150 }
151β
152 if (debug) {
153 logDiff(code, compiledResult.code)
154 console.log('Output:\n', compiledResult.code + '\n\n')
155 }
156β
157 return compiledResult
158}