1- import { PHASE_DEVELOPMENT_SERVER , PHASE_PRODUCTION_BUILD } from 'next/constants' ;
2-
31import type {
42 ExportedNextConfig ,
53 NextConfigFunction ,
@@ -8,6 +6,7 @@ import type {
86 SentryWebpackPluginOptions ,
97 UserSentryOptions ,
108} from './types' ;
9+ import { constructWebpackConfigFunction } from './webpack' ;
1110
1211/**
1312 * Add Sentry options to the config to be exported from the user's `next.config.js` file.
@@ -22,49 +21,44 @@ export function withSentryConfig(
2221 userSentryWebpackPluginOptions : Partial < SentryWebpackPluginOptions > = { } ,
2322 sentryOptions ?: UserSentryOptions ,
2423) : 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+ }
3338}
3439
3540// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
3641// `webpack` property
3742function getFinalConfigObject (
38- phase : string ,
3943 incomingUserNextConfigObject : NextConfigObjectWithSentry ,
44+ userSentryOptions : UserSentryOptions ,
4045 userSentryWebpackPluginOptions : Partial < SentryWebpackPluginOptions > ,
4146) : 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`.
4648 delete incomingUserNextConfigObject . sentry ;
47- // Remind TS that there's now no `sentry` property
48- const userNextConfigObject = incomingUserNextConfigObject as NextConfigObject ;
4949
5050 if ( userSentryOptions ?. tunnelRoute ) {
51- setUpTunnelRewriteRules ( userNextConfigObject , userSentryOptions . tunnelRoute ) ;
51+ setUpTunnelRewriteRules ( incomingUserNextConfigObject , userSentryOptions . tunnelRoute ) ;
5252 }
5353
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+ } ;
6862}
6963
7064/**
0 commit comments