Skip to content

Commit 40a7ba4

Browse files
committed
refactor
1 parent b877f51 commit 40a7ba4

File tree

2 files changed

+225
-261
lines changed

2 files changed

+225
-261
lines changed

packages/tanstackstart-react/src/vite/autoInstrumentMiddleware.ts

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,74 @@ type AutoInstrumentMiddlewareOptions = {
55
debug?: boolean;
66
};
77

8+
type WrapResult = {
9+
code: string;
10+
didWrap: boolean;
11+
skipped: string[];
12+
};
13+
14+
/**
15+
* Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files.
16+
*/
17+
export function wrapGlobalMiddleware(code: string, id: string, debug: boolean): WrapResult {
18+
const skipped: string[] = [];
19+
let didWrap = false;
20+
21+
const transformed = code.replace(
22+
/(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g,
23+
(match: string, key: string, contents: string) => {
24+
const objContents = arrayToObjectShorthand(contents);
25+
if (objContents) {
26+
didWrap = true;
27+
if (debug) {
28+
// eslint-disable-next-line no-console
29+
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
30+
}
31+
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
32+
}
33+
// Track middlewares that couldn't be auto-wrapped
34+
// Skip if we matched whitespace only
35+
if (contents.trim()) {
36+
skipped.push(key);
37+
}
38+
return match;
39+
},
40+
);
41+
42+
return { code: transformed, didWrap, skipped };
43+
}
44+
45+
/**
46+
* Wraps route middleware arrays in createFileRoute() files.
47+
*/
48+
export function wrapRouteMiddleware(code: string, id: string, debug: boolean): WrapResult {
49+
const skipped: string[] = [];
50+
let didWrap = false;
51+
52+
const transformed = code.replace(
53+
/(\s+)(middleware)\s*:\s*\[([^\]]*)\]/g,
54+
(match: string, whitespace: string, key: string, contents: string) => {
55+
const objContents = arrayToObjectShorthand(contents);
56+
if (objContents) {
57+
didWrap = true;
58+
if (debug) {
59+
// eslint-disable-next-line no-console
60+
console.log(`[Sentry] Auto-wrapping route ${key} in ${id}`);
61+
}
62+
return `${whitespace}${key}: wrapMiddlewaresWithSentry(${objContents})`;
63+
}
64+
// Track middlewares that couldn't be auto-wrapped
65+
// Skip if we matched whitespace only
66+
if (contents.trim()) {
67+
skipped.push(`route ${key}`);
68+
}
69+
return match;
70+
},
71+
);
72+
73+
return { code: transformed, didWrap, skipped };
74+
}
75+
876
/**
977
* A Vite plugin that automatically instruments TanStack Start middlewares:
1078
* - `requestMiddleware` and `functionMiddleware` arrays in `createStart()`
@@ -44,52 +112,18 @@ export function makeAutoInstrumentMiddlewarePlugin(options: AutoInstrumentMiddle
44112
let needsImport = false;
45113
const skippedMiddlewares: string[] = [];
46114

47-
// Transform global middleware arrays in createStart() files
48115
if (isStartFile) {
49-
transformed = transformed.replace(
50-
/(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g,
51-
(match: string, key: string, contents: string) => {
52-
const objContents = arrayToObjectShorthand(contents);
53-
if (objContents) {
54-
needsImport = true;
55-
if (debug) {
56-
// eslint-disable-next-line no-console
57-
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
58-
}
59-
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
60-
}
61-
// Track middlewares that couldn't be auto-wrapped
62-
// Skip if we matched whitespace only
63-
if (contents.trim()) {
64-
skippedMiddlewares.push(key);
65-
}
66-
return match;
67-
},
68-
);
116+
const result = wrapGlobalMiddleware(transformed, id, debug);
117+
transformed = result.code;
118+
needsImport = needsImport || result.didWrap;
119+
skippedMiddlewares.push(...result.skipped);
69120
}
70121

71-
// Transform route middleware arrays in createFileRoute() files
72122
if (isRouteFile) {
73-
transformed = transformed.replace(
74-
/(\s+)(middleware)\s*:\s*\[([^\]]*)\]/g,
75-
(match: string, whitespace: string, key: string, contents: string) => {
76-
const objContents = arrayToObjectShorthand(contents);
77-
if (objContents) {
78-
needsImport = true;
79-
if (debug) {
80-
// eslint-disable-next-line no-console
81-
console.log(`[Sentry] Auto-wrapping route ${key} in ${id}`);
82-
}
83-
return `${whitespace}${key}: wrapMiddlewaresWithSentry(${objContents})`;
84-
}
85-
// Track middlewares that couldn't be auto-wrapped
86-
// Skip if we matched whitespace only
87-
if (contents.trim()) {
88-
skippedMiddlewares.push(`route ${key}`);
89-
}
90-
return match;
91-
},
92-
);
123+
const result = wrapRouteMiddleware(transformed, id, debug);
124+
transformed = result.code;
125+
needsImport = needsImport || result.didWrap;
126+
skippedMiddlewares.push(...result.skipped);
93127
}
94128

95129
// Warn about middlewares that couldn't be auto-wrapped

0 commit comments

Comments
 (0)