diff --git a/dev-packages/e2e-tests/test-applications/nuxt-3/package.json b/dev-packages/e2e-tests/test-applications/nuxt-3/package.json index 93471fff7aab..9da11f778a6b 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-3/package.json +++ b/dev-packages/e2e-tests/test-applications/nuxt-3/package.json @@ -6,7 +6,7 @@ "build": "nuxt build", "dev": "nuxt dev", "generate": "nuxt generate", - "preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview", + "preview": "NODE_OPTIONS='--import ./server/instrument-sentry.mjs' nuxt preview", "clean": "npx nuxi cleanup", "test": "playwright test", "test:build": "pnpm install && npx playwright install && pnpm build", diff --git a/dev-packages/e2e-tests/test-applications/nuxt-3/public/instrument.server.mjs b/dev-packages/e2e-tests/test-applications/nuxt-3/sentry.server.config.ts similarity index 100% rename from dev-packages/e2e-tests/test-applications/nuxt-3/public/instrument.server.mjs rename to dev-packages/e2e-tests/test-applications/nuxt-3/sentry.server.config.ts diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 5d529c99330c..9ded800c3c54 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'; import type { SentryNuxtModuleOptions } from './common/types'; +import { addServerConfig } from './vite/addServerConfig'; import { setupSourceMaps } from './vite/sourceMaps'; export type ModuleOptions = SentryNuxtModuleOptions; @@ -62,6 +63,9 @@ export default defineNuxtModule({ if (clientConfigFile || serverConfigFile) { setupSourceMaps(moduleOptions, nuxt); } + if (serverConfigFile) { + addServerConfig(moduleOptions, nuxt, serverConfigFile); + } }, }); diff --git a/packages/nuxt/src/vite/addServerConfig.ts b/packages/nuxt/src/vite/addServerConfig.ts new file mode 100644 index 000000000000..6e670ada3fd1 --- /dev/null +++ b/packages/nuxt/src/vite/addServerConfig.ts @@ -0,0 +1,50 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { createResolver } from '@nuxt/kit'; +import type { Nuxt } from '@nuxt/schema'; +import type { SentryNuxtModuleOptions } from '../common/types'; + +/** + * Adds the `server.config.ts` file to the `.output` directory to be able to reference this file in the node --import option. + * 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file). + * 2. Copying the file to the `.output` directory after the build process is finished. + */ +export function addServerConfig(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt, serverConfigFile: string): void { + nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { + if ( + typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' && + 'server' in viteInlineConfig.build.rollupOptions.input + ) { + // Create a rollup entry for the server config to add it to the build + (viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] = + createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`); + } + + /** + * When the build process is finished, copy the `sentry.server.config` file to the `.output` directory. + * This is necessary because we need to reference this file path in the node --import option. + */ + nuxt.hook('close', async () => { + const source = path.resolve('.nuxt/dist/server/instrument-sentry.mjs'); + const destination = path.resolve('.output/server/instrument-sentry.mjs'); + + try { + await fs.promises.access(source, fs.constants.F_OK); + await fs.promises.copyFile(source, destination); + + if (moduleOptions.debug) { + // eslint-disable-next-line no-console + console.log('[Sentry] Successfully added the `sentry.server.config` file to the `.output` directory'); + } + } catch (error) { + if (moduleOptions.debug) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] An error occurred when trying to add the `sentry.server.config` file to the `.output` directory', + error, + ); + } + } + }); + }); +}