@tanstack/start-plugin-core 1.132.0-alpha.8 → 1.132.0-alpha.9
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.
- package/dist/esm/create-server-fn-plugin/compiler.d.ts +61 -0
- package/dist/esm/create-server-fn-plugin/compiler.js +336 -0
- package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.d.ts +6 -0
- package/dist/esm/{start-compiler-plugin/serverFn.js → create-server-fn-plugin/handleCreateServerFn.js} +11 -13
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/plugin.d.ts +3 -0
- package/dist/esm/create-server-fn-plugin/plugin.js +113 -0
- package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -0
- package/dist/esm/plugin.js +3 -4
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/start-compiler-plugin/compilers.js +0 -6
- package/dist/esm/start-compiler-plugin/compilers.js.map +1 -1
- package/dist/esm/start-compiler-plugin/constants.d.ts +1 -1
- package/dist/esm/start-compiler-plugin/constants.js +0 -1
- package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
- package/dist/esm/start-compiler-plugin/plugin.d.ts +1 -8
- package/dist/esm/start-compiler-plugin/plugin.js +6 -13
- package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
- package/package.json +6 -6
- package/src/create-server-fn-plugin/compiler.ts +456 -0
- package/src/{start-compiler-plugin/serverFn.ts → create-server-fn-plugin/handleCreateServerFn.ts} +26 -36
- package/src/create-server-fn-plugin/plugin.ts +138 -0
- package/src/plugin.ts +3 -4
- package/src/start-compiler-plugin/compilers.ts +0 -6
- package/src/start-compiler-plugin/constants.ts +0 -1
- package/src/start-compiler-plugin/plugin.ts +7 -22
- package/dist/esm/start-compiler-plugin/serverFn.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/serverFn.js.map +0 -1
|
@@ -10,7 +10,6 @@ import { transformFuncs } from './constants'
|
|
|
10
10
|
import { handleCreateServerFileRouteCallExpressionFactory } from './serverFileRoute'
|
|
11
11
|
import { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'
|
|
12
12
|
import { handleCreateMiddlewareCallExpression } from './middleware'
|
|
13
|
-
import { handleCreateServerFnCallExpression } from './serverFn'
|
|
14
13
|
import {
|
|
15
14
|
handleCreateClientOnlyFnCallExpression,
|
|
16
15
|
handleCreateServerOnlyFnCallExpression,
|
|
@@ -47,11 +46,6 @@ const getIdentifiers = (
|
|
|
47
46
|
),
|
|
48
47
|
paths: [],
|
|
49
48
|
},
|
|
50
|
-
createServerFn: {
|
|
51
|
-
name: 'createServerFn',
|
|
52
|
-
handleCallExpression: handleCreateServerFnCallExpression,
|
|
53
|
-
paths: [],
|
|
54
|
-
},
|
|
55
49
|
createMiddleware: {
|
|
56
50
|
name: 'createMiddleware',
|
|
57
51
|
handleCallExpression: handleCreateMiddlewareCallExpression,
|
|
@@ -8,6 +8,7 @@ import path from 'pathe'
|
|
|
8
8
|
import { VITE_ENVIRONMENT_NAMES } from '../constants'
|
|
9
9
|
import { compileStartOutputFactory } from './compilers'
|
|
10
10
|
import { transformFuncs } from './constants'
|
|
11
|
+
import type { ViteEnvironmentNames } from '../constants'
|
|
11
12
|
import type { Plugin } from 'vite'
|
|
12
13
|
import type { CompileStartFrameworkOptions } from './compilers'
|
|
13
14
|
|
|
@@ -37,33 +38,17 @@ function resolvePackage(packageName: string): string {
|
|
|
37
38
|
|
|
38
39
|
export function startCompilerPlugin(
|
|
39
40
|
framework: CompileStartFrameworkOptions,
|
|
40
|
-
inputOpts?: {
|
|
41
|
-
client?: {
|
|
42
|
-
envName?: string
|
|
43
|
-
}
|
|
44
|
-
server?: {
|
|
45
|
-
envName?: string
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
41
|
): Plugin {
|
|
49
|
-
const opts = {
|
|
50
|
-
client: {
|
|
51
|
-
envName: VITE_ENVIRONMENT_NAMES.client,
|
|
52
|
-
...inputOpts?.client,
|
|
53
|
-
},
|
|
54
|
-
server: {
|
|
55
|
-
envName: VITE_ENVIRONMENT_NAMES.server,
|
|
56
|
-
...inputOpts?.server,
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
|
|
60
42
|
const compileStartOutput = compileStartOutputFactory(framework)
|
|
61
43
|
|
|
62
44
|
return {
|
|
63
45
|
name: 'tanstack-start-core:compiler',
|
|
64
46
|
enforce: 'pre',
|
|
65
47
|
applyToEnvironment(env) {
|
|
66
|
-
return [
|
|
48
|
+
return [
|
|
49
|
+
VITE_ENVIRONMENT_NAMES.client,
|
|
50
|
+
VITE_ENVIRONMENT_NAMES.server,
|
|
51
|
+
].includes(env.name as ViteEnvironmentNames)
|
|
67
52
|
},
|
|
68
53
|
transform: {
|
|
69
54
|
filter: {
|
|
@@ -103,9 +88,9 @@ export function startCompilerPlugin(
|
|
|
103
88
|
},
|
|
104
89
|
handler(code, id) {
|
|
105
90
|
const env =
|
|
106
|
-
this.environment.name ===
|
|
91
|
+
this.environment.name === VITE_ENVIRONMENT_NAMES.client
|
|
107
92
|
? 'client'
|
|
108
|
-
: this.environment.name ===
|
|
93
|
+
: this.environment.name === VITE_ENVIRONMENT_NAMES.server
|
|
109
94
|
? 'server'
|
|
110
95
|
: (() => {
|
|
111
96
|
throw new Error(
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serverFn.js","sources":["../../../src/start-compiler-plugin/serverFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { codeFrameError, getRootCallExpression } from './utils'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nexport function handleCreateServerFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const calledOptions = path.node.arguments[0]\n ? (path.get('arguments.0') as babel.NodePath<t.ObjectExpression>)\n : null\n\n const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {\n return (\n t.isObjectProperty(prop) &&\n t.isIdentifier(prop.key) &&\n prop.key.name === 'validateClient' &&\n t.isBooleanLiteral(prop.value) &&\n prop.value.value === true\n )\n })\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n handler: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, and we're not validating the client, remove the validator call expression\n if (\n opts.env === 'client' &&\n !shouldValidateClient &&\n t.isMemberExpression(callExpressionPaths.validator.node.callee)\n ) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral('use server'))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push(handlerFn)\n }\n}\n"],"names":[],"mappings":";;AAMO,SAAS,mCACd,MACA,MACA;AAKA,QAAM,gBAAgB,KAAK,KAAK,UAAU,CAAC,IACtC,KAAK,IAAI,aAAa,IACvB;AAEJ,QAAM,uBAAuB,CAAC,CAAC,eAAe,KAAK,WAAW,KAAK,CAAC,SAAS;AAC3E,WACE,EAAE,iBAAiB,IAAI,KACvB,EAAE,aAAa,KAAK,GAAG,KACvB,KAAK,IAAI,SAAS,oBAClB,EAAE,i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