Skip to content
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

fix(nextjs): Make withSentryConfig return type match given config type #3760

Merged
merged 1 commit into from
Jun 29, 2021
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
33 changes: 16 additions & 17 deletions packages/nextjs/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ import { constructWebpackConfigFunction } from './webpack';
export function withSentryConfig(
userNextConfig: ExportedNextConfig = {},
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
): NextConfigFunction {
const newWebpackExport = constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions);

const finalNextConfig = (
phase: string,
defaults: { defaultConfig: { [key: string]: unknown } },
): NextConfigObject => {
const materializedUserNextConfig =
typeof userNextConfig === 'function' ? userNextConfig(phase, defaults) : userNextConfig;

return {
...materializedUserNextConfig,
// TODO When we add a way to disable the webpack plugin, doing so should turn this off, too
productionBrowserSourceMaps: true,
webpack: newWebpackExport,
};
): NextConfigFunction | NextConfigObject {
const partialConfig = {
// TODO When we add a way to disable the webpack plugin, doing so should turn this off, too
productionBrowserSourceMaps: true,
webpack: constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions),
};

return finalNextConfig;
// 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 (phase: string, defaults: { defaultConfig: { [key: string]: unknown } }): NextConfigObject => ({
...userNextConfig(phase, defaults),
...partialConfig,
});
}

// Otherwise, we can just merge their config with ours and return an object.
return { ...userNextConfig, ...partialConfig };
}
23 changes: 15 additions & 8 deletions packages/nextjs/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,31 @@ const clientWebpackConfig = {
const buildContext = { isServer: true, dev: false, buildId: 'doGsaREgReaT' };

/**
* Derive the final values of all next config options, by first applying `withSentryConfig` and then running the
* resulting function.
* Derive the final values of all next config options, by first applying `withSentryConfig` and then, if it returns a
* function, running that function.
*
* @param userNextConfig Next config options provided by the user
* @param userSentryWebpackPluginConfig SentryWebpackPlugin options provided by the user
*
* @returns The config values next will receive when it calls the function returned by `withSentryConfig`
* @returns The config values next will receive directly from `withSentryConfig` or when it calls the function returned
* by `withSentryConfig`
*/
function materializeFinalNextConfig(
userNextConfig: ExportedNextConfig,
userSentryWebpackPluginConfig: SentryWebpackPluginOptions,
): NextConfigObject {
const configFunction = withSentryConfig(userNextConfig, userSentryWebpackPluginConfig);
const finalConfigValues = configFunction('phase-production-build', {
defaultConfig: {},
});
const sentrifiedConfig = withSentryConfig(userNextConfig, userSentryWebpackPluginConfig);
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('phase-production-build', {
defaultConfig: {},
});
}

return finalConfigValues;
return finalConfigValues as NextConfigObject;
}

/**
Expand Down