diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 23b18f39b253..1fa245412f2c 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -507,7 +507,7 @@ export type SentryBuildOptions = { * * When false, use the traditional approach of uploading sourcemaps during each webpack build. For Turbopack no sourcemaps will be uploaded. * - * @default false + * @default true for Turbopack, false for Webpack */ useRunAfterProductionCompileHook?: boolean; diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index e27f2bf37e25..b5c2be2f25bb 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -294,7 +294,11 @@ function getFinalConfigObject( } } - if (userSentryOptions?.useRunAfterProductionCompileHook === true && supportsProductionCompileHook()) { + // If not explicitly set, turbopack uses the runAfterProductionCompile hook (as there are no alternatives), webpack does not. + const shouldUseRunAfterProductionCompileHook = + userSentryOptions?.useRunAfterProductionCompileHook ?? (isTurbopack ? true : false); + + if (shouldUseRunAfterProductionCompileHook && supportsProductionCompileHook()) { if (incomingUserNextConfigObject?.compiler?.runAfterProductionCompile === undefined) { incomingUserNextConfigObject.compiler ??= {}; incomingUserNextConfigObject.compiler.runAfterProductionCompile = async ({ distDir }) => { @@ -379,7 +383,7 @@ function getFinalConfigObject( releaseName, routeManifest, nextJsVersion, - useRunAfterProductionCompileHook: userSentryOptions?.useRunAfterProductionCompileHook, + useRunAfterProductionCompileHook: shouldUseRunAfterProductionCompileHook, }), ...(isTurbopackSupported && isTurbopack ? { diff --git a/packages/nextjs/test/config/withSentryConfig.test.ts b/packages/nextjs/test/config/withSentryConfig.test.ts index 5121cb51d6fd..0697fc56b9e4 100644 --- a/packages/nextjs/test/config/withSentryConfig.test.ts +++ b/packages/nextjs/test/config/withSentryConfig.test.ts @@ -876,16 +876,13 @@ describe('withSentryConfig', () => { expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function); }); - it('works with turbopack builds when TURBOPACK env is set', () => { + it('defaults to true for turbopack when useRunAfterProductionCompileHook is not specified', () => { process.env.TURBOPACK = '1'; vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1'); vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); - const sentryOptions = { - useRunAfterProductionCompileHook: true, - }; + const sentryOptions = {}; // No useRunAfterProductionCompileHook specified - // Use a clean copy of the config to avoid test interference const cleanConfig = { ...exportedNextConfig }; delete cleanConfig.compiler; @@ -896,20 +893,78 @@ describe('withSentryConfig', () => { delete process.env.TURBOPACK; }); - it('works with webpack builds when TURBOPACK env is not set', () => { + it('defaults to false for webpack when useRunAfterProductionCompileHook is not specified', () => { delete process.env.TURBOPACK; vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); + const sentryOptions = {}; // No useRunAfterProductionCompileHook specified + + const cleanConfig = { ...exportedNextConfig }; + delete cleanConfig.compiler; + + const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions); + + expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined(); + }); + + it('respects explicit false setting for turbopack', () => { + process.env.TURBOPACK = '1'; + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1'); + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); + const sentryOptions = { - useRunAfterProductionCompileHook: true, + useRunAfterProductionCompileHook: false, }; - // Use a clean copy of the config to avoid test interference const cleanConfig = { ...exportedNextConfig }; delete cleanConfig.compiler; const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions); + expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined(); + + delete process.env.TURBOPACK; + }); + + it('respects explicit true setting for webpack', () => { + delete process.env.TURBOPACK; + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); + + const sentryOptions = { + useRunAfterProductionCompileHook: true, + }; + + const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions); + + expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function); + }); + + it('works with turbopack builds when TURBOPACK env is set', () => { + process.env.TURBOPACK = '1'; + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1'); + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); + + const sentryOptions = { + useRunAfterProductionCompileHook: true, + }; + + const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions); + + expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function); + + delete process.env.TURBOPACK; + }); + + it('works with webpack builds when TURBOPACK env is not set', () => { + delete process.env.TURBOPACK; + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); + + const sentryOptions = { + useRunAfterProductionCompileHook: true, + }; + + const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions); + expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function); }); });