Skip to content

Commit 9a8026b

Browse files
committed
refactor
1 parent 3660c72 commit 9a8026b

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ export function wrapGlobalMiddleware(code: string, id: string, debug: boolean):
4242
return { code: transformed, didWrap, skipped };
4343
}
4444

45+
/**
46+
* Adds the wrapMiddlewaresWithSentry import to the code.
47+
* Handles 'use client' and 'use server' directives by inserting the import after them.
48+
*/
49+
export function addSentryImport(code: string): string {
50+
const sentryImport = "import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\n";
51+
52+
// Check for 'use server' or 'use client' directives, these need to be before any imports
53+
const directiveMatch = code.match(/^(['"])use (client|server)\1;?\s*\n?/);
54+
55+
if (!directiveMatch) {
56+
return sentryImport + code;
57+
}
58+
59+
const directive = directiveMatch[0];
60+
return directive + sentryImport + code.slice(directive.length);
61+
}
62+
4563
/**
4664
* Wraps route middleware arrays in createFileRoute() files.
4765
*/
@@ -140,17 +158,7 @@ export function makeAutoInstrumentMiddlewarePlugin(options: AutoInstrumentMiddle
140158
return null;
141159
}
142160

143-
const sentryImport = "import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\n";
144-
145-
// Check for 'use server' or 'use client' directives, these need to be before any imports
146-
const directiveMatch = transformed.match(/^(['"])use (client|server)\1;?\s*\n?/);
147-
if (directiveMatch) {
148-
// Insert import after the directive
149-
const directive = directiveMatch[0];
150-
transformed = directive + sentryImport + transformed.slice(directive.length);
151-
} else {
152-
transformed = sentryImport + transformed;
153-
}
161+
transformed = addSentryImport(transformed);
154162

155163
return { code: transformed, map: null };
156164
},

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Plugin } from 'vite';
22
import { describe, expect, it, vi } from 'vitest';
33
import {
4+
addSentryImport,
45
arrayToObjectShorthand,
56
makeAutoInstrumentMiddlewarePlugin,
67
wrapGlobalMiddleware,
@@ -61,30 +62,6 @@ createStart(() => ({ requestMiddleware: wrapMiddlewaresWithSentry({ myMiddleware
6162
expect(result).toBeNull();
6263
});
6364

64-
it('handles files with use server directive', () => {
65-
const plugin = makeAutoInstrumentMiddlewarePlugin() as PluginWithTransform;
66-
const code = `'use server';
67-
import { createStart } from '@tanstack/react-start';
68-
createStart(() => ({ requestMiddleware: [authMiddleware] }));
69-
`;
70-
const result = plugin.transform(code, '/app/start.ts');
71-
72-
expect(result).not.toBeNull();
73-
expect(result!.code).toMatch(/^'use server';\s*\nimport \{ wrapMiddlewaresWithSentry \}/);
74-
});
75-
76-
it('handles files with use client directive', () => {
77-
const plugin = makeAutoInstrumentMiddlewarePlugin() as PluginWithTransform;
78-
const code = `"use client";
79-
import { createStart } from '@tanstack/react-start';
80-
createStart(() => ({ requestMiddleware: [authMiddleware] }));
81-
`;
82-
const result = plugin.transform(code, '/app/start.ts');
83-
84-
expect(result).not.toBeNull();
85-
expect(result!.code).toMatch(/^"use client";\s*\nimport \{ wrapMiddlewaresWithSentry \}/);
86-
});
87-
8865
it('adds import statement when wrapping middlewares', () => {
8966
const plugin = makeAutoInstrumentMiddlewarePlugin() as PluginWithTransform;
9067
const result = plugin.transform(createStartFile, '/app/start.ts');
@@ -352,6 +329,33 @@ export const Route = createFileRoute('/foo')({
352329
});
353330
});
354331

332+
describe('addSentryImport', () => {
333+
it('prepends import to code without directives', () => {
334+
const code = 'const foo = 1;';
335+
const result = addSentryImport(code);
336+
337+
expect(result).toBe(
338+
"import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\nconst foo = 1;",
339+
);
340+
});
341+
342+
it('inserts import after use server directive', () => {
343+
const code = "'use server';\nconst foo = 1;";
344+
const result = addSentryImport(code);
345+
346+
expect(result).toMatch(/^'use server';\nimport \{ wrapMiddlewaresWithSentry \}/);
347+
expect(result).toContain('const foo = 1;');
348+
});
349+
350+
it('inserts import after use client directive', () => {
351+
const code = '"use client";\nconst foo = 1;';
352+
const result = addSentryImport(code);
353+
354+
expect(result).toMatch(/^"use client";\nimport \{ wrapMiddlewaresWithSentry \}/);
355+
expect(result).toContain('const foo = 1;');
356+
});
357+
});
358+
355359
describe('arrayToObjectShorthand', () => {
356360
it('converts single identifier', () => {
357361
expect(arrayToObjectShorthand('foo')).toBe('{ foo }');

0 commit comments

Comments
 (0)