Skip to content

Commit 0ca7389

Browse files
authored
fix(nextjs): Avoid importing SentryWebpackPlugin in dev mode (#8557)
As reported in #8541, our NextJS SDK currently breaks dev mode for the newest NextJS 13.4.10 version I still have absolutely no idea which of the changes in [13.4.10](https://github.com/vercel/next.js/releases/tag/v13.4.10) is causing this. However, I traced the error back and it happens as soon as our NextJS SDK package requires @sentry/webpack-plugin: * @sentry/nextjs calls `require('@sentry/webpack-plugin')` * @sentry/webpack-plugin calls `const { RawSource } = require('webpack-sources');` * For _whatever_ reason, NextJS can't require `webpack-sources` and throws Since we don't enable our Webpack plugin [in dev mode](https://github.com/getsentry/sentry-javascript/blob/723f851f358b75cd39da353804c51ff27ebb0c11/packages/nextjs/src/config/webpack.ts#L305) anyway, one way to get rid of this error is to only require it if we're _not_ in dev mode. This hotfix therefore moves the top-level require of `@sentry/webpack-plugin` to a dynamic require. This isn't a great solution and honestly quite ugly but if it unblocks users for now I'd say we merge it. I think we should definitely revisit this though once we know more about why NextJS suddenly isn't able to import `webpack-sources`. closes #8541
1 parent 2919511 commit 0ca7389

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-disable complexity */
22
/* eslint-disable max-lines */
33
import { getSentryRelease } from '@sentry/node';
4-
import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger } from '@sentry/utils';
5-
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
4+
import { arrayify, dropUndefinedKeys, escapeStringForRegex, loadModule, logger } from '@sentry/utils';
65
import * as chalk from 'chalk';
76
import * as fs from 'fs';
87
import * as path from 'path';
@@ -313,8 +312,11 @@ export function constructWebpackConfigFunction(
313312
// without, the option to use `hidden-source-map` only applies to the client-side build.
314313
newConfig.devtool = userSentryOptions.hideSourceMaps && !isServer ? 'hidden-source-map' : 'source-map';
315314

315+
const SentryWebpackPlugin = loadModule('@sentry/webpack-plugin');
316+
316317
newConfig.plugins = newConfig.plugins || [];
317318
newConfig.plugins.push(
319+
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
318320
new SentryWebpackPlugin(
319321
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
320322
),
@@ -767,6 +769,9 @@ function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions
767769
// architecture-specific version of the `sentry-cli` binary. If `yarn install`, `npm install`, or `npm ci` are run
768770
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
769771
// try to build their apps.
772+
const SentryWebpackPlugin = loadModule('@sentry/webpack-plugin');
773+
774+
// @ts-expect-error - this exists, the dynamic import just doesn't know it
770775
if (!SentryWebpackPlugin.cliBinaryExists()) {
771776
// eslint-disable-next-line no-console
772777
console.error(

0 commit comments

Comments
 (0)