1- import type { ExportedNextConfig , NextConfigFunction , NextConfigObject , SentryWebpackPluginOptions } from './types' ;
2- import { constructWebpackConfigFunction } from './webpack' ;
1+ import { isBuild } from '../utils/isBuild' ;
2+ import type {
3+ ExportedNextConfig ,
4+ NextConfigFunction ,
5+ NextConfigObject ,
6+ NextConfigObjectWithSentry ,
7+ SentryWebpackPluginOptions ,
8+ } from './types' ;
39
410/**
511 * Add Sentry options to the config to be exported from the user's `next.config.js` file.
@@ -16,38 +22,42 @@ 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 ;
48+
49+ // In order to prevent all of our build-time code from being bundled in people's route-handling serverless functions,
50+ // we exclude `webpack.ts` and all of its dependencies from nextjs's `@vercel/nft` filetracing. We therefore need to
51+ // make sure that we only require it at build time.
52+ if ( isBuild ( ) ) {
53+ // eslint-disable-next-line @typescript-eslint/no-var-requires
54+ const { constructWebpackConfigFunction } = require ( './webpack' ) ;
55+ return {
56+ ...userNextConfigObject ,
57+ webpack : constructWebpackConfigFunction ( userNextConfigObject , userSentryWebpackPluginOptions , userSentryOptions ) ,
58+ } ;
59+ }
4860
49- return {
50- ...userNextConfigObject ,
51- webpack : constructWebpackConfigFunction ( userNextConfigObject , userSentryWebpackPluginOptions , userSentryOptions ) ,
52- } ;
61+ // At runtime, we just return the user's config untouched.
62+ return userNextConfigObject ;
5363}
0 commit comments