|
2 | 2 | import { isThenable, parseSemver } from '@sentry/utils'; |
3 | 3 |
|
4 | 4 | import * as fs from 'fs'; |
| 5 | +import { getSentryRelease } from '@sentry/node'; |
5 | 6 | import { sync as resolveSync } from 'resolve'; |
6 | 7 | import type { |
7 | 8 | ExportedNextConfig as NextConfig, |
@@ -73,6 +74,8 @@ function getFinalConfigObject( |
73 | 74 | } |
74 | 75 | } |
75 | 76 |
|
| 77 | + setUpBuildTimeVariables(incomingUserNextConfigObject); |
| 78 | + |
76 | 79 | const nextJsVersion = getNextjsVersion(); |
77 | 80 |
|
78 | 81 | // Add the `clientTraceMetadata` experimental option based on Next.js version. The option got introduced in Next.js version 15.0.0 (actually 14.3.0-canary.64). |
@@ -253,6 +256,37 @@ function setUpTunnelRewriteRules(userNextConfig: NextConfigObject, tunnelPath: s |
253 | 256 | }; |
254 | 257 | } |
255 | 258 |
|
| 259 | +function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOptions: SentryBuildOptions): void { |
| 260 | + const assetPrefix = userNextConfig.assetPrefix || userNextConfig.basePath || ''; |
| 261 | + |
| 262 | + const buildTimeVariables = { |
| 263 | + // `rewritesTunnel` set by the user in Next.js config |
| 264 | + __sentryRewritesTunnelPath: |
| 265 | + userSentryOptions.tunnelRoute !== undefined && userNextConfig.output !== 'export' |
| 266 | + ? `${userNextConfig.basePath ?? ''}${userSentryOptions.tunnelRoute}` |
| 267 | + : undefined, |
| 268 | + SENTRY_RELEASE: |
| 269 | + process.env.NODE_ENV === 'production' |
| 270 | + ? undefined |
| 271 | + : { id: userSentryOptions.release?.name ?? getSentryRelease('TODO') }, |
| 272 | + __sentryBasePath: userNextConfig.basePath, |
| 273 | + // Make sure that if we have a windows path, the backslashes are interpreted as such (rather than as escape |
| 274 | + // characters) |
| 275 | + __sentryRewriteFramesDistDir: userNextConfig.distDir?.replace(/\\/g, '\\\\') || '.next', |
| 276 | + // Get the path part of `assetPrefix`, minus any trailing slash. (We use a placeholder for the origin if |
| 277 | + // `assetPrefix` doesn't include one. Since we only care about the path, it doesn't matter what it is.) |
| 278 | + __sentryRewriteFramesAssetPrefixPath: assetPrefix |
| 279 | + ? new URL(assetPrefix, 'http://dogs.are.great').pathname.replace(/\/$/, '') |
| 280 | + : '', |
| 281 | + }; |
| 282 | + |
| 283 | + if (typeof userNextConfig.env === 'object') { |
| 284 | + userNextConfig.env = { ...buildTimeVariables, ...userNextConfig.env }; |
| 285 | + } else if (userNextConfig.env === undefined) { |
| 286 | + userNextConfig.env = buildTimeVariables; |
| 287 | + } |
| 288 | +} |
| 289 | + |
256 | 290 | function getNextjsVersion(): string | undefined { |
257 | 291 | const nextjsPackageJsonPath = resolveNextjsPackageJson(); |
258 | 292 | if (nextjsPackageJsonPath) { |
|
0 commit comments