|
| 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