Skip to content

Commit e8ee328

Browse files
committed
fix(nuxt): Use correct server output file path
1 parent 1e9a1a3 commit e8ee328

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

packages/nuxt/src/module.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,23 @@ export default defineNuxtModule<ModuleOptions>({
6464
setupSourceMaps(moduleOptions, nuxt);
6565
}
6666

67-
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
68-
addServerConfigToBuild(moduleOptions, nuxt, serverConfigFile);
67+
nuxt.hooks.hook('nitro:init', nitro => {
68+
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
69+
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);
6970

70-
if (moduleOptions.experimental_basicServerTracing) {
71-
addSentryTopImport(moduleOptions, nuxt);
72-
} else {
73-
if (moduleOptions.debug) {
74-
consoleSandbox(() => {
75-
// eslint-disable-next-line no-console
76-
console.log(
77-
`[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.`,
78-
);
79-
});
71+
if (moduleOptions.experimental_basicServerTracing) {
72+
addSentryTopImport(moduleOptions, nitro);
73+
} else {
74+
if (moduleOptions.debug) {
75+
consoleSandbox(() => {
76+
// eslint-disable-next-line no-console
77+
console.log(
78+
`[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.`,
79+
);
80+
});
81+
}
8082
}
8183
}
82-
}
84+
});
8385
},
8486
});

packages/nuxt/src/vite/addServerConfig.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs';
22
import { createResolver } from '@nuxt/kit';
33
import type { Nuxt } from '@nuxt/schema';
44
import { consoleSandbox } from '@sentry/utils';
5+
import type { Nitro } from 'nitropack';
56
import type { SentryNuxtModuleOptions } from '../common/types';
67

78
/**
@@ -13,6 +14,7 @@ import type { SentryNuxtModuleOptions } from '../common/types';
1314
export function addServerConfigToBuild(
1415
moduleOptions: SentryNuxtModuleOptions,
1516
nuxt: Nuxt,
17+
nitro: Nitro,
1618
serverConfigFile: string,
1719
): void {
1820
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => {
@@ -29,10 +31,11 @@ export function addServerConfigToBuild(
2931
* When the build process is finished, copy the `sentry.server.config` file to the `.output` directory.
3032
* This is necessary because we need to reference this file path in the node --import option.
3133
*/
32-
nuxt.hook('close', async () => {
33-
const rootDirResolver = createResolver(nuxt.options.rootDir);
34+
nitro.hooks.hook('close', async () => {
35+
const rootDirResolver = createResolver(nitro.options.rootDir);
36+
const serverDirResolver = createResolver(nitro.options.output.serverDir);
3437
const source = rootDirResolver.resolve('.nuxt/dist/server/sentry.server.config.mjs');
35-
const destination = rootDirResolver.resolve('.output/server/sentry.server.config.mjs');
38+
const destination = serverDirResolver.resolve('sentry.server.config.mjs');
3639

3740
try {
3841
await fs.promises.access(source, fs.constants.F_OK);
@@ -66,10 +69,19 @@ export function addServerConfigToBuild(
6669
* This is necessary for environments where modifying the node option `--import` is not possible.
6770
* However, only limited tracing instrumentation is supported when doing this.
6871
*/
69-
export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void {
70-
nuxt.hook('close', async () => {
71-
const rootDirResolver = createResolver(nuxt.options.rootDir);
72-
const entryFilePath = rootDirResolver.resolve('.output/server/index.mjs');
72+
export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nitro: Nitro): void {
73+
nitro.hooks.hook('close', () => {
74+
// other presets ('node-server' or 'vercel') have an index.mjs
75+
const presetsWithServerFile = ['netlify'];
76+
const entryFileName =
77+
typeof nitro.options.rollupConfig?.output.entryFileNames === 'string'
78+
? nitro.options.rollupConfig?.output.entryFileNames
79+
: presetsWithServerFile.includes(nitro.options.preset)
80+
? 'server.mjs'
81+
: 'index.mjs';
82+
83+
const serverDirResolver = createResolver(nitro.options.output.serverDir);
84+
const entryFilePath = serverDirResolver.resolve(entryFileName);
7385

7486
try {
7587
fs.readFile(entryFilePath, 'utf8', (err, data) => {

packages/nuxt/src/vite/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ export function findDefaultSdkInitFile(type: 'server' | 'client'): string | unde
2626

2727
return filePath ? path.basename(filePath) : undefined;
2828
}
29+
30+
/**
31+
* Get the diff suffix part between two strings.
32+
*
33+
* Example: getStringDiff('abcdef', 'abc') => 'def'
34+
*/
35+
export function getStringSuffixDiff(longerStr: string, shorterStr: string): string {
36+
const commonPrefixLength = [...longerStr].findIndex((char, index) => char !== shorterStr[index]);
37+
return commonPrefixLength === -1 ? '' : longerStr.slice(commonPrefixLength);
38+
}

0 commit comments

Comments
 (0)