-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.ts
49 lines (43 loc) · 2.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { ExportedNextConfig, NextConfigFunction, NextConfigObject, SentryWebpackPluginOptions } from './types';
import { constructWebpackConfigFunction } from './webpack';
/**
* Add Sentry options to the config to be exported from the user's `next.config.js` file.
*
* @param userNextConfig The existing config to be exported prior to adding Sentry
* @param userSentryWebpackPluginOptions Configuration for SentryWebpackPlugin
* @returns The modified config to be exported
*/
export function withSentryConfig(
userNextConfig: ExportedNextConfig = {},
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
): NextConfigFunction | Partial<NextConfigObject> {
// If the user has passed us a function, we need to return a function, so that we have access to `phase` and
// `defaults` in order to pass them along to the user's function
if (typeof userNextConfig === 'function') {
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): Partial<NextConfigObject> {
const materializedUserNextConfig = userNextConfig(phase, defaults);
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
// property there. Where we actually need it is in the webpack config function we're going to create, so pass it
// to `constructWebpackConfigFunction` so that it will be in the created function's closure.
const { sentry: userSentryOptions } = materializedUserNextConfig;
delete materializedUserNextConfig.sentry;
return {
...materializedUserNextConfig,
webpack: constructWebpackConfigFunction(
materializedUserNextConfig,
userSentryWebpackPluginOptions,
userSentryOptions,
),
};
};
}
// Otherwise, we can just merge their config with ours and return an object.
// Prevent nextjs from getting mad about having a non-standard config property in `userNextConfig`. (See note above
// for a more thorough explanation of what we're doing here.)
const { sentry: userSentryOptions } = userNextConfig;
delete userNextConfig.sentry;
return {
...userNextConfig,
webpack: constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions, userSentryOptions),
};
}