|
| 1 | +import type { Options } from '@sentry/bundler-plugin-core'; |
| 2 | +import { createSentryBuildPluginManager } from '@sentry/bundler-plugin-core'; |
| 3 | +import type { Nitro } from 'nitro/types'; |
| 4 | +import type { SentryNitroOptions } from './config'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Registers a `compiled` hook to upload source maps after the build completes. |
| 8 | + */ |
| 9 | +export function setupSourceMaps(nitro: Nitro, options?: SentryNitroOptions): void { |
| 10 | + // The `compiled` hook fires on EVERY rebuild during `nitro dev` watch mode. |
| 11 | + // nitro.options.dev is reliably set by the time module setup runs. |
| 12 | + if (nitro.options.dev) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + // Respect user's explicit disable |
| 17 | + if (options?.sourcemaps?.disable === true || options?.disable === true) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + nitro.hooks.hook('compiled', async (_nitro: Nitro) => { |
| 22 | + await handleSourceMapUpload(_nitro, options); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Handles the actual source map upload after the build completes. |
| 28 | + */ |
| 29 | +async function handleSourceMapUpload(nitro: Nitro, options?: SentryNitroOptions): Promise<void> { |
| 30 | + const outputDir = nitro.options.output.serverDir; |
| 31 | + const pluginOptions = getPluginOptions(options); |
| 32 | + |
| 33 | + const sentryBuildPluginManager = createSentryBuildPluginManager(pluginOptions, { |
| 34 | + buildTool: 'nitro', |
| 35 | + loggerPrefix: '[@sentry/nitro]', |
| 36 | + }); |
| 37 | + |
| 38 | + await sentryBuildPluginManager.telemetry.emitBundlerPluginExecutionSignal(); |
| 39 | + await sentryBuildPluginManager.createRelease(); |
| 40 | + |
| 41 | + if (options?.sourcemaps?.disable !== 'disable-upload') { |
| 42 | + await sentryBuildPluginManager.injectDebugIds([outputDir]); |
| 43 | + await sentryBuildPluginManager.uploadSourcemaps([outputDir], { |
| 44 | + // We don't prepare the artifacts because we injected debug IDs manually before |
| 45 | + prepareArtifacts: false, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + await sentryBuildPluginManager.deleteArtifacts(); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Normalizes the beginning of a path from e.g. ../../../ to ./ |
| 54 | + */ |
| 55 | +function normalizePath(path: string): string { |
| 56 | + return path.replace(/^(\.\.\/)+/, './'); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Builds the plugin options for `createSentryBuildPluginManager` from the Sentry Nitro options. |
| 61 | + * |
| 62 | + * Only exported for testing purposes. |
| 63 | + */ |
| 64 | +export function getPluginOptions(options?: SentryNitroOptions): Options { |
| 65 | + return { |
| 66 | + org: options?.org ?? process.env.SENTRY_ORG, |
| 67 | + project: options?.project ?? process.env.SENTRY_PROJECT, |
| 68 | + authToken: options?.authToken ?? process.env.SENTRY_AUTH_TOKEN, |
| 69 | + url: options?.url ?? process.env.SENTRY_URL, |
| 70 | + headers: options?.headers, |
| 71 | + telemetry: options?.telemetry ?? true, |
| 72 | + debug: options?.debug ?? false, |
| 73 | + silent: options?.silent ?? false, |
| 74 | + errorHandler: options?.errorHandler, |
| 75 | + sourcemaps: { |
| 76 | + disable: options?.sourcemaps?.disable, |
| 77 | + assets: options?.sourcemaps?.assets, |
| 78 | + ignore: options?.sourcemaps?.ignore, |
| 79 | + filesToDeleteAfterUpload: options?.sourcemaps?.filesToDeleteAfterUpload ?? ['**/*.map'], |
| 80 | + rewriteSources: (source: string) => normalizePath(source), |
| 81 | + }, |
| 82 | + release: options?.release, |
| 83 | + bundleSizeOptimizations: options?.bundleSizeOptimizations, |
| 84 | + _metaOptions: { |
| 85 | + telemetry: { |
| 86 | + metaFramework: 'nitro', |
| 87 | + }, |
| 88 | + ...options?._metaOptions, |
| 89 | + }, |
| 90 | + }; |
| 91 | +} |
0 commit comments