From 0886efbd012428c0d0ea5f426092bcd6f59de6c3 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Fri, 26 Sep 2025 05:12:15 +0200 Subject: [PATCH 1/2] fix: compile createMiddleware using start-compiler-plugin this first pass handles the createMiddleware imported from `@tanstack/react-start`` second pass handles ``` startInstance.createMiddleware() ``` --- .../src/create-server-fn-plugin/compiler.ts | 48 +++++++++++----- .../src/create-server-fn-plugin/plugin.ts | 56 +++++++++++++------ .../src/start-compiler-plugin/compilers.ts | 6 ++ .../src/start-compiler-plugin/constants.ts | 1 + .../createMiddleware.test.ts | 6 +- .../snapshots/client/createStart.tsx | 0 .../snapshots/server/createStart.tsx | 0 .../test-files/createStart.tsx | 0 .../createMiddleware.test.ts | 37 ++++++++++++ .../client/create-function-middleware.ts | 1 + .../client/createMiddlewareDestructured.tsx | 24 ++++++++ .../createMiddlewareDestructuredRename.tsx | 24 ++++++++ .../client/createMiddlewareStarImport.tsx | 24 ++++++++ .../client/createMiddlewareValidator.tsx | 1 + .../snapshots/client/factory.ts | 15 +++++ .../server/create-function-middleware.ts | 0 .../server/createMiddlewareDestructured.tsx | 0 .../createMiddlewareDestructuredRename.tsx | 0 .../server/createMiddlewareStarImport.tsx | 0 .../server/createMiddlewareValidator.tsx | 0 .../snapshots/server/factory.ts | 39 +++++++++++++ .../test-files/create-function-middleware.ts | 0 .../createMiddlewareDestructured.tsx | 0 .../createMiddlewareDestructuredRename.tsx | 0 .../test-files/createMiddlewareStarImport.tsx | 0 .../test-files/createMiddlewareValidator.tsx | 0 .../test-files/factory.ts | 38 +++++++++++++ .../client/createMiddlewareDestructured.tsx | 13 ----- .../createMiddlewareDestructuredRename.tsx | 13 ----- .../client/createMiddlewareStarImport.tsx | 13 ----- .../createServerFn/createServerFn.test.ts | 10 +--- .../snapshots/client/createStart.tsx | 7 --- .../snapshots/client/factory.tsx | 16 ++++++ .../snapshots/server/createStart.tsx | 11 ---- .../createServerFn/test-files/createStart.tsx | 9 --- 35 files changed, 301 insertions(+), 111 deletions(-) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-create-server-fn-plugin}/createMiddleware.test.ts (94%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-create-server-fn-plugin}/snapshots/client/createStart.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-create-server-fn-plugin}/snapshots/server/createStart.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-create-server-fn-plugin}/test-files/createStart.tsx (100%) create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/createMiddleware.test.ts rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/client/create-function-middleware.ts (83%) create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructured.tsx create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructuredRename.tsx create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareStarImport.tsx rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/client/createMiddlewareValidator.tsx (82%) create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/server/create-function-middleware.ts (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/server/createMiddlewareDestructured.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/server/createMiddlewareDestructuredRename.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/server/createMiddlewareStarImport.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/snapshots/server/createMiddlewareValidator.tsx (100%) create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/test-files/create-function-middleware.ts (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/test-files/createMiddlewareDestructured.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/test-files/createMiddlewareDestructuredRename.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/test-files/createMiddlewareStarImport.tsx (100%) rename packages/start-plugin-core/tests/{createMiddleware => createMiddleware-start-compiler-plugin}/test-files/createMiddlewareValidator.tsx (100%) create mode 100644 packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/factory.ts delete mode 100644 packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructured.tsx delete mode 100644 packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructuredRename.tsx delete mode 100644 packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareStarImport.tsx delete mode 100644 packages/start-plugin-core/tests/createServerFn/snapshots/client/createStart.tsx delete mode 100644 packages/start-plugin-core/tests/createServerFn/snapshots/server/createStart.tsx delete mode 100644 packages/start-plugin-core/tests/createServerFn/test-files/createStart.tsx diff --git a/packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts b/packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts index 6737f68e5ab..f00f89dbb8a 100644 --- a/packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts +++ b/packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts @@ -29,10 +29,18 @@ type ExportEntry = type Kind = 'None' | `Root` | `Builder` | LookupKind -type LookupKind = 'ServerFn' | 'Middleware' +export type LookupKind = 'ServerFn' | 'Middleware' + +const LookupSetup: Record< + LookupKind, + { candidateCallIdentifier: Set } +> = { + ServerFn: { candidateCallIdentifier: new Set(['handler']) }, + Middleware: { + candidateCallIdentifier: new Set(['server', 'client', 'createMiddlewares']), + }, +} -const validLookupKinds: Array = ['ServerFn', 'Middleware'] -const candidateCallIdentifier = ['handler', 'server', 'client'] export type LookupConfig = { libName: string rootExport: string @@ -48,14 +56,18 @@ interface ModuleInfo { export class ServerFnCompiler { private moduleCache = new Map() private initialized = false + private validLookupKinds: Set constructor( private options: { env: 'client' | 'server' lookupConfigurations: Array + lookupKinds: Set loadModule: (id: string) => Promise resolveId: (id: string, importer?: string) => Promise }, - ) {} + ) { + this.validLookupKinds = options.lookupKinds + } private async init(id: string) { await Promise.all( @@ -207,7 +219,7 @@ export class ServerFnCompiler { }> = [] for (const handler of candidates) { const kind = await this.resolveExprKind(handler, id) - if (validLookupKinds.includes(kind as LookupKind)) { + if (this.validLookupKinds.has(kind as LookupKind)) { toRewrite.push({ callExpression: handler, kind: kind as LookupKind }) } } @@ -261,7 +273,10 @@ export class ServerFnCompiler { for (const binding of bindings.values()) { if (binding.type === 'var') { - const handler = isCandidateCallExpression(binding.init) + const handler = isCandidateCallExpression( + binding.init, + this.validLookupKinds, + ) if (handler) { candidates.push(handler) } @@ -368,7 +383,7 @@ export class ServerFnCompiler { if (calleeKind === `Root` || calleeKind === `Builder`) { return `Builder` } - for (const kind of validLookupKinds) { + for (const kind of this.validLookupKinds) { if (calleeKind === kind) { return kind } @@ -407,16 +422,18 @@ export class ServerFnCompiler { if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) { const prop = callee.property.name - if (prop === 'handler') { + if ( + this.validLookupKinds.has('ServerFn') && + LookupSetup['ServerFn'].candidateCallIdentifier.has(prop) + ) { const base = await this.resolveExprKind(callee.object, fileId, visited) if (base === 'Root' || base === 'Builder') { return 'ServerFn' } return 'None' } else if ( - prop === 'client' || - prop === 'server' || - prop === 'createMiddleware' + this.validLookupKinds.has('Middleware') && + LookupSetup['Middleware'].candidateCallIdentifier.has(prop) ) { const base = await this.resolveExprKind(callee.object, fileId, visited) if (base === 'Root' || base === 'Builder' || base === 'Middleware') { @@ -483,6 +500,7 @@ export class ServerFnCompiler { function isCandidateCallExpression( node: t.Node | null | undefined, + lookupKinds: Set, ): undefined | t.CallExpression { if (!t.isCallExpression(node)) return undefined @@ -490,9 +508,11 @@ function isCandidateCallExpression( if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property)) { return undefined } - if (!candidateCallIdentifier.includes(callee.property.name)) { - return undefined + for (const kind of lookupKinds) { + if (LookupSetup[kind].candidateCallIdentifier.has(callee.property.name)) { + return node + } } - return node + return undefined } diff --git a/packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts b/packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts index 0e873cb4c30..6cc21597276 100644 --- a/packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts +++ b/packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts @@ -1,5 +1,6 @@ import { VITE_ENVIRONMENT_NAMES } from '../constants' import { ServerFnCompiler } from './compiler' +import type { LookupConfig, LookupKind } from './compiler' import type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers' import type { ViteEnvironmentNames } from '../constants' import type { PluginOption } from 'vite' @@ -8,6 +9,36 @@ function cleanId(id: string): string { return id.split('?')[0]! } +const LookupKindsPerEnv: Record<'client' | 'server', Set> = { + client: new Set(['Middleware', 'ServerFn'] as const), + server: new Set(['ServerFn'] as const), +} + +const getLookupConfigurationsForEnv = ( + env: 'client' | 'server', + framework: CompileStartFrameworkOptions, +): Array => { + const createServerFnConfig: LookupConfig = { + libName: `@tanstack/${framework}-start`, + rootExport: 'createServerFn', + } + if (env === 'client') { + return [ + { + libName: `@tanstack/${framework}-start`, + rootExport: 'createMiddleware', + }, + { + libName: `@tanstack/${framework}-start`, + rootExport: 'createStart', + }, + + createServerFnConfig, + ] + } else { + return [createServerFnConfig] + } +} export function createServerFnPlugin( framework: CompileStartFrameworkOptions, ): PluginOption { @@ -52,8 +83,9 @@ export function createServerFnPlugin( exclude: new RegExp(`${SERVER_FN_LOOKUP}$`), }, code: { - // only scan files that mention `.handler(` | `.server(` | `.client(` - include: [/\.handler\(/, /\.server\(/, /\.client\(/], + // TODO apply this plugin with a different filter per environment so that .createMiddleware() calls are not scanned in server env + // only scan files that mention `.handler(` | `.createMiddleware()` + include: [/\.handler\(/, /.createMiddleware\(\)/], }, }, async handler(code, id) { @@ -73,21 +105,11 @@ export function createServerFnPlugin( compiler = new ServerFnCompiler({ env, - lookupConfigurations: [ - { - libName: `@tanstack/${framework}-start`, - rootExport: 'createMiddleware', - }, - - { - libName: `@tanstack/${framework}-start`, - rootExport: 'createServerFn', - }, - { - libName: `@tanstack/${framework}-start`, - rootExport: 'createStart', - }, - ], + lookupKinds: LookupKindsPerEnv[env], + lookupConfigurations: getLookupConfigurationsForEnv( + env, + framework, + ), loadModule: async (id: string) => { if (this.environment.mode === 'build') { const loaded = await this.load({ id }) 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 d7c335243fa..f5d244e5c1a 100644 --- a/packages/start-plugin-core/src/start-compiler-plugin/compilers.ts +++ b/packages/start-plugin-core/src/start-compiler-plugin/compilers.ts @@ -6,6 +6,7 @@ import { findReferencedIdentifiers, } from 'babel-dead-code-elimination' import { generateFromAst, parseAst } from '@tanstack/router-utils' +import { handleCreateMiddleware } from '../create-server-fn-plugin/handleCreateMiddleware' import { transformFuncs } from './constants' import { handleCreateIsomorphicFnCallExpression } from './isomorphicFn' import { @@ -38,6 +39,11 @@ export function compileStartOutputFactory( handleCallExpression: handleCreateIsomorphicFnCallExpression, paths: [], }, + createMiddleware: { + name: 'createMiddleware', + handleCallExpression: handleCreateMiddleware, + paths: [], + }, } const ast = parseAst(opts) diff --git a/packages/start-plugin-core/src/start-compiler-plugin/constants.ts b/packages/start-plugin-core/src/start-compiler-plugin/constants.ts index d5416a2c03e..08f171d1e97 100644 --- a/packages/start-plugin-core/src/start-compiler-plugin/constants.ts +++ b/packages/start-plugin-core/src/start-compiler-plugin/constants.ts @@ -2,4 +2,5 @@ export const transformFuncs = [ 'createServerOnlyFn', 'createClientOnlyFn', 'createIsomorphicFn', + 'createMiddleware', ] as const diff --git a/packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts b/packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/createMiddleware.test.ts similarity index 94% rename from packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts rename to packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/createMiddleware.test.ts index cb75c8baacf..5382d96dba2 100644 --- a/packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts +++ b/packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/createMiddleware.test.ts @@ -17,16 +17,12 @@ async function compile(opts: { loadModule: async (id) => { // do nothing in test }, + lookupKinds: new Set(['Middleware']), lookupConfigurations: [ { libName: `@tanstack/react-start`, rootExport: 'createMiddleware', }, - - { - libName: `@tanstack/react-start`, - rootExport: 'createServerFn', - }, { libName: `@tanstack/react-start`, rootExport: 'createStart', diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createStart.tsx b/packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/snapshots/client/createStart.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/client/createStart.tsx rename to packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/snapshots/client/createStart.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/server/createStart.tsx b/packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/snapshots/server/createStart.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/server/createStart.tsx rename to packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/snapshots/server/createStart.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/test-files/createStart.tsx b/packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/test-files/createStart.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/test-files/createStart.tsx rename to packages/start-plugin-core/tests/createMiddleware-create-server-fn-plugin/test-files/createStart.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/createMiddleware.test.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/createMiddleware.test.ts new file mode 100644 index 00000000000..b1c2cb3c414 --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/createMiddleware.test.ts @@ -0,0 +1,37 @@ +import { readFile, readdir } from 'node:fs/promises' +import path from 'node:path' +import { describe, expect, test } from 'vitest' +import { compileStartOutputFactory } from '../../src/start-compiler-plugin/compilers' + +const compileStartOutput = compileStartOutputFactory('react') + +async function getFilenames() { + return await readdir(path.resolve(import.meta.dirname, './test-files')) +} + +describe('createMiddleware compiles correctly', async () => { + const filenames = await getFilenames() + + describe.each(filenames)('should handle "%s"', async (filename) => { + const file = await readFile( + path.resolve(import.meta.dirname, `./test-files/${filename}`), + ) + const code = file.toString() + + test.each(['client', 'server'] as const)( + `should compile for ${filename} %s`, + async (env) => { + const compiledResult = compileStartOutput({ + env, + code, + filename, + dce: false, + }) + + await expect(compiledResult.code).toMatchFileSnapshot( + `./snapshots/${env}/${filename}`, + ) + }, + ) + }) +}) diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/create-function-middleware.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/create-function-middleware.ts similarity index 83% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/client/create-function-middleware.ts rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/create-function-middleware.ts index b37e25e4986..7f39c06036f 100644 --- a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/create-function-middleware.ts +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/create-function-middleware.ts @@ -1,4 +1,5 @@ import { createMiddleware } from '@tanstack/react-start'; +import { foo } from '@some/lib'; export const fnMw = createMiddleware({ type: 'function' }).client(() => { diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructured.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructured.tsx new file mode 100644 index 00000000000..c513c274c5f --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructured.tsx @@ -0,0 +1,24 @@ +import { createMiddleware } from '@tanstack/react-start'; +import { z } from 'zod'; +export const withUseServer = createMiddleware({ + id: 'test' +}); +export const withoutUseServer = createMiddleware({ + id: 'test' +}); +export const withVariable = createMiddleware({ + id: 'test' +}); +async function abstractedFunction() { + console.info('Fetching posts...'); + await new Promise(r => setTimeout(r, 500)); + return axios.get>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); +} +function zodValidator(schema: TSchema, fn: (input: z.output) => TResult) { + return async (input: unknown) => { + return fn(schema.parse(input)); + }; +} +export const withZodValidator = createMiddleware({ + id: 'test' +}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructuredRename.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructuredRename.tsx new file mode 100644 index 00000000000..4ab09a99335 --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructuredRename.tsx @@ -0,0 +1,24 @@ +import { createMiddleware as middlewareFn } from '@tanstack/react-start'; +import { z } from 'zod'; +export const withUseServer = middlewareFn({ + id: 'test' +}); +export const withoutUseServer = middlewareFn({ + id: 'test' +}); +export const withVariable = middlewareFn({ + id: 'test' +}); +async function abstractedFunction() { + console.info('Fetching posts...'); + await new Promise(r => setTimeout(r, 500)); + return axios.get>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); +} +function zodValidator(schema: TSchema, fn: (input: z.output) => TResult) { + return async (input: unknown) => { + return fn(schema.parse(input)); + }; +} +export const withZodValidator = middlewareFn({ + id: 'test' +}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareStarImport.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareStarImport.tsx new file mode 100644 index 00000000000..7253e9510c9 --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareStarImport.tsx @@ -0,0 +1,24 @@ +import * as TanStackStart from '@tanstack/react-start'; +import { z } from 'zod'; +export const withUseServer = TanStackStart.createMiddleware({ + id: 'test' +}); +export const withoutUseServer = TanStackStart.createMiddleware({ + id: 'test' +}); +export const withVariable = TanStackStart.createMiddleware({ + id: 'test' +}); +async function abstractedFunction() { + console.info('Fetching posts...'); + await new Promise(r => setTimeout(r, 500)); + return axios.get>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); +} +function zodValidator(schema: TSchema, fn: (input: z.output) => TResult) { + return async (input: unknown) => { + return fn(schema.parse(input)); + }; +} +export const withZodValidator = TanStackStart.createMiddleware({ + id: 'test' +}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareValidator.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareValidator.tsx similarity index 82% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareValidator.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareValidator.tsx index 3b1beac2c03..280dc3a0670 100644 --- a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareValidator.tsx +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareValidator.tsx @@ -1,4 +1,5 @@ import { createMiddleware } from '@tanstack/react-start'; +import { z } from 'zod'; export const withUseServer = createMiddleware({ id: 'test' }); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts new file mode 100644 index 00000000000..368142abc98 --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts @@ -0,0 +1,15 @@ +import { createMiddleware } from "@tanstack/react-start"; +import { getCookie } from "@tanstack/react-start/server"; +interface AuthMiddlewareOptions { + allowUnauthenticated?: boolean; +} +interface AuthContext { + session: { + id: string; + } | null; +} +export const createAuthMiddleware = (opts: AuthMiddlewareOptions = { + allowUnauthenticated: false +}) => createMiddleware({ + type: "function" +}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/server/create-function-middleware.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/create-function-middleware.ts similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/server/create-function-middleware.ts rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/create-function-middleware.ts diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareDestructured.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareDestructured.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareDestructured.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareDestructured.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareDestructuredRename.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareDestructuredRename.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareDestructuredRename.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareDestructuredRename.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareStarImport.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareStarImport.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareStarImport.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareStarImport.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareValidator.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareValidator.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/snapshots/server/createMiddlewareValidator.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/createMiddlewareValidator.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts new file mode 100644 index 00000000000..5965a1f37ad --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts @@ -0,0 +1,39 @@ +import { createMiddleware } from "@tanstack/react-start"; +import { getCookie } from "@tanstack/react-start/server"; +interface AuthMiddlewareOptions { + allowUnauthenticated?: boolean; +} +interface AuthContext { + session: { + id: string; + } | null; +} +export const createAuthMiddleware = (opts: AuthMiddlewareOptions = { + allowUnauthenticated: false +}) => createMiddleware({ + type: "function" +}).server(({ + next +}) => { + const token = getCookie("session"); + if (!token) { + if (!opts.allowUnauthenticated) { + throw new Error("Unauthorized"); + } + return next({ + context: { + session: null + } + }); + } + + // ... token validation should be here + + return next({ + context: { + session: { + id: (Math.random() * 1000).toString() + } + } + }); +}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware/test-files/create-function-middleware.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/create-function-middleware.ts similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/test-files/create-function-middleware.ts rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/create-function-middleware.ts diff --git a/packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareDestructured.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareDestructured.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareDestructured.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareDestructured.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareDestructuredRename.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareDestructuredRename.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareDestructuredRename.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareDestructuredRename.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareStarImport.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareStarImport.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareStarImport.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareStarImport.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareValidator.tsx b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareValidator.tsx similarity index 100% rename from packages/start-plugin-core/tests/createMiddleware/test-files/createMiddlewareValidator.tsx rename to packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/createMiddlewareValidator.tsx diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/factory.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/factory.ts new file mode 100644 index 00000000000..4abd567628b --- /dev/null +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/test-files/factory.ts @@ -0,0 +1,38 @@ +import { createMiddleware } from '@tanstack/react-start' +import { getCookie } from '@tanstack/react-start/server' + +interface AuthMiddlewareOptions { + allowUnauthenticated?: boolean +} + +interface AuthContext { + session: { id: string } | null +} + +export const createAuthMiddleware = ( + opts: AuthMiddlewareOptions = { allowUnauthenticated: false }, +) => + createMiddleware({ type: 'function' }).server(({ next }) => { + const token = getCookie('session') + if (!token) { + if (!opts.allowUnauthenticated) { + throw new Error('Unauthorized') + } + + return next({ + context: { + session: null, + }, + }) + } + + // ... token validation should be here + + return next({ + context: { + session: { + id: (Math.random() * 1000).toString(), + }, + }, + }) + }) diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructured.tsx b/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructured.tsx deleted file mode 100644 index b5cf588a1fc..00000000000 --- a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructured.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import { createMiddleware } from '@tanstack/react-start'; -export const withUseServer = createMiddleware({ - id: 'test' -}); -export const withoutUseServer = createMiddleware({ - id: 'test' -}); -export const withVariable = createMiddleware({ - id: 'test' -}); -export const withZodValidator = createMiddleware({ - id: 'test' -}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructuredRename.tsx b/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructuredRename.tsx deleted file mode 100644 index ee061797034..00000000000 --- a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareDestructuredRename.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import { createMiddleware as middlewareFn } from '@tanstack/react-start'; -export const withUseServer = middlewareFn({ - id: 'test' -}); -export const withoutUseServer = middlewareFn({ - id: 'test' -}); -export const withVariable = middlewareFn({ - id: 'test' -}); -export const withZodValidator = middlewareFn({ - id: 'test' -}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareStarImport.tsx b/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareStarImport.tsx deleted file mode 100644 index 6f2f40a2a32..00000000000 --- a/packages/start-plugin-core/tests/createMiddleware/snapshots/client/createMiddlewareStarImport.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import * as TanStackStart from '@tanstack/react-start'; -export const withUseServer = TanStackStart.createMiddleware({ - id: 'test' -}); -export const withoutUseServer = TanStackStart.createMiddleware({ - id: 'test' -}); -export const withVariable = TanStackStart.createMiddleware({ - id: 'test' -}); -export const withZodValidator = TanStackStart.createMiddleware({ - id: 'test' -}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts index 89ef744be58..325459b694f 100644 --- a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts +++ b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts @@ -17,20 +17,12 @@ async function compile(opts: { loadModule: async (id) => { // do nothing in test }, + lookupKinds: new Set(['ServerFn']), lookupConfigurations: [ - { - libName: `@tanstack/react-start`, - rootExport: 'createMiddleware', - }, - { libName: `@tanstack/react-start`, rootExport: 'createServerFn', }, - { - libName: `@tanstack/react-start`, - rootExport: 'createStart', - }, ], resolveId: async (id) => { return id diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createStart.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createStart.tsx deleted file mode 100644 index ab6cae386fb..00000000000 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createStart.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { createStart } from '@tanstack/react-start'; -export const startInstance = createStart(() => {}); -export const someServerFn = startInstance.createServerFn().handler((opts, signal) => { - "use server"; - - return someServerFn.__executeServer(opts, signal); -}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx index 65d40878bc2..fe95c7e47ee 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx @@ -1,9 +1,25 @@ import { createServerFn, createMiddleware } from '@tanstack/react-start'; const authMiddleware = createMiddleware({ type: 'function' +}).server(({ + next +}) => { + return next({ + context: { + auth: 'auth' + } + }); }); const adminMiddleware = createMiddleware({ type: 'function' +}).server(({ + next +}) => { + return next({ + context: { + admin: 'admin' + } + }); }); export const createAuthServerFn = createServerFn().middleware([authMiddleware]); const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]); diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server/createStart.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server/createStart.tsx deleted file mode 100644 index 94bb4200499..00000000000 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server/createStart.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import { createStart } from '@tanstack/react-start'; -import { foo } from '@some/lib'; -export const startInstance = createStart(() => {}); -export const someServerFn = startInstance.createServerFn().handler((opts, signal) => { - "use server"; - - return someServerFn.__executeServer(opts, signal); -}, () => { - console.log('server mw'); - foo(); -}); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createServerFn/test-files/createStart.tsx b/packages/start-plugin-core/tests/createServerFn/test-files/createStart.tsx deleted file mode 100644 index bbd1378a11f..00000000000 --- a/packages/start-plugin-core/tests/createServerFn/test-files/createStart.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import { createStart } from '@tanstack/react-start' - -import { foo } from '@some/lib' -export const startInstance = createStart(() => {}) - -export const someServerFn = startInstance.createServerFn().handler(() => { - console.log('server mw') - foo() -}) From 3c1ece78952dfdce5181e423db7560ea17835900 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Fri, 26 Sep 2025 05:22:52 +0200 Subject: [PATCH 2/2] formatting --- .../snapshots/client/factory.ts | 6 +++--- .../snapshots/server/factory.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts index 368142abc98..f5e43fb22ad 100644 --- a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts @@ -1,5 +1,5 @@ -import { createMiddleware } from "@tanstack/react-start"; -import { getCookie } from "@tanstack/react-start/server"; +import { createMiddleware } from '@tanstack/react-start'; +import { getCookie } from '@tanstack/react-start/server'; interface AuthMiddlewareOptions { allowUnauthenticated?: boolean; } @@ -11,5 +11,5 @@ interface AuthContext { export const createAuthMiddleware = (opts: AuthMiddlewareOptions = { allowUnauthenticated: false }) => createMiddleware({ - type: "function" + type: 'function' }); \ No newline at end of file diff --git a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts index 5965a1f37ad..5b1c2f3709c 100644 --- a/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts +++ b/packages/start-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/server/factory.ts @@ -1,5 +1,5 @@ -import { createMiddleware } from "@tanstack/react-start"; -import { getCookie } from "@tanstack/react-start/server"; +import { createMiddleware } from '@tanstack/react-start'; +import { getCookie } from '@tanstack/react-start/server'; interface AuthMiddlewareOptions { allowUnauthenticated?: boolean; } @@ -11,14 +11,14 @@ interface AuthContext { export const createAuthMiddleware = (opts: AuthMiddlewareOptions = { allowUnauthenticated: false }) => createMiddleware({ - type: "function" + type: 'function' }).server(({ next }) => { - const token = getCookie("session"); + const token = getCookie('session'); if (!token) { if (!opts.allowUnauthenticated) { - throw new Error("Unauthorized"); + throw new Error('Unauthorized'); } return next({ context: {