-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): Add server config to root folder
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
}); | ||
}); | ||
} |