1- import { addServerImports , createResolver } from '@nuxt/kit' ;
1+ import { 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-
186/**
197 * Adds middleware instrumentation to the Nitro build.
208 *
@@ -42,6 +30,9 @@ export function addMiddlewareInstrumentation(nitro: Nitro): void {
4230 */
4331function middlewareInstrumentationPlugin ( nitro : Nitro ) : InputPluginOption {
4432 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' ) ;
4536
4637 return {
4738 name : 'sentry-nuxt-middleware-instrumentation' ,
@@ -58,7 +49,7 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
5849 if ( middlewareFiles . has ( id ) ) {
5950 const fileName = path . basename ( id ) ;
6051 return {
61- code : wrapMiddlewareCode ( code , fileName ) ,
52+ code : wrapMiddlewareCode ( code , fileName , wrapperModule ) ,
6253 map : null ,
6354 } ;
6455 }
@@ -72,15 +63,16 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
7263 *
7364 * @param originalCode The original user code of the middleware.
7465 * @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`.
7567 *
7668 * @returns The wrapped user code of the middleware.
7769 */
78- function wrapMiddlewareCode ( originalCode : string , fileName : string ) : string {
70+ function wrapMiddlewareCode ( originalCode : string , fileName : string , wrapperModule : string ) : string {
7971 // Remove common file extensions
8072 const cleanFileName = fileName . replace ( / \. ( t s | j s | m j s | m t s | c t s ) $ / , '' ) ;
8173
8274 return `
83- import { wrapMiddlewareHandlerWithSentry } from '#imports' ;
75+ import { wrapMiddlewareHandlerWithSentry } from ${ JSON . stringify ( wrapperModule ) } ;
8476
8577function defineInstrumentedEventHandler(handlerOrObject) {
8678 return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${ cleanFileName } '));
0 commit comments