Skip to content

Commit c811a5a

Browse files
committed
create separate types for before and after deleting sentry property
1 parent ce8d8f8 commit c811a5a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
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 = {

0 commit comments

Comments
 (0)