|
1 | 1 | import { isThenable } from '@sentry/utils';
|
2 | 2 |
|
3 | 3 | import type {
|
4 |
| - ExportedNextConfig, |
| 4 | + ExportedNextConfig as NextConfig, |
5 | 5 | NextConfigFunction,
|
6 | 6 | NextConfigObject,
|
7 |
| - NextConfigObjectWithSentry, |
| 7 | + SentryBuildtimeOptions, |
8 | 8 | SentryWebpackPluginOptions,
|
9 |
| - UserSentryOptions, |
10 | 9 | } from './types';
|
11 | 10 | import { constructWebpackConfigFunction } from './webpack';
|
12 | 11 |
|
13 | 12 | let showedExportModeTunnelWarning = false;
|
14 | 13 |
|
15 | 14 | /**
|
16 |
| - * Add Sentry options to the config to be exported from the user's `next.config.js` file. |
| 15 | + * Modifies the passed in Next.js configuration with automatic build-time instrumentation and source map upload. |
17 | 16 | *
|
18 |
| - * @param exportedUserNextConfig The existing config to be exported prior to adding Sentry |
19 |
| - * @param userSentryWebpackPluginOptions Configuration for SentryWebpackPlugin |
20 |
| - * @param sentryOptions Optional additional options to add as alternative to `sentry` property of config |
| 17 | + * @param nextConfig A Next.js configuration object, as usually exported in `next.config.js` or `next.config.mjs`. |
| 18 | + * @param sentryWebpackPluginOptions Options to configure the automatically included Sentry Webpack Plugin for source maps and release management in Sentry. |
| 19 | + * @param sentryBuildtimeOptions Additional options to configure instrumentation and |
21 | 20 | * @returns The modified config to be exported
|
22 | 21 | */
|
23 | 22 | export function withSentryConfig(
|
24 |
| - exportedUserNextConfig: ExportedNextConfig = {}, |
25 |
| - userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {}, |
26 |
| - sentryOptions?: UserSentryOptions, |
| 23 | + nextConfig: NextConfig = {}, |
| 24 | + sentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {}, |
| 25 | + sentryBuildtimeOptions: SentryBuildtimeOptions = {}, |
27 | 26 | ): NextConfigFunction | NextConfigObject {
|
28 |
| - if (typeof exportedUserNextConfig === 'function') { |
| 27 | + if (typeof nextConfig === 'function') { |
29 | 28 | return function (this: unknown, ...webpackConfigFunctionArgs: unknown[]): ReturnType<NextConfigFunction> {
|
30 |
| - const maybeUserNextConfigObject: NextConfigObjectWithSentry = exportedUserNextConfig.apply( |
31 |
| - this, |
32 |
| - webpackConfigFunctionArgs, |
33 |
| - ); |
| 29 | + const maybePromiseNextConfig: ReturnType<typeof nextConfig> = nextConfig.apply(this, webpackConfigFunctionArgs); |
34 | 30 |
|
35 |
| - if (isThenable(maybeUserNextConfigObject)) { |
36 |
| - return maybeUserNextConfigObject.then(function (userNextConfigObject: NextConfigObjectWithSentry) { |
37 |
| - const userSentryOptions = { ...userNextConfigObject.sentry, ...sentryOptions }; |
38 |
| - return getFinalConfigObject(userNextConfigObject, userSentryOptions, userSentryWebpackPluginOptions); |
| 31 | + if (isThenable(maybePromiseNextConfig)) { |
| 32 | + return maybePromiseNextConfig.then(promiseResultNextConfig => { |
| 33 | + return getFinalConfigObject(promiseResultNextConfig, sentryBuildtimeOptions, sentryWebpackPluginOptions); |
39 | 34 | });
|
40 | 35 | }
|
41 | 36 |
|
42 |
| - // Reassign for naming-consistency sake. |
43 |
| - const userNextConfigObject = maybeUserNextConfigObject; |
44 |
| - const userSentryOptions = { ...userNextConfigObject.sentry, ...sentryOptions }; |
45 |
| - return getFinalConfigObject(userNextConfigObject, userSentryOptions, userSentryWebpackPluginOptions); |
| 37 | + return getFinalConfigObject(maybePromiseNextConfig, sentryBuildtimeOptions, sentryWebpackPluginOptions); |
46 | 38 | };
|
47 | 39 | } else {
|
48 |
| - const userSentryOptions = { ...exportedUserNextConfig.sentry, ...sentryOptions }; |
49 |
| - return getFinalConfigObject(exportedUserNextConfig, userSentryOptions, userSentryWebpackPluginOptions); |
| 40 | + return getFinalConfigObject(nextConfig, sentryBuildtimeOptions, sentryWebpackPluginOptions); |
50 | 41 | }
|
51 | 42 | }
|
52 | 43 |
|
53 | 44 | // Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
|
54 | 45 | // `webpack` property
|
55 | 46 | function getFinalConfigObject(
|
56 |
| - incomingUserNextConfigObject: NextConfigObjectWithSentry, |
57 |
| - userSentryOptions: UserSentryOptions, |
| 47 | + incomingUserNextConfigObject: NextConfigObject, |
| 48 | + userSentryOptions: SentryBuildtimeOptions, |
58 | 49 | userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>,
|
59 | 50 | ): NextConfigObject {
|
60 |
| - // Next 12.2.3+ warns about non-canonical properties on `userNextConfig`. |
61 |
| - delete incomingUserNextConfigObject.sentry; |
| 51 | + if ('sentry' in incomingUserNextConfigObject) { |
| 52 | + // eslint-disable-next-line no-console |
| 53 | + console.warn( |
| 54 | + '[@sentry/nextjs] Setting a `sentry` property on the Next.js config is no longer supported. Please use the `sentrySDKOptions` argument of `withSentryConfig` instead.', |
| 55 | + ); |
| 56 | + |
| 57 | + // Next 12.2.3+ warns about non-canonical properties on `userNextConfig`. |
| 58 | + delete incomingUserNextConfigObject.sentry; |
| 59 | + } |
62 | 60 |
|
63 | 61 | if (userSentryOptions?.tunnelRoute) {
|
64 | 62 | if (incomingUserNextConfigObject.output === 'export') {
|
|
0 commit comments