Skip to content

fix(nextjs): Apply Webpack configuration in dev mode #6291

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 2 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 11 additions & 14 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild } from '../utils/isBuild';
import { NEXT_PHASE_DEVELOPMENT_SERVER, NEXT_PHASE_PRODUCTION_BUILD } from '../utils/phases';
import type {
ExportedNextConfig,
NextConfigFunction,
Expand All @@ -18,23 +18,20 @@ export function withSentryConfig(
exportedUserNextConfig: ExportedNextConfig = {},
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
): NextConfigFunction | 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 exportedUserNextConfig === 'function') {
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): NextConfigObject {
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): NextConfigObject {
if (typeof exportedUserNextConfig === 'function') {
const userNextConfigObject = exportedUserNextConfig(phase, defaults);

return getFinalConfigObject(userNextConfigObject, userSentryWebpackPluginOptions);
};
}

// Otherwise, we can just merge their config with ours and return an object.
return getFinalConfigObject(exportedUserNextConfig, userSentryWebpackPluginOptions);
return getFinalConfigObject(phase, userNextConfigObject, userSentryWebpackPluginOptions);
} else {
return getFinalConfigObject(phase, exportedUserNextConfig, userSentryWebpackPluginOptions);
}
};
}

// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
// `webpack` property
function getFinalConfigObject(
phase: string,
incomingUserNextConfigObject: NextConfigObjectWithSentry,
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>,
): NextConfigObject {
Expand All @@ -48,8 +45,8 @@ function getFinalConfigObject(

// In order to prevent all of our build-time code from being bundled in people's route-handling serverless functions,
// we exclude `webpack.ts` and all of its dependencies from nextjs's `@vercel/nft` filetracing. We therefore need to
// make sure that we only require it at build time.
if (isBuild()) {
// make sure that we only require it at build time or in development mode.
if (phase === NEXT_PHASE_PRODUCTION_BUILD || phase === NEXT_PHASE_DEVELOPMENT_SERVER) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { constructWebpackConfigFunction } = require('./webpack');
return {
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/src/utils/isBuild.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NEXT_PHASE_PRODUCTION_BUILD } from './phases';

/**
* Decide if the currently running process is part of the build phase or happening at runtime.
*/
Expand All @@ -18,7 +20,7 @@ export function isBuild(): boolean {
process.env.SENTRY_BUILD_PHASE ||
// This is set by next, but not until partway through the build process, which is why we need the above checks. That
// said, in case this function isn't called until we're in a child process, it can serve as a good backup.
process.env.NEXT_PHASE === 'phase-production-build'
process.env.NEXT_PHASE === NEXT_PHASE_PRODUCTION_BUILD
) {
process.env.SENTRY_BUILD_PHASE = 'true';
return true;
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/src/utils/phases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NEXT_PHASE_PRODUCTION_BUILD = 'phase-production-build';
export const NEXT_PHASE_PRODUCTION_SERVER = 'phase-production-server';
export const NEXT_PHASE_DEVELOPMENT_SERVER = 'phase-development-server';