Skip to content

Commit

Permalink
fix(nuxt): Use correct server output file path
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d committed Sep 19, 2024
1 parent 1e9a1a3 commit 7813a0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
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.`,
);
});
}
}
}
}
});
},
});
26 changes: 19 additions & 7 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);
nitro.hooks.hook('close', async () => {
const rootDirResolver = createResolver(nitro.options.rootDir);
const serverDirResolver = createResolver(nitro.options.output.serverDir);
const source = rootDirResolver.resolve('.nuxt/dist/server/sentry.server.config.mjs');
const destination = rootDirResolver.resolve('.output/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

0 comments on commit 7813a0a

Please sign in to comment.