Skip to content

Commit 4b40f61

Browse files
committed
extract main logic in withSentryConfig into a helper function
1 parent 99864fa commit 4b40f61

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ExportedNextConfig, NextConfigFunction, NextConfigObject, SentryWebpackPluginOptions } from './types';
1+
import type {
2+
ExportedNextConfig,
3+
NextConfigFunction,
4+
NextConfigObject,
5+
NextConfigObjectWithSentry,
6+
SentryWebpackPluginOptions,
7+
} from './types';
28
import { constructWebpackConfigFunction } from './webpack';
39

410
/**
@@ -16,35 +22,29 @@ export function withSentryConfig(
1622
// `defaults` in order to pass them along to the user's function
1723
if (typeof exportedUserNextConfig === 'function') {
1824
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): NextConfigObject {
19-
let userNextConfigObject = exportedUserNextConfig(phase, defaults);
25+
const userNextConfigObject = exportedUserNextConfig(phase, defaults);
2026

21-
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
22-
// property there. Where we actually need it is in the webpack config function we're going to create, so pass it
23-
// to `constructWebpackConfigFunction` so that it will be in the created function's closure.
24-
const { sentry: userSentryOptions } = userNextConfigObject;
25-
delete userNextConfigObject.sentry;
26-
// Remind TS that there's now no `sentry` property
27-
userNextConfigObject = userNextConfigObject as NextConfigObject;
28-
29-
return {
30-
...userNextConfigObject,
31-
webpack: constructWebpackConfigFunction(
32-
userNextConfigObject,
33-
userSentryWebpackPluginOptions,
34-
userSentryOptions,
35-
),
36-
};
27+
return getFinalConfigObject(userNextConfigObject, userSentryWebpackPluginOptions);
3728
};
3829
}
3930

4031
// Otherwise, we can just merge their config with ours and return an object.
32+
return getFinalConfigObject(exportedUserNextConfig, userSentryWebpackPluginOptions);
33+
}
4134

42-
// Prevent nextjs from getting mad about having a non-standard config property in `userNextConfig`. (See note above
43-
// for a more thorough explanation of what we're doing here.)
44-
const { sentry: userSentryOptions } = exportedUserNextConfig;
45-
delete exportedUserNextConfig.sentry;
35+
// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
36+
// `webpack` property
37+
function getFinalConfigObject(
38+
incomingUserNextConfigObject: NextConfigObjectWithSentry,
39+
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>,
40+
): NextConfigObject {
41+
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
42+
// property there. Where we actually need it is in the webpack config function we're going to create, so pass it
43+
// to `constructWebpackConfigFunction` so that it can live in the returned function's closure.
44+
const { sentry: userSentryOptions } = incomingUserNextConfigObject;
45+
delete incomingUserNextConfigObject.sentry;
4646
// Remind TS that there's now no `sentry` property
47-
const userNextConfigObject = exportedUserNextConfig as NextConfigObject;
47+
const userNextConfigObject = incomingUserNextConfigObject as NextConfigObject;
4848

4949
return {
5050
...userNextConfigObject,

0 commit comments

Comments
 (0)