@@ -19,8 +19,9 @@ export function addMiddlewareImports(): void {
1919 * Adds middleware instrumentation to the Nitro build.
2020 *
2121 * @param nitro Nitro instance
22+ * @param isNitroV3 Whether the app builds with Nitro v3 (Nuxt 5)
2223 */
23- export function addMiddlewareInstrumentation ( nitro : Nitro ) : void {
24+ export function addMiddlewareInstrumentation ( nitro : Nitro , isNitroV3 : boolean ) : void {
2425 nitro . hooks . hook ( 'rollup:before' , ( nitro , rollupConfig ) => {
2526 if ( ! rollupConfig . plugins ) {
2627 rollupConfig . plugins = [ ] ;
@@ -30,18 +31,20 @@ export function addMiddlewareInstrumentation(nitro: Nitro): void {
3031 rollupConfig . plugins = [ rollupConfig . plugins ] ;
3132 }
3233
33- rollupConfig . plugins . push ( middlewareInstrumentationPlugin ( nitro ) ) ;
34+ rollupConfig . plugins . push ( middlewareInstrumentationPlugin ( nitro , isNitroV3 ) ) ;
3435 } ) ;
3536}
3637
3738/**
3839 * Creates a rollup plugin for the middleware instrumentation by transforming the middleware code.
3940 *
4041 * @param nitro Nitro instance
42+ * @param isNitroV3 Whether the app builds with Nitro v3 (Nuxt 5)
4143 * @returns The rollup plugin for the middleware instrumentation.
4244 */
43- function middlewareInstrumentationPlugin ( nitro : Nitro ) : InputPluginOption {
45+ function middlewareInstrumentationPlugin ( nitro : Nitro , isNitroV3 : boolean ) : InputPluginOption {
4446 const middlewareFiles = new Set < string > ( ) ;
47+ const wrapperModule = isNitroV3 ? '#imports/server' : '#imports' ;
4548
4649 return {
4750 name : 'sentry-nuxt-middleware-instrumentation' ,
@@ -58,7 +61,7 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
5861 if ( middlewareFiles . has ( id ) ) {
5962 const fileName = path . basename ( id ) ;
6063 return {
61- code : wrapMiddlewareCode ( code , fileName ) ,
64+ code : wrapMiddlewareCode ( code , fileName , wrapperModule ) ,
6265 map : null ,
6366 } ;
6467 }
@@ -72,15 +75,16 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
7275 *
7376 * @param originalCode The original user code of the middleware.
7477 * @param fileName The name of the middleware file, used for the span name and logging.
78+ * @param wrapperModule Import specifier resolving to `wrapMiddlewareHandlerWithSentry`.
7579 *
7680 * @returns The wrapped user code of the middleware.
7781 */
78- function wrapMiddlewareCode ( originalCode : string , fileName : string ) : string {
82+ function wrapMiddlewareCode ( originalCode : string , fileName : string , wrapperModule : string ) : string {
7983 // Remove common file extensions
8084 const cleanFileName = fileName . replace ( / \. ( t s | j s | m j s | m t s | c t s ) $ / , '' ) ;
8185
8286 return `
83- import { wrapMiddlewareHandlerWithSentry } from '#imports ';
87+ import { wrapMiddlewareHandlerWithSentry } from '${ wrapperModule } ';
8488
8589function defineInstrumentedEventHandler(handlerOrObject) {
8690 return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${ cleanFileName } '));
0 commit comments