import { getBuildLogger } from '../buildLogger'; import type { NextConfigObject, SentryBuildOptions } from '../types'; import { getNextjsVersion } from '../util'; import { setUpBuildTimeVariables } from './buildTime'; import type { BundlerInfo } from './getFinalConfigObjectBundlerUtils'; import { getBundlerInfo, getServerExternalPackagesPatch, getTurbopackPatch, getWebpackPatch, maybeConstructTurbopackConfig, maybeEnableTurbopackSourcemaps, maybeSetTurbopackTreeshakeDefines, maybeSetUpRunAfterProductionCompileHook, maybeWarnAboutUnsupportedRunAfterProductionCompileHook, maybeWarnAboutTurbopackModuleMetadata, maybeWarnAboutUnsupportedTurbopack, resolveBuildTimeInstrumentationOption, resolveUseRunAfterProductionCompileHookOption, } from './getFinalConfigObjectBundlerUtils'; import { getNextMajor, maybeCreateRouteManifest, maybeGetVercelCronsConfig, maybeSetClientTraceMetadataOption, maybeSetInstrumentationHookOption, maybeSetTurbopackPluginRuntimeStrategyOption, maybeSetUpTunnelRouteRewriteRules, resolveReleaseName, shouldReturnEarlyInExperimentalBuildMode, warnIfMissingInstrumentationFile, warnIfMissingOnRouterTransitionStartHook, } from './getFinalConfigObjectUtils'; import { maybeAddReleaseServerTimingHeader } from './release'; /** * Materializes the final Next.js config object with Sentry's build-time integrations applied. * * Note: this mutates both `incomingUserNextConfigObject` and `userSentryOptions` (to apply defaults). */ export function getFinalConfigObject( incomingUserNextConfigObject: NextConfigObject, userSentryOptions: SentryBuildOptions, ): NextConfigObject { const releaseName = resolveReleaseName(userSentryOptions); maybeSetUpTunnelRouteRewriteRules(incomingUserNextConfigObject, userSentryOptions); if (shouldReturnEarlyInExperimentalBuildMode(userSentryOptions.silent)) { return incomingUserNextConfigObject; } const routeManifest = maybeCreateRouteManifest(incomingUserNextConfigObject, userSentryOptions); const vercelCronsConfigResult = maybeGetVercelCronsConfig(userSentryOptions); setUpBuildTimeVariables(incomingUserNextConfigObject, userSentryOptions, releaseName); maybeAddReleaseServerTimingHeader(incomingUserNextConfigObject, userSentryOptions, releaseName); const nextJsVersion = getNextjsVersion(); const nextMajor = getNextMajor(nextJsVersion); maybeSetClientTraceMetadataOption(incomingUserNextConfigObject, nextJsVersion, userSentryOptions.silent); maybeSetInstrumentationHookOption(incomingUserNextConfigObject, nextJsVersion, userSentryOptions.silent); warnIfMissingOnRouterTransitionStartHook(userSentryOptions); warnIfMissingInstrumentationFile(incomingUserNextConfigObject, userSentryOptions); const bundlerInfo = getBundlerInfo(nextJsVersion); maybeSetTurbopackPluginRuntimeStrategyOption( incomingUserNextConfigObject, nextJsVersion, bundlerInfo.isTurbopack, userSentryOptions.silent, ); maybeWarnAboutUnsupportedTurbopack(nextJsVersion, bundlerInfo, userSentryOptions.silent); maybeWarnAboutTurbopackModuleMetadata(userSentryOptions, bundlerInfo); maybeWarnAboutApplicationKeyExcludeDependencies(userSentryOptions, bundlerInfo); maybeWarnAboutUnsupportedRunAfterProductionCompileHook(nextJsVersion, userSentryOptions, bundlerInfo); const turboPackConfig = maybeConstructTurbopackConfig( incomingUserNextConfigObject, userSentryOptions, routeManifest, nextJsVersion, bundlerInfo, vercelCronsConfigResult, releaseName, ); const shouldUseRunAfterProductionCompileHook = resolveUseRunAfterProductionCompileHookOption( userSentryOptions, bundlerInfo, ); maybeSetUpRunAfterProductionCompileHook({ incomingUserNextConfigObject, userSentryOptions, releaseName, nextJsVersion, bundlerInfo, turboPackConfig, shouldUseRunAfterProductionCompileHook, }); maybeEnableTurbopackSourcemaps(incomingUserNextConfigObject, userSentryOptions, bundlerInfo); maybeSetTurbopackTreeshakeDefines(incomingUserNextConfigObject, userSentryOptions, bundlerInfo); const buildTimeInstrumentation = resolveBuildTimeInstrumentationOption(userSentryOptions, bundlerInfo, nextJsVersion); return { ...incomingUserNextConfigObject, ...getServerExternalPackagesPatch(incomingUserNextConfigObject, nextMajor, buildTimeInstrumentation), ...getWebpackPatch({ incomingUserNextConfigObject, userSentryOptions, releaseName, routeManifest, nextJsVersion, shouldUseRunAfterProductionCompileHook, bundlerInfo, vercelCronsConfigResult, }), ...getTurbopackPatch(bundlerInfo, turboPackConfig), }; } function maybeWarnAboutApplicationKeyExcludeDependencies( userSentryOptions: SentryBuildOptions, bundlerInfo: BundlerInfo, ): void { if (!userSentryOptions.applicationKeyExcludeDependencies) { return; } const logger = getBuildLogger(userSentryOptions.silent); if (!userSentryOptions.applicationKey) { logger.warn( '[@sentry/nextjs] `applicationKeyExcludeDependencies` requires `applicationKey` and has no effect without it.', ); } else if (bundlerInfo.isWebpack) { logger.warn( '[@sentry/nextjs] `applicationKeyExcludeDependencies` only applies to Turbopack builds and has no effect on webpack builds.', ); } }