Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
nicohrubec committed Jan 27, 2026
commit 40a7ba42d403e4f96754dacf5e4d89763516d250
118 changes: 76 additions & 42 deletions packages/tanstackstart-react/src/vite/autoInstrumentMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,74 @@ type AutoInstrumentMiddlewareOptions = {
debug?: boolean;
};

type WrapResult = {
code: string;
didWrap: boolean;
skipped: string[];
};

/**
* Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files.
*/
export function wrapGlobalMiddleware(code: string, id: string, debug: boolean): WrapResult {
const skipped: string[] = [];
let didWrap = false;

const transformed = code.replace(
/(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g,
(match: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
didWrap = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
}
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skipped.push(key);
}
return match;
},
);

return { code: transformed, didWrap, skipped };
}

/**
* Wraps route middleware arrays in createFileRoute() files.
*/
export function wrapRouteMiddleware(code: string, id: string, debug: boolean): WrapResult {
const skipped: string[] = [];
let didWrap = false;

const transformed = code.replace(
/(\s+)(middleware)\s*:\s*\[([^\]]*)\]/g,
(match: string, whitespace: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
didWrap = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping route ${key} in ${id}`);
}
return `${whitespace}${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skipped.push(`route ${key}`);
}
return match;
},
);

return { code: transformed, didWrap, skipped };
}

/**
* A Vite plugin that automatically instruments TanStack Start middlewares:
* - `requestMiddleware` and `functionMiddleware` arrays in `createStart()`
Expand Down Expand Up @@ -44,52 +112,18 @@ export function makeAutoInstrumentMiddlewarePlugin(options: AutoInstrumentMiddle
let needsImport = false;
const skippedMiddlewares: string[] = [];

// Transform global middleware arrays in createStart() files
if (isStartFile) {
transformed = transformed.replace(
/(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g,
(match: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
needsImport = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
}
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skippedMiddlewares.push(key);
}
return match;
},
);
const result = wrapGlobalMiddleware(transformed, id, debug);
transformed = result.code;
needsImport = needsImport || result.didWrap;
skippedMiddlewares.push(...result.skipped);
}

// Transform route middleware arrays in createFileRoute() files
if (isRouteFile) {
transformed = transformed.replace(
/(\s+)(middleware)\s*:\s*\[([^\]]*)\]/g,
(match: string, whitespace: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
needsImport = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping route ${key} in ${id}`);
}
return `${whitespace}${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skippedMiddlewares.push(`route ${key}`);
}
return match;
},
);
const result = wrapRouteMiddleware(transformed, id, debug);
transformed = result.code;
needsImport = needsImport || result.didWrap;
skippedMiddlewares.push(...result.skipped);
}

// Warn about middlewares that couldn't be auto-wrapped
Expand Down
Loading
Loading