Skip to content

Commit 5eafa40

Browse files
authored
fix(nextjs): Attempt to ignore critical dependency warnings (#12694)
1 parent c7b8503 commit 5eafa40

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,22 @@ export type NextConfigFunction = (
404404
* Webpack config
405405
*/
406406

407+
// Note: The interface for `ignoreWarnings` is larger but we only need this. See https://webpack.js.org/configuration/other-options/#ignorewarnings
408+
export type IgnoreWarningsOption = (
409+
| { module?: RegExp; message?: RegExp }
410+
| ((
411+
webpackError: {
412+
module?: {
413+
readableIdentifier: (requestShortener: unknown) => string;
414+
};
415+
message: string;
416+
},
417+
compilation: {
418+
requestShortener: unknown;
419+
},
420+
) => boolean)
421+
)[];
422+
407423
// The two possible formats for providing custom webpack config in `next.config.js`
408424
export type WebpackConfigFunction = (config: WebpackConfigObject, options: BuildContext) => WebpackConfigObject;
409425
export type WebpackConfigObject = {
@@ -413,7 +429,7 @@ export type WebpackConfigObject = {
413429
output: { filename: string; path: string };
414430
target: string;
415431
context: string;
416-
ignoreWarnings?: { module?: RegExp }[]; // Note: The interface for `ignoreWarnings` is larger but we only need this. See https://webpack.js.org/configuration/other-options/#ignorewarnings
432+
ignoreWarnings?: IgnoreWarningsOption;
417433
resolve?: {
418434
modules?: string[];
419435
alias?: { [key: string]: string | boolean };

packages/nextjs/src/config/webpack.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { VercelCronsConfig } from '../common/types';
1414
import type {
1515
BuildContext,
1616
EntryPropertyObject,
17+
IgnoreWarningsOption,
1718
NextConfigObject,
1819
SentryBuildOptions,
1920
WebpackConfigFunction,
@@ -72,9 +73,7 @@ export function constructWebpackConfigFunction(
7273
// Add a loader which will inject code that sets global values
7374
addValueInjectionLoader(newConfig, userNextConfig, userSentryOptions, buildContext);
7475

75-
if (isServer) {
76-
addOtelWarningIgnoreRule(newConfig);
77-
}
76+
addOtelWarningIgnoreRule(newConfig);
7877

7978
let pagesDirPath: string | undefined;
8079
const maybePagesDirPath = path.join(projectDir, 'pages');
@@ -668,9 +667,28 @@ function getRequestAsyncStorageModuleLocation(
668667

669668
function addOtelWarningIgnoreRule(newConfig: WebpackConfigObjectWithModuleRules): void {
670669
const ignoreRules = [
670+
// Inspired by @matmannion: https://github.com/getsentry/sentry-javascript/issues/12077#issuecomment-2180307072
671+
(warning, compilation) => {
672+
// This is wapped in try-catch because we are vendoring types for this hook and we can't be 100% sure that we are accessing API that is there
673+
try {
674+
if (!warning.module) {
675+
return false;
676+
}
677+
678+
const isDependencyThatMayRaiseCriticalDependencyMessage =
679+
/@opentelemetry\/instrumentation/.test(warning.module.readableIdentifier(compilation.requestShortener)) ||
680+
/@prisma\/instrumentation/.test(warning.module.readableIdentifier(compilation.requestShortener));
681+
const isCriticalDependencyMessage = /Critical dependency/.test(warning.message);
682+
683+
return isDependencyThatMayRaiseCriticalDependencyMessage && isCriticalDependencyMessage;
684+
} catch {
685+
return false;
686+
}
687+
},
688+
// We provide these objects in addition to the hook above to provide redundancy in case the hook fails.
671689
{ module: /@opentelemetry\/instrumentation/, message: /Critical dependency/ },
672690
{ module: /@prisma\/instrumentation/, message: /Critical dependency/ },
673-
];
691+
] satisfies IgnoreWarningsOption;
674692

675693
if (newConfig.ignoreWarnings === undefined) {
676694
newConfig.ignoreWarnings = ignoreRules;

0 commit comments

Comments
 (0)