Skip to content

Commit 7c51b9e

Browse files
authored
fix(nextjs): Ensure Webpack plugin is available after dynamic require (#8584)
For some reason, dynamic requires of our webpack plugin don't work for some users. We still don't have a better fix for #8541 so let's just get this in to unblock folks. closes #8576
1 parent 204d3e3 commit 7c51b9e

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable max-lines */
33
import { getSentryRelease } from '@sentry/node';
44
import { arrayify, dropUndefinedKeys, escapeStringForRegex, loadModule, logger } from '@sentry/utils';
5+
import type SentryCliPlugin from '@sentry/webpack-plugin';
56
import * as chalk from 'chalk';
67
import * as fs from 'fs';
78
import * as path from 'path';
@@ -312,15 +313,16 @@ export function constructWebpackConfigFunction(
312313
// without, the option to use `hidden-source-map` only applies to the client-side build.
313314
newConfig.devtool = userSentryOptions.hideSourceMaps && !isServer ? 'hidden-source-map' : 'source-map';
314315

315-
const SentryWebpackPlugin = loadModule('@sentry/webpack-plugin');
316-
317-
newConfig.plugins = newConfig.plugins || [];
318-
newConfig.plugins.push(
319-
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
320-
new SentryWebpackPlugin(
321-
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
322-
),
323-
);
316+
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
317+
if (SentryWebpackPlugin) {
318+
newConfig.plugins = newConfig.plugins || [];
319+
newConfig.plugins.push(
320+
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
321+
new SentryWebpackPlugin(
322+
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
323+
),
324+
);
325+
}
324326
}
325327
}
326328

@@ -769,10 +771,10 @@ function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions
769771
// architecture-specific version of the `sentry-cli` binary. If `yarn install`, `npm install`, or `npm ci` are run
770772
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
771773
// try to build their apps.
772-
const SentryWebpackPlugin = loadModule('@sentry/webpack-plugin');
774+
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
773775

774776
// @ts-expect-error - this exists, the dynamic import just doesn't know it
775-
if (!SentryWebpackPlugin.cliBinaryExists()) {
777+
if (!SentryWebpackPlugin || !SentryWebpackPlugin.cliBinaryExists()) {
776778
// eslint-disable-next-line no-console
777779
console.error(
778780
`${chalk.red('error')} - ${chalk.bold('Sentry CLI binary not found.')} Source maps will not be uploaded.\n`,

0 commit comments

Comments
 (0)