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

compiler.test.ts

978 lines34.7 KB

1import { describe, expect, test } from 'vitest'
2​
3import { compileDirectives } from '../src/compilers'
4import type { CompileDirectivesOpts } from '../src/compilers'
5​
6function makeFunctionIdUrlSafe(location: string): string {
7 return location
8 .replace(/[^a-zA-Z0-9-_]/g, '_') // Replace unsafe chars with underscore
9 .replace(/_{2,}/g, '_') // Collapse multiple underscores
10 .replace(/^_|_$/g, '') // Trim leading/trailing underscores
11 .replace(/_--/g, '--') // Clean up the joiner
13​
14const generateFunctionId: CompileDirectivesOpts['generateFunctionId'] = (
15 opts,
16) => {
17 return makeFunctionIdUrlSafe(`${opts.filename}--${opts.functionName}`)
19​
20const clientConfig: Omit<CompileDirectivesOpts, 'code'> = {
21 directive: 'use server',
22 root: './test-files',
23 filename: './test-files/test.ts',
24 getRuntimeCode: () => 'import { createClientRpc } from "my-rpc-lib-client"',
25 generateFunctionId,
26 replacer: (opts) => `createClientRpc(${JSON.stringify(opts.functionId)})`,
27 directiveSplitParam: 'tsr-directive-use-server',
28 isDirectiveSplitParam: false,
30​
31const ssrConfig: Omit<CompileDirectivesOpts, 'code'> = {
32 directive: 'use server',
33 root: './test-files',
34 filename: './test-files/test.ts',
35 getRuntimeCode: () => 'import { createSsrRpc } from "my-rpc-lib-server"',
36 generateFunctionId,
37 replacer: (opts) => `createSsrRpc(${JSON.stringify(opts.functionId)})`,
38 directiveSplitParam: 'tsr-directive-use-server',
39 isDirectiveSplitParam: false,
41​
42const serverConfig: Omit<CompileDirectivesOpts, 'code'> = {
43 directive: 'use server',
44 root: './test-files',
45 filename: './test-files/test.ts',
46 getRuntimeCode: () => 'import { createServerRpc } from "my-rpc-lib-server"',
47 generateFunctionId,
48 replacer: (opts) =>
49 // On the server build, we need different code for the split function
50 // vs any other server functions the split function may reference
51​
52 // For the split function itself, we use the original function.
53 // For any other server functions the split function may reference,
54 // we use the splitImportFn which is a dynamic import of the split file.
55 `createServerRpc(${JSON.stringify(opts.functionId)}, ${opts.fn})`,
56 directiveSplitParam: 'tsr-directive-use-server',
57 isDirectiveSplitParam: true,
59​
60describe('server function compilation', () => {
61 test('basic function declaration nested in other variable', () => {
62 const code = `
63 export const namedFunction = createServerFn(function namedFunction() {
64 'use server'
65 return 'hello'
66 })
68 export const namedGeneratorFunction = createServerFn(function* namedGeneratorFunction () {
69 'use server'
70 yield 'hello'
71 return 'hello world'
72 })
74 export const arrowFunction = createServerFn(() => {
75 'use server'
76 return 'hello'
77 })
79 export const anonymousFunction = createServerFn(function () {
80 'use server'
81 return 'hello'
82 })
83​
84 export const anonymousGeneratorFunction = createServerFn(function* () {
85 'use server'
86 yield 'hello'
87 return 'hello world'
88 })
90 export const multipleDirectives = function multipleDirectives() {
91 'use server'
92 'use strict'
93 return 'hello'
94 }
96 export const iife = (function () {
97 'use server'
98 return 'hello'
99 })()
101 export default function defaultExportFn() {
102 'use server'
103 return 'hello'
104 }
106 export function namedExportFn() {
107 'use server'
108 return 'hello'
109 }
111 export const exportedArrowFunction = wrapper(() => {
112 'use server'
113 return 'hello'
114 })
116 export const namedExportConst = () => {
117 'use server'
118 return usedFn()
119 }
120​
121 export const namedExportConstGenerator = function* () {
122 'use server'
123 yield 'hello'
124 return usedFn()
125 }
127 function usedFn() {
128 return 'hello'
129 }
131 function unusedFn() {
132 return 'hello'
133 }
135 const namedDefaultExport = 'namedDefaultExport'
136 export default namedDefaultExport
138 const usedButNotExported = 'usedButNotExported'
140 const namedExport = 'namedExport'
142 export {
143 namedExport
144 }
145 `
146 const client = compileDirectives({
147 ...clientConfig,
148 code,
149 })
150 const ssr = compileDirectives({ ...ssrConfig, code })
151​
152 const server = compileDirectives({
153 ...serverConfig,
154 code,
155 filename: `${ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!.extractedFilename}`,
156 })
157​
158 expect(client.compiledResult.code).toMatchInlineSnapshot(`
159 "import { createClientRpc } from "my-rpc-lib-client";
160 const namedFunction_createServerFn_namedFunction = createClientRpc("test_ts--namedFunction_createServerFn_namedFunction");
161 export const namedFunction = createServerFn(namedFunction_createServerFn_namedFunction);
162 const namedGeneratorFunction_createServerFn_namedGeneratorFunction = createClientRpc("test_ts--namedGeneratorFunction_createServerFn_namedGeneratorFunction");
163 export const namedGeneratorFunction = createServerFn(namedGeneratorFunction_createServerFn_namedGeneratorFunction);
164 const arrowFunction_createServerFn = createClientRpc("test_ts--arrowFunction_createServerFn");
165 export const arrowFunction = createServerFn(arrowFunction_createServerFn);
166 const anonymousFunction_createServerFn = createClientRpc("test_ts--anonymousFunction_createServerFn");
167 export const anonymousFunction = createServerFn(anonymousFunction_createServerFn);
168 const anonymousGeneratorFunction_createServerFn = createClientRpc("test_ts--anonymousGeneratorFunction_createServerFn");
169 export const anonymousGeneratorFunction = createServerFn(anonymousGeneratorFunction_createServerFn);
170 const multipleDirectives_multipleDirectives = createClientRpc("test_ts--multipleDirectives_multipleDirectives");
171 export const multipleDirectives = multipleDirectives_multipleDirectives;
172 const iife_1 = createClientRpc("test_ts--iife_1");
173 export const iife = iife_1();
174 const defaultExportFn_1 = createClientRpc("test_ts--defaultExportFn_1");
175 export default defaultExportFn_1;
176 const namedExportFn_1 = createClientRpc("test_ts--namedExportFn_1");
177 export const namedExportFn = namedExportFn_1;
178 const exportedArrowFunction_wrapper = createClientRpc("test_ts--exportedArrowFunction_wrapper");
179 export const exportedArrowFunction = wrapper(exportedArrowFunction_wrapper);
180 const namedExportConst_1 = createClientRpc("test_ts--namedExportConst_1");
181 export const namedExportConst = namedExportConst_1;
182 const namedExportConstGenerator_1 = createClientRpc("test_ts--namedExportConstGenerator_1");
183 export const namedExportConstGenerator = namedExportConstGenerator_1;
184 function unusedFn() {
185 return 'hello';
186 }
187 const namedDefaultExport = 'namedDefaultExport';
188 export default namedDefaultExport;
189 const usedButNotExported = 'usedButNotExported';
190 const namedExport = 'namedExport';
191 export { namedExport };"
192 `)
193​
194 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
195 "import { createSsrRpc } from "my-rpc-lib-server";
196 const namedFunction_createServerFn_namedFunction = createSsrRpc("test_ts--namedFunction_createServerFn_namedFunction");
197 export const namedFunction = createServerFn(namedFunction_createServerFn_namedFunction);
198 const namedGeneratorFunction_createServerFn_namedGeneratorFunction = createSsrRpc("test_ts--namedGeneratorFunction_createServerFn_namedGeneratorFunction");
199 export const namedGeneratorFunction = createServerFn(namedGeneratorFunction_createServerFn_namedGeneratorFunction);
200 const arrowFunction_createServerFn = createSsrRpc("test_ts--arrowFunction_createServerFn");
201 export const arrowFunction = createServerFn(arrowFunction_createServerFn);
202 const anonymousFunction_createServerFn = createSsrRpc("test_ts--anonymousFunction_createServerFn");
203 export const anonymousFunction = createServerFn(anonymousFunction_createServerFn);
204 const anonymousGeneratorFunction_createServerFn = createSsrRpc("test_ts--anonymousGeneratorFunction_createServerFn");
205 export const anonymousGeneratorFunction = createServerFn(anonymousGeneratorFunction_createServerFn);
206 const multipleDirectives_multipleDirectives = createSsrRpc("test_ts--multipleDirectives_multipleDirectives");
207 export const multipleDirectives = multipleDirectives_multipleDirectives;
208 const iife_1 = createSsrRpc("test_ts--iife_1");
209 export const iife = iife_1();
210 const defaultExportFn_1 = createSsrRpc("test_ts--defaultExportFn_1");
211 export default defaultExportFn_1;
212 const namedExportFn_1 = createSsrRpc("test_ts--namedExportFn_1");
213 export const namedExportFn = namedExportFn_1;
214 const exportedArrowFunction_wrapper = createSsrRpc("test_ts--exportedArrowFunction_wrapper");
215 export const exportedArrowFunction = wrapper(exportedArrowFunction_wrapper);
216 const namedExportConst_1 = createSsrRpc("test_ts--namedExportConst_1");
217 export const namedExportConst = namedExportConst_1;
218 const namedExportConstGenerator_1 = createSsrRpc("test_ts--namedExportConstGenerator_1");
219 export const namedExportConstGenerator = namedExportConstGenerator_1;
220 function unusedFn() {
221 return 'hello';
222 }
223 const namedDefaultExport = 'namedDefaultExport';
224 export default namedDefaultExport;
225 const usedButNotExported = 'usedButNotExported';
226 const namedExport = 'namedExport';
227 export { namedExport };"
228 `)
229​
230 expect(server.compiledResult.code).toMatchInlineSnapshot(
231 `
232 "import { createServerRpc } from "my-rpc-lib-server";
233 const namedFunction_createServerFn_namedFunction = createServerRpc("test_ts--namedFunction_createServerFn_namedFunction", function namedFunction() {
234 return 'hello';
235 });
236 const namedGeneratorFunction_createServerFn_namedGeneratorFunction = createServerRpc("test_ts--namedGeneratorFunction_createServerFn_namedGeneratorFunction", function* namedGeneratorFunction() {
237 yield 'hello';
238 return 'hello world';
239 });
240 const arrowFunction_createServerFn = createServerRpc("test_ts--arrowFunction_createServerFn", () => {
241 return 'hello';
242 });
243 const anonymousFunction_createServerFn = createServerRpc("test_ts--anonymousFunction_createServerFn", function () {
244 return 'hello';
245 });
246 const anonymousGeneratorFunction_createServerFn = createServerRpc("test_ts--anonymousGeneratorFunction_createServerFn", function* () {
247 yield 'hello';
248 return 'hello world';
249 });
250 const multipleDirectives_multipleDirectives = createServerRpc("test_ts--multipleDirectives_multipleDirectives", function multipleDirectives() {
251 'use strict';
252​
253 return 'hello';
254 });
255 const iife_1 = createServerRpc("test_ts--iife_1", function () {
256 return 'hello';
257 });
258 const defaultExportFn_1 = createServerRpc("test_ts--defaultExportFn_1", function defaultExportFn() {
259 return 'hello';
260 });
261 const defaultExportFn = defaultExportFn_1;
262 const namedExportFn_1 = createServerRpc("test_ts--namedExportFn_1", function namedExportFn() {
263 return 'hello';
264 });
265 const namedExportFn = namedExportFn_1;
266 const exportedArrowFunction_wrapper = createServerRpc("test_ts--exportedArrowFunction_wrapper", () => {
267 return 'hello';
268 });
269 const namedExportConst_1 = createServerRpc("test_ts--namedExportConst_1", () => {
270 return usedFn();
271 });
272 const namedExportConstGenerator_1 = createServerRpc("test_ts--namedExportConstGenerator_1", function* () {
273 yield 'hello';
274 return usedFn();
275 });
276 function usedFn() {
277 return 'hello';
278 }
279 function unusedFn() {
280 return 'hello';
281 }
282 const namedDefaultExport = 'namedDefaultExport';
283 export default namedDefaultExport;
284 const usedButNotExported = 'usedButNotExported';
285 export { namedFunction_createServerFn_namedFunction, namedGeneratorFunction_createServerFn_namedGeneratorFunction, arrowFunction_createServerFn, anonymousFunction_createServerFn, anonymousGeneratorFunction_createServerFn, multipleDirectives_multipleDirectives, iife_1, defaultExportFn_1, namedExportFn_1, exportedArrowFunction_wrapper, namedExportConst_1, namedExportConstGenerator_1 };"
286 `,
287 )
288 })
289​
290 test('Does not support function declarations nested in other blocks', () => {
291 const code = `
292 outer(() => {
293 function useServer() {
294 'use server'
295 return 'hello'
296 }
297 })
298 `
299​
300 expect(() => compileDirectives({ ...clientConfig, code })).toThrow()
301 expect(() => compileDirectives({ ...serverConfig, code })).toThrow()
302 expect(() =>
303 compileDirectives({
304 ...serverConfig,
305 code,
306 filename: serverConfig.filename + `?tsr-serverfn-split=temp`,
307 }),
308 ).toThrow()
309 })
310​
311 test('does not support class methods', () => {
312 const code = `
313 class TestClass {
314 method() {
315 'use server'
316 return 'hello'
317 }
318​
319 static staticMethod() {
320 'use server'
321 return 'hello'
322 }
323 }
324 `
325​
326 expect(() => compileDirectives({ ...clientConfig, code })).toThrow()
327 expect(() => compileDirectives({ ...serverConfig, code })).toThrow()
328 expect(() =>
329 compileDirectives({
330 ...serverConfig,
331 code,
332 filename: serverConfig.filename + `?tsr-serverfn-split=temp`,
333 }),
334 ).toThrow()
335 })
336​
337 test('does not support object methods', () => {
338 const code = `
339 const obj = {
340 method() {
341 'use server'
342 return 'hello'
343 },
344 }
345 `
346​
347 expect(() => compileDirectives({ ...clientConfig, code })).toThrow()
348 expect(() => compileDirectives({ ...serverConfig, code })).toThrow()
349 expect(() =>
350 compileDirectives({
351 ...serverConfig,
352 code,
353 filename: serverConfig.filename + `?tsr-serverfn-split=temp`,
354 }),
355 ).toThrow()
356 })
357​
358 test('multiple directiveFnsById', () => {
359 const code = `
360 function multiDirective() {
361 'use strict'
362 'use server'
363 return 'hello'
364 }
365​
366 multiDirective()
367 `
368​
369 const client = compileDirectives({ ...clientConfig, code })
370 const ssr = compileDirectives({ ...ssrConfig, code })
371​
372 const server = compileDirectives({
373 ...serverConfig,
374 code,
375 filename:
376 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
377 .extractedFilename,
378 })
379​
380 expect(client.compiledResult.code).toMatchInlineSnapshot(`
381 "import { createClientRpc } from "my-rpc-lib-client";
382 const multiDirective_1 = createClientRpc("test_ts--multiDirective_1");
383 const multiDirective = multiDirective_1;
384 multiDirective();"
385 `)
386 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
387 "import { createSsrRpc } from "my-rpc-lib-server";
388 const multiDirective_1 = createSsrRpc("test_ts--multiDirective_1");
389 const multiDirective = multiDirective_1;
390 multiDirective();"
391 `)
392 expect(server.compiledResult.code).toMatchInlineSnapshot(`
393 "import { createServerRpc } from "my-rpc-lib-server";
394 const multiDirective_1 = createServerRpc("test_ts--multiDirective_1", function () {
395 'use strict';
396​
397 return 'hello';
398 });
399 const multiDirective = multiDirective_1;
400 multiDirective();
401 export { multiDirective_1 };"
402 `)
403 })
404​
405 test('IIFE', () => {
406 const code = `
407 export const iife = (function () {
408 'use server'
409 return 'hello'
410 })()
411 `
412​
413 const client = compileDirectives({ ...clientConfig, code })
414 const ssr = compileDirectives({ ...ssrConfig, code })
415​
416 const server = compileDirectives({
417 ...serverConfig,
418 code,
419 filename:
420 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
421 .extractedFilename,
422 })
423​
424 expect(client.compiledResult.code).toMatchInlineSnapshot(`
425 "import { createClientRpc } from "my-rpc-lib-client";
426 const iife_1 = createClientRpc("test_ts--iife_1");
427 export const iife = iife_1();"
428 `)
429​
430 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
431 "import { createSsrRpc } from "my-rpc-lib-server";
432 const iife_1 = createSsrRpc("test_ts--iife_1");
433 export const iife = iife_1();"
434 `)
435​
436 expect(server.compiledResult.code).toMatchInlineSnapshot(`
437 "import { createServerRpc } from "my-rpc-lib-server";
438 const iife_1 = createServerRpc("test_ts--iife_1", function () {
439 return 'hello';
440 });
441 export { iife_1 };"
442 `)
443 })
444​
445 test('functions that might have the same functionId', () => {
446 const code = `
447 outer(function useServer() {
448 'use server'
449 return 'hello'
450 })
451​
452 outer(function useServer() {
453 'use server'
454 return 'hello'
455 })
456 `
457​
458 const client = compileDirectives({ ...clientConfig, code })
459 const ssr = compileDirectives({ ...ssrConfig, code })
460​
461 const server = compileDirectives({
462 ...serverConfig,
463 code,
464 filename:
465 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
466 .extractedFilename,
467 })
468​
469 expect(client.compiledResult.code).toMatchInlineSnapshot(`
470 "import { createClientRpc } from "my-rpc-lib-client";
471 const outer_useServer = createClientRpc("test_ts--outer_useServer");
472 outer(outer_useServer);
473 const outer_useServer_1 = createClientRpc("test_ts--outer_useServer_1");
474 outer(outer_useServer_1);"
475 `)
476​
477 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
478 "import { createSsrRpc } from "my-rpc-lib-server";
479 const outer_useServer = createSsrRpc("test_ts--outer_useServer");
480 outer(outer_useServer);
481 const outer_useServer_1 = createSsrRpc("test_ts--outer_useServer_1");
482 outer(outer_useServer_1);"
483 `)
484​
485 expect(server.compiledResult.code).toMatchInlineSnapshot(`
486 "import { createServerRpc } from "my-rpc-lib-server";
487 const outer_useServer = createServerRpc("test_ts--outer_useServer", function useServer() {
488 return 'hello';
489 });
490 outer(outer_useServer);
491 const outer_useServer_1 = createServerRpc("test_ts--outer_useServer_1", function useServer() {
492 return 'hello';
493 });
494 outer(outer_useServer_1);
495 export { outer_useServer, outer_useServer_1 };"
496 `)
497 })
498​
499 test('use server directive in program body', () => {
500 const code = `
501 'use server'
502​
503 export function useServer() {
504 return usedInUseServer()
505 }
506​
507 function notExported() {
508 return 'hello'
509 }
510​
511 function usedInUseServer() {
512 return 'hello'
513 }
514​
515 export default function defaultExport() {
516 return 'hello'
517 }
518 `
519​
520 const client = compileDirectives({ ...clientConfig, code })
521 const ssr = compileDirectives({ ...ssrConfig, code })
522 const server = compileDirectives({
523 ...serverConfig,
524 code,
525 filename:
526 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
527 .extractedFilename,
528 })
529​
530 expect(client.compiledResult.code).toMatchInlineSnapshot(`
531 "'use server';
532​
533 import { createClientRpc } from "my-rpc-lib-client";
534 const useServer_1 = createClientRpc("test_ts--useServer_1");
535 export const useServer = useServer_1;
536 function notExported() {
537 return 'hello';
538 }
539 const defaultExport_1 = createClientRpc("test_ts--defaultExport_1");
540 export default defaultExport_1;"
541 `)
542 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
543 "'use server';
544​
545 import { createSsrRpc } from "my-rpc-lib-server";
546 const useServer_1 = createSsrRpc("test_ts--useServer_1");
547 export const useServer = useServer_1;
548 function notExported() {
549 return 'hello';
550 }
551 const defaultExport_1 = createSsrRpc("test_ts--defaultExport_1");
552 export default defaultExport_1;"
553 `)
554 expect(server.compiledResult.code).toMatchInlineSnapshot(`
555 "'use server';
556​
557 import { createServerRpc } from "my-rpc-lib-server";
558 const useServer_1 = createServerRpc("test_ts--useServer_1", function useServer() {
559 return usedInUseServer();
560 });
561 const useServer = useServer_1;
562 function notExported() {
563 return 'hello';
564 }
565 function usedInUseServer() {
566 return 'hello';
567 }
568 const defaultExport_1 = createServerRpc("test_ts--defaultExport_1", function defaultExport() {
569 return 'hello';
570 });
571 const defaultExport = defaultExport_1;
572 export { useServer_1, defaultExport_1 };"
573 `)
574 })
575​
576 test('createServerFn with identifier', () => {
577 // The following code is the client output of the tanstack-start-plugin
578 // that compiles `createServerFn` calls to automatically add the `use server`
579 // directive in the right places.
580 const clientOrSsrCode = `import { createServerFn } from '@tanstack/react-start';
581 export const myServerFn = createServerFn().handler(opts => {
582 "use server";
583​
584 return myServerFn.__executeServer(opts);
585 });
587 export const myServerFn2 = createServerFn().handler(opts => {
588 "use server";
589​
590 return myServerFn2.__executeServer(opts);
591 });`
592​
593 // The following code is the server output of the tanstack-start-plugin
594 // that compiles `createServerFn` calls to automatically add the `use server`
595 // directive in the right places.
596 const serverCode = `import { createServerFn } from '@tanstack/react-start';
597 const myFunc = () => {
598 return 'hello from the server'
599 };
600 export const myServerFn = createServerFn().handler(opts => {
601 "use server";
603 return myServerFn.__executeServer(opts);
604 }, myFunc);
605​
606 const myFunc2 = () => {
607 return myServerFn({ data: 'hello 2 from the server' });
608 };
609 export const myServerFn2 = createServerFn().handler(opts => {
610 "use server";
611​
612 return myServerFn2.__executeServer(opts);
613 }, myFunc2);`
614​
615 const client = compileDirectives({ ...clientConfig, code: clientOrSsrCode })
616 const ssr = compileDirectives({ ...ssrConfig, code: clientOrSsrCode })
617 const server = compileDirectives({
618 ...serverConfig,
619 code: serverCode,
620 filename:
621 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
622 .extractedFilename,
623 })
624​
625 expect(client.compiledResult.code).toMatchInlineSnapshot(`
626 "import { createClientRpc } from "my-rpc-lib-client";
627 import { createServerFn } from '@tanstack/react-start';
628 const myServerFn_createServerFn_handler = createClientRpc("test_ts--myServerFn_createServerFn_handler");
629 export const myServerFn = createServerFn().handler(myServerFn_createServerFn_handler);
630 const myServerFn2_createServerFn_handler = createClientRpc("test_ts--myServerFn2_createServerFn_handler");
631 export const myServerFn2 = createServerFn().handler(myServerFn2_createServerFn_handler);"
632 `)
633​
634 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
635 "import { createSsrRpc } from "my-rpc-lib-server";
636 import { createServerFn } from '@tanstack/react-start';
637 const myServerFn_createServerFn_handler = createSsrRpc("test_ts--myServerFn_createServerFn_handler");
638 export const myServerFn = createServerFn().handler(myServerFn_createServerFn_handler);
639 const myServerFn2_createServerFn_handler = createSsrRpc("test_ts--myServerFn2_createServerFn_handler");
640 export const myServerFn2 = createServerFn().handler(myServerFn2_createServerFn_handler);"
641 `)
642​
643 expect(server.compiledResult.code).toMatchInlineSnapshot(`
644 "import { createServerRpc } from "my-rpc-lib-server";
645 import { createServerFn } from '@tanstack/react-start';
646 const myFunc = () => {
647 return 'hello from the server';
648 };
649 const myServerFn_createServerFn_handler = createServerRpc("test_ts--myServerFn_createServerFn_handler", opts => {
650 return myServerFn.__executeServer(opts);
651 });
652 const myServerFn = createServerFn().handler(myServerFn_createServerFn_handler, myFunc);
653 const myFunc2 = () => {
654 return myServerFn({
655 data: 'hello 2 from the server'
656 });
657 };
658 const myServerFn2_createServerFn_handler = createServerRpc("test_ts--myServerFn2_createServerFn_handler", opts => {
659 return myServerFn2.__executeServer(opts);
660 });
661 const myServerFn2 = createServerFn().handler(myServerFn2_createServerFn_handler, myFunc2);
662 export { myServerFn_createServerFn_handler, myServerFn2_createServerFn_handler };"
663 `)
664 })
665​
666 test('async function with directive', () => {
667 const code = `
668 async function bytesSignupServerFn({ email }: { email: string }) {
669 'use server'
670​
671 return 'test'
672 }
673​
674 bytesSignupServerFn()
675​
676 `
677​
678 const client = compileDirectives({ ...clientConfig, code })
679 const ssr = compileDirectives({ ...ssrConfig, code })
680 const server = compileDirectives({
681 ...serverConfig,
682 code,
683 filename:
684 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
685 .extractedFilename,
686 })
687​
688 expect(client.compiledResult.code).toMatchInlineSnapshot(`
689 "import { createClientRpc } from "my-rpc-lib-client";
690 const bytesSignupServerFn_1 = createClientRpc("test_ts--bytesSignupServerFn_1");
691 const bytesSignupServerFn = bytesSignupServerFn_1;
692 bytesSignupServerFn();"
693 `)
694 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
695 "import { createSsrRpc } from "my-rpc-lib-server";
696 const bytesSignupServerFn_1 = createSsrRpc("test_ts--bytesSignupServerFn_1");
697 const bytesSignupServerFn = bytesSignupServerFn_1;
698 bytesSignupServerFn();"
699 `)
700 expect(server.compiledResult.code).toMatchInlineSnapshot(`
701 "import { createServerRpc } from "my-rpc-lib-server";
702 const bytesSignupServerFn_1 = createServerRpc("test_ts--bytesSignupServerFn_1", async function ({
703 email
704 }: {
705 email: string;
706 }) {
707 return 'test';
708 });
709 const bytesSignupServerFn = bytesSignupServerFn_1;
710 bytesSignupServerFn();
711 export { bytesSignupServerFn_1 };"
712 `)
713 })
714​
715 test('file-wide use server directive', () => {
716 const code = `
717 'use server'
718​
719 import { imported } from 'imported'
720​
721 export const serverFnConstWithImport = async () => {
722 return imported
723 }
724​
725 export function serverFnNamedWithImport () {
726 return imported
727 }
728 `
729​
730 const client = compileDirectives({ ...clientConfig, code })
731 const ssr = compileDirectives({ ...ssrConfig, code })
732 const server = compileDirectives({
733 ...serverConfig,
734 code,
735 filename:
736 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
737 .extractedFilename,
738 })
739​
740 expect(client.compiledResult.code).toMatchInlineSnapshot(`
741 "'use server';
742​
743 import { createClientRpc } from "my-rpc-lib-client";
744 const serverFnConstWithImport_1 = createClientRpc("test_ts--serverFnConstWithImport_1");
745 export const serverFnConstWithImport = serverFnConstWithImport_1;
746 const serverFnNamedWithImport_1 = createClientRpc("test_ts--serverFnNamedWithImport_1");
747 export const serverFnNamedWithImport = serverFnNamedWithImport_1;"
748 `)
749​
750 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
751 "'use server';
752​
753 import { createSsrRpc } from "my-rpc-lib-server";
754 const serverFnConstWithImport_1 = createSsrRpc("test_ts--serverFnConstWithImport_1");
755 export const serverFnConstWithImport = serverFnConstWithImport_1;
756 const serverFnNamedWithImport_1 = createSsrRpc("test_ts--serverFnNamedWithImport_1");
757 export const serverFnNamedWithImport = serverFnNamedWithImport_1;"
758 `)
759​
760 expect(server.compiledResult.code).toMatchInlineSnapshot(`
761 "'use server';
762​
763 import { createServerRpc } from "my-rpc-lib-server";
764 import { imported } from 'imported';
765 const serverFnConstWithImport_1 = createServerRpc("test_ts--serverFnConstWithImport_1", async () => {
766 return imported;
767 });
768 const serverFnNamedWithImport_1 = createServerRpc("test_ts--serverFnNamedWithImport_1", function serverFnNamedWithImport() {
769 return imported;
770 });
771 const serverFnNamedWithImport = serverFnNamedWithImport_1;
772 export { serverFnConstWithImport_1, serverFnNamedWithImport_1 };"
773 `)
774 })
775 test('async function with anonymous default export', () => {
776 const code = `
777 async function bytesSignupServerFn({ email }: { email: string }) {
778 'use server'
779 return 'test'
780 }
781 export default function () {
782 return null;
783 }
784 `
785​
786 const client = compileDirectives({ ...clientConfig, code })
787 const ssr = compileDirectives({ ...ssrConfig, code })
788 const server = compileDirectives({
789 ...serverConfig,
790 code,
791 filename:
792 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
793 .extractedFilename,
794 })
795​
796 expect(client.compiledResult.code).toMatchInlineSnapshot(`
797 "import { createClientRpc } from "my-rpc-lib-client";
798 const bytesSignupServerFn_1 = createClientRpc("test_ts--bytesSignupServerFn_1");
799 const bytesSignupServerFn = bytesSignupServerFn_1;
800 export default function () {
801 return null;
802 }"
803 `)
804 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
805 "import { createSsrRpc } from "my-rpc-lib-server";
806 const bytesSignupServerFn_1 = createSsrRpc("test_ts--bytesSignupServerFn_1");
807 const bytesSignupServerFn = bytesSignupServerFn_1;
808 export default function () {
809 return null;
810 }"
811 `)
812 expect(server.compiledResult.code).toMatchInlineSnapshot(`
813 "import { createServerRpc } from "my-rpc-lib-server";
814 const bytesSignupServerFn_1 = createServerRpc("test_ts--bytesSignupServerFn_1", async function ({
815 email
816 }: {
817 email: string;
818 }) {
819 return 'test';
820 });
821 const bytesSignupServerFn = bytesSignupServerFn_1;
822 export default function () {
823 return null;
824 }
825 export { bytesSignupServerFn_1 };"
826 `)
827 })
828​
829 test('generator function', () => {
830 const code = `
831 function* generator() {
832 'use server'
833 yield 'hello'
834 return 'hello world'
835 }
836 `
837 const client = compileDirectives({
838 ...clientConfig,
839 code,
840 })
841 const ssr = compileDirectives({ ...ssrConfig, code })
842​
843 const server = compileDirectives({
844 ...serverConfig,
845 code,
846 filename: `${ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!.extractedFilename}`,
847 })
848​
849 expect(client.compiledResult.code).toMatchInlineSnapshot(`
850 "import { createClientRpc } from "my-rpc-lib-client";
851 const generator_1 = createClientRpc("test_ts--generator_1");
852 const generator = generator_1;"
853 `)
854​
855 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
856 "import { createSsrRpc } from "my-rpc-lib-server";
857 const generator_1 = createSsrRpc("test_ts--generator_1");
858 const generator = generator_1;"
859 `)
860​
861 expect(server.compiledResult.code).toMatchInlineSnapshot(`
862 "import { createServerRpc } from "my-rpc-lib-server";
863 const generator_1 = createServerRpc("test_ts--generator_1", function* () {
864 yield 'hello';
865 return 'hello world';
866 });
867 const generator = generator_1;
868 export { generator_1 };"
869 `)
870 })
871 test('async generator function', () => {
872 const code = `
873 async function* asyncGenerator() {
874 'use server'
875 yield 'hello'
876 return 'hello world'
877 }
878 `
879 const client = compileDirectives({
880 ...clientConfig,
881 code,
882 })
883 const ssr = compileDirectives({ ...ssrConfig, code })
884​
885 const server = compileDirectives({
886 ...serverConfig,
887 code,
888 filename: `${ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!.extractedFilename}`,
889 })
890​
891 expect(client.compiledResult.code).toMatchInlineSnapshot(`
892 "import { createClientRpc } from "my-rpc-lib-client";
893 const asyncGenerator_1 = createClientRpc("test_ts--asyncGenerator_1");
894 const asyncGenerator = asyncGenerator_1;"
895 `)
896​
897 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
898 "import { createSsrRpc } from "my-rpc-lib-server";
899 const asyncGenerator_1 = createSsrRpc("test_ts--asyncGenerator_1");
900 const asyncGenerator = asyncGenerator_1;"
901 `)
902​
903 expect(server.compiledResult.code).toMatchInlineSnapshot(`
904 "import { createServerRpc } from "my-rpc-lib-server";
905 const asyncGenerator_1 = createServerRpc("test_ts--asyncGenerator_1", async function* () {
906 yield 'hello';
907 return 'hello world';
908 });
909 const asyncGenerator = asyncGenerator_1;
910 export { asyncGenerator_1 };"
911 `)
912 })
913​
914 test('server function references exported value', () => {
915 const code = `
916​
917 import z from "zod";
918​
919 export const zExampleSchema = z.object({
920 name: z.string(),
921 age: z.number(),
922 });
923​
924 export const namedFunction = createServerFn(() => {
925 'use server'
926 zExampleSchema.safeParse({ name: 'test', age: 123 })
927 return 'hello'
928 })
929 `
930​
931 const client = compileDirectives({ ...clientConfig, code })
932 const ssr = compileDirectives({ ...ssrConfig, code })
933 const server = compileDirectives({
934 ...serverConfig,
935 code,
936 filename:
937 ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
938 .extractedFilename,
939 })
940​
941 expect(client.compiledResult.code).toMatchInlineSnapshot(`
942 "import { createClientRpc } from "my-rpc-lib-client";
943 import z from "zod";
944 export const zExampleSchema = z.object({
945 name: z.string(),
946 age: z.number()
947 });
948 const namedFunction_createServerFn = createClientRpc("test_ts--namedFunction_createServerFn");
949 export const namedFunction = createServerFn(namedFunction_createServerFn);"
950 `)
951 expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
952 "import { createSsrRpc } from "my-rpc-lib-server";
953 import z from "zod";
954 export const zExampleSchema = z.object({
955 name: z.string(),
956 age: z.number()
957 });
958 const namedFunction_createServerFn = createSsrRpc("test_ts--namedFunction_createServerFn");
959 export const namedFunction = createServerFn(namedFunction_createServerFn);"
960 `)
961 expect(server.compiledResult.code).toMatchInlineSnapshot(`
962 "import { createServerRpc } from "my-rpc-lib-server";
963 import z from "zod";
964 const zExampleSchema = z.object({
965 name: z.string(),
966 age: z.number()
967 });
968 const namedFunction_createServerFn = createServerRpc("test_ts--namedFunction_createServerFn", () => {
969 zExampleSchema.safeParse({
970 name: 'test',
971 age: 123
972 });
973 return 'hello';
974 });
975 export { namedFunction_createServerFn };"
976 `)
977 })
978})