Skip to content

Commit 61b3f97

Browse files
authored
feat(nextjs): Flip default value for useRunAfterProductionCompileHook turbopack builds (#17722)
1 parent cf7913c commit 61b3f97

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export type SentryBuildOptions = {
507507
*
508508
* When false, use the traditional approach of uploading sourcemaps during each webpack build. For Turbopack no sourcemaps will be uploaded.
509509
*
510-
* @default false
510+
* @default true for Turbopack, false for Webpack
511511
*/
512512
useRunAfterProductionCompileHook?: boolean;
513513

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,11 @@ function getFinalConfigObject(
294294
}
295295
}
296296

297-
if (userSentryOptions?.useRunAfterProductionCompileHook === true && supportsProductionCompileHook()) {
297+
// If not explicitly set, turbopack uses the runAfterProductionCompile hook (as there are no alternatives), webpack does not.
298+
const shouldUseRunAfterProductionCompileHook =
299+
userSentryOptions?.useRunAfterProductionCompileHook ?? (isTurbopack ? true : false);
300+
301+
if (shouldUseRunAfterProductionCompileHook && supportsProductionCompileHook()) {
298302
if (incomingUserNextConfigObject?.compiler?.runAfterProductionCompile === undefined) {
299303
incomingUserNextConfigObject.compiler ??= {};
300304
incomingUserNextConfigObject.compiler.runAfterProductionCompile = async ({ distDir }) => {
@@ -379,7 +383,7 @@ function getFinalConfigObject(
379383
releaseName,
380384
routeManifest,
381385
nextJsVersion,
382-
useRunAfterProductionCompileHook: userSentryOptions?.useRunAfterProductionCompileHook,
386+
useRunAfterProductionCompileHook: shouldUseRunAfterProductionCompileHook,
383387
}),
384388
...(isTurbopackSupported && isTurbopack
385389
? {

packages/nextjs/test/config/withSentryConfig.test.ts

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -876,16 +876,13 @@ describe('withSentryConfig', () => {
876876
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
877877
});
878878

879-
it('works with turbopack builds when TURBOPACK env is set', () => {
879+
it('defaults to true for turbopack when useRunAfterProductionCompileHook is not specified', () => {
880880
process.env.TURBOPACK = '1';
881881
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
882882
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
883883

884-
const sentryOptions = {
885-
useRunAfterProductionCompileHook: true,
886-
};
884+
const sentryOptions = {}; // No useRunAfterProductionCompileHook specified
887885

888-
// Use a clean copy of the config to avoid test interference
889886
const cleanConfig = { ...exportedNextConfig };
890887
delete cleanConfig.compiler;
891888

@@ -896,20 +893,78 @@ describe('withSentryConfig', () => {
896893
delete process.env.TURBOPACK;
897894
});
898895

899-
it('works with webpack builds when TURBOPACK env is not set', () => {
896+
it('defaults to false for webpack when useRunAfterProductionCompileHook is not specified', () => {
900897
delete process.env.TURBOPACK;
901898
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
902899

900+
const sentryOptions = {}; // No useRunAfterProductionCompileHook specified
901+
902+
const cleanConfig = { ...exportedNextConfig };
903+
delete cleanConfig.compiler;
904+
905+
const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions);
906+
907+
expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined();
908+
});
909+
910+
it('respects explicit false setting for turbopack', () => {
911+
process.env.TURBOPACK = '1';
912+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
913+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
914+
903915
const sentryOptions = {
904-
useRunAfterProductionCompileHook: true,
916+
useRunAfterProductionCompileHook: false,
905917
};
906918

907-
// Use a clean copy of the config to avoid test interference
908919
const cleanConfig = { ...exportedNextConfig };
909920
delete cleanConfig.compiler;
910921

911922
const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions);
912923

924+
expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined();
925+
926+
delete process.env.TURBOPACK;
927+
});
928+
929+
it('respects explicit true setting for webpack', () => {
930+
delete process.env.TURBOPACK;
931+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
932+
933+
const sentryOptions = {
934+
useRunAfterProductionCompileHook: true,
935+
};
936+
937+
const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
938+
939+
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
940+
});
941+
942+
it('works with turbopack builds when TURBOPACK env is set', () => {
943+
process.env.TURBOPACK = '1';
944+
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
945+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
946+
947+
const sentryOptions = {
948+
useRunAfterProductionCompileHook: true,
949+
};
950+
951+
const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
952+
953+
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
954+
955+
delete process.env.TURBOPACK;
956+
});
957+
958+
it('works with webpack builds when TURBOPACK env is not set', () => {
959+
delete process.env.TURBOPACK;
960+
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);
961+
962+
const sentryOptions = {
963+
useRunAfterProductionCompileHook: true,
964+
};
965+
966+
const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
967+
913968
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
914969
});
915970
});

0 commit comments

Comments
 (0)