From ff18dfd522b06c8c97b5a084a84661e42de33ed5 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Tue, 5 Nov 2024 13:56:16 +0100 Subject: [PATCH] fix(nextjs): Resolve path for dynamic webpack import (#13751) --- packages/nextjs/src/config/webpack.ts | 3 ++- packages/utils/src/node.ts | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 79bd230a3ee7..6b96b96ecec1 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -332,7 +332,8 @@ export function constructWebpackConfigFunction( // Symbolication for dev-mode errors is done elsewhere. if (!isDev) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { sentryWebpackPlugin } = loadModule('@sentry/webpack-plugin') as any; + const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin') ?? {}; + if (sentryWebpackPlugin) { if (!userSentryOptions.sourcemaps?.disable) { // `hidden-source-map` produces the same sourcemaps as `source-map`, but doesn't include the `sourceMappingURL` diff --git a/packages/utils/src/node.ts b/packages/utils/src/node.ts index 39b4258eccc7..8640a5a95801 100644 --- a/packages/utils/src/node.ts +++ b/packages/utils/src/node.ts @@ -52,11 +52,13 @@ export function loadModule(moduleName: string): T | undefined { // no-empty } - try { - const { cwd } = dynamicRequire(module, 'process'); - mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T; - } catch (e) { - // no-empty + if (!mod) { + try { + const { cwd } = dynamicRequire(module, 'process'); + mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T; + } catch (e) { + // no-empty + } } return mod;