Skip to content

Commit

Permalink
fix(nextjs): Ensure Webpack plugin is available after dynamic require (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
Lms24 committed Jul 19, 2023
1 parent 204d3e3 commit 7c51b9e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable max-lines */
import { getSentryRelease } from '@sentry/node';
import { arrayify, dropUndefinedKeys, escapeStringForRegex, loadModule, logger } from '@sentry/utils';
import type SentryCliPlugin from '@sentry/webpack-plugin';
import * as chalk from 'chalk';
import * as fs from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -312,15 +313,16 @@ export function constructWebpackConfigFunction(
// without, the option to use `hidden-source-map` only applies to the client-side build.
newConfig.devtool = userSentryOptions.hideSourceMaps && !isServer ? 'hidden-source-map' : 'source-map';

const SentryWebpackPlugin = loadModule('@sentry/webpack-plugin');

newConfig.plugins = newConfig.plugins || [];
newConfig.plugins.push(
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
new SentryWebpackPlugin(
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
),
);
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
if (SentryWebpackPlugin) {
newConfig.plugins = newConfig.plugins || [];
newConfig.plugins.push(
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
new SentryWebpackPlugin(
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
),
);
}
}
}

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

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

0 comments on commit 7c51b9e

Please sign in to comment.