Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 6 additions & 2 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -379,7 +383,7 @@ function getFinalConfigObject(
releaseName,
routeManifest,
nextJsVersion,
useRunAfterProductionCompileHook: userSentryOptions?.useRunAfterProductionCompileHook,
useRunAfterProductionCompileHook: shouldUseRunAfterProductionCompileHook,
}),
...(isTurbopackSupported && isTurbopack
? {
Expand Down
71 changes: 63 additions & 8 deletions packages/nextjs/test/config/withSentryConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
});
});
Expand Down