1- import { createResolver } from '@nuxt/kit' ;
1+ import { addServerImports , createResolver } from '@nuxt/kit' ;
22import type { Nitro } from 'nitropack/types' ;
33import * as path from 'path' ;
44import type { InputPluginOption } from 'rollup' ;
55
6+ /**
7+ * Adds a server import for the middleware instrumentation.
8+ */
9+ export function addMiddlewareImports ( ) : void {
10+ addServerImports ( [
11+ {
12+ name : 'wrapMiddlewareHandlerWithSentry' ,
13+ from : createResolver ( import . meta. url ) . resolve ( './runtime/hooks/wrapMiddlewareHandler' ) ,
14+ } ,
15+ ] ) ;
16+ }
17+
618/**
719 * Adds middleware instrumentation to the Nitro build.
820 *
921 * @param nitro Nitro instance
22+ * @param isNitroV3 Whether the app builds with Nitro v3 (Nuxt 5)
1023 */
11- export function addMiddlewareInstrumentation ( nitro : Nitro ) : void {
24+ export function addMiddlewareInstrumentation ( nitro : Nitro , isNitroV3 : boolean ) : void {
1225 nitro . hooks . hook ( 'rollup:before' , ( nitro , rollupConfig ) => {
1326 if ( ! rollupConfig . plugins ) {
1427 rollupConfig . plugins = [ ] ;
@@ -18,21 +31,20 @@ export function addMiddlewareInstrumentation(nitro: Nitro): void {
1831 rollupConfig . plugins = [ rollupConfig . plugins ] ;
1932 }
2033
21- rollupConfig . plugins . push ( middlewareInstrumentationPlugin ( nitro ) ) ;
34+ rollupConfig . plugins . push ( middlewareInstrumentationPlugin ( nitro , isNitroV3 ) ) ;
2235 } ) ;
2336}
2437
2538/**
2639 * Creates a rollup plugin for the middleware instrumentation by transforming the middleware code.
2740 *
2841 * @param nitro Nitro instance
42+ * @param isNitroV3 Whether the app builds with Nitro v3 (Nuxt 5)
2943 * @returns The rollup plugin for the middleware instrumentation.
3044 */
31- function middlewareInstrumentationPlugin ( nitro : Nitro ) : InputPluginOption {
45+ function middlewareInstrumentationPlugin ( nitro : Nitro , isNitroV3 : boolean ) : InputPluginOption {
3246 const middlewareFiles = new Set < string > ( ) ;
33- // Imported by absolute path rather than through `#imports`: with `imports.autoImport: false`,
34- // Nuxt 5 generates an empty server `#imports` module, dropping `addServerImports` registrations.
35- const wrapperModule = createResolver ( import . meta. url ) . resolve ( './runtime/hooks/wrapMiddlewareHandler' ) ;
47+ const wrapperModule = isNitroV3 ? '#imports/server' : '#imports' ;
3648
3749 return {
3850 name : 'sentry-nuxt-middleware-instrumentation' ,
@@ -63,7 +75,7 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
6375 *
6476 * @param originalCode The original user code of the middleware.
6577 * @param fileName The name of the middleware file, used for the span name and logging.
66- * @param wrapperModule Absolute path of the module exporting `wrapMiddlewareHandlerWithSentry`.
78+ * @param wrapperModule Import specifier resolving to `wrapMiddlewareHandlerWithSentry`.
6779 *
6880 * @returns The wrapped user code of the middleware.
6981 */
@@ -72,7 +84,7 @@ function wrapMiddlewareCode(originalCode: string, fileName: string, wrapperModul
7284 const cleanFileName = fileName . replace ( / \. ( t s | j s | m j s | m t s | c t s ) $ / , '' ) ;
7385
7486 return `
75- import { wrapMiddlewareHandlerWithSentry } from ${ JSON . stringify ( wrapperModule ) } ;
87+ import { wrapMiddlewareHandlerWithSentry } from ' ${ wrapperModule } ' ;
7688
7789function defineInstrumentedEventHandler(handlerOrObject) {
7890 return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${ cleanFileName } '));
0 commit comments