diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 26279d19271c..dc951d35aefe 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -3,6 +3,11 @@ import { WebpackPluginInstance } from 'webpack'; export type SentryWebpackPluginOptions = SentryCliPluginOptions; export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpackPluginOptions }; +// Export this from here because importing something from Webpack (the library) in `webpack.ts` confuses the heck out of +// madge, which we use for circular dependency checking. We've manually excluded this file from the check (which is +// safe, since it only includes types), so we can import it here without causing madge to fail. See +// https://github.com/pahen/madge/issues/306. +export type { WebpackPluginInstance }; /** * Overall Nextjs config diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 81e38cee6007..739c93021a0b 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -6,7 +6,9 @@ import * as chalk from 'chalk'; import * as fs from 'fs'; import * as path from 'path'; -import { +// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our +// circular dependency check thinks this file is importing from itself. See https://github.com/pahen/madge/issues/306. +import type { BuildContext, EntryPropertyObject, NextConfigObject, @@ -16,6 +18,7 @@ import { WebpackConfigObject, WebpackEntryProperty, WebpackModuleRule, + WebpackPluginInstance, } from './types'; export { SentryWebpackPlugin }; @@ -100,6 +103,23 @@ export function constructWebpackConfigFunction( ], }); } + + // Prevent `@vercel/nft` (which nextjs uses to determine which files are needed when packaging up a lambda) from + // including any of our build-time code or dependencies. (Otherwise it'll include files like this one and even the + // entirety of rollup and sucrase.) + const nftPlugin = newConfig.plugins?.find((plugin: WebpackPluginInstance) => { + const proto = Object.getPrototypeOf(plugin) as WebpackPluginInstance; + return proto.constructor.name === 'TraceEntryPointsPlugin'; + }) as WebpackPluginInstance & { excludeFiles: string[] }; + if (nftPlugin) { + nftPlugin.excludeFiles = nftPlugin.excludeFiles || []; + nftPlugin.excludeFiles.push(path.join(__dirname, 'withSentryConfig.js')); + } else { + __DEBUG_BUILD__ && + logger.warn( + 'Unable to exclude Sentry build-time helpers from nft files. Could not find `TraceEntryPointsPlugin`.', + ); + } } // The SDK uses syntax (ES6 and ES6+ features like object spread) which isn't supported by older browsers. For users