|
| 1 | +import { existsSync } from 'node:fs'; |
| 2 | +import type { Nuxt } from '@nuxt/schema'; |
| 3 | +import type { createSentryBuildPluginManager as createSentryBuildPluginManagerType } from '@sentry/bundler-plugin-core'; |
| 4 | +import * as path from 'path'; |
| 5 | +import type { SentryNuxtModuleOptions } from '../common/types'; |
| 6 | +import { getPluginOptions } from './sourceMaps'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A build-end hook that handles Sentry release creation and source map uploads. |
| 10 | + * It creates a new Sentry release if configured, uploads source maps to Sentry, |
| 11 | + * and optionally deletes the source map files after upload. |
| 12 | + * |
| 13 | + * This runs after both Vite (Nuxt) and Rollup (Nitro) builds complete, ensuring |
| 14 | + * debug IDs are injected and source maps uploaded only once. |
| 15 | + */ |
| 16 | +// eslint-disable-next-line complexity |
| 17 | +export async function handleBuildDoneHook(sentryModuleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): Promise<void> { |
| 18 | + const debug = sentryModuleOptions.debug ?? false; |
| 19 | + if (debug) { |
| 20 | + // eslint-disable-next-line no-console |
| 21 | + console.log('[Sentry] Running build:done hook to upload source maps.'); |
| 22 | + } |
| 23 | + |
| 24 | + // eslint-disable-next-line deprecation/deprecation |
| 25 | + const sourceMapsUploadOptions = sentryModuleOptions.sourceMapsUploadOptions || {}; |
| 26 | + |
| 27 | + const sourceMapsEnabled = |
| 28 | + sentryModuleOptions.sourcemaps?.disable === true |
| 29 | + ? false |
| 30 | + : sentryModuleOptions.sourcemaps?.disable === false |
| 31 | + ? true |
| 32 | + : // eslint-disable-next-line deprecation/deprecation |
| 33 | + (sourceMapsUploadOptions.enabled ?? true); |
| 34 | + |
| 35 | + if (!sourceMapsEnabled) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + let createSentryBuildPluginManager: typeof createSentryBuildPluginManagerType | undefined; |
| 40 | + try { |
| 41 | + const bundlerPluginCore = await import('@sentry/bundler-plugin-core'); |
| 42 | + createSentryBuildPluginManager = bundlerPluginCore.createSentryBuildPluginManager; |
| 43 | + } catch (error) { |
| 44 | + // eslint-disable-next-line no-console |
| 45 | + debug && console.warn('[Sentry] Could not load build manager package. Will not upload source maps.', error); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (!createSentryBuildPluginManager) { |
| 50 | + // eslint-disable-next-line no-console |
| 51 | + debug && console.warn('[Sentry] Could not find createSentryBuildPluginManager in bundler plugin core.'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const outputDir = nuxt.options.nitro?.output?.dir || path.join(nuxt.options.rootDir, '.output'); |
| 56 | + |
| 57 | + if (!existsSync(outputDir)) { |
| 58 | + // eslint-disable-next-line no-console |
| 59 | + debug && console.warn(`[Sentry] Output directory does not exist yet: ${outputDir}. Skipping source map upload.`); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const options = getPluginOptions(sentryModuleOptions, undefined); |
| 64 | + |
| 65 | + const existingIgnore = options.sourcemaps?.ignore || []; |
| 66 | + const ignorePatterns = Array.isArray(existingIgnore) ? existingIgnore : [existingIgnore]; |
| 67 | + |
| 68 | + // node_modules source maps are ignored |
| 69 | + const nodeModulesPatterns = ['**/node_modules/**', '**/node_modules/**/*.map']; |
| 70 | + const hasNodeModulesIgnore = ignorePatterns.some( |
| 71 | + pattern => typeof pattern === 'string' && pattern.includes('node_modules'), |
| 72 | + ); |
| 73 | + |
| 74 | + if (!hasNodeModulesIgnore) { |
| 75 | + ignorePatterns.push(...nodeModulesPatterns); |
| 76 | + } |
| 77 | + |
| 78 | + options.sourcemaps = { |
| 79 | + ...options.sourcemaps, |
| 80 | + ignore: ignorePatterns.length > 0 ? ignorePatterns : undefined, |
| 81 | + }; |
| 82 | + |
| 83 | + if (debug && ignorePatterns.length > 0) { |
| 84 | + // eslint-disable-next-line no-console |
| 85 | + console.log(`[Sentry] Excluding patterns from source map upload: ${ignorePatterns.join(', ')}`); |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + const sentryBuildPluginManager = createSentryBuildPluginManager(options, { |
| 90 | + buildTool: 'nuxt', |
| 91 | + loggerPrefix: '[Sentry Nuxt Module]', |
| 92 | + }); |
| 93 | + |
| 94 | + await sentryBuildPluginManager.telemetry.emitBundlerPluginExecutionSignal(); |
| 95 | + await sentryBuildPluginManager.createRelease(); |
| 96 | + await sentryBuildPluginManager.injectDebugIds([outputDir]); |
| 97 | + await sentryBuildPluginManager.uploadSourcemaps([outputDir], { |
| 98 | + prepareArtifacts: false, |
| 99 | + }); |
| 100 | + |
| 101 | + await sentryBuildPluginManager.deleteArtifacts(); |
| 102 | + |
| 103 | + // eslint-disable-next-line no-console |
| 104 | + debug && console.log('[Sentry] Successfully uploaded source maps.'); |
| 105 | + } catch (error) { |
| 106 | + // eslint-disable-next-line no-console |
| 107 | + console.error('[Sentry] Error during source map upload:', error); |
| 108 | + } |
| 109 | +} |
0 commit comments