@tanstack/start-plugin-core 1.132.0-alpha.5 → 1.132.0-alpha.7
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/plugin.js +2 -2
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/schema.d.ts +20 -28
- package/dist/esm/schema.js +10 -14
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/start-compiler-plugin/compilers.d.ts +0 -6
- package/dist/esm/start-compiler-plugin/compilers.js +12 -271
- 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 +2 -2
- package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
- package/dist/esm/start-compiler-plugin/envOnly.d.ts +5 -0
- package/dist/esm/start-compiler-plugin/envOnly.js +41 -0
- package/dist/esm/start-compiler-plugin/envOnly.js.map +1 -0
- package/dist/esm/start-compiler-plugin/isomorphicFn.d.ts +4 -0
- package/dist/esm/start-compiler-plugin/isomorphicFn.js +49 -0
- package/dist/esm/start-compiler-plugin/isomorphicFn.js.map +1 -0
- package/dist/esm/start-compiler-plugin/middleware.d.ts +4 -0
- package/dist/esm/start-compiler-plugin/middleware.js +51 -0
- package/dist/esm/start-compiler-plugin/middleware.js.map +1 -0
- package/dist/esm/start-compiler-plugin/plugin.js +59 -26
- package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
- package/dist/esm/start-compiler-plugin/serverFileRoute.d.ts +4 -0
- package/dist/esm/start-compiler-plugin/serverFileRoute.js +38 -0
- package/dist/esm/start-compiler-plugin/serverFileRoute.js.map +1 -0
- package/dist/esm/start-compiler-plugin/serverFn.d.ts +4 -0
- package/dist/esm/start-compiler-plugin/serverFn.js +87 -0
- package/dist/esm/start-compiler-plugin/serverFn.js.map +1 -0
- package/dist/esm/start-compiler-plugin/utils.d.ts +13 -0
- package/dist/esm/start-compiler-plugin/utils.js +30 -0
- package/dist/esm/start-compiler-plugin/utils.js.map +1 -0
- package/package.json +2 -2
- package/src/plugin.ts +2 -2
- package/src/schema.ts +10 -16
- package/src/start-compiler-plugin/compilers.ts +16 -462
- package/src/start-compiler-plugin/constants.ts +2 -2
- package/src/start-compiler-plugin/envOnly.ts +58 -0
- package/src/start-compiler-plugin/isomorphicFn.ts +78 -0
- package/src/start-compiler-plugin/middleware.ts +79 -0
- package/src/start-compiler-plugin/plugin.ts +67 -36
- package/src/start-compiler-plugin/serverFileRoute.ts +59 -0
- package/src/start-compiler-plugin/serverFn.ts +163 -0
- package/src/start-compiler-plugin/utils.ts +41 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compilers.js","sources":["../../../src/start-compiler-plugin/compilers.ts"],"sourcesContent":["import * as babel from '@babel/core'\nimport * as t from '@babel/types'\nimport { codeFrameColumns } from '@babel/code-frame'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport { transformFuncs } from './constants'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\ntype Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }\nconst getIdentifiers = (\n framework: CompileStartFrameworkOptions,\n): Identifiers => ({\n createServerRootRoute: {\n name: 'createServerRootRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRootRoute',\n ),\n paths: [],\n },\n createServerRoute: {\n name: 'createServerRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRoute',\n ),\n paths: [],\n },\n createServerFileRoute: {\n name: 'createServerFileRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerFileRoute',\n ),\n paths: [],\n },\n createServerFn: {\n name: 'createServerFn',\n handleCallExpression: handleCreateServerFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddlewareCallExpression,\n paths: [],\n },\n serverOnly: {\n name: 'serverOnly',\n handleCallExpression: handleServerOnlyCallExpression,\n paths: [],\n },\n clientOnly: {\n name: 'clientOnly',\n handleCallExpression: handleClientOnlyCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n})\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\n const ast = parseAst(opts)\n\n const doDce = opts.dce ?? true\n // find referenced identifiers *before* we transform anything\n const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined\n\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n const identifiers = getIdentifiers(framework)\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\n return\n }\n\n // handle a destructured imports being renamed like \"import { createServerFn as myCreateServerFn } from '@tanstack/react-start';\"\n path.node.specifiers.forEach((specifier) => {\n transformFuncs.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n\n if (\n specifier.type === 'ImportSpecifier' &&\n specifier.imported.type === 'Identifier'\n ) {\n if (specifier.imported.name === identifierKey) {\n identifier.name = specifier.local.name\n }\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n if (specifier.type === 'ImportNamespaceSpecifier') {\n identifier.name = `${specifier.local.name}.${identifierKey}`\n }\n })\n })\n },\n CallExpression: (path) => {\n transformFuncs.forEach((identifierKey) => {\n // Check to see if the call expression is a call to the\n // identifiers[identifierKey].name\n if (\n t.isIdentifier(path.node.callee) &&\n path.node.callee.name === identifiers[identifierKey].name\n ) {\n // The identifier could be a call to the original function\n // in the source code. If this is case, we need to ignore it.\n // Check the scope to see if the identifier is a function declaration.\n // if it is, then we can ignore it.\n\n if (\n path.scope.getBinding(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\n if (t.isMemberExpression(path.node.callee)) {\n if (\n t.isIdentifier(path.node.callee.object) &&\n t.isIdentifier(path.node.callee.property)\n ) {\n const callname = [\n path.node.callee.object.name,\n path.node.callee.property.name,\n ].join('.')\n\n if (callname === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].handleCallExpression(\n path as babel.NodePath<t.CallExpression>,\n opts,\n )\n })\n })\n },\n },\n })\n\n if (doDce) {\n deadCodeElimination(ast, refIdents)\n }\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: opts.filename,\n filename: opts.filename,\n })\n }\n}\n\nfunction handleCreateServerFileRouteCallExpressionFactory(\n framework: CompileStartFrameworkOptions,\n method:\n | 'createServerFileRoute'\n | 'createServerRoute'\n | 'createServerRootRoute',\n) {\n return function handleCreateServerFileRouteCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n const PACKAGES = { start: `@tanstack/${framework}-start/server` }\n\n let highestParent: babel.NodePath<any> = path\n\n while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {\n highestParent = highestParent.parentPath\n }\n\n const programPath = highestParent.parentPath as babel.NodePath<t.Program>\n\n // If we're on the client, remove the entire variable\n if (opts.env === 'client') {\n highestParent.remove()\n return\n }\n\n let isCreateServerFileRouteImported = false as boolean\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const importSource = importPath.node.source.value\n if (importSource === PACKAGES.start) {\n const specifiers = importPath.node.specifiers\n isCreateServerFileRouteImported ||= specifiers.some((specifier) => {\n return (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === method\n )\n })\n }\n },\n })\n\n if (!isCreateServerFileRouteImported) {\n const importDeclaration = t.importDeclaration(\n [t.importSpecifier(t.identifier(method), t.identifier(method))],\n t.stringLiteral(PACKAGES.start),\n )\n programPath.node.body.unshift(importDeclaration)\n }\n }\n}\n\n// build these once and reuse them\nexport const handleServerOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('server')\nexport const handleClientOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('client')\n\nexport type CompileOptions = ParseAstOptions & {\n env: 'server' | 'client'\n dce?: boolean\n filename: string\n}\n\nexport type IdentifierConfig = {\n name: string\n handleCallExpression: (\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) => void\n paths: Array<babel.NodePath>\n}\n\nexport function handleCreateServerFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n // The function is the 'fn' property of the object passed to createServerFn\n\n // const firstArg = path.node.arguments[0]\n // if (t.isObjectExpression(firstArg)) {\n // // Was called with some options\n // }\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\nexport function handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\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 'createMiddleware().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n\nfunction buildEnvOnlyCallExpressionHandler(env: 'client' | 'server') {\n return function envOnlyCallExpressionHandler(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n // if (debug)\n // console.info(`Handling ${env}Only call expression:`, path.toString())\n\n const isEnvMatch =\n env === 'client' ? opts.env === 'client' : opts.env === 'server'\n\n if (isEnvMatch) {\n // extract the inner function from the call expression\n const innerInputExpression = path.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `${env}Only() functions must be called with a function!`,\n )\n }\n\n path.replaceWith(innerInputExpression)\n return\n }\n\n // If we're on the wrong environment, replace the call expression\n // with a function that always throws an error.\n path.replaceWith(\n t.arrowFunctionExpression(\n [],\n t.blockStatement([\n t.throwStatement(\n t.newExpression(t.identifier('Error'), [\n t.stringLiteral(\n `${env}Only() functions can only be called on the ${env}!`,\n ),\n ]),\n ),\n ]),\n ),\n )\n }\n}\n\nexport function handleCreateIsomorphicFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createIsomorphicFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\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 (\n validMethods.every(\n (method) =>\n !callExpressionPaths[method as keyof typeof callExpressionPaths],\n )\n ) {\n const variableId = rootCallExpression.parentPath.isVariableDeclarator()\n ? rootCallExpression.parentPath.node.id\n : null\n console.warn(\n 'createIsomorphicFn called without a client or server implementation!',\n 'This will result in a no-op function.',\n 'Variable name:',\n t.isIdentifier(variableId) ? variableId.name : 'unknown',\n )\n }\n\n const envCallExpression = callExpressionPaths[opts.env]\n\n if (!envCallExpression) {\n // if we don't have an implementation for this environment, default to a no-op\n rootCallExpression.replaceWith(\n t.arrowFunctionExpression([], t.blockStatement([])),\n )\n return\n }\n\n const innerInputExpression = envCallExpression.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `createIsomorphicFn().${opts.env}(func) must be called with a function!`,\n )\n }\n\n rootCallExpression.replaceWith(innerInputExpression)\n}\n\nexport function getRootCallExpression(path: babel.NodePath<t.CallExpression>) {\n // Find the highest callExpression parent\n let rootCallExpression: babel.NodePath<t.CallExpression> = path\n\n // Traverse up the chain of CallExpressions\n while (rootCallExpression.parentPath.isMemberExpression()) {\n const parent = rootCallExpression.parentPath\n if (parent.parentPath.isCallExpression()) {\n rootCallExpression = parent.parentPath\n }\n }\n\n return rootCallExpression\n}\n\nfunction codeFrameError(\n code: string,\n loc: {\n start: { line: number; column: number }\n end: { line: number; column: number }\n },\n message: string,\n) {\n const frame = codeFrameColumns(\n code,\n {\n start: loc.start,\n end: loc.end,\n },\n {\n highlightCode: true,\n message,\n },\n )\n\n return new Error(frame)\n}\n"],"names":[],"mappings":";;;;;;AAeA,MAAM,iBAAiB,CACrB,eACiB;AAAA,EACjB,uBAAuB;AAAA,IACrB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AA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M,uBAAuB,kBAAkB,KAAK,UAAU,CAAC;AAE/D,MAAI,CAAC,EAAE,aAAa,oBAAoB,GAAG;AACzC,UAAM,IAAI;AAAA,MACR,wBAAwB,KAAK,GAAG;AAAA,IAAA;AAAA,EAEpC;AAEA,qBAAmB,YAAY,oBAAoB;AACrD;AAEO,SAAS,sBAAsB,MAAwC;AAE5E,MAAI,qBAAuD;AAG3D,SAAO,mBAAmB,WAAW,sBAAsB;AACzD,UAAM,SAAS,mBAAmB;AAClC,QAAI,OAAO,WAAW,oBAAoB;AACxC,2BAAqB,OAAO;AAAA,IAC9B;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,eACP,MACA,KAIA,SACA;AACA,QAAM,QAAQ;AAAA,IACZ;AAAA,IACA;AAAA,MACE,OAAO,IAAI;AAAA,MACX,KAAK,IAAI;AAAA,IAAA;AAAA,IAEX;AAAA,MACE,eAAe;AAAA,MACf;AAAA,IAAA;AAAA,EACF;AAGF,SAAO,IAAI,MAAM,KAAK;AACxB;"}
|
|
1
|
+
{"version":3,"file":"compilers.js","sources":["../../../src/start-compiler-plugin/compilers.ts"],"sourcesContent":["import * as babel from '@babel/core'\nimport * as t from '@babel/types'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport { transformFuncs } from './constants'\nimport { handleCreateServerFileRouteCallExpressionFactory } from './serverFileRoute'\nimport { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'\nimport { handleCreateMiddlewareCallExpression } from './middleware'\nimport { handleCreateServerFnCallExpression } from './serverFn'\nimport {\n handleCreateClientOnlyFnCallExpression,\n handleCreateServerOnlyFnCallExpression,\n} from './envOnly'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\ntype Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }\nconst getIdentifiers = (\n framework: CompileStartFrameworkOptions,\n): Identifiers => ({\n createServerRootRoute: {\n name: 'createServerRootRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRootRoute',\n ),\n paths: [],\n },\n createServerRoute: {\n name: 'createServerRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRoute',\n ),\n paths: [],\n },\n createServerFileRoute: {\n name: 'createServerFileRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerFileRoute',\n ),\n paths: [],\n },\n createServerFn: {\n name: 'createServerFn',\n handleCallExpression: handleCreateServerFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddlewareCallExpression,\n paths: [],\n },\n createServerOnlyFn: {\n name: 'createServerOnlyFn',\n handleCallExpression: handleCreateServerOnlyFnCallExpression,\n paths: [],\n },\n createClientOnlyFn: {\n name: 'createClientOnlyFn',\n handleCallExpression: handleCreateClientOnlyFnCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n})\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\n const ast = parseAst(opts)\n\n const doDce = opts.dce ?? true\n // find referenced identifiers *before* we transform anything\n const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined\n\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n const identifiers = getIdentifiers(framework)\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\n return\n }\n\n // handle a destructured imports being renamed like \"import { createServerFn as myCreateServerFn } from '@tanstack/react-start';\"\n path.node.specifiers.forEach((specifier) => {\n transformFuncs.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n\n if (\n specifier.type === 'ImportSpecifier' &&\n specifier.imported.type === 'Identifier'\n ) {\n if (specifier.imported.name === identifierKey) {\n identifier.name = specifier.local.name\n }\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n if (specifier.type === 'ImportNamespaceSpecifier') {\n identifier.name = `${specifier.local.name}.${identifierKey}`\n }\n })\n })\n },\n CallExpression: (path) => {\n transformFuncs.forEach((identifierKey) => {\n // Check to see if the call expression is a call to the\n // identifiers[identifierKey].name\n if (\n t.isIdentifier(path.node.callee) &&\n path.node.callee.name === identifiers[identifierKey].name\n ) {\n // The identifier could be a call to the original function\n // in the source code. If this is case, we need to ignore it.\n // Check the scope to see if the identifier is a function declaration.\n // if it is, then we can ignore it.\n\n if (\n path.scope.getBinding(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n // which are then called like \"TanStackStart.createServerFn()\"\n if (t.isMemberExpression(path.node.callee)) {\n if (\n t.isIdentifier(path.node.callee.object) &&\n t.isIdentifier(path.node.callee.property)\n ) {\n const callname = [\n path.node.callee.object.name,\n path.node.callee.property.name,\n ].join('.')\n\n if (callname === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].handleCallExpression(\n path as babel.NodePath<t.CallExpression>,\n opts,\n )\n })\n })\n },\n },\n })\n\n if (doDce) {\n deadCodeElimination(ast, refIdents)\n }\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: opts.filename,\n filename: opts.filename,\n })\n }\n}\n\nexport type CompileOptions = ParseAstOptions & {\n env: 'server' | 'client'\n dce?: boolean\n filename: string\n}\n\nexport type IdentifierConfig = {\n name: string\n handleCallExpression: (\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) => void\n paths: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,gBACF;AAEA;AAAA,cACF,CAAC;AAAA,YACH;AAAA,UAAA,CACD;AAED,yBAAe,QAAQ,CAAC,kBAAkB;AACxC,wBAAY,aAAa,EAAE,MAAM,QAAQ,CAAC,SAAS;AACjD,0BAAY,aAAa,EAAE;AAAA,gBACzB;AAAA,gBACA;AAAA,cAAA;AAAA,YAEJ,CAAC;AAAA,UACH,CAAC;AAAA,QACH;AAAA,MAAA;AAAA,IACF,CACD;AAED,QAAI,OAAO;AACT,0BAAoB,KAAK,SAAS;AAAA,IACpC;AAEA,WAAO,gBAAgB,KAAK;AAAA,MAC1B,YAAY;AAAA,MACZ,gBAAgB,KAAK;AAAA,MACrB,UAAU,KAAK;AAAA,IAAA,CAChB;AAAA,EACH;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const transformFuncs: readonly ["createServerFn", "createMiddleware", "
|
|
1
|
+
export declare const transformFuncs: readonly ["createServerFn", "createMiddleware", "createServerOnlyFn", "createClientOnlyFn", "createIsomorphicFn", "createServerRoute", "createServerFileRoute", "createServerRootRoute"];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sources":["../../../src/start-compiler-plugin/constants.ts"],"sourcesContent":["export const transformFuncs = [\n 'createServerFn',\n 'createMiddleware',\n '
|
|
1
|
+
{"version":3,"file":"constants.js","sources":["../../../src/start-compiler-plugin/constants.ts"],"sourcesContent":["export const transformFuncs = [\n 'createServerFn',\n 'createMiddleware',\n 'createServerOnlyFn',\n 'createClientOnlyFn',\n 'createIsomorphicFn',\n 'createServerRoute',\n 'createServerFileRoute',\n 'createServerRootRoute',\n] as const\n"],"names":[],"mappings":"AAAO,MAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CompileOptions } from './compilers.js';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
import type * as babel from '@babel/core';
|
|
4
|
+
export declare const handleCreateServerOnlyFnCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
|
|
5
|
+
export declare const handleCreateClientOnlyFnCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
function capitalize(str) {
|
|
3
|
+
if (!str) return "";
|
|
4
|
+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
function buildEnvOnlyCallExpressionHandler(env) {
|
|
7
|
+
return function envOnlyCallExpressionHandler(path, opts) {
|
|
8
|
+
const isEnvMatch = env === "client" ? opts.env === "client" : opts.env === "server";
|
|
9
|
+
if (isEnvMatch) {
|
|
10
|
+
const innerInputExpression = path.node.arguments[0];
|
|
11
|
+
if (!t.isExpression(innerInputExpression)) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`${env}Only() functions must be called with a function!`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
path.replaceWith(innerInputExpression);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
path.replaceWith(
|
|
20
|
+
t.arrowFunctionExpression(
|
|
21
|
+
[],
|
|
22
|
+
t.blockStatement([
|
|
23
|
+
t.throwStatement(
|
|
24
|
+
t.newExpression(t.identifier("Error"), [
|
|
25
|
+
t.stringLiteral(
|
|
26
|
+
`create${capitalize(env)}OnlyFn() functions can only be called on the ${env}!`
|
|
27
|
+
)
|
|
28
|
+
])
|
|
29
|
+
)
|
|
30
|
+
])
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const handleCreateServerOnlyFnCallExpression = buildEnvOnlyCallExpressionHandler("server");
|
|
36
|
+
const handleCreateClientOnlyFnCallExpression = buildEnvOnlyCallExpressionHandler("client");
|
|
37
|
+
export {
|
|
38
|
+
handleCreateClientOnlyFnCallExpression,
|
|
39
|
+
handleCreateServerOnlyFnCallExpression
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=envOnly.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envOnly.js","sources":["../../../src/start-compiler-plugin/envOnly.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nfunction capitalize(str: string) {\n if (!str) return ''\n return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()\n}\n\nfunction buildEnvOnlyCallExpressionHandler(env: 'client' | 'server') {\n return function envOnlyCallExpressionHandler(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n // if (debug)\n // console.info(`Handling ${env}Only call expression:`, path.toString())\n\n const isEnvMatch =\n env === 'client' ? opts.env === 'client' : opts.env === 'server'\n\n if (isEnvMatch) {\n // extract the inner function from the call expression\n const innerInputExpression = path.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `${env}Only() functions must be called with a function!`,\n )\n }\n\n path.replaceWith(innerInputExpression)\n return\n }\n\n // If we're on the wrong environment, replace the call expression\n // with a function that always throws an error.\n path.replaceWith(\n t.arrowFunctionExpression(\n [],\n t.blockStatement([\n t.throwStatement(\n t.newExpression(t.identifier('Error'), [\n t.stringLiteral(\n `create${capitalize(env)}OnlyFn() functions can only be called on the ${env}!`,\n ),\n ]),\n ),\n ]),\n ),\n )\n }\n}\n\nexport const handleCreateServerOnlyFnCallExpression =\n buildEnvOnlyCallExpressionHandler('server')\nexport const handleCreateClientOnlyFnCallExpression =\n buildEnvOnlyCallExpressionHandler('client')\n"],"names":[],"mappings":";AAKA,SAAS,WAAW,KAAa;AAC/B,MAAI,CAAC,IAAK,QAAO;AACjB,SAAO,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC,EAAE,YAAA;AACpD;AAEA,SAAS,kCAAkC,KAA0B;AACnE,SAAO,SAAS,6BACd,MACA,MACA;AAIA,UAAM,aACJ,QAAQ,WAAW,KAAK,QAAQ,WAAW,KAAK,QAAQ;AAE1D,QAAI,YAAY;AAEd,YAAM,uBAAuB,KAAK,KAAK,UAAU,CAAC;AAElD,UAAI,CAAC,EAAE,aAAa,oBAAoB,GAAG;AACzC,cAAM,IAAI;AAAA,UACR,GAAG,GAAG;AAAA,QAAA;AAAA,MAEV;AAEA,WAAK,YAAY,oBAAoB;AACrC;AAAA,IACF;AAIA,SAAK;AAAA,MACH,EAAE;AAAA,QACA,CAAA;AAAA,QACA,EAAE,eAAe;AAAA,UACf,EAAE;AAAA,YACA,EAAE,cAAc,EAAE,WAAW,OAAO,GAAG;AAAA,cACrC,EAAE;AAAA,gBACA,SAAS,WAAW,GAAG,CAAC,gDAAgD,GAAG;AAAA,cAAA;AAAA,YAC7E,CACD;AAAA,UAAA;AAAA,QACH,CACD;AAAA,MAAA;AAAA,IACH;AAAA,EAEJ;AACF;AAEO,MAAM,yCACX,kCAAkC,QAAQ;AACrC,MAAM,yCACX,kCAAkC,QAAQ;"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import { getRootCallExpression } from "./utils.js";
|
|
3
|
+
function handleCreateIsomorphicFnCallExpression(path, opts) {
|
|
4
|
+
const rootCallExpression = getRootCallExpression(path);
|
|
5
|
+
const callExpressionPaths = {
|
|
6
|
+
client: null,
|
|
7
|
+
server: null
|
|
8
|
+
};
|
|
9
|
+
const validMethods = Object.keys(callExpressionPaths);
|
|
10
|
+
rootCallExpression.traverse({
|
|
11
|
+
MemberExpression(memberExpressionPath) {
|
|
12
|
+
if (t.isIdentifier(memberExpressionPath.node.property)) {
|
|
13
|
+
const name = memberExpressionPath.node.property.name;
|
|
14
|
+
if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
|
|
15
|
+
callExpressionPaths[name] = memberExpressionPath.parentPath;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
if (validMethods.every(
|
|
21
|
+
(method) => !callExpressionPaths[method]
|
|
22
|
+
)) {
|
|
23
|
+
const variableId = rootCallExpression.parentPath.isVariableDeclarator() ? rootCallExpression.parentPath.node.id : null;
|
|
24
|
+
console.warn(
|
|
25
|
+
"createIsomorphicFn called without a client or server implementation!",
|
|
26
|
+
"This will result in a no-op function.",
|
|
27
|
+
"Variable name:",
|
|
28
|
+
t.isIdentifier(variableId) ? variableId.name : "unknown"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
const envCallExpression = callExpressionPaths[opts.env];
|
|
32
|
+
if (!envCallExpression) {
|
|
33
|
+
rootCallExpression.replaceWith(
|
|
34
|
+
t.arrowFunctionExpression([], t.blockStatement([]))
|
|
35
|
+
);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const innerInputExpression = envCallExpression.node.arguments[0];
|
|
39
|
+
if (!t.isExpression(innerInputExpression)) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`createIsomorphicFn().${opts.env}(func) must be called with a function!`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
rootCallExpression.replaceWith(innerInputExpression);
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
handleCreateIsomorphicFnCallExpression
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=isomorphicFn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isomorphicFn.js","sources":["../../../src/start-compiler-plugin/isomorphicFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { getRootCallExpression } from './utils'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nexport function handleCreateIsomorphicFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createIsomorphicFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\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 (\n validMethods.every(\n (method) =>\n !callExpressionPaths[method as keyof typeof callExpressionPaths],\n )\n ) {\n const variableId = rootCallExpression.parentPath.isVariableDeclarator()\n ? rootCallExpression.parentPath.node.id\n : null\n console.warn(\n 'createIsomorphicFn called without a client or server implementation!',\n 'This will result in a no-op function.',\n 'Variable name:',\n t.isIdentifier(variableId) ? variableId.name : 'unknown',\n )\n }\n\n const envCallExpression = callExpressionPaths[opts.env]\n\n if (!envCallExpression) {\n // if we don't have an implementation for this environment, default to a no-op\n rootCallExpression.replaceWith(\n t.arrowFunctionExpression([], t.blockStatement([])),\n )\n return\n }\n\n const innerInputExpression = envCallExpression.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `createIsomorphicFn().${opts.env}(func) must be called with a function!`,\n )\n }\n\n rootCallExpression.replaceWith(innerInputExpression)\n}\n"],"names":[],"mappings":";;AAMO,SAAS,uCACd,MACA,MACA;AACA,QAAM,qBAAqB,sBAAsB,IAAI;AAQrD,QAAM,sBAAsB;AAAA,IAC1B,QAAQ;AAAA,IACR,QAAQ;AAAA,EAAA;AAGV,QAAM,eAAe,OAAO,KAAK,mBAAmB;AAEpD,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SACpC;AAEH,YACE,aAAa,SAAS,IAAI,KAC1B,qBAAqB,WAAW,oBAChC;AACA,8BAAoB,IAAI,IAAI,qBAAqB;AAAA,QACnD;AAAA,MACF;AAAA,IACF;AAAA,EAAA,CACD;AAED,MACE,aAAa;AAAA,IACX,CAAC,WACC,CAAC,oBAAoB,MAA0C;AAAA,EAAA,GAEnE;AACA,UAAM,aAAa,mBAAmB,WAAW,qBAAA,IAC7C,mBAAmB,WAAW,KAAK,KACnC;AACJ,YAAQ;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA,EAAE,aAAa,UAAU,IAAI,WAAW,OAAO;AAAA,IAAA;AAAA,EAEnD;AAEA,QAAM,oBAAoB,oBAAoB,KAAK,GAAG;AAEtD,MAAI,CAAC,mBAAmB;AAEtB,uBAAmB;AAAA,MACjB,EAAE,wBAAwB,CAAA,GAAI,EAAE,eAAe,CAAA,CAAE,CAAC;AAAA,IAAA;AAEpD;AAAA,EACF;AAEA,QAAM,uBAAuB,kBAAkB,KAAK,UAAU,CAAC;AAE/D,MAAI,CAAC,EAAE,aAAa,oBAAoB,GAAG;AACzC,UAAM,IAAI;AAAA,MACR,wBAAwB,KAAK,GAAG;AAAA,IAAA;AAAA,EAEpC;AAEA,qBAAmB,YAAY,oBAAoB;AACrD;"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import { getRootCallExpression } from "./utils.js";
|
|
3
|
+
function handleCreateMiddlewareCallExpression(path, opts) {
|
|
4
|
+
const rootCallExpression = getRootCallExpression(path);
|
|
5
|
+
const callExpressionPaths = {
|
|
6
|
+
middleware: null,
|
|
7
|
+
validator: null,
|
|
8
|
+
client: null,
|
|
9
|
+
server: null
|
|
10
|
+
};
|
|
11
|
+
const validMethods = Object.keys(callExpressionPaths);
|
|
12
|
+
rootCallExpression.traverse({
|
|
13
|
+
MemberExpression(memberExpressionPath) {
|
|
14
|
+
if (t.isIdentifier(memberExpressionPath.node.property)) {
|
|
15
|
+
const name = memberExpressionPath.node.property.name;
|
|
16
|
+
if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
|
|
17
|
+
callExpressionPaths[name] = memberExpressionPath.parentPath;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
if (callExpressionPaths.validator) {
|
|
23
|
+
const innerInputExpression = callExpressionPaths.validator.node.arguments[0];
|
|
24
|
+
if (!innerInputExpression) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"createMiddleware().validator() must be called with a validator!"
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
if (opts.env === "client") {
|
|
30
|
+
if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
|
|
31
|
+
callExpressionPaths.validator.replaceWith(
|
|
32
|
+
callExpressionPaths.validator.node.callee.object
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const serverFnPath = callExpressionPaths.server?.get(
|
|
38
|
+
"arguments.0"
|
|
39
|
+
);
|
|
40
|
+
if (callExpressionPaths.server && serverFnPath.node && opts.env === "client") {
|
|
41
|
+
if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {
|
|
42
|
+
callExpressionPaths.server.replaceWith(
|
|
43
|
+
callExpressionPaths.server.node.callee.object
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
handleCreateMiddlewareCallExpression
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sources":["../../../src/start-compiler-plugin/middleware.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { getRootCallExpression } from './utils'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nexport function handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\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 'createMiddleware().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n"],"names":[],"mappings":";;AAMO,SAAS,qCACd,MACA,MACA;AACA,QAAM,qBAAqB,sBAAsB,IAAI;AAQrD,QAAM,sBAAsB;AAAA,IAC1B,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,QAAQ;AAAA,IACR,QAAQ;AAAA,EAAA;AAGV,QAAM,eAAe,OAAO,KAAK,mBAAmB;AAEpD,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SACpC;AAEH,YACE,aAAa,SAAS,IAAI,KAC1B,qBAAqB,WAAW,oBAChC;AACA,8BAAoB,IAAI,IAAI,qBAAqB;AAAA,QACnD;AAAA,MACF;AAAA,IACF;AAAA,EAAA,CACD;AAED,MAAI,oBAAoB,WAAW;AACjC,UAAM,uBAAuB,oBAAoB,UAAU,KAAK,UAAU,CAAC;AAE3E,QAAI,CAAC,sBAAsB;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAGA,QAAI,KAAK,QAAQ,UAAU;AACzB,UAAI,EAAE,mBAAmB,oBAAoB,UAAU,KAAK,MAAM,GAAG;AACnE,4BAAoB,UAAU;AAAA,UAC5B,oBAAoB,UAAU,KAAK,OAAO;AAAA,QAAA;AAAA,MAE9C;AAAA,IACF;AAAA,EACF;AAEA,QAAM,eAAe,oBAAoB,QAAQ;AAAA,IAC/C;AAAA,EAAA;AAGF,MACE,oBAAoB,UACpB,aAAa,QACb,KAAK,QAAQ,UACb;AAEA,QAAI,EAAE,mBAAmB,oBAAoB,OAAO,KAAK,MAAM,GAAG;AAChE,0BAAoB,OAAO;AAAA,QACzB,oBAAoB,OAAO,KAAK,OAAO;AAAA,MAAA;AAAA,IAE3C;AAAA,EACF;AACF;"}
|
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
import { pathToFileURL, fileURLToPath } from "node:url";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
2
3
|
import { logDiff } from "@tanstack/router-utils";
|
|
3
4
|
import { VIRTUAL_MODULES } from "@tanstack/start-server-core";
|
|
5
|
+
import { normalizePath } from "vite";
|
|
6
|
+
import path from "pathe";
|
|
7
|
+
import { VITE_ENVIRONMENT_NAMES } from "../constants.js";
|
|
4
8
|
import { compileStartOutputFactory } from "./compilers.js";
|
|
5
9
|
import { transformFuncs } from "./constants.js";
|
|
6
10
|
const debug = process.env.TSR_VITE_DEBUG && ["true", "start-plugin"].includes(process.env.TSR_VITE_DEBUG);
|
|
7
11
|
const tokenRegex = new RegExp(transformFuncs.join("|"));
|
|
12
|
+
const require2 = createRequire(import.meta.url);
|
|
13
|
+
function resolveRuntimeFiles(opts) {
|
|
14
|
+
const pkgRoot = resolvePackage(opts.package);
|
|
15
|
+
const basePath = path.join(pkgRoot, "dist", "esm");
|
|
16
|
+
return opts.files.map((file) => normalizePath(path.join(basePath, file)));
|
|
17
|
+
}
|
|
18
|
+
function resolvePackage(packageName) {
|
|
19
|
+
const pkgRoot = path.dirname(require2.resolve(packageName + "/package.json"));
|
|
20
|
+
return pkgRoot;
|
|
21
|
+
}
|
|
8
22
|
function startCompilerPlugin(framework, inputOpts) {
|
|
9
23
|
const opts = {
|
|
10
24
|
client: {
|
|
11
|
-
envName:
|
|
25
|
+
envName: VITE_ENVIRONMENT_NAMES.client,
|
|
12
26
|
...inputOpts?.client
|
|
13
27
|
},
|
|
14
28
|
server: {
|
|
15
|
-
envName:
|
|
29
|
+
envName: VITE_ENVIRONMENT_NAMES.server,
|
|
16
30
|
...inputOpts?.server
|
|
17
31
|
}
|
|
18
32
|
};
|
|
33
|
+
const compileStartOutput = compileStartOutputFactory(framework);
|
|
19
34
|
return {
|
|
20
35
|
name: "tanstack-start-core:compiler",
|
|
21
36
|
enforce: "pre",
|
|
@@ -26,7 +41,36 @@ function startCompilerPlugin(framework, inputOpts) {
|
|
|
26
41
|
filter: {
|
|
27
42
|
code: tokenRegex,
|
|
28
43
|
id: {
|
|
29
|
-
exclude:
|
|
44
|
+
exclude: [
|
|
45
|
+
VIRTUAL_MODULES.serverFnManifest,
|
|
46
|
+
// N.B. the following files either just re-export or provide the runtime implementation of those functions
|
|
47
|
+
// we do not want to include them in the transformation
|
|
48
|
+
// however, those packages (especially start-client-core ATM) also USE these functions
|
|
49
|
+
// (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed
|
|
50
|
+
...resolveRuntimeFiles({
|
|
51
|
+
package: "@tanstack/start-client-core",
|
|
52
|
+
files: [
|
|
53
|
+
"index.js",
|
|
54
|
+
"createIsomorphicFn.js",
|
|
55
|
+
"envOnly.js",
|
|
56
|
+
"createServerFn.js",
|
|
57
|
+
"createMiddleware.js",
|
|
58
|
+
"serverFnFetcher.js"
|
|
59
|
+
]
|
|
60
|
+
}),
|
|
61
|
+
...resolveRuntimeFiles({
|
|
62
|
+
package: "@tanstack/start-server-core",
|
|
63
|
+
files: [
|
|
64
|
+
"index.js",
|
|
65
|
+
"server-functions-handler.js",
|
|
66
|
+
"serverRoute.js"
|
|
67
|
+
]
|
|
68
|
+
}),
|
|
69
|
+
...resolveRuntimeFiles({
|
|
70
|
+
package: `@tanstack/${framework}-start-client`,
|
|
71
|
+
files: ["index.js"]
|
|
72
|
+
})
|
|
73
|
+
]
|
|
30
74
|
}
|
|
31
75
|
},
|
|
32
76
|
handler(code, id) {
|
|
@@ -35,35 +79,24 @@ function startCompilerPlugin(framework, inputOpts) {
|
|
|
35
79
|
`Environment ${this.environment.name} not configured`
|
|
36
80
|
);
|
|
37
81
|
})();
|
|
38
|
-
|
|
82
|
+
const url = pathToFileURL(id);
|
|
83
|
+
url.searchParams.delete("v");
|
|
84
|
+
id = fileURLToPath(url).replace(/\\/g, "/");
|
|
85
|
+
if (debug) console.info(`${env} Compiling Start: `, id);
|
|
86
|
+
const compiled = compileStartOutput({
|
|
39
87
|
code,
|
|
40
|
-
id,
|
|
41
|
-
env
|
|
42
|
-
framework
|
|
88
|
+
filename: id,
|
|
89
|
+
env
|
|
43
90
|
});
|
|
91
|
+
if (debug) {
|
|
92
|
+
logDiff(code, compiled.code);
|
|
93
|
+
console.log("Output:\n", compiled.code + "\n\n");
|
|
94
|
+
}
|
|
95
|
+
return compiled;
|
|
44
96
|
}
|
|
45
97
|
}
|
|
46
98
|
};
|
|
47
99
|
}
|
|
48
|
-
function transformCode(opts) {
|
|
49
|
-
const { code, env, framework } = opts;
|
|
50
|
-
let { id } = opts;
|
|
51
|
-
const url = pathToFileURL(id);
|
|
52
|
-
url.searchParams.delete("v");
|
|
53
|
-
id = fileURLToPath(url).replace(/\\/g, "/");
|
|
54
|
-
if (debug) console.info(`${env} Compiling Start: `, id);
|
|
55
|
-
const compileStartOutput = compileStartOutputFactory(framework);
|
|
56
|
-
const compiled = compileStartOutput({
|
|
57
|
-
code,
|
|
58
|
-
filename: id,
|
|
59
|
-
env
|
|
60
|
-
});
|
|
61
|
-
if (debug) {
|
|
62
|
-
logDiff(code, compiled.code);
|
|
63
|
-
console.log("Output:\n", compiled.code + "\n\n");
|
|
64
|
-
}
|
|
65
|
-
return compiled;
|
|
66
|
-
}
|
|
67
100
|
export {
|
|
68
101
|
startCompilerPlugin
|
|
69
102
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../../../src/start-compiler-plugin/plugin.ts"],"sourcesContent":["import { fileURLToPath, pathToFileURL } from 'node:url'\nimport { logDiff } from '@tanstack/router-utils'\n\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { compileStartOutputFactory } from './compilers'\nimport { transformFuncs } from './constants'\nimport type { Plugin } from 'vite'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nconst debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin'].includes(process.env.TSR_VITE_DEBUG)\n\nexport type TanStackStartViteOptions = {\n globalMiddlewareEntry: string\n}\n\nconst tokenRegex = new RegExp(transformFuncs.join('|'))\n\nexport function startCompilerPlugin(\n framework: CompileStartFrameworkOptions,\n inputOpts?: {\n client?: {\n envName?: string\n }\n server?: {\n envName?: string\n }\n },\n): Plugin {\n const opts = {\n client: {\n envName:
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../../../src/start-compiler-plugin/plugin.ts"],"sourcesContent":["import { fileURLToPath, pathToFileURL } from 'node:url'\nimport { createRequire } from 'node:module'\nimport { logDiff } from '@tanstack/router-utils'\n\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { normalizePath } from 'vite'\nimport path from 'pathe'\nimport { VITE_ENVIRONMENT_NAMES } from '../constants'\nimport { compileStartOutputFactory } from './compilers'\nimport { transformFuncs } from './constants'\nimport type { Plugin } from 'vite'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nconst debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin'].includes(process.env.TSR_VITE_DEBUG)\n\nexport type TanStackStartViteOptions = {\n globalMiddlewareEntry: string\n}\n\nconst tokenRegex = new RegExp(transformFuncs.join('|'))\n\nconst require = createRequire(import.meta.url)\n\nfunction resolveRuntimeFiles(opts: { package: string; files: Array<string> }) {\n const pkgRoot = resolvePackage(opts.package)\n const basePath = path.join(pkgRoot, 'dist', 'esm')\n\n return opts.files.map((file) => normalizePath(path.join(basePath, file)))\n}\n\nfunction resolvePackage(packageName: string): string {\n const pkgRoot = path.dirname(require.resolve(packageName + '/package.json'))\n return pkgRoot\n}\n\nexport function startCompilerPlugin(\n framework: CompileStartFrameworkOptions,\n inputOpts?: {\n client?: {\n envName?: string\n }\n server?: {\n envName?: string\n }\n },\n): Plugin {\n const opts = {\n client: {\n envName: VITE_ENVIRONMENT_NAMES.client,\n ...inputOpts?.client,\n },\n server: {\n envName: VITE_ENVIRONMENT_NAMES.server,\n ...inputOpts?.server,\n },\n }\n\n const compileStartOutput = compileStartOutputFactory(framework)\n\n return {\n name: 'tanstack-start-core:compiler',\n enforce: 'pre',\n applyToEnvironment(env) {\n return [opts.client.envName, opts.server.envName].includes(env.name)\n },\n transform: {\n filter: {\n code: tokenRegex,\n id: {\n exclude: [\n VIRTUAL_MODULES.serverFnManifest,\n // N.B. the following files either just re-export or provide the runtime implementation of those functions\n // we do not want to include them in the transformation\n // however, those packages (especially start-client-core ATM) also USE these functions\n // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed\n ...resolveRuntimeFiles({\n package: '@tanstack/start-client-core',\n files: [\n 'index.js',\n 'createIsomorphicFn.js',\n 'envOnly.js',\n 'createServerFn.js',\n 'createMiddleware.js',\n 'serverFnFetcher.js',\n ],\n }),\n ...resolveRuntimeFiles({\n package: '@tanstack/start-server-core',\n files: [\n 'index.js',\n 'server-functions-handler.js',\n 'serverRoute.js',\n ],\n }),\n ...resolveRuntimeFiles({\n package: `@tanstack/${framework}-start-client`,\n files: ['index.js'],\n }),\n ],\n },\n },\n handler(code, id) {\n const env =\n this.environment.name === opts.client.envName\n ? 'client'\n : this.environment.name === opts.server.envName\n ? 'server'\n : (() => {\n throw new Error(\n `Environment ${this.environment.name} not configured`,\n )\n })()\n\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (debug) console.info(`${env} Compiling Start: `, id)\n\n const compiled = compileStartOutput({\n code,\n filename: id,\n env,\n })\n\n if (debug) {\n logDiff(code, compiled.code)\n console.log('Output:\\n', compiled.code + '\\n\\n')\n }\n\n return compiled\n },\n },\n }\n}\n"],"names":["require"],"mappings":";;;;;;;;;AAaA,MAAM,QACJ,QAAQ,IAAI,kBACZ,CAAC,QAAQ,cAAc,EAAE,SAAS,QAAQ,IAAI,cAAc;AAM9D,MAAM,aAAa,IAAI,OAAO,eAAe,KAAK,GAAG,CAAC;AAEtD,MAAMA,WAAU,cAAc,YAAY,GAAG;AAE7C,SAAS,oBAAoB,MAAiD;AAC5E,QAAM,UAAU,eAAe,KAAK,OAAO;AAC3C,QAAM,WAAW,KAAK,KAAK,SAAS,QAAQ,KAAK;AAEjD,SAAO,KAAK,MAAM,IAAI,CAAC,SAAS,cAAc,KAAK,KAAK,UAAU,IAAI,CAAC,CAAC;AAC1E;AAEA,SAAS,eAAe,aAA6B;AACnD,QAAM,UAAU,KAAK,QAAQA,SAAQ,QAAQ,cAAc,eAAe,CAAC;AAC3E,SAAO;AACT;AAEO,SAAS,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,GAAG;AAC3B,aAAK,cAAc,GAAG,EAAE,QAAQ,OAAO,GAAG;AAE1C,YAAI,MAAO,SAAQ,KAAK,GAAG,GAAG,sBAAsB,EAAE;AAEtD,cAAM,WAAW,mBAAmB;AAAA,UAClC;AAAA,UACA,UAAU;AAAA,UACV;AAAA,QAAA,CACD;AAED,YAAI,OAAO;AACT,kBAAQ,MAAM,SAAS,IAAI;AAC3B,kBAAQ,IAAI,aAAa,SAAS,OAAO,MAAM;AAAA,QACjD;AAEA,eAAO;AAAA,MACT;AAAA,IAAA;AAAA,EACF;AAEJ;"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CompileOptions, CompileStartFrameworkOptions } from './compilers.js';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
import type * as babel from '@babel/core';
|
|
4
|
+
export declare function handleCreateServerFileRouteCallExpressionFactory(framework: CompileStartFrameworkOptions, method: 'createServerFileRoute' | 'createServerRoute' | 'createServerRootRoute'): (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
function handleCreateServerFileRouteCallExpressionFactory(framework, method) {
|
|
3
|
+
return function handleCreateServerFileRouteCallExpression(path, opts) {
|
|
4
|
+
const PACKAGES = { start: `@tanstack/${framework}-start/server` };
|
|
5
|
+
let highestParent = path;
|
|
6
|
+
while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {
|
|
7
|
+
highestParent = highestParent.parentPath;
|
|
8
|
+
}
|
|
9
|
+
const programPath = highestParent.parentPath;
|
|
10
|
+
if (opts.env === "client") {
|
|
11
|
+
highestParent.remove();
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
let isCreateServerFileRouteImported = false;
|
|
15
|
+
programPath.traverse({
|
|
16
|
+
ImportDeclaration(importPath) {
|
|
17
|
+
const importSource = importPath.node.source.value;
|
|
18
|
+
if (importSource === PACKAGES.start) {
|
|
19
|
+
const specifiers = importPath.node.specifiers;
|
|
20
|
+
isCreateServerFileRouteImported ||= specifiers.some((specifier) => {
|
|
21
|
+
return t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported) && specifier.imported.name === method;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
if (!isCreateServerFileRouteImported) {
|
|
27
|
+
const importDeclaration = t.importDeclaration(
|
|
28
|
+
[t.importSpecifier(t.identifier(method), t.identifier(method))],
|
|
29
|
+
t.stringLiteral(PACKAGES.start)
|
|
30
|
+
);
|
|
31
|
+
programPath.node.body.unshift(importDeclaration);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
handleCreateServerFileRouteCallExpressionFactory
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=serverFileRoute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverFileRoute.js","sources":["../../../src/start-compiler-plugin/serverFileRoute.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions, CompileStartFrameworkOptions } from './compilers'\n\nexport function handleCreateServerFileRouteCallExpressionFactory(\n framework: CompileStartFrameworkOptions,\n method:\n | 'createServerFileRoute'\n | 'createServerRoute'\n | 'createServerRootRoute',\n) {\n return function handleCreateServerFileRouteCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n const PACKAGES = { start: `@tanstack/${framework}-start/server` }\n\n let highestParent: babel.NodePath<any> = path\n\n while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {\n highestParent = highestParent.parentPath\n }\n\n const programPath = highestParent.parentPath as babel.NodePath<t.Program>\n\n // If we're on the client, remove the entire variable\n if (opts.env === 'client') {\n highestParent.remove()\n return\n }\n\n let isCreateServerFileRouteImported = false as boolean\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const importSource = importPath.node.source.value\n if (importSource === PACKAGES.start) {\n const specifiers = importPath.node.specifiers\n isCreateServerFileRouteImported ||= specifiers.some((specifier) => {\n return (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === method\n )\n })\n }\n },\n })\n\n if (!isCreateServerFileRouteImported) {\n const importDeclaration = t.importDeclaration(\n [t.importSpecifier(t.identifier(method), t.identifier(method))],\n t.stringLiteral(PACKAGES.start),\n )\n programPath.node.body.unshift(importDeclaration)\n }\n }\n}\n"],"names":[],"mappings":";AAKO,SAAS,iDACd,WACA,QAIA;AACA,SAAO,SAAS,0CACd,MACA,MACA;AACA,UAAM,WAAW,EAAE,OAAO,aAAa,SAAS,gBAAA;AAEhD,QAAI,gBAAqC;AAEzC,WAAO,cAAc,cAAc,CAAC,cAAc,WAAW,aAAa;AACxE,sBAAgB,cAAc;AAAA,IAChC;AAEA,UAAM,cAAc,cAAc;AAGlC,QAAI,KAAK,QAAQ,UAAU;AACzB,oBAAc,OAAA;AACd;AAAA,IACF;AAEA,QAAI,kCAAkC;AAEtC,gBAAY,SAAS;AAAA,MACnB,kBAAkB,YAAY;AAC5B,cAAM,eAAe,WAAW,KAAK,OAAO;AAC5C,YAAI,iBAAiB,SAAS,OAAO;AACnC,gBAAM,aAAa,WAAW,KAAK;AACnC,8CAAoC,WAAW,KAAK,CAAC,cAAc;AACjE,mBACE,EAAE,kBAAkB,SAAS,KAC7B,EAAE,aAAa,UAAU,QAAQ,KACjC,UAAU,SAAS,SAAS;AAAA,UAEhC,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IAAA,CACD;AAED,QAAI,CAAC,iCAAiC;AACpC,YAAM,oBAAoB,EAAE;AAAA,QAC1B,CAAC,EAAE,gBAAgB,EAAE,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,CAAC,CAAC;AAAA,QAC9D,EAAE,cAAc,SAAS,KAAK;AAAA,MAAA;AAEhC,kBAAY,KAAK,KAAK,QAAQ,iBAAiB;AAAA,IACjD;AAAA,EACF;AACF;"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import { getRootCallExpression, codeFrameError } from "./utils.js";
|
|
3
|
+
function handleCreateServerFnCallExpression(path, opts) {
|
|
4
|
+
const calledOptions = path.node.arguments[0] ? path.get("arguments.0") : null;
|
|
5
|
+
const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {
|
|
6
|
+
return t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "validateClient" && t.isBooleanLiteral(prop.value) && prop.value.value === true;
|
|
7
|
+
});
|
|
8
|
+
const callExpressionPaths = {
|
|
9
|
+
middleware: null,
|
|
10
|
+
validator: null,
|
|
11
|
+
handler: null
|
|
12
|
+
};
|
|
13
|
+
const validMethods = Object.keys(callExpressionPaths);
|
|
14
|
+
const rootCallExpression = getRootCallExpression(path);
|
|
15
|
+
if (!rootCallExpression.parentPath.isVariableDeclarator()) {
|
|
16
|
+
throw new Error("createServerFn must be assigned to a variable!");
|
|
17
|
+
}
|
|
18
|
+
const variableDeclarator = rootCallExpression.parentPath.node;
|
|
19
|
+
const existingVariableName = variableDeclarator.id.name;
|
|
20
|
+
rootCallExpression.traverse({
|
|
21
|
+
MemberExpression(memberExpressionPath) {
|
|
22
|
+
if (t.isIdentifier(memberExpressionPath.node.property)) {
|
|
23
|
+
const name = memberExpressionPath.node.property.name;
|
|
24
|
+
if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
|
|
25
|
+
callExpressionPaths[name] = memberExpressionPath.parentPath;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
if (callExpressionPaths.validator) {
|
|
31
|
+
const innerInputExpression = callExpressionPaths.validator.node.arguments[0];
|
|
32
|
+
if (!innerInputExpression) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
"createServerFn().validator() must be called with a validator!"
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
if (opts.env === "client" && !shouldValidateClient && t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
|
|
38
|
+
callExpressionPaths.validator.replaceWith(
|
|
39
|
+
callExpressionPaths.validator.node.callee.object
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const handlerFnPath = callExpressionPaths.handler?.get(
|
|
44
|
+
"arguments.0"
|
|
45
|
+
);
|
|
46
|
+
if (!callExpressionPaths.handler || !handlerFnPath.node) {
|
|
47
|
+
throw codeFrameError(
|
|
48
|
+
opts.code,
|
|
49
|
+
path.node.callee.loc,
|
|
50
|
+
`createServerFn must be called with a "handler" property!`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const handlerFn = handlerFnPath.node;
|
|
54
|
+
if (t.isIdentifier(handlerFn)) {
|
|
55
|
+
if (opts.env === "client") {
|
|
56
|
+
const binding = handlerFnPath.scope.getBinding(handlerFn.name);
|
|
57
|
+
if (binding) {
|
|
58
|
+
binding.path.remove();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
handlerFnPath.replaceWith(
|
|
63
|
+
t.arrowFunctionExpression(
|
|
64
|
+
[t.identifier("opts"), t.identifier("signal")],
|
|
65
|
+
t.blockStatement(
|
|
66
|
+
// Everything in here is server-only, since the client
|
|
67
|
+
// will strip out anything in the 'use server' directive.
|
|
68
|
+
[
|
|
69
|
+
t.returnStatement(
|
|
70
|
+
t.callExpression(
|
|
71
|
+
t.identifier(`${existingVariableName}.__executeServer`),
|
|
72
|
+
[t.identifier("opts"), t.identifier("signal")]
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
],
|
|
76
|
+
[t.directive(t.directiveLiteral("use server"))]
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
);
|
|
80
|
+
if (opts.env === "server") {
|
|
81
|
+
callExpressionPaths.handler.node.arguments.push(handlerFn);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
handleCreateServerFnCallExpression
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=serverFn.js.map
|