Skip to content

Commit fc84c0d

Browse files
committed
move webpack plugin enablement check to function
1 parent 68eef79 commit fc84c0d

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,7 @@ export function constructWebpackConfigFunction(
134134
newConfig.entry = async () => addSentryToEntryProperty(origEntryProperty, buildContext);
135135

136136
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
137-
const enableWebpackPlugin =
138-
// TODO: this is a hack to fix https://github.com/getsentry/sentry-cli/issues/1085, which is caused by
139-
// https://github.com/getsentry/sentry-cli/issues/915. Once the latter is addressed, this existence check can come
140-
// out. (The check is necessary because currently, `@sentry/cli` uses a post-install script to download an
141-
// architecture-specific version of the `sentry-cli` binary. If `yarn install`, `npm install`, or `npm ci` are run
142-
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
143-
// try to build their apps.)
144-
ensureCLIBinaryExists() &&
145-
(isServer ? !userSentryOptions.disableServerWebpackPlugin : !userSentryOptions.disableClientWebpackPlugin);
146-
147-
if (enableWebpackPlugin) {
137+
if (shouldEnableWebpackPlugin(buildContext, userSentryOptions)) {
148138
// TODO Handle possibility that user is using `SourceMapDevToolPlugin` (see
149139
// https://webpack.js.org/plugins/source-map-dev-tool-plugin/)
150140

@@ -460,3 +450,41 @@ function ensureCLIBinaryExists(): boolean {
460450
}
461451
return false;
462452
}
453+
454+
/** Check various conditions to decide if we should run the plugin */
455+
function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions: UserSentryOptions): boolean {
456+
const { isServer, dev: isDev } = buildContext;
457+
const { disableServerWebpackPlugin, disableClientWebpackPlugin } = userSentryOptions;
458+
459+
/** Non-negotiable */
460+
461+
// TODO: this is a hack to fix https://github.com/getsentry/sentry-cli/issues/1085, which is caused by
462+
// https://github.com/getsentry/sentry-cli/issues/915. Once the latter is addressed, this existence check can come
463+
// out. (The check is necessary because currently, `@sentry/cli` uses a post-install script to download an
464+
// architecture-specific version of the `sentry-cli` binary. If `yarn install`, `npm install`, or `npm ci` are run
465+
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
466+
// try to build their apps.)
467+
if (!ensureCLIBinaryExists()) {
468+
return false;
469+
}
470+
471+
/** User override */
472+
473+
if ((isServer && disableServerWebpackPlugin) || (!isServer && disableClientWebpackPlugin)) {
474+
return false;
475+
}
476+
477+
/** Situations where the default is to disable the plugin */
478+
479+
// TODO: Are there analogs to Vercel's preveiw and dev modes on other deployment platforms?
480+
481+
if (isDev || process.env.NODE_ENV === 'development') {
482+
// TODO (v8): Right now in dev we set the plugin to dryrun mode, and our boilerplate includes setting the plugin to
483+
// `silent`, so for the vast majority of users, it's as if the plugin doesn't run at all in dev. Making that
484+
// official is technically a breaking change, though, so we probably should wait until v8.
485+
// return false
486+
}
487+
488+
// We've passed all of the tests!
489+
return true;
490+
}

0 commit comments

Comments
 (0)