1- import {
2- addPlugin ,
3- addPluginTemplate ,
4- addServerPlugin ,
5- addTemplate ,
6- addVitePlugin ,
7- createResolver ,
8- defineNuxtModule ,
9- } from '@nuxt/kit' ;
1+ import { addPlugin , addPluginTemplate , addTemplate , addVitePlugin , createResolver , defineNuxtModule } from '@nuxt/kit' ;
102// Needed to make TS evaluate the augmentation of Nitro types (https://github.com/nuxt/nuxt/pull/34039)
113import type { } from '@nuxt/nitro-server' ;
124
13- import { consoleSandbox } from '@sentry/core' ;
145import * as path from 'path' ;
156import type { SentryNuxtModuleOptions } from './common/types' ;
16- import {
17- addDynamicImportEntryFileWrapper ,
18- addServerConfigShimWithWarning ,
19- addSentryTopImport ,
20- addServerConfigPlugin ,
21- addServerConfigToBuild ,
22- } from './vite/addServerConfig' ;
23- import { addDatabaseInstrumentation } from './vite/databaseConfig' ;
24- import { addMiddlewareImports , addMiddlewareInstrumentation } from './vite/middlewareConfig' ;
7+ import { setupNitroServer } from './vite/nitroServer' ;
258import { setupOrchestrion } from './vite/orchestrion' ;
269import { setupSourceMaps } from './vite/sourceMaps' ;
27- import { addStorageInstrumentation } from './vite/storageConfig' ;
28- import { addOTelCommonJSImportAlias , findDefaultSdkInitFile , getNitroMajorVersion } from './vite/utils' ;
10+ import { addOTelCommonJSImportAlias , findDefaultSdkInitFile , resolveServerApi } from './vite/utils' ;
2911
3012export type ModuleOptions = SentryNuxtModuleOptions ;
3113type NuxtPageSubset = { file ?: string ; path : string } ;
@@ -44,6 +26,8 @@ export default defineNuxtModule<ModuleOptions>({
4426 return ;
4527 }
4628
29+ const serverApi = resolveServerApi ( ) ;
30+
4731 const moduleOptions = {
4832 ...moduleOptionsParam ,
4933 // oxlint-disable-next-line typescript/no-deprecated -- supported until removal
@@ -91,7 +75,6 @@ export default defineNuxtModule<ModuleOptions>({
9175 }
9276
9377 const serverConfigFile = await findDefaultSdkInitFile ( 'server' , nuxt , moduleOptions ) ;
94- const isNitroV3 = ( await getNitroMajorVersion ( nuxt . options . rootDir ) ) >= 3 ;
9578 const nuxtMajor = parseInt ( ( nuxt as unknown as { _version : string } ) . _version ?. split ( '.' ) [ 0 ] ?? '3' , 10 ) ;
9679 const isMinNuxtV4 = nuxtMajor >= 4 ;
9780
@@ -100,43 +83,21 @@ export default defineNuxtModule<ModuleOptions>({
10083 // Cloudflare detection happens inside, keyed off the resolved Nitro preset.
10184 setupOrchestrion ( nuxt , ! ! serverConfigFile , moduleOptions . buildTimeInstrumentation ) ;
10285
103- // The deprecated inject modes replace the default in-bundle initialization until their removal
104- const usesDeprecatedInjectMode =
105- moduleOptions . autoInjectServerSentry === 'top-level-import' ||
106- moduleOptions . autoInjectServerSentry === 'experimental_dynamic-import' ;
107-
10886 if ( serverConfigFile ) {
109- if ( ! usesDeprecatedInjectMode ) {
110- addServerConfigPlugin ( nuxt , serverConfigFile , ! isNitroV3 ) ;
111- }
112-
113- if ( isNitroV3 ) {
114- addServerPlugin ( moduleDirResolver . resolve ( './runtime/plugins/handler.server' ) ) ;
115- addServerPlugin ( moduleDirResolver . resolve ( './runtime/plugins/update-route-name.server' ) ) ;
116- } else {
117- addServerPlugin ( moduleDirResolver . resolve ( './runtime/plugins/handler-legacy.server' ) ) ;
118- addServerPlugin ( moduleDirResolver . resolve ( './runtime/plugins/update-route-name-legacy.server' ) ) ;
119- }
120-
121- addServerPlugin ( moduleDirResolver . resolve ( './runtime/plugins/sentry.server' ) ) ;
87+ setupNitroServer ( nuxt , serverApi , serverConfigFile , moduleOptions ) ;
12288
12389 if ( isMinNuxtV4 ) {
12490 addPlugin ( { src : moduleDirResolver . resolve ( './runtime/plugins/route-detector.server' ) , mode : 'server' } ) ;
12591 } else {
12692 addPlugin ( { src : moduleDirResolver . resolve ( './runtime/plugins/route-detector-legacy.server' ) , mode : 'server' } ) ;
12793 }
128-
129- // Preps the middleware instrumentation module.
130- addMiddlewareImports ( ) ;
131- addStorageInstrumentation ( nuxt , ! isNitroV3 ) ;
132- addDatabaseInstrumentation ( nuxt . options . nitro , ! isNitroV3 , moduleOptions ) ;
13394 }
13495
13596 if ( clientConfigFile || serverConfigFile ) {
13697 setupSourceMaps ( moduleOptions , nuxt , addVitePlugin ) ;
13798 }
13899
139- addOTelCommonJSImportAlias ( nuxt , isNitroV3 ) ;
100+ addOTelCommonJSImportAlias ( nuxt , serverApi === 'nitro3' ) ;
140101
141102 let pagesData : NuxtPageSubset [ ] = [ ] ;
142103
@@ -155,8 +116,8 @@ export default defineNuxtModule<ModuleOptions>({
155116 // Vite virtual plugin (for the Vite SSR build, where addPlugin mode:'server' plugins are bundled)
156117 addVitePlugin ( {
157118 name : 'sentry-nuxt-pages-data-virtual' ,
158- resolveId : id => ( id === pagesDataVirtualModuleId ? `\0${ pagesDataVirtualModuleId } ` : null ) ,
159- load : id =>
119+ resolveId : ( id : string ) => ( id === pagesDataVirtualModuleId ? `\0${ pagesDataVirtualModuleId } ` : null ) ,
120+ load : ( id : string ) =>
160121 id === `\0${ pagesDataVirtualModuleId } ` ? `export default ${ JSON . stringify ( pagesData , null , 2 ) } ;` : undefined ,
161122 } ) ;
162123 } else {
@@ -186,56 +147,5 @@ export default defineNuxtModule<ModuleOptions>({
186147 tsConfig . include . push ( relativePath ) ;
187148 }
188149 } ) ;
189-
190- nuxt . hooks . hook ( 'nitro:init' , nitro => {
191- if ( nuxt . options ?. _prepare ) {
192- return ;
193- }
194-
195- if ( serverConfigFile ) {
196- addMiddlewareInstrumentation ( nitro , isNitroV3 ) ;
197-
198- if ( ! usesDeprecatedInjectMode ) {
199- addServerConfigShimWithWarning ( nitro ) ;
200-
201- if ( moduleOptions . debug ) {
202- consoleSandbox ( ( ) => {
203- // eslint-disable-next-line no-console
204- console . log (
205- `[Sentry] Bundled \`${ serverConfigFile } \` into the Nitro server build. The SDK initializes itself at server startup — no \`node --import\` preload needed.` ,
206- ) ;
207- } ) ;
208- }
209- } else {
210- consoleSandbox ( ( ) => {
211- // eslint-disable-next-line no-console
212- console . warn (
213- `[Sentry] \`autoInjectServerSentry: '${ moduleOptions . autoInjectServerSentry } '\` is deprecated and will be removed in a future major version. The Sentry server config is bundled into the Nitro server build by default now. Remove the option to use the default behavior.` ,
214- ) ;
215- } ) ;
216-
217- if ( moduleOptions . autoInjectServerSentry === 'top-level-import' ) {
218- // Nitro 3 (in Nuxt 5) is not bundled in dev mode, so there is no build to emit into.
219- if ( ! ( isNitroV3 && nitro . options . dev ) ) {
220- addServerConfigToBuild ( moduleOptions , nitro , serverConfigFile ) ;
221- }
222- addSentryTopImport ( moduleOptions , nitro ) ;
223- }
224-
225- if ( moduleOptions . autoInjectServerSentry === 'experimental_dynamic-import' ) {
226- addDynamicImportEntryFileWrapper ( nitro , serverConfigFile , moduleOptions ) ;
227-
228- if ( moduleOptions . debug ) {
229- consoleSandbox ( ( ) => {
230- // eslint-disable-next-line no-console
231- console . log (
232- '[Sentry] Wrapping the server entry file with a dynamic `import()`, so Sentry can be preloaded before the server initializes.' ,
233- ) ;
234- } ) ;
235- }
236- }
237- }
238- }
239- } ) ;
240150 } ,
241151} ) ;
0 commit comments