From c7cfa35bbdc2adbb96269def1e66da50a6fef56a Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Thu, 30 Oct 2025 19:01:52 +0100 Subject: [PATCH] fix: only run middleware compiler in client env --- .../src/start-compiler-plugin/compilers.ts | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/start-plugin-core/src/start-compiler-plugin/compilers.ts b/packages/start-plugin-core/src/start-compiler-plugin/compilers.ts index f5d244e5c1a..d300fe48c97 100644 --- a/packages/start-plugin-core/src/start-compiler-plugin/compilers.ts +++ b/packages/start-plugin-core/src/start-compiler-plugin/compilers.ts @@ -23,7 +23,7 @@ export function compileStartOutputFactory( framework: CompileStartFrameworkOptions, ) { return function compileStartOutput(opts: CompileOptions): GeneratorResult { - const identifiers: Identifiers = { + const identifiers: Partial = { createServerOnlyFn: { name: 'createServerOnlyFn', handleCallExpression: handleCreateServerOnlyFnCallExpression, @@ -39,11 +39,16 @@ export function compileStartOutputFactory( handleCallExpression: handleCreateIsomorphicFnCallExpression, paths: [], }, - createMiddleware: { + } + + // createMiddleware only performs modifications in the client environment + // so we can avoid executing this on the server + if (opts.env === 'client') { + identifiers.createMiddleware = { name: 'createMiddleware', handleCallExpression: handleCreateMiddleware, paths: [], - }, + } } const ast = parseAst(opts) @@ -65,7 +70,9 @@ export function compileStartOutputFactory( path.node.specifiers.forEach((specifier) => { transformFuncs.forEach((identifierKey) => { const identifier = identifiers[identifierKey] - + if (!identifier) { + return + } if ( specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' @@ -84,11 +91,15 @@ export function compileStartOutputFactory( }, CallExpression: (path) => { transformFuncs.forEach((identifierKey) => { + const identifier = identifiers[identifierKey] + if (!identifier) { + return + } // Check to see if the call expression is a call to the // identifiers[identifierKey].name if ( t.isIdentifier(path.node.callee) && - path.node.callee.name === identifiers[identifierKey].name + path.node.callee.name === identifier.name ) { // The identifier could be a call to the original function // in the source code. If this is case, we need to ignore it. @@ -96,13 +107,13 @@ export function compileStartOutputFactory( // if it is, then we can ignore it. if ( - path.scope.getBinding(identifiers[identifierKey].name)?.path - .node.type === 'FunctionDeclaration' + path.scope.getBinding(identifier.name)?.path.node.type === + 'FunctionDeclaration' ) { return } - return identifiers[identifierKey].paths.push(path) + return identifier.paths.push(path) } // handle namespace imports like "import * as TanStackStart from '@tanstack/react-start';" @@ -117,8 +128,8 @@ export function compileStartOutputFactory( path.node.callee.property.name, ].join('.') - if (callname === identifiers[identifierKey].name) { - identifiers[identifierKey].paths.push(path) + if (callname === identifier.name) { + identifier.paths.push(path) } } } @@ -129,8 +140,12 @@ export function compileStartOutputFactory( }) transformFuncs.forEach((identifierKey) => { - identifiers[identifierKey].paths.forEach((path) => { - identifiers[identifierKey].handleCallExpression( + const identifier = identifiers[identifierKey] + if (!identifier) { + return + } + identifier.paths.forEach((path) => { + identifier.handleCallExpression( path as babel.NodePath, opts, )