Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ export default defineNuxtModule<ModuleOptions>({
setupSourceMaps(moduleOptions, nuxt);
}

if (serverConfigFile && serverConfigFile.includes('.server.config')) {
addServerConfigToBuild(moduleOptions, nuxt, serverConfigFile);
nuxt.hooks.hook('nitro:init', nitro => {
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);

if (moduleOptions.experimental_basicServerTracing) {
addSentryTopImport(moduleOptions, nuxt);
} else {
if (moduleOptions.debug) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches the Sentry server config file in your \`.output\` folder and has a \`.mjs\` extension.`,
);
});
if (moduleOptions.experimental_basicServerTracing) {
addSentryTopImport(moduleOptions, nitro);
} else {
if (moduleOptions.debug) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches the Sentry server config file in your \`.output\` folder and has a \`.mjs\` extension.`,
);
});
}
}
}
}
});
},
});
28 changes: 20 additions & 8 deletions packages/nuxt/src/vite/addServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import { createResolver } from '@nuxt/kit';
import type { Nuxt } from '@nuxt/schema';
import { consoleSandbox } from '@sentry/utils';
import type { Nitro } from 'nitropack';
import type { SentryNuxtModuleOptions } from '../common/types';

/**
Expand All @@ -13,6 +14,7 @@ import type { SentryNuxtModuleOptions } from '../common/types';
export function addServerConfigToBuild(
moduleOptions: SentryNuxtModuleOptions,
nuxt: Nuxt,
nitro: Nitro,
serverConfigFile: string,
): void {
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => {
Expand All @@ -29,10 +31,11 @@ export function addServerConfigToBuild(
* 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 rootDirResolver = createResolver(nuxt.options.rootDir);
const source = rootDirResolver.resolve('.nuxt/dist/server/sentry.server.config.mjs');
const destination = rootDirResolver.resolve('.output/server/sentry.server.config.mjs');
nitro.hooks.hook('close', async () => {
const buildDirResolver = createResolver(nitro.options.buildDir);
const serverDirResolver = createResolver(nitro.options.output.serverDir);
const source = buildDirResolver.resolve('dist/server/sentry.server.config.mjs');
const destination = serverDirResolver.resolve('sentry.server.config.mjs');

try {
await fs.promises.access(source, fs.constants.F_OK);
Expand Down Expand Up @@ -66,10 +69,19 @@ export function addServerConfigToBuild(
* This is necessary for environments where modifying the node option `--import` is not possible.
* However, only limited tracing instrumentation is supported when doing this.
*/
export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void {
nuxt.hook('close', async () => {
const rootDirResolver = createResolver(nuxt.options.rootDir);
const entryFilePath = rootDirResolver.resolve('.output/server/index.mjs');
export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nitro: Nitro): void {
nitro.hooks.hook('close', () => {
// other presets ('node-server' or 'vercel') have an index.mjs
const presetsWithServerFile = ['netlify'];
const entryFileName =
typeof nitro.options.rollupConfig?.output.entryFileNames === 'string'
? nitro.options.rollupConfig?.output.entryFileNames
: presetsWithServerFile.includes(nitro.options.preset)
? 'server.mjs'
: 'index.mjs';

const serverDirResolver = createResolver(nitro.options.output.serverDir);
const entryFilePath = serverDirResolver.resolve(entryFileName);

try {
fs.readFile(entryFilePath, 'utf8', (err, data) => {
Expand Down