@@ -23,136 +23,104 @@ export type {
2323export type DirectiveFunctionsViteEnvOptions = Pick <
2424 CompileDirectivesOpts ,
2525 'getRuntimeCode' | 'replacer'
26- > & {
27- envLabel : string
26+ >
27+ export type DirectiveFunctionsViteOptions = DirectiveFunctionsViteEnvOptions & {
28+ directive : string
29+ onDirectiveFnsById ?: ( directiveFnsById : Record < string , DirectiveFn > ) => void
30+ generateFunctionId : GenerateFunctionIdFn
2831}
2932
30- export type DirectiveFunctionsViteOptions = Pick <
31- CompileDirectivesOpts ,
32- 'directive' | 'directiveLabel'
33- > &
34- DirectiveFunctionsViteEnvOptions & {
35- onDirectiveFnsById ?: ( directiveFnsById : Record < string , DirectiveFn > ) => void
36- generateFunctionId : GenerateFunctionIdFn
37- }
38-
3933const createDirectiveRx = ( directive : string ) =>
4034 new RegExp ( `"${ directive } "|'${ directive } '` , 'gm' )
4135
42- export type DirectiveFunctionsVitePluginEnvOptions = Pick <
43- CompileDirectivesOpts ,
44- 'directive' | 'directiveLabel'
45- > & {
46- environments : {
47- client : DirectiveFunctionsViteEnvOptions & { envName ?: string }
48- server : DirectiveFunctionsViteEnvOptions & { envName ?: string }
49- }
36+ export type DirectiveFunctionsVitePluginEnvOptions = {
37+ directive : string
38+ callers : Array < DirectiveFunctionsViteEnvOptions & { envName : string } >
39+ provider : DirectiveFunctionsViteEnvOptions & { envName : string }
5040 onDirectiveFnsById ?: ( directiveFnsById : Record < string , DirectiveFn > ) => void
5141 generateFunctionId : GenerateFunctionIdFn
5242}
5343
44+ function buildDirectiveSplitParam ( directive : string ) {
45+ return `tsr-directive-${ directive . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '-' ) } `
46+ }
47+
5448export function TanStackDirectiveFunctionsPluginEnv (
5549 opts : DirectiveFunctionsVitePluginEnvOptions ,
5650) : 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-
7151 let root : string = process . cwd ( )
7252
7353 const directiveRx = createDirectiveRx ( opts . directive )
7454
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+
7562 return {
7663 name : 'tanstack-start-directive-vite-plugin' ,
7764 enforce : 'pre' ,
7865 buildStart ( ) {
7966 root = this . environment . config . root
8067 } ,
8168 applyToEnvironment ( env ) {
82- return [
83- opts . environments . client . envName ,
84- opts . environments . server . envName ,
85- ] . includes ( env . name )
69+ return appliedEnvironments . has ( env . name )
8670 } ,
8771 transform : {
8872 filter : {
8973 code : directiveRx ,
9074 } ,
9175 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` )
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+ )
9999 }
100-
101- return transformCode ( {
102- ...opts ,
103- ...envOptions ,
100+ const { compiledResult, directiveFnsById } = compileDirectives ( {
101+ directive : opts . directive ,
102+ getRuntimeCode : envOptions . getRuntimeCode ,
103+ generateFunctionId : opts . generateFunctionId ,
104+ replacer : envOptions . replacer ,
104105 code,
105- id,
106106 root,
107+ filename : id ,
108+ directiveSplitParam,
109+ isDirectiveSplitParam,
107110 } )
108- } ,
109- } ,
110- }
111- }
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+ }
112116
113- function 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- }
117+ if ( debug ) {
118+ logDiff ( code , compiledResult . code )
119+ console . log ( 'Output:\n' , compiledResult . code + '\n\n' )
120+ }
151121
152- if ( debug ) {
153- logDiff ( code , compiledResult . code )
154- console . log ( 'Output:\n' , compiledResult . code + '\n\n' )
122+ return compiledResult
123+ } ,
124+ } ,
155125 }
156-
157- return compiledResult
158126}
0 commit comments