@tanstack/start-plugin-core 1.132.0-alpha.1 → 1.132.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/esm/create-server-fn-plugin/compiler.d.ts +61 -0
  2. package/dist/esm/create-server-fn-plugin/compiler.js +336 -0
  3. package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -0
  4. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.d.ts +6 -0
  5. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js +85 -0
  6. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -0
  7. package/dist/esm/create-server-fn-plugin/plugin.d.ts +3 -0
  8. package/dist/esm/create-server-fn-plugin/plugin.js +113 -0
  9. package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -0
  10. package/dist/esm/output-directory.js +5 -2
  11. package/dist/esm/output-directory.js.map +1 -1
  12. package/dist/esm/plugin.d.ts +1 -1
  13. package/dist/esm/plugin.js +12 -15
  14. package/dist/esm/plugin.js.map +1 -1
  15. package/dist/esm/schema.d.ts +20 -28
  16. package/dist/esm/schema.js +10 -14
  17. package/dist/esm/schema.js.map +1 -1
  18. package/dist/esm/start-compiler-plugin/compilers.d.ts +15 -0
  19. package/dist/esm/start-compiler-plugin/compilers.js +131 -0
  20. package/dist/esm/start-compiler-plugin/compilers.js.map +1 -0
  21. package/dist/esm/start-compiler-plugin/constants.d.ts +1 -0
  22. package/dist/esm/start-compiler-plugin/constants.js +13 -0
  23. package/dist/esm/start-compiler-plugin/constants.js.map +1 -0
  24. package/dist/esm/start-compiler-plugin/envOnly.d.ts +5 -0
  25. package/dist/esm/start-compiler-plugin/envOnly.js +41 -0
  26. package/dist/esm/start-compiler-plugin/envOnly.js.map +1 -0
  27. package/dist/esm/start-compiler-plugin/isomorphicFn.d.ts +4 -0
  28. package/dist/esm/start-compiler-plugin/isomorphicFn.js +49 -0
  29. package/dist/esm/start-compiler-plugin/isomorphicFn.js.map +1 -0
  30. package/dist/esm/start-compiler-plugin/middleware.d.ts +4 -0
  31. package/dist/esm/start-compiler-plugin/middleware.js +51 -0
  32. package/dist/esm/start-compiler-plugin/middleware.js.map +1 -0
  33. package/dist/esm/{start-compiler-plugin.d.ts → start-compiler-plugin/plugin.d.ts} +1 -8
  34. package/dist/esm/start-compiler-plugin/plugin.js +96 -0
  35. package/dist/esm/start-compiler-plugin/plugin.js.map +1 -0
  36. package/dist/esm/start-compiler-plugin/serverFileRoute.d.ts +4 -0
  37. package/dist/esm/start-compiler-plugin/serverFileRoute.js +38 -0
  38. package/dist/esm/start-compiler-plugin/serverFileRoute.js.map +1 -0
  39. package/dist/esm/start-compiler-plugin/utils.d.ts +13 -0
  40. package/dist/esm/start-compiler-plugin/utils.js +30 -0
  41. package/dist/esm/start-compiler-plugin/utils.js.map +1 -0
  42. package/package.json +8 -8
  43. package/src/create-server-fn-plugin/compiler.ts +456 -0
  44. package/src/create-server-fn-plugin/handleCreateServerFn.ts +153 -0
  45. package/src/create-server-fn-plugin/plugin.ts +138 -0
  46. package/src/output-directory.ts +13 -6
  47. package/src/plugin.ts +13 -22
  48. package/src/schema.ts +10 -16
  49. package/src/start-compiler-plugin/compilers.ts +195 -0
  50. package/src/start-compiler-plugin/constants.ts +9 -0
  51. package/src/start-compiler-plugin/envOnly.ts +58 -0
  52. package/src/start-compiler-plugin/isomorphicFn.ts +78 -0
  53. package/src/start-compiler-plugin/middleware.ts +79 -0
  54. package/src/start-compiler-plugin/plugin.ts +122 -0
  55. package/src/start-compiler-plugin/serverFileRoute.ts +59 -0
  56. package/src/start-compiler-plugin/utils.ts +41 -0
  57. package/dist/esm/compilers.d.ts +0 -21
  58. package/dist/esm/compilers.js +0 -395
  59. package/dist/esm/compilers.js.map +0 -1
  60. package/dist/esm/start-compiler-plugin.js +0 -78
  61. package/dist/esm/start-compiler-plugin.js.map +0 -1
  62. package/src/compilers.ts +0 -659
  63. package/src/start-compiler-plugin.ts +0 -115
