From 878d1e4d8c235d964263912d2618a7e1a47109eb Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 28 Jul 2023 11:37:18 +0200 Subject: [PATCH 1/6] Revert "fix(nextjs): Ensure Webpack plugin is available after dynamic require (#8584)" This reverts commit 7c51b9e9b3c8f9156a9e42f1a83395a41e9456a2. --- packages/nextjs/src/config/webpack.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 49f450bb27a4..56d0e64a9559 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -2,7 +2,6 @@ /* 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'; @@ -313,16 +312,15 @@ 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'); - 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), - ), - ); - } + 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), + ), + ); } } @@ -771,10 +769,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('@sentry/webpack-plugin'); // @ts-expect-error - this exists, the dynamic import just doesn't know it - if (!SentryWebpackPlugin || !SentryWebpackPlugin.cliBinaryExists()) { + if (!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`, From 7fd2d85020262d86b155d468c9ff2af202d88003 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 28 Jul 2023 11:37:27 +0200 Subject: [PATCH 2/6] Revert "fix(nextjs): Avoid importing `SentryWebpackPlugin` in dev mode (#8557)" This reverts commit 0ca73897a895d1673d0a7b2c3145b10dd52953f3. --- packages/nextjs/src/config/webpack.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 56d0e64a9559..e406c3da1560 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -1,7 +1,8 @@ /* eslint-disable complexity */ /* eslint-disable max-lines */ import { getSentryRelease } from '@sentry/node'; -import { arrayify, dropUndefinedKeys, escapeStringForRegex, loadModule, logger } from '@sentry/utils'; +import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger } from '@sentry/utils'; +import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin'; import * as chalk from 'chalk'; import * as fs from 'fs'; import * as path from 'path'; @@ -312,11 +313,8 @@ 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), ), @@ -769,9 +767,6 @@ 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'); - - // @ts-expect-error - this exists, the dynamic import just doesn't know it if (!SentryWebpackPlugin.cliBinaryExists()) { // eslint-disable-next-line no-console console.error( From e75b825475b18acf651d9d5420ad36b588931166 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 28 Jul 2023 12:45:20 +0200 Subject: [PATCH 3/6] bump e2e test to latest next version --- .../e2e-tests/test-applications/nextjs-app-dir/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/package.json b/packages/e2e-tests/test-applications/nextjs-app-dir/package.json index d8701ef8df7c..08509afe0264 100644 --- a/packages/e2e-tests/test-applications/nextjs-app-dir/package.json +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/package.json @@ -19,7 +19,7 @@ "@types/node": "18.11.17", "@types/react": "18.0.26", "@types/react-dom": "18.0.9", - "next": "13.2.4", + "next": "13.4.12", "react": "18.2.0", "react-dom": "18.2.0", "typescript": "4.9.5", From a8b6e79056e4b482fb984a100a1d4bc2b1cffb49 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 31 Jul 2023 17:57:20 +0200 Subject: [PATCH 4/6] require `constructWebpackConfigFunction` instead of importing it --- packages/nextjs/src/config/withSentryConfig.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index 86f8c5a67031..a22928c8352b 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -6,7 +6,8 @@ import type { SentryWebpackPluginOptions, UserSentryOptions, } from './types'; -import { constructWebpackConfigFunction } from './webpack'; +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { constructWebpackConfigFunction } = require('./webpack'); let showedExportModeTunnelWarning = false; From 171bd9ac9aad89b9637866dcc1b734973c7c5aa6 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 1 Aug 2023 10:12:46 +0200 Subject: [PATCH 5/6] move req to getFinalConfigObject function --- packages/nextjs/src/config/withSentryConfig.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index a22928c8352b..1fa2e957885c 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -6,8 +6,6 @@ import type { SentryWebpackPluginOptions, UserSentryOptions, } from './types'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { constructWebpackConfigFunction } = require('./webpack'); let showedExportModeTunnelWarning = false; @@ -47,6 +45,8 @@ function getFinalConfigObject( userSentryOptions: UserSentryOptions, userSentryWebpackPluginOptions: Partial, ): NextConfigObject { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { constructWebpackConfigFunction } = require('./webpack'); // Next 12.2.3+ warns about non-canonical properties on `userNextConfig`. delete incomingUserNextConfigObject.sentry; From 9994a3438f8b0d148541d2f2b8a54f591f1b59c5 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 1 Aug 2023 10:54:33 +0200 Subject: [PATCH 6/6] mod.require --- packages/nextjs/src/config/withSentryConfig.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index 1fa2e957885c..a9405913560a 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -45,8 +45,7 @@ function getFinalConfigObject( userSentryOptions: UserSentryOptions, userSentryWebpackPluginOptions: Partial, ): NextConfigObject { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const { constructWebpackConfigFunction } = require('./webpack'); + const { constructWebpackConfigFunction } = module.require('./webpack'); // Next 12.2.3+ warns about non-canonical properties on `userNextConfig`. delete incomingUserNextConfigObject.sentry;