Skip to content

ref: Allow withSentryConfig to accept async config function #8721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type NextConfigObjectWithSentry = NextConfigObject & {
export type NextConfigFunctionWithSentry = (
phase: string,
defaults: { defaultConfig: NextConfigObject },
) => NextConfigObjectWithSentry;
) => NextConfigObjectWithSentry | PromiseLike<NextConfigObjectWithSentry>;

// Vendored from Next.js (this type is not complete - extend if necessary)
type NextRewrite = {
Expand Down Expand Up @@ -144,7 +144,10 @@ export type UserSentryOptions = {
automaticVercelMonitors?: boolean;
};

export type NextConfigFunction = (phase: string, defaults: { defaultConfig: NextConfigObject }) => NextConfigObject;
export type NextConfigFunction = (
phase: string,
defaults: { defaultConfig: NextConfigObject },
) => NextConfigObject | PromiseLike<NextConfigObject>;

/**
* Webpack config
Expand Down
15 changes: 13 additions & 2 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isThenable } from '@sentry/utils';

import type {
ExportedNextConfig,
NextConfigFunction,
Expand All @@ -24,12 +26,21 @@ export function withSentryConfig(
sentryOptions?: UserSentryOptions,
): NextConfigFunction | NextConfigObject {
if (typeof exportedUserNextConfig === 'function') {
return function (this: unknown, ...webpackConfigFunctionArgs: unknown[]): NextConfigObject {
const userNextConfigObject: NextConfigObjectWithSentry = exportedUserNextConfig.apply(
return function (this: unknown, ...webpackConfigFunctionArgs: unknown[]): ReturnType<NextConfigFunction> {
const maybeUserNextConfigObject: NextConfigObjectWithSentry = exportedUserNextConfig.apply(
this,
webpackConfigFunctionArgs,
);

if (isThenable(maybeUserNextConfigObject)) {
return maybeUserNextConfigObject.then(function (userNextConfigObject: NextConfigObjectWithSentry) {
const userSentryOptions = { ...userNextConfigObject.sentry, ...sentryOptions };
return getFinalConfigObject(userNextConfigObject, userSentryOptions, userSentryWebpackPluginOptions);
});
}

// Reassign for naming-consistency sake.
const userNextConfigObject = maybeUserNextConfigObject;
const userSentryOptions = { ...userNextConfigObject.sentry, ...sentryOptions };
return getFinalConfigObject(userNextConfigObject, userSentryOptions, userSentryWebpackPluginOptions);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/test/config/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function materializeFinalNextConfig(
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);
finalConfigValues = sentrifiedConfig(runtimePhase ?? defaultRuntimePhase, defaultsObject) as NextConfigObject;
}

return finalConfigValues as NextConfigObject;
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function materializeFinalWebpackConfig(options: {
// if the user's next config is a function, run it so we have access to the values
const materializedUserNextConfig =
typeof exportedNextConfig === 'function'
? exportedNextConfig('phase-production-build', defaultsObject)
? await exportedNextConfig('phase-production-build', defaultsObject)
: exportedNextConfig;

// extract the `sentry` property as we do in `withSentryConfig`
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/test/integration/next12.config.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { withSentryConfig } = require('@sentry/nextjs');

const moduleExports = {
const moduleExports = async () => ({
eslint: {
ignoreDuringBuilds: true,
},
Expand All @@ -11,7 +11,7 @@ const moduleExports = {
hideSourceMaps: false,
excludeServerRoutes: ['/api/excludedEndpoints/excludedWithString', /\/api\/excludedEndpoints\/excludedWithRegExp/],
},
};
});

const SentryWebpackPluginOptions = {
dryRun: true,
Expand Down