Skip to content

Commit 6cd63cb

Browse files
committed
feat(nuxt): Add server config to root folder
1 parent ec7d9e3 commit 6cd63cb

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

dev-packages/e2e-tests/test-applications/nuxt-3/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"build": "nuxt build",
77
"dev": "nuxt dev",
88
"generate": "nuxt generate",
9-
"preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview",
9+
"preview": "NODE_OPTIONS='--import ./server/instrument-sentry.mjs' nuxt preview",
1010
"clean": "npx nuxi cleanup",
1111
"test": "playwright test",
1212
"test:build": "pnpm install && npx playwright install && pnpm build",

packages/nuxt/src/module.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
44
import type { SentryNuxtModuleOptions } from './common/types';
5+
import { addServerConfig } from './vite/addServerConfig';
56
import { setupSourceMaps } from './vite/sourceMaps';
67

78
export type ModuleOptions = SentryNuxtModuleOptions;
@@ -62,6 +63,9 @@ export default defineNuxtModule<ModuleOptions>({
6263
if (clientConfigFile || serverConfigFile) {
6364
setupSourceMaps(moduleOptions, nuxt);
6465
}
66+
if (serverConfigFile) {
67+
addServerConfig(moduleOptions, nuxt, serverConfigFile);
68+
}
6569
},
6670
});
6771

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { createResolver } from '@nuxt/kit';
4+
import type { Nuxt } from '@nuxt/schema';
5+
import type { SentryNuxtModuleOptions } from '../common/types';
6+
7+
/**
8+
* Adds the `server.config.ts` file to the `.output` directory to be able to reference this file in the node --import option.
9+
* 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file).
10+
* 2. Copying the file to the `.output` directory after the build process is finished.
11+
*/
12+
export function addServerConfig(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt, serverConfigFile: string): void {
13+
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => {
14+
if (
15+
typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' &&
16+
'server' in viteInlineConfig.build.rollupOptions.input
17+
) {
18+
// Create a rollup entry for the server config to add it to the build
19+
(viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] =
20+
createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`);
21+
}
22+
23+
/**
24+
* When the build process is finished, copy the `sentry.server.config` file to the `.output` directory.
25+
* This is necessary because we need to reference this file path in the node --import option.
26+
*/
27+
nuxt.hook('close', async () => {
28+
const source = path.resolve('.nuxt/dist/server/instrument-sentry.mjs');
29+
const destination = path.resolve('.output/server/instrument-sentry.mjs');
30+
31+
try {
32+
await fs.promises.access(source, fs.constants.F_OK);
33+
await fs.promises.copyFile(source, destination);
34+
35+
if (moduleOptions.debug) {
36+
// eslint-disable-next-line no-console
37+
console.log('[Sentry] Successfully added the `sentry.server.config` file to the `.output` directory');
38+
}
39+
} catch (error) {
40+
if (moduleOptions.debug) {
41+
// eslint-disable-next-line no-console
42+
console.warn(
43+
'[Sentry] An error occurred when trying to add the `sentry.server.config` file to the `.output` directory',
44+
error,
45+
);
46+
}
47+
}
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)