Skip to content

Commit

Permalink
feat(nuxt): Add server config to root folder
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d committed Sep 5, 2024
1 parent a69da1b commit ea95e37
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview",
"preview": "NODE_OPTIONS='--import ./server/instrument-sentry.mjs' nuxt preview",
"clean": "npx nuxi cleanup",
"test": "playwright test",
"test:build": "pnpm install && npx playwright install && pnpm build",
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import type { SentryNuxtModuleOptions } from './common/types';
import { addServerConfig } from './vite/addServerConfig';
import { setupSourceMaps } from './vite/sourceMaps';

export type ModuleOptions = SentryNuxtModuleOptions;
Expand Down Expand Up @@ -62,6 +63,9 @@ export default defineNuxtModule<ModuleOptions>({
if (clientConfigFile || serverConfigFile) {
setupSourceMaps(moduleOptions, nuxt);
}
if (serverConfigFile) {
addServerConfig(moduleOptions, nuxt, serverConfigFile);
}
},
});

Expand Down
50 changes: 50 additions & 0 deletions packages/nuxt/src/vite/addServerConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as fs from 'fs';
import * as path from 'path';
import { createResolver } from '@nuxt/kit';
import type { Nuxt } from '@nuxt/schema';
import type { SentryNuxtModuleOptions } from '../common/types';

/**
* Adds the `server.config.ts` file to the `.output` directory to be able to reference this file in the node --import option.
* 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file).
* 2. Copying the file to the `.output` directory after the build process is finished.
*/
export function addServerConfig(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt, serverConfigFile: string): void {
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => {
if (
typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' &&
'server' in viteInlineConfig.build.rollupOptions.input
) {
// Create a rollup entry for the server config to add it to the build
(viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] =
createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`);
}

/**
* 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 source = path.resolve('.nuxt/dist/server/instrument-sentry.mjs');
const destination = path.resolve('.output/server/instrument-sentry.mjs');

try {
await fs.promises.access(source, fs.constants.F_OK);
await fs.promises.copyFile(source, destination);

if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.log('[Sentry] Successfully added the `sentry.server.config` file to the `.output` directory');
}
} catch (error) {
if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.warn(
'[Sentry] An error occurred when trying to add the `sentry.server.config` file to the `.output` directory',
error,
);
}
}
});
});
}

0 comments on commit ea95e37

Please sign in to comment.