10000 ref(nextjs): Simplify `NextConfigObject` type (#5514) · cirdes/sentry-javascript@9b7131f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b7131f

Browse files
authored
ref(nextjs): Simplify NextConfigObject type (getsentry#5514)
This simplifies the `NextConfigObject` type in the nextjs SDK, to enable future work. Key changes: - Removed index signature. - Added missing `basePath` and `publicRuntimeConfig` properties. - Made all properties optional. (This allowed for removal of the `Partial` annotation everywhere the type was used.) - Clarified variable names in `config/index.ts` to better reflect their types.
1 parent 6f4ad56 commit 9b7131f

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

packages/nextjs/src/config/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import { ExportedNextConfig, NextConfigFunction, NextConfigObject, SentryWebpackPluginOptions } from './types';
1+
import type { ExportedNextConfig, NextConfigFunction, NextConfigObject, SentryWebpackPluginOptions } from './types';
22
import { constructWebpackConfigFunction } from './webpack';
33

44
/**
55
* Add Sentry options to the config to be exported from the user's `next.config.js` file.
66
*
7-
* @param userNextConfig The existing config to be exported prior to adding Sentry
7+
* @param exportedUserNextConfig The existing config to be exported prior to adding Sentry
88
* @param userSentryWebpackPluginOptions Configuration for SentryWebpackPlugin
99
* @returns The modified config to be exported
1010
*/
1111
export function withSentryConfig(
12-
userNextConfig: ExportedNextConfig = {},
12+
exportedUserNextConfig: ExportedNextConfig = {},
1313
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
14-
): NextConfigFunction | Partial<NextConfigObject> {
14+
): NextConfigFunction | NextConfigObject {
1515
// If the user has passed us a function, we need to return a function, so that we have access to `phase` and
1616
// `defaults` in order to pass them along to the user's function
17-
if (typeof userNextConfig === 'function') {
18-
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): Partial<NextConfigObject> {
19-
const materializedUserNextConfig = userNextConfig(phase, defaults);
17+
if (typeof exportedUserNextConfig === 'function') {
18+
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): NextConfigObject {
19+
const userNextConfigObject = exportedUserNextConfig(phase, defaults);
2020

2121
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
2222
// property there. Where we actually need it is in the webpack config function we're going to create, so pass it
2323
// to `constructWebpackConfigFunction` so that it will be in the created function's closure.
24-
const { sentry: userSentryOptions } = materializedUserNextConfig;
25-
delete materializedUserNextConfig.sentry;
24+
const { sentry: userSentryOptions } = userNextConfigObject;
25+
delete userNextConfigObject.sentry;
2626

2727
return {
28-
...materializedUserNextConfig,
28+
...userNextConfigObject,
2929
webpack: constructWebpackConfigFunction(
30-
materializedUserNextConfig,
30+
userNextConfigObject,
3131
userSentryWebpackPluginOptions,
3232
userSentryOptions,
3333
),
@@ -39,11 +39,11 @@ export function withSentryConfig(
3939

4040
// Prevent nextjs from getting mad about having a non-standard config property in `userNextConfig`. (See note above
4141
// for a more thorough explanation of what we're doing here.)
42-
const { sentry: userSentryOptions } = userNextConfig;
43-
delete userNextConfig.sentry;
42+
const { sentry: userSentryOptions } = exportedUserNextConfig;
43+
delete exportedUserNextConfig.sentry;
4444

4545
return {
46-
...userNextConfig,
47-
webpack: constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions, userSentryOptions),
46+
...exportedUserNextConfig,
47+
webpack: constructWebpackConfigFunction(exportedUserNextConfig, userSentryWebpackPluginOptions, userSentryOptions),
4848
};
4949
}

packages/nextjs/src/config/types.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpa
88
* Overall Nextjs config
99
*/
1010

11-
export type ExportedNextConfig = Partial<NextConfigObject> | NextConfigFunction;
11+
export type ExportedNextConfig = NextConfigObject | NextConfigFunction;
1212

1313
export type NextConfigObject = {
1414
// custom webpack options
15-
webpack: WebpackConfigFunction;
15+
webpack?: WebpackConfigFunction;
1616
// whether to build serverless functions for all pages, not just API routes
17-
target: 'server' | 'experimental-serverless-trace';
17+
target?: 'server' | 'experimental-serverless-trace';
1818
// the output directory for the built app (defaults to ".next")
19-
distDir: string;
19+
distDir?: string;
20+
// the root at which the nextjs app will be served (defaults to "/")
21+
basePath?: string;
22+
// config which will be available at runtime
23+
publicRuntimeConfig?: { [key: string]: unknown };
2024
sentry?: UserSentryOptions;
21-
} & {
22-
// other `next.config.js` options
23-
[key: string]: unknown;
2425
};
2526

2627
export type UserSentryOptions = {
@@ -40,10 +41,7 @@ export type UserSentryOptions = {
4041
widenClientFileUpload?: boolean;
4142
};
4243

43-
export type NextConfigFunction = (
44-
phase: string,
45-
defaults: { defaultConfig: NextConfigObject },
46-
) => Partial<NextConfigObject>;
44+
export type NextConfigFunction = (phase: string, defaults: { defaultConfig: NextConfigObject }) => NextConfigObject;
4745

4846
/**
4947
* Webpack config

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export { SentryWebpackPlugin };
3636
* @returns The function to set as the nextjs config's `webpack` value
3737
*/
3838
export function constructWebpackConfigFunction(
39-
userNextConfig: Partial<NextConfigObject> = {},
39+
userNextConfig: NextConfigObject = {},
4040
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
4141
userSentryOptions: UserSentryOptions = {},
4242
): WebpackConfigFunction {

packages/nextjs/test/config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ afterEach(() => {
6464
});
6565

6666
/** Mocks of the arguments passed to `withSentryConfig` */
67-
const userNextConfig: Partial<NextConfigObject> = {
67+
const userNextConfig: NextConfigObject = {
6868
publicRuntimeConfig: { location: 'dogpark', activities: ['fetch', 'chasing', 'digging'] },
6969
webpack: (config: WebpackConfigObject, _options: BuildContext) => ({
7070
...config,
@@ -124,7 +124,7 @@ const clientWebpackConfig = {
124124
// dynamically.
125125
function getBuildContext(
126126
buildTarget: 'server' | 'client',
127-
userNextConfig: Partial<NextConfigObject>,
127+
userNextConfig: NextConfigObject,
128128
webpackVersion: string = '5.4.15',
129129
): BuildContext {
130130
return {

0 commit comments

Comments
 (0)
0