|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { sentryVitePlugin } from '@sentry/vite-plugin'; |
| 3 | +import type { AstroIntegration } from 'astro'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as path from 'path'; |
| 6 | + |
| 7 | +import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets'; |
| 8 | +import type { SentryOptions } from './types'; |
| 9 | + |
| 10 | +const PKG_NAME = '@sentry/astro'; |
| 11 | + |
| 12 | +export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { |
| 13 | + return { |
| 14 | + name: PKG_NAME, |
| 15 | + hooks: { |
| 16 | + 'astro:config:setup': async ({ updateConfig, injectScript }) => { |
| 17 | + // The third param here enables loading of all env vars, regardless of prefix |
| 18 | + // see: https://main.vitejs.dev/config/#using-environment-variables-in-config |
| 19 | + |
| 20 | + // TODO: Ideally, we want to load the environment with vite like this: |
| 21 | + // const env = loadEnv('production', process.cwd(), ''); |
| 22 | + // However, this currently throws a build error. |
| 23 | + // Will revisit this later. |
| 24 | + const env = process.env; |
| 25 | + |
| 26 | + const uploadOptions = options.sourceMapsUploadOptions || {}; |
| 27 | + |
| 28 | + const shouldUploadSourcemaps = uploadOptions?.enabled ?? true; |
| 29 | + const authToken = uploadOptions.authToken || env.SENTRY_AUTH_TOKEN; |
| 30 | + |
| 31 | + if (shouldUploadSourcemaps && authToken) { |
| 32 | + updateConfig({ |
| 33 | + vite: { |
| 34 | + build: { |
| 35 | + sourcemap: true, |
| 36 | + }, |
| 37 | + plugins: [ |
| 38 | + sentryVitePlugin({ |
| 39 | + org: uploadOptions.org ?? env.SENTRY_ORG, |
| 40 | + project: uploadOptions.project ?? env.SENTRY_PROJECT, |
| 41 | + authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN, |
| 42 | + telemetry: uploadOptions.telemetry ?? true, |
| 43 | + }), |
| 44 | + ], |
| 45 | + }, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const pathToClientInit = options.clientInitPath |
| 50 | + ? path.resolve(options.clientInitPath) |
| 51 | + : findDefaultSdkInitFile('client'); |
| 52 | + const pathToServerInit = options.serverInitPath |
| 53 | + ? path.resolve(options.serverInitPath) |
| 54 | + : findDefaultSdkInitFile('server'); |
| 55 | + |
| 56 | + if (pathToClientInit) { |
| 57 | + options.debug && console.log(`[sentry-astro] Using ${pathToClientInit} for client init.`); |
| 58 | + injectScript('page', buildSdkInitFileImportSnippet(pathToClientInit)); |
| 59 | + } else { |
| 60 | + options.debug && console.log('[sentry-astro] Using default client init.'); |
| 61 | + injectScript('page', buildClientSnippet(options || {})); |
| 62 | + } |
| 63 | + |
| 64 | + if (pathToServerInit) { |
| 65 | + options.debug && console.log(`[sentry-astro] Using ${pathToServerInit} for server init.`); |
| 66 | + injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit)); |
| 67 | + } else { |
| 68 | + options.debug && console.log('[sentry-astro] Using default server init.'); |
| 69 | + injectScript('page-ssr', buildServerSnippet(options || {})); |
| 70 | + } |
| 71 | + }, |
| 72 | + }, |
| 73 | + }; |
| 74 | +}; |
| 75 | + |
| 76 | +function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { |
| 77 | + const fileExtensions = ['ts', 'js', 'tsx', 'jsx', 'mjs', 'cjs', 'mts']; |
| 78 | + return fileExtensions |
| 79 | + .map(ext => path.resolve(path.join(process.cwd(), `sentry.${type}.config.${ext}`))) |
| 80 | + .find(filename => fs.existsSync(filename)); |
| 81 | +} |
0 commit comments