-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtestUtils.ts
81 lines (73 loc) · 3.54 KB
/
testUtils.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type {
BuildContext,
EntryPropertyFunction,
ExportedNextConfig,
NextConfigObject,
SentryBuildOptions,
WebpackConfigObject,
WebpackConfigObjectWithModuleRules,
} from '../../src/config/types';
import { constructWebpackConfigFunction } from '../../src/config/webpack';
import { withSentryConfig } from '../../src/config/withSentryConfig';
import { defaultRuntimePhase, defaultsObject } from './fixtures';
/**
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
* function, running that function.
*
* @param exportedNextConfig Next config options provided by the user
* @param userSentryWebpackPluginConfig SentryWebpackPlugin options provided by the user
*
* @returns The config values next will receive directly from `withSentryConfig` or when it calls the function returned
* by `withSentryConfig`
*/
export function materializeFinalNextConfig(
exportedNextConfig: ExportedNextConfig,
runtimePhase?: string,
sentryBuildOptions?: SentryBuildOptions,
): NextConfigObject {
const sentrifiedConfig = withSentryConfig(exportedNextConfig, sentryBuildOptions);
let finalConfigValues = sentrifiedConfig;
if (typeof sentrifiedConfig === 'function') {
// for some reason TS won't recognize that `finalConfigValues` is now a NextConfigObject, which is why the cast
// below is necessary
finalConfigValues = sentrifiedConfig(runtimePhase ?? defaultRuntimePhase, defaultsObject) as NextConfigObject;
}
return finalConfigValues as NextConfigObject;
}
/**
* Derive the final values of all webpack config options, by first applying `constructWebpackConfigFunction` and then
* running the resulting function. Since the `entry` property of the resulting object is itself a function, also call
* that.
*
* @param options An object including the following:
* - `exportedNextConfig` Next config options provided by the user
* - `userSentryWebpackPluginConfig` SentryWebpackPlugin options provided by the user
* - `incomingWebpackConfig` The existing webpack config, passed to the function as `config`
* - `incomingWebpackBuildContext` The existing webpack build context, passed to the function as `options`
*
* @returns The webpack config values next will use when it calls the function that `createFinalWebpackConfig` returns
*/
export async function materializeFinalWebpackConfig(options: {
exportedNextConfig: ExportedNextConfig;
incomingWebpackConfig: WebpackConfigObject;
incomingWebpackBuildContext: BuildContext;
sentryBuildTimeOptions?: SentryBuildOptions;
}): Promise<WebpackConfigObjectWithModuleRules> {
const { exportedNextConfig, incomingWebpackConfig, incomingWebpackBuildContext } = options;
// if the user's next config is a function, run it so we have access to the values
const materializedUserNextConfig =
typeof exportedNextConfig === 'function'
? await exportedNextConfig('phase-production-build', defaultsObject)
: exportedNextConfig;
// get the webpack config function we'd normally pass back to next
const webpackConfigFunction = constructWebpackConfigFunction(
materializedUserNextConfig,
options.sentryBuildTimeOptions,
undefined,
);
// call it to get concrete values for comparison
const finalWebpackConfigValue = webpackConfigFunction(incomingWebpackConfig, incomingWebpackBuildContext);
const webpackEntryProperty = finalWebpackConfigValue.entry as EntryPropertyFunction;
finalWebpackConfigValue.entry = await webpackEntryProperty();
return finalWebpackConfigValue as WebpackConfigObjectWithModuleRules;
}