Skip to content

Commit 28bb272

Browse files
committed
import from #imports/server
1 parent 9929a2a commit 28bb272

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

packages/nuxt/src/module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
DEV_SERVER_CONFIG_PATH,
2222
} from './vite/addServerConfig';
2323
import { addDatabaseInstrumentation } from './vite/databaseConfig';
24-
import { addMiddlewareInstrumentation } from './vite/middlewareConfig';
24+
import { addMiddlewareImports, addMiddlewareInstrumentation } from './vite/middlewareConfig';
2525
import { setupOrchestrion } from './vite/orchestrion';
2626
import { setupSourceMaps } from './vite/sourceMaps';
2727
import { addStorageInstrumentation } from './vite/storageConfig';
@@ -120,6 +120,8 @@ export default defineNuxtModule<ModuleOptions>({
120120
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' });
121121
}
122122

123+
// Preps the middleware instrumentation module.
124+
addMiddlewareImports();
123125
addStorageInstrumentation(nuxt, !isNitroV3);
124126
addDatabaseInstrumentation(nuxt.options.nitro, !isNitroV3, moduleOptions);
125127

@@ -190,7 +192,7 @@ export default defineNuxtModule<ModuleOptions>({
190192
}
191193

192194
if (serverConfigFile) {
193-
addMiddlewareInstrumentation(nitro);
195+
addMiddlewareInstrumentation(nitro, isNitroV3);
194196

195197
consoleSandbox(() => {
196198
const serverDir = nitro.options.output.serverDir;

packages/nuxt/src/vite/middlewareConfig.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
import { createResolver } from '@nuxt/kit';
1+
import { addServerImports, createResolver } from '@nuxt/kit';
22
import type { Nitro } from 'nitropack/types';
33
import * as path from 'path';
44
import 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(/\.(ts|js|mjs|mts|cts)$/, '');
7385

7486
return `
75-
import { wrapMiddlewareHandlerWithSentry } from ${JSON.stringify(wrapperModule)};
87+
import { wrapMiddlewareHandlerWithSentry } from '${wrapperModule}';
7688
7789
function defineInstrumentedEventHandler(handlerOrObject) {
7890
return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${cleanFileName}'));

0 commit comments

Comments
 (0)