11import * as fs from 'fs' ;
22import { createResolver } from '@nuxt/kit' ;
3- import type { Nuxt } from '@nuxt/schema' ;
4- import { consoleSandbox } from '@sentry/core' ;
3+ import { consoleSandbox , logger } from '@sentry/core' ;
54import type { Nitro } from 'nitropack' ;
65import type { InputPluginOption } from 'rollup' ;
76import type { SentryNuxtModuleOptions } from '../common/types' ;
@@ -15,6 +14,7 @@ import {
1514 getFilenameFromNodeStartCommand ,
1615 removeSentryQueryFromPath ,
1716} from './utils' ;
17+ import { existsSync } from 'node:fs' ;
1818
1919const SERVER_CONFIG_FILENAME = 'sentry.server.config' ;
2020
@@ -26,19 +26,18 @@ const SERVER_CONFIG_FILENAME = 'sentry.server.config';
2626 */
2727export function addServerConfigToBuild (
2828 moduleOptions : SentryNuxtModuleOptions ,
29- nuxt : Nuxt ,
3029 nitro : Nitro ,
3130 serverConfigFile : string ,
3231) : void {
33- nuxt . hook ( 'vite:extendConfig' , async ( viteInlineConfig , _env ) => {
34- if (
35- typeof viteInlineConfig ?. build ?. rollupOptions ?. input === 'object' &&
36- 'server' in viteInlineConfig . build . rollupOptions . input
37- ) {
38- // Create a rollup entry for the server config to add it as `sentry.server.config.mjs` to the build
39- ( viteInlineConfig . build . rollupOptions . input as { [ entryName : string ] : string } ) [ SERVER_CONFIG_FILENAME ] =
40- createResolver ( nuxt . options . srcDir ) . resolve ( `/${ serverConfigFile } ` ) ;
32+ nitro . hooks . hook ( 'rollup:before' , ( nitro , rollupConfig ) => {
33+ if ( rollupConfig ?. plugins === null || rollupConfig ?. plugins === undefined ) {
34+ rollupConfig . plugins = [ ] ;
35+ } else if ( ! Array . isArray ( rollupConfig . plugins ) ) {
36+ // `rollupConfig.plugins` can be a single plugin, so we want to put it into an array so that we can push our own plugin
37+ rollupConfig . plugins = [ rollupConfig . plugins ] ;
4138 }
39+
40+ rollupConfig . plugins . push ( injectServerConfigPlugin ( nitro , serverConfigFile , moduleOptions . debug ) ) ;
4241 } ) ;
4342
4443 /**
@@ -158,6 +157,45 @@ export function addDynamicImportEntryFileWrapper(
158157 ) ;
159158}
160159
160+ /**
161+ * Rollup plugin to include the Sentry server configuration file to the server build output.
162+ */
163+ function injectServerConfigPlugin ( nitro : Nitro , serverConfigFile : string , debug ?: boolean ) : InputPluginOption {
164+ const filePrefix = '\0virtual:sentry-server-config:' ;
165+
166+ return {
167+ name : 'rollup-plugin-inject-sentry-server-config' ,
168+
169+ buildStart ( ) {
170+ const configPath = createResolver ( nitro . options . srcDir ) . resolve ( `/${ serverConfigFile } ` ) ;
171+
172+ if ( ! existsSync ( configPath ) ) {
173+ if ( debug ) {
174+ logger . log ( `[Sentry] Sentry server config file not found: ${ configPath } ` ) ;
175+ }
176+ return ;
177+ }
178+
179+ // Emitting a file adds it to the build output (Rollup is aware of the file, and we can later return the code in resolveId)
180+ this . emitFile ( {
181+ type : 'chunk' ,
182+ id : `${ filePrefix } ${ serverConfigFile } ` ,
183+ fileName : `${ SERVER_CONFIG_FILENAME } .mjs` ,
184+ } ) ;
185+ } ,
186+
187+ resolveId ( source ) {
188+ if ( source . startsWith ( filePrefix ) ) {
189+ const originalFilePath = source . replace ( filePrefix , '' ) ;
190+ const configPath = createResolver ( nitro . options . rootDir ) . resolve ( `/${ originalFilePath } ` ) ;
191+
192+ return { id : configPath } ;
193+ }
194+ return null ;
195+ } ,
196+ } ;
197+ }
198+
161199/**
162200 * A Rollup plugin which wraps the server entry with a dynamic `import()`. This makes it possible to initialize Sentry first
163201 * by using a regular `import` and load the server after that.
0 commit comments