Skip to content

Commit ee5d353

Browse files
authored
fix(nuxt): Import from #imports/server in addMiddlewareImports() (#24150)
The nuxt-5 (canary) E2E build fails with `"wrapMiddlewareHandlerWithSentry" is not exported by ".nuxt/types/server-imports.mjs"`. In Nuxt 5, the import path is import path is `#imports/server`. Reference: nuxt/nuxt#36296
1 parent 516a674 commit ee5d353

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
allowBuilds:
22
'@sentry/cli': true
3+
minimumReleaseAge: 0

packages/nuxt/src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default defineNuxtModule<ModuleOptions>({
193193
}
194194

195195
if (serverConfigFile) {
196-
addMiddlewareInstrumentation(nitro);
196+
addMiddlewareInstrumentation(nitro, isNitroV3);
197197

198198
if (!usesDeprecatedInjectMode) {
199199
addServerConfigShimWithWarning(nitro);

packages/nuxt/src/vite/middlewareConfig.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/\.(ts|js|mjs|mts|cts)$/, '');
8185

8286
return `
83-
import { wrapMiddlewareHandlerWithSentry } from '#imports';
87+
import { wrapMiddlewareHandlerWithSentry } from '${wrapperModule}';
8488
8589
function defineInstrumentedEventHandler(handlerOrObject) {
8690
return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${cleanFileName}'));

0 commit comments

Comments
 (0)