Skip to content

Commit fef86cf

Browse files
committed
fix(nuxt): Add Nitro Rollup plugin to inject Sentry server config
1 parent 16e40f4 commit fef86cf

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

packages/nuxt/src/module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default defineNuxtModule<ModuleOptions>({
113113
});
114114

115115
if (moduleOptions.autoInjectServerSentry !== 'experimental_dynamic-import') {
116-
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);
116+
addServerConfigToBuild(moduleOptions, nitro, serverConfigFile);
117117

118118
if (moduleOptions.debug) {
119119
const serverDirResolver = createResolver(nitro.options.output.serverDir);

packages/nuxt/src/vite/addServerConfig.ts

+49-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as fs from 'fs';
22
import { createResolver } from '@nuxt/kit';
3-
import type { Nuxt } from '@nuxt/schema';
4-
import { consoleSandbox } from '@sentry/core';
3+
import { consoleSandbox, logger } from '@sentry/core';
54
import type { Nitro } from 'nitropack';
65
import type { InputPluginOption } from 'rollup';
76
import type { SentryNuxtModuleOptions } from '../common/types';
@@ -15,6 +14,7 @@ import {
1514
getFilenameFromNodeStartCommand,
1615
removeSentryQueryFromPath,
1716
} from './utils';
17+
import { existsSync } from 'node:fs';
1818

1919
const SERVER_CONFIG_FILENAME = 'sentry.server.config';
2020

@@ -26,19 +26,18 @@ const SERVER_CONFIG_FILENAME = 'sentry.server.config';
2626
*/
2727
export 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

Comments
 (0)