|
| 1 | +import type { Options as SentryBuildPluginOptions } from '@sentry/bundler-plugin-core'; |
| 2 | +import * as path from 'path'; |
| 3 | +import type { SentryBuildOptions } from './types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get Sentry Build Plugin options for the runAfterProductionCompile hook. |
| 7 | + */ |
| 8 | +export function getBuildPluginOptions({ |
| 9 | + sentryBuildOptions, |
| 10 | + releaseName, |
| 11 | + distDirAbsPath, |
| 12 | +}: { |
| 13 | + sentryBuildOptions: SentryBuildOptions; |
| 14 | + releaseName: string | undefined; |
| 15 | + distDirAbsPath: string; |
| 16 | +}): SentryBuildPluginOptions { |
| 17 | + const sourcemapUploadAssets: string[] = []; |
| 18 | + const sourcemapUploadIgnore: string[] = []; |
| 19 | + |
| 20 | + const filesToDeleteAfterUpload: string[] = []; |
| 21 | + |
| 22 | + // We need to convert paths to posix because Glob patterns use `\` to escape |
| 23 | + // glob characters. This clashes with Windows path separators. |
| 24 | + // See: https://www.npmjs.com/package/glob |
| 25 | + const normalizedDistDirAbsPath = distDirAbsPath.replace(/\\/g, '/'); |
| 26 | + |
| 27 | + sourcemapUploadAssets.push( |
| 28 | + path.posix.join(normalizedDistDirAbsPath, '**'), // Next.js build output |
| 29 | + ); |
| 30 | + if (sentryBuildOptions.sourcemaps?.deleteSourcemapsAfterUpload) { |
| 31 | + filesToDeleteAfterUpload.push( |
| 32 | + path.posix.join(normalizedDistDirAbsPath, '**', '*.js.map'), |
| 33 | + path.posix.join(normalizedDistDirAbsPath, '**', '*.mjs.map'), |
| 34 | + path.posix.join(normalizedDistDirAbsPath, '**', '*.cjs.map'), |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + return { |
| 39 | + authToken: sentryBuildOptions.authToken, |
| 40 | + headers: sentryBuildOptions.headers, |
| 41 | + org: sentryBuildOptions.org, |
| 42 | + project: sentryBuildOptions.project, |
| 43 | + telemetry: sentryBuildOptions.telemetry, |
| 44 | + debug: sentryBuildOptions.debug, |
| 45 | + errorHandler: sentryBuildOptions.errorHandler, |
| 46 | + reactComponentAnnotation: { |
| 47 | + ...sentryBuildOptions.reactComponentAnnotation, |
| 48 | + ...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.reactComponentAnnotation, |
| 49 | + }, |
| 50 | + silent: sentryBuildOptions.silent, |
| 51 | + url: sentryBuildOptions.sentryUrl, |
| 52 | + sourcemaps: { |
| 53 | + disable: sentryBuildOptions.sourcemaps?.disable, |
| 54 | + rewriteSources(source) { |
| 55 | + if (source.startsWith('webpack://_N_E/')) { |
| 56 | + return source.replace('webpack://_N_E/', ''); |
| 57 | + } else if (source.startsWith('webpack://')) { |
| 58 | + return source.replace('webpack://', ''); |
| 59 | + } else { |
| 60 | + return source; |
| 61 | + } |
| 62 | + }, |
| 63 | + assets: sentryBuildOptions.sourcemaps?.assets ?? sourcemapUploadAssets, |
| 64 | + ignore: sentryBuildOptions.sourcemaps?.ignore ?? sourcemapUploadIgnore, |
| 65 | + filesToDeleteAfterUpload, |
| 66 | + ...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.sourcemaps, |
| 67 | + }, |
| 68 | + release: |
| 69 | + releaseName !== undefined |
| 70 | + ? { |
| 71 | + inject: false, // The webpack plugin's release injection breaks the `app` directory - we inject the release manually with the value injection loader instead. |
| 72 | + name: releaseName, |
| 73 | + create: sentryBuildOptions.release?.create, |
| 74 | + finalize: sentryBuildOptions.release?.finalize, |
| 75 | + dist: sentryBuildOptions.release?.dist, |
| 76 | + vcsRemote: sentryBuildOptions.release?.vcsRemote, |
| 77 | + setCommits: sentryBuildOptions.release?.setCommits, |
| 78 | + deploy: sentryBuildOptions.release?.deploy, |
| 79 | + ...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.release, |
| 80 | + } |
| 81 | + : { |
| 82 | + inject: false, |
| 83 | + create: false, |
| 84 | + finalize: false, |
| 85 | + }, |
| 86 | + bundleSizeOptimizations: { |
| 87 | + ...sentryBuildOptions.bundleSizeOptimizations, |
| 88 | + }, |
| 89 | + _metaOptions: { |
| 90 | + loggerPrefixOverride: '[@sentry/nextjs]', |
| 91 | + telemetry: { |
| 92 | + metaFramework: 'nextjs', |
| 93 | + }, |
| 94 | + }, |
| 95 | + ...sentryBuildOptions.unstable_sentryWebpackPluginOptions, |
| 96 | + }; |
| 97 | +} |
0 commit comments