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>
27export type DirectiveFunctionsViteOptions = DirectiveFunctionsViteEnvOptions & {
28 directive: string
29 onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
30 generateFunctionId: GenerateFunctionIdFn
31}
32β
33const createDirectiveRx = (directive: string) =>
34 new RegExp(`"${directive}"|'${directive}'`, 'gm')
35β
36export type DirectiveFunctionsVitePluginEnvOptions = {
37 directive: string
38 callers: Array<DirectiveFunctionsViteEnvOptions & { envName: string }>
39 provider: DirectiveFunctionsViteEnvOptions & { envName: string }
40 onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
41 generateFunctionId: GenerateFunctionIdFn
42}
43β
44function buildDirectiveSplitParam(directive: string) {
45 return `tsr-directive-${directive.replace(/[^a-zA-Z0-9]/g, '-')}`
46}
47β
48export function TanStackDirectiveFunctionsPluginEnv(
49 opts: DirectiveFunctionsVitePluginEnvOptions,
50): Plugin {
51 let root: string = process.cwd()
52β
53 const directiveRx = createDirectiveRx(opts.directive)
54β
55 const appliedEnvironments = new Set([
56 ...opts.callers.map((c) => c.envName),
57 opts.provider.envName,
58 ])
59β
60 const directiveSplitParam = buildDirectiveSplitParam(opts.directive)
61β
62 return {
63 name: 'tanstack-start-directive-vite-plugin',
64 enforce: 'pre',
65 buildStart() {
66 root = this.environment.config.root
67 },
68 applyToEnvironment(env) {
69 return appliedEnvironments.has(env.name)
70 },
71 transform: {
72 filter: {
73 code: directiveRx,
74 },
75 handler(code, id) {
76 const url = pathToFileURL(id)
77 url.searchParams.delete('v')
78 id = fileURLToPath(url).replace(/\\/g, '/')
79β
80 const isDirectiveSplitParam = id.includes(directiveSplitParam)
81β
82 let envOptions: DirectiveFunctionsViteEnvOptions & { envName: string }
83 if (isDirectiveSplitParam) {
84 envOptions = opts.provider
85 if (debug)
86 console.info(
87 `Compiling Directives for provider in environment ${envOptions.envName}: `,
88 id,
89 )
90 } else {
91 envOptions = opts.callers.find(
92 (e) => e.envName === this.environment.name,
93 )!
94 if (debug)
95 console.info(
96 `Compiling Directives for caller in environment ${envOptions.envName}: `,
97 id,
98 )
99 }
100 const { compiledResult, directiveFnsById } = compileDirectives({
101 directive: opts.directive,
102 getRuntimeCode: envOptions.getRuntimeCode,
103 generateFunctionId: opts.generateFunctionId,
104 replacer: envOptions.replacer,
105 code,
106 root,
107 filename: id,
108 directiveSplitParam,
109 isDirectiveSplitParam,
110 })
111 // when we process a file with a directive split param, we have already encountered the directives in that file
112 // (otherwise we wouldn't have gotten here)
113 if (!isDirectiveSplitParam) {
114 opts.onDirectiveFnsById?.(directiveFnsById)
115 }
116β
117 if (debug) {
118 logDiff(code, compiledResult.code)
119 console.log('Output:\n', compiledResult.code + '\n\n')
120 }
121β
122 return compiledResult
123 },
124 },
125 }
126}