Skip to content

Commit 78395c6

Browse files
authored
fix(nextjs): Pull transpileClientSDK option from correct location (#5516)
In the process of working simultaneously on both #5472 (which adds the `sentry.transpileClientSDK` option to `next.config.js`) and #5473 (which moves the `sentry` options object from `next.config.js` to a new location halfway through the build process, in order to prevent nextjs from throwing warnings), I missed their overlap. As a result, the `transpileClientSDK` option is still currently being retrieved from the old, pre-move location, even though said retrieval happens in the second half of the build, after the move. (Unsurprisingly, it's therefore always undefined, rendering it useless). This fixes that by pulling it from the new, correct location instead. It also adjusts our types so that future mistakes like this will show up as errors, by creating separate pre-move and post-move `userNextConfig` types and using them as appropriate. Fixes #5452.
1 parent ce8d8f8 commit 78395c6

File tree

4 files changed

+128
-84
lines changed

4 files changed

+128
-84
lines changed

packages/nextjs/src/config/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ export function withSentryConfig(
1616
// `defaults` in order to pass them along to the user's function
1717
if (typeof exportedUserNextConfig === 'function') {
1818
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): NextConfigObject {
19-
const userNextConfigObject = exportedUserNextConfig(phase, defaults);
19+
let userNextConfigObject = exportedUserNextConfig(phase, defaults);
2020

2121
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
2222
// property there. Where we actually need it is in the webpack config function we're going to create, so pass it
2323
// to `constructWebpackConfigFunction` so that it will be in the created function's closure.
2424
const { sentry: userSentryOptions } = userNextConfigObject;
2525
delete userNextConfigObject.sentry;
26+
// Remind TS that there's now no `sentry` property
27+
userNextConfigObject = userNextConfigObject as NextConfigObject;
2628

2729
return {
2830
...userNextConfigObject,
@@ -41,9 +43,11 @@ export function withSentryConfig(
4143
// for a more thorough explanation of what we're doing here.)
4244
const { sentry: userSentryOptions } = exportedUserNextConfig;
4345
delete exportedUserNextConfig.sentry;
46+
// Remind TS that there's now no `sentry` property
47+
const userNextConfigObject = exportedUserNextConfig as NextConfigObject;
4448

4549
return {
46-
...exportedUserNextConfig,
47-
webpack: constructWebpackConfigFunction(exportedUserNextConfig, userSentryWebpackPluginOptions, userSentryOptions),
50+
...userNextConfigObject,
51+
webpack: constructWebpackConfigFunction(userNextConfigObject, userSentryWebpackPluginOptions, userSentryOptions),
4852
};
4953
}

packages/nextjs/src/config/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpa
88
* Overall Nextjs config
99
*/
1010

11-
export type ExportedNextConfig = NextConfigObject | NextConfigFunction;
11+
// The first argument to `withSentryConfig` (which is the user's next config) may contain a `sentry` key, which we'll
12+
// remove once we've captured it, in order to prevent nextjs from throwing warnings. Since it's only in there
13+
// temporarily, we don't include it in the main `NextConfigObject` or `NextConfigFunction` types.
14+
export type ExportedNextConfig = NextConfigObjectWithSentry | NextConfigFunctionWithSentry;
15+
16+
export type NextConfigObjectWithSentry = NextConfigObject & {
17+
sentry?: UserSentryOptions;
18+
};
19+
export type NextConfigFunctionWithSentry = (
20+
phase: string,
21+
defaults: { defaultConfig: NextConfigObject },
22+
) => NextConfigObjectWithSentry;
1223

1324
export type NextConfigObject = {
1425
// custom webpack options
@@ -21,7 +32,6 @@ export type NextConfigObject = {
2132
basePath?: string;
2233
// config which will be available at runtime
2334
publicRuntimeConfig?: { [key: string]: unknown };
24-
sentry?: UserSentryOptions;
2535
};
2636

2737
export type UserSentryOptions = {

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function constructWebpackConfigFunction(
8080
// who want to support such browsers, `transpileClientSDK` allows them to force the SDK code to go through the same
8181
// transpilation that their code goes through. We don't turn this on by default because it increases bundle size
8282
// fairly massively.
83-
if (!isServer && userNextConfig.sentry?.transpileClientSDK) {
83+
if (!isServer && userSentryOptions?.transpileClientSDK) {
8484
// Find all loaders which apply transpilation to user code
8585
const transpilationRules = findTranspilationRules(newConfig.module?.rules, projectDir);
8686

0 commit comments

Comments
 (0)