1
- import { PHASE_DEVELOPMENT_SERVER , PHASE_PRODUCTION_BUILD } from 'next/constants' ;
2
-
3
1
import type {
4
2
ExportedNextConfig ,
5
3
NextConfigFunction ,
@@ -8,6 +6,7 @@ import type {
8
6
SentryWebpackPluginOptions ,
9
7
UserSentryOptions ,
10
8
} from './types' ;
9
+ import { constructWebpackConfigFunction } from './webpack' ;
11
10
12
11
/**
13
12
* Add Sentry options to the config to be exported from the user's `next.config.js` file.
@@ -22,49 +21,44 @@ export function withSentryConfig(
22
21
userSentryWebpackPluginOptions : Partial < SentryWebpackPluginOptions > = { } ,
23
22
sentryOptions ?: UserSentryOptions ,
24
23
) : NextConfigFunction | NextConfigObject {
25
- return function ( phase : string , defaults : { defaultConfig : NextConfigObject } ) : NextConfigObject {
26
- const userNextConfigObject =
27
- typeof exportedUserNextConfig === 'function' ? exportedUserNextConfig ( phase , defaults ) : exportedUserNextConfig ;
28
- // Inserts additional `sentry` options into the existing config, allows for backwards compatability
29
- // in case nothing is passed into the optional `sentryOptions` argument
30
- userNextConfigObject . sentry = { ...userNextConfigObject . sentry , ...sentryOptions } ;
31
- return getFinalConfigObject ( phase , userNextConfigObject , userSentryWebpackPluginOptions ) ;
32
- } ;
24
+ if ( typeof exportedUserNextConfig === 'function' ) {
25
+ return function ( this : unknown , ...webpackConfigFunctionArgs : unknown [ ] ) : NextConfigObject {
26
+ const userNextConfigObject : NextConfigObjectWithSentry = exportedUserNextConfig . apply (
27
+ this ,
28
+ webpackConfigFunctionArgs ,
29
+ ) ;
30
+
31
+ const userSentryOptions = { ...userNextConfigObject . sentry , ...sentryOptions } ;
32
+ return getFinalConfigObject ( userNextConfigObject , userSentryOptions , userSentryWebpackPluginOptions ) ;
33
+ } ;
34
+ } else {
35
+ const userSentryOptions = { ...exportedUserNextConfig . sentry , ...sentryOptions } ;
36
+ return getFinalConfigObject ( exportedUserNextConfig , userSentryOptions , userSentryWebpackPluginOptions ) ;
37
+ }
33
38
}
34
39
35
40
// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
36
41
// `webpack` property
37
42
function getFinalConfigObject (
38
- phase : string ,
39
43
incomingUserNextConfigObject : NextConfigObjectWithSentry ,
44
+ userSentryOptions : UserSentryOptions ,
40
45
userSentryWebpackPluginOptions : Partial < SentryWebpackPluginOptions > ,
41
46
) : NextConfigObject {
42
- // Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
43
- // property there. Where we actually need it is in the webpack config function we're going to create, so pass it
44
- // to `constructWebpackConfigFunction` so that it can live in the returned function's closure.
45
- const { sentry : userSentryOptions } = incomingUserNextConfigObject ;
47
+ // Next 12.2.3+ warns about non-canonical properties on `userNextConfig`.
46
48
delete incomingUserNextConfigObject . sentry ;
47
- // Remind TS that there's now no `sentry` property
48
- const userNextConfigObject = incomingUserNextConfigObject as NextConfigObject ;
49
49
50
50
if ( userSentryOptions ?. tunnelRoute ) {
51
- setUpTunnelRewriteRules ( userNextConfigObject , userSentryOptions . tunnelRoute ) ;
51
+ setUpTunnelRewriteRules ( incomingUserNextConfigObject , userSentryOptions . tunnelRoute ) ;
52
52
}
53
53
54
- // In order to prevent all of our build-time code from being bundled in people's route-handling serverless functions,
55
- // we exclude `webpack.ts` and all of its dependencies from nextjs's `@vercel/nft` filetracing. We therefore need to
56
- // make sure that we only require it at build time or in development mode.
57
- if ( phase === PHASE_PRODUCTION_BUILD || phase === PHASE_DEVELOPMENT_SERVER ) {
58
- // eslint-disable-next-line @typescript-eslint/no-var-requires
59
- const { constructWebpackConfigFunction } = require ( './webpack' ) ;
60
- return {
61
- ...userNextConfigObject ,
62
- webpack : constructWebpackConfigFunction ( userNextConfigObject , userSentryWebpackPluginOptions , userSentryOptions ) ,
63
- } ;
64
- }
65
-
66
- // At runtime, we just return the user's config untouched.
67
- return userNextConfigObject ;
54
+ return {
55
+ ...incomingUserNextConfigObject ,
56
+ webpack : constructWebpackConfigFunction (
57
+ incomingUserNextConfigObject ,
58
+ userSentryWebpackPluginOptions ,
59
+ userSentryOptions ,
60
+ ) ,
61
+ } ;
68
62
}
69
63
70
64
/**
0 commit comments