package/src/compilers.ts DELETED
@@ -1,659 +0,0 @@
1
- import * as babel from '@babel/core'
2
- import * as t from '@babel/types'
3
- import { codeFrameColumns } from '@babel/code-frame'
4
-
5
- import {
6
- deadCodeElimination,
7
- findReferencedIdentifiers,
8
- } from 'babel-dead-code-elimination'
9
- import { generateFromAst, parseAst } from '@tanstack/router-utils'
10
- import type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'
11
-
12
- export type CompileStartFrameworkOptions = 'react' | 'solid'
13
-
14
- export function compileStartOutputFactory(
15
- framework: CompileStartFrameworkOptions,
16
- ) {
17
- return function compileStartOutput(opts: CompileOptions): GeneratorResult {
18
- const ast = parseAst(opts)
19
-
20
- const doDce = opts.dce ?? true
21
- // find referenced identifiers *before* we transform anything
22
- const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined
23
-
24
- babel.traverse(ast, {
25
- Program: {
26
- enter(programPath) {
27
- const identifiers: {
28
- createServerRoute: IdentifierConfig
29
- createServerFileRoute: IdentifierConfig
30
- createServerFn: IdentifierConfig
31
- createMiddleware: IdentifierConfig
32
- serverOnly: IdentifierConfig
33
- clientOnly: IdentifierConfig
34
- createIsomorphicFn: IdentifierConfig
35
- createServerRootRoute: IdentifierConfig
36
- } = {
37
- createServerRootRoute: {
38
- name: 'createServerRootRoute',
39
- handleCallExpression:
40
- handleCreateServerFileRouteCallExpressionFactory(
41
- framework,
42
- 'createServerRootRoute',
43
- ),
44
- paths: [],
45
- },
46
- createServerRoute: {
47
- name: 'createServerRoute',
48
- handleCallExpression:
49
- handleCreateServerFileRouteCallExpressionFactory(
50
- framework,
51
- 'createServerRoute',
52
- ),
53
- paths: [],
54
- },
55
- createServerFileRoute: {
56
- name: 'createServerFileRoute',
57
- handleCallExpression:
58
- handleCreateServerFileRouteCallExpressionFactory(
59
- framework,
60
- 'createServerFileRoute',
61
- ),
62
- paths: [],
63
- },
64
- createServerFn: {
65
- name: 'createServerFn',
66
- handleCallExpression: handleCreateServerFnCallExpression,
67
- paths: [],
68
- },
69
- createMiddleware: {
70
- name: 'createMiddleware',
71
- handleCallExpression: handleCreateMiddlewareCallExpression,
72
- paths: [],
73
- },
74
- serverOnly: {
75
- name: 'serverOnly',
76
- handleCallExpression: handleServerOnlyCallExpression,
77
- paths: [],
78
- },
79
- clientOnly: {
80
- name: 'clientOnly',
81
- handleCallExpression: handleClientOnlyCallExpression,
82
- paths: [],
83
- },
84
- createIsomorphicFn: {
85
- name: 'createIsomorphicFn',
86
- handleCallExpression: handleCreateIsomorphicFnCallExpression,
87
- paths: [],
88
- },
89
- }
90
-
91
- const identifierKeys = Object.keys(identifiers) as Array<
92
- keyof typeof identifiers
93
- >
94
-
95
- programPath.traverse({
96
- ImportDeclaration: (path) => {
97
- if (path.node.source.value !== `@tanstack/${framework}-start`) {
98
- return
99
- }
100
-
101
- // handle a destructured imports being renamed like "import { createServerFn as myCreateServerFn } from '@tanstack/react-start';"
102
- path.node.specifiers.forEach((specifier) => {
103
- identifierKeys.forEach((identifierKey) => {
104
- const identifier = identifiers[identifierKey]
105
-
106
- if (
107
- specifier.type === 'ImportSpecifier' &&
108
- specifier.imported.type === 'Identifier'
109
- ) {
110
- if (specifier.imported.name === identifierKey) {
111
- identifier.name = specifier.local.name
112
- }
113
- }
114
-
115
- // handle namespace imports like "import * as TanStackStart from '@tanstack/react-start';"
116
- if (specifier.type === 'ImportNamespaceSpecifier') {
117
- identifier.name = `${specifier.local.name}.${identifierKey}`
118
- }
119
- })
120
- })
121
- },
122
- CallExpression: (path) => {
123
- identifierKeys.forEach((identifierKey) => {
124
- // Check to see if the call expression is a call to the
125
- // identifiers[identifierKey].name
126
- if (
127
- t.isIdentifier(path.node.callee) &&
128
- path.node.callee.name === identifiers[identifierKey].name
129
- ) {
130
- // The identifier could be a call to the original function
131
- // in the source code. If this is case, we need to ignore it.
132
- // Check the scope to see if the identifier is a function declaration.
133
- // if it is, then we can ignore it.
134
-
135
- if (
136
- path.scope.getBinding(identifiers[identifierKey].name)?.path
137
- .node.type === 'FunctionDeclaration'
138
- ) {
139
- return
140
- }
141
-
142
- return identifiers[identifierKey].paths.push(path)
143
- }
144
-
145
- if (t.isMemberExpression(path.node.callee)) {
146
- if (
147
- t.isIdentifier(path.node.callee.object) &&
148
- t.isIdentifier(path.node.callee.property)
149
- ) {
150
- const callname = [
151
- path.node.callee.object.name,
152
- path.node.callee.property.name,
153
- ].join('.')
154
-
155
- if (callname === identifiers[identifierKey].name) {
156
- identifiers[identifierKey].paths.push(path)
157
- }
158
- }
159
- }
160
-
161
- return
162
- })
163
- },
164
- })
165
-
166
- identifierKeys.forEach((identifierKey) => {
167
- identifiers[identifierKey].paths.forEach((path) => {
168
- identifiers[identifierKey].handleCallExpression(
169
- path as babel.NodePath<t.CallExpression>,
170
- opts,
171
- )
172
- })
173
- })
174
- },
175
- },
176
- })
177
-
178
- if (doDce) {
179
- deadCodeElimination(ast, refIdents)
180
- }
181
-
182
- return generateFromAst(ast, {
183
- sourceMaps: true,
184
- sourceFileName: opts.filename,
185
- filename: opts.filename,
186
- })
187
- }
188
- }
189
-
190
- function handleCreateServerFileRouteCallExpressionFactory(
191
- factory: CompileStartFrameworkOptions,
192
- method:
193
- | 'createServerFileRoute'
194
- | 'createServerRoute'
195
- | 'createServerRootRoute',
196
- ) {
197
- return function handleCreateServerFileRouteCallExpression(
198
- path: babel.NodePath<t.CallExpression>,
199
- opts: CompileOptions,
200
- ) {
201
- const PACKAGES = { start: `@tanstack/${factory}-start/server` }
202
-
203
- let highestParent: babel.NodePath<any> = path
204
-
205
- while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {
206
- highestParent = highestParent.parentPath
207
- }
208
-
209
- const programPath = highestParent.parentPath as babel.NodePath<t.Program>
210
-
211
- // If we're on the client, remove the entire variable
212
- if (opts.env === 'client') {
213
- // console.debug('createServerFileRoute -> manifest:\n', manifest)
214
- highestParent.remove()
215
- return
216
- }
217
-
218
- let isCreateServerFileRouteImported = false as boolean
219
-
220
- programPath.traverse({
221
- ImportDeclaration(importPath) {
222
- const importSource = importPath.node.source.value
223
- if (importSource === PACKAGES.start) {
224
- const specifiers = importPath.node.specifiers
225
- isCreateServerFileRouteImported ||= specifiers.some((specifier) => {
226
- return (
227
- t.isImportSpecifier(specifier) &&
228
- t.isIdentifier(specifier.imported) &&
229
- specifier.imported.name === method
230
- )
231
- })
232
- }
233
- },
234
- })
235
-
236
- if (!isCreateServerFileRouteImported) {
237
- const importDeclaration = t.importDeclaration(
238
- [t.importSpecifier(t.identifier(method), t.identifier(method))],
239
- t.stringLiteral(PACKAGES.start),
240
- )
241
- programPath.node.body.unshift(importDeclaration)
242
- }
243
- }
244
- }
245
-
246
- // build these once and reuse them
247
- export const handleServerOnlyCallExpression =
248
- buildEnvOnlyCallExpressionHandler('server')
249
- export const handleClientOnlyCallExpression =
250
- buildEnvOnlyCallExpressionHandler('client')
251
-
252
- export type CompileOptions = ParseAstOptions & {
253
- env: 'server' | 'client'
254
- dce?: boolean
255
- filename: string
256
- }
257
-
258
- export type IdentifierConfig = {
259
- name: string
260
- handleCallExpression: (
261
- path: babel.NodePath<t.CallExpression>,
262
- opts: CompileOptions,
263
- ) => void
264
- paths: Array<babel.NodePath>
265
- }
266
-
267
- export function handleCreateServerFnCallExpression(
268
- path: babel.NodePath<t.CallExpression>,
269
- opts: CompileOptions,
270
- ) {
271
- // The function is the 'fn' property of the object passed to createServerFn
272
-
273
- // const firstArg = path.node.arguments[0]
274
- // if (t.isObjectExpression(firstArg)) {
275
- // // Was called with some options
276
- // }
277
-
278
- // Traverse the member expression and find the call expressions for
279
- // the validator, handler, and middleware methods. Check to make sure they
280
- // are children of the createServerFn call expression.
281
-
282
- const calledOptions = path.node.arguments[0]
283
- ? (path.get('arguments.0') as babel.NodePath<t.ObjectExpression>)
284
- : null
285
-
286
- const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {
287
- return (
288
- t.isObjectProperty(prop) &&
289
- t.isIdentifier(prop.key) &&
290
- prop.key.name === 'validateClient' &&
291
- t.isBooleanLiteral(prop.value) &&
292
- prop.value.value === true
293
- )
294
- })
295
-
296
- const callExpressionPaths = {
297
- middleware: null as babel.NodePath<t.CallExpression> | null,
298
- validator: null as babel.NodePath<t.CallExpression> | null,
299
- handler: null as babel.NodePath<t.CallExpression> | null,
300
- }
301
-
302
- const validMethods = Object.keys(callExpressionPaths)
303
-
304
- const rootCallExpression = getRootCallExpression(path)
305
-
306
- // if (debug)
307
- // console.info(
308
- // 'Handling createServerFn call expression:',
309
- // rootCallExpression.toString(),
310
- // )
311
-
312
- // Check if the call is assigned to a variable
313
- if (!rootCallExpression.parentPath.isVariableDeclarator()) {
314
- throw new Error('createServerFn must be assigned to a variable!')
315
- }
316
-
317
- // Get the identifier name of the variable
318
- const variableDeclarator = rootCallExpression.parentPath.node
319
- const existingVariableName = (variableDeclarator.id as t.Identifier).name
320
-
321
- rootCallExpression.traverse({
322
- MemberExpression(memberExpressionPath) {
323
- if (t.isIdentifier(memberExpressionPath.node.property)) {
324
- const name = memberExpressionPath.node.property
325
- .name as keyof typeof callExpressionPaths
326
-
327
- if (
328
- validMethods.includes(name) &&
329
- memberExpressionPath.parentPath.isCallExpression()
330
- ) {
331
- callExpressionPaths[name] = memberExpressionPath.parentPath
332
- }
333
- }
334
- },
335
- })
336
-
337
- if (callExpressionPaths.validator) {
338
- const innerInputExpression = callExpressionPaths.validator.node.arguments[0]
339
-
340
- if (!innerInputExpression) {
341
- throw new Error(
342
- 'createServerFn().validator() must be called with a validator!',
343
- )
344
- }
345
-
346
- // If we're on the client, and we're not validating the client, remove the validator call expression
347
- if (
348
- opts.env === 'client' &&
349
- !shouldValidateClient &&
350
- t.isMemberExpression(callExpressionPaths.validator.node.callee)
351
- ) {
352
- callExpressionPaths.validator.replaceWith(
353
- callExpressionPaths.validator.node.callee.object,
354
- )
355
- }
356
- }
357
-
358
- // First, we need to move the handler function to a nested function call
359
- // that is applied to the arguments passed to the server function.
360
-
361
- const handlerFnPath = callExpressionPaths.handler?.get(
362
- 'arguments.0',
363
- ) as babel.NodePath<any>
364
-
365
- if (!callExpressionPaths.handler || !handlerFnPath.node) {
366
- throw codeFrameError(
367
- opts.code,
368
- path.node.callee.loc!,
369
- `createServerFn must be called with a "handler" property!`,
370
- )
371
- }
372
-
373
- const handlerFn = handlerFnPath.node
374
-
375
- // So, the way we do this is we give the handler function a way
376
- // to access the serverFn ctx on the server via function scope.
377
- // The 'use server' extracted function will be called with the
378
- // payload from the client, then use the scoped serverFn ctx
379
- // to execute the handler function.
380
- // This way, we can do things like data and middleware validation
381
- // in the __execute function without having to AST transform the
382
- // handler function too much itself.
383
-
384
- // .handler((optsOut, ctx) => {
385
- // return ((optsIn) => {
386
- // 'use server'
387
- // ctx.__execute(handlerFn, optsIn)
388
- // })(optsOut)
389
- // })
390
-
391
- // If the handler function is an identifier and we're on the client, we need to
392
- // remove the bound function from the file.
393
- // If we're on the server, you can leave it, since it will get referenced
394
- // as a second argument.
395
-
396
- if (t.isIdentifier(handlerFn)) {
397
- if (opts.env === 'client') {
398
- // Find the binding for the handler function
399
- const binding = handlerFnPath.scope.getBinding(handlerFn.name)
400
- // Remove it
401
- if (binding) {
402
- binding.path.remove()
403
- }
404
- }
405
- // If the env is server, just leave it alone
406
- }
407
-
408
- handlerFnPath.replaceWith(
409
- t.arrowFunctionExpression(
410
- [t.identifier('opts'), t.identifier('signal')],
411
- t.blockStatement(
412
- // Everything in here is server-only, since the client
413
- // will strip out anything in the 'use server' directive.
414
- [
415
- t.returnStatement(
416
- t.callExpression(
417
- t.identifier(`${existingVariableName}.__executeServer`),
418
- [t.identifier('opts'), t.identifier('signal')],
419
- ),
420
- ),
421
- ],
422
- [t.directive(t.directiveLiteral('use server'))],
423
- ),
424
- ),
425
- )
426
-
427
- if (opts.env === 'server') {
428
- callExpressionPaths.handler.node.arguments.push(handlerFn)
429
- }
430
- }
431
-
432
- export function handleCreateMiddlewareCallExpression(
433
- path: babel.NodePath<t.CallExpression>,
434
- opts: CompileOptions,
435
- ) {
436
- const rootCallExpression = getRootCallExpression(path)
437
-
438
- // if (debug)
439
- // console.info(
440
- // 'Handling createMiddleware call expression:',
441
- // rootCallExpression.toString(),
442
- // )
443
-
444
- const callExpressionPaths = {
445
- middleware: null as babel.NodePath<t.CallExpression> | null,
446
- validator: null as babel.NodePath<t.CallExpression> | null,
447
- client: null as babel.NodePath<t.CallExpression> | null,
448
- server: null as babel.NodePath<t.CallExpression> | null,
449
- }
450
-
451
- const validMethods = Object.keys(callExpressionPaths)
452
-
453
- rootCallExpression.traverse({
454
- MemberExpression(memberExpressionPath) {
455
- if (t.isIdentifier(memberExpressionPath.node.property)) {
456
- const name = memberExpressionPath.node.property
457
- .name as keyof typeof callExpressionPaths
458
-
459
- if (
460
- validMethods.includes(name) &&
461
- memberExpressionPath.parentPath.isCallExpression()
462
- ) {
463
- callExpressionPaths[name] = memberExpressionPath.parentPath
464
- }
465
- }
466
- },
467
- })
468
-
469
- if (callExpressionPaths.validator) {
470
- const innerInputExpression = callExpressionPaths.validator.node.arguments[0]
471
-
472
- if (!innerInputExpression) {
473
- throw new Error(
474
- 'createMiddleware().validator() must be called with a validator!',
475
- )
476
- }
477
-
478
- // If we're on the client, remove the validator call expression
479
- if (opts.env === 'client') {
480
- if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
481
- callExpressionPaths.validator.replaceWith(
482
- callExpressionPaths.validator.node.callee.object,
483
- )
484
- }
485
- }
486
- }
487
-
488
- const serverFnPath = callExpressionPaths.server?.get(
489
- 'arguments.0',
490
- ) as babel.NodePath<any>
491
-
492
- if (
493
- callExpressionPaths.server &&
494
- serverFnPath.node &&
495
- opts.env === 'client'
496
- ) {
497
- // If we're on the client, remove the server call expression
498
- if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {
499
- callExpressionPaths.server.replaceWith(
500
- callExpressionPaths.server.node.callee.object,
501
- )
502
- }
503
- }
504
- }
505
-
506
- function buildEnvOnlyCallExpressionHandler(env: 'client' | 'server') {
507
- return function envOnlyCallExpressionHandler(
508
- path: babel.NodePath<t.CallExpression>,
509
- opts: CompileOptions,
510
- ) {
511
- // if (debug)
512
- // console.info(`Handling ${env}Only call expression:`, path.toString())
513
-
514
- const isEnvMatch =
515
- env === 'client' ? opts.env === 'client' : opts.env === 'server'
516
-
517
- if (isEnvMatch) {
518
- // extract the inner function from the call expression
519
- const innerInputExpression = path.node.arguments[0]
520
-
521
- if (!t.isExpression(innerInputExpression)) {
522
- throw new Error(
523
- `${env}Only() functions must be called with a function!`,
524
- )
525
- }
526
-
527
- path.replaceWith(innerInputExpression)
528
- return
529
- }
530
-
531
- // If we're on the wrong environment, replace the call expression
532
- // with a function that always throws an error.
533
- path.replaceWith(
534
- t.arrowFunctionExpression(
535
- [],
536
- t.blockStatement([
537
- t.throwStatement(
538
- t.newExpression(t.identifier('Error'), [
539
- t.stringLiteral(
540
- `${env}Only() functions can only be called on the ${env}!`,
541
- ),
542
- ]),
543
- ),
544
- ]),
545
- ),
546
- )
547
- }
548
- }
549
-
550
- export function handleCreateIsomorphicFnCallExpression(
551
- path: babel.NodePath<t.CallExpression>,
552
- opts: CompileOptions,
553
- ) {
554
- const rootCallExpression = getRootCallExpression(path)
555
-
556
- // if (debug)
557
- // console.info(
558
- // 'Handling createIsomorphicFn call expression:',
559
- // rootCallExpression.toString(),
560
- // )
561
-
562
- const callExpressionPaths = {
563
- client: null as babel.NodePath<t.CallExpression> | null,
564
- server: null as babel.NodePath<t.CallExpression> | null,
565
- }
566
-
567
- const validMethods = Object.keys(callExpressionPaths)
568
-
569
- rootCallExpression.traverse({
570
- MemberExpression(memberExpressionPath) {
571
- if (t.isIdentifier(memberExpressionPath.node.property)) {
572
- const name = memberExpressionPath.node.property
573
- .name as keyof typeof callExpressionPaths
574
-
575
- if (
576
- validMethods.includes(name) &&
577
- memberExpressionPath.parentPath.isCallExpression()
578
- ) {
579
- callExpressionPaths[name] = memberExpressionPath.parentPath
580
- }
581
- }
582
- },
583
- })
584
-
585
- if (
586
- validMethods.every(
587
- (method) =>
588
- !callExpressionPaths[method as keyof typeof callExpressionPaths],
589
- )
590
- ) {
591
- const variableId = rootCallExpression.parentPath.isVariableDeclarator()
592
- ? rootCallExpression.parentPath.node.id
593
- : null
594
- console.warn(
595
- 'createIsomorphicFn called without a client or server implementation!',
596
- 'This will result in a no-op function.',
597
- 'Variable name:',
598
- t.isIdentifier(variableId) ? variableId.name : 'unknown',
599
- )
600
- }
601
-
602
- const envCallExpression = callExpressionPaths[opts.env]
603
-
604
- if (!envCallExpression) {
605
- // if we don't have an implementation for this environment, default to a no-op
606
- rootCallExpression.replaceWith(
607
- t.arrowFunctionExpression([], t.blockStatement([])),
608
- )
609
- return
610
- }
611
-
612
- const innerInputExpression = envCallExpression.node.arguments[0]
613
-
614
- if (!t.isExpression(innerInputExpression)) {
615
- throw new Error(
616
- `createIsomorphicFn().${opts.env}(func) must be called with a function!`,
617
- )
618
- }
619
-
620
- rootCallExpression.replaceWith(innerInputExpression)
621
- }
622
-
623
- export function getRootCallExpression(path: babel.NodePath<t.CallExpression>) {
624
- // Find the highest callExpression parent
625
- let rootCallExpression: babel.NodePath<t.CallExpression> = path
626
-
627
- // Traverse up the chain of CallExpressions
628
- while (rootCallExpression.parentPath.isMemberExpression()) {
629
- const parent = rootCallExpression.parentPath
630
- if (parent.parentPath.isCallExpression()) {
631
- rootCallExpression = parent.parentPath
632
- }
633
- }
634
-
635
- return rootCallExpression
636
- }
637
-
638
- function codeFrameError(
639
- code: string,
640
- loc: {
641
- start: { line: number; column: number }
642
- end: { line: number; column: number }
643
- },
644
- message: string,
645
- ) {
646
- const frame = codeFrameColumns(
647
- code,
648
- {
649
- start: loc.start,
650
- end: loc.end,
651
- },
652
- {
653
- highlightCode: true,
654
- message,
655
- },
656
- )
657
-
658
- return new Error(frame)
659
- }