Skip to content

Commit a8499e4

Browse files
committed
exclude build-time files
1 parent f8de61c commit a8499e4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { WebpackPluginInstance } from 'webpack';
33

44
export type SentryWebpackPluginOptions = SentryCliPluginOptions;
55
export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpackPluginOptions };
6+
// Export this from here because importing something from Webpack (the library) in `webpack.ts` confuses the heck out of
7+
// madge, which we use for circular dependency checking. We've manually excluded this file from the check (which is
8+
// safe, since it only includes types), so we can import it here without causing madge to fail. See
9+
// https://github.com/pahen/madge/issues/306.
10+
export type { WebpackPluginInstance };
611

712
/**
813
* Overall Nextjs config

packages/nextjs/src/config/webpack.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import * as chalk from 'chalk';
66
import * as fs from 'fs';
77
import * as path from 'path';
88

9-
import {
9+
// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our
10+
// circular dependency check thinks this file is importing from itself. See https://github.com/pahen/madge/issues/306.
11+
import type {
1012
BuildContext,
1113
EntryPropertyObject,
1214
NextConfigObject,
@@ -16,6 +18,7 @@ import {
1618
WebpackConfigObject,
1719
WebpackEntryProperty,
1820
WebpackModuleRule,
21+
WebpackPluginInstance,
1922
} from './types';
2023

2124
export { SentryWebpackPlugin };
@@ -100,6 +103,23 @@ export function constructWebpackConfigFunction(
100103
],
101104
});
102105
}
106+
107+
// Prevent `@vercel/nft` (which nextjs uses to determine which files are needed when packaging up a lambda) from
108+
// including any of our build-time code or dependencies. (Otherwise it'll include files like this one and even the
109+
// entirety of rollup and sucrase.)
110+
const nftPlugin = newConfig.plugins?.find((plugin: WebpackPluginInstance) => {
111+
const proto = Object.getPrototypeOf(plugin) as WebpackPluginInstance;
112+
return proto.constructor.name === 'TraceEntryPointsPlugin';
113+
}) as WebpackPluginInstance & { excludeFiles: string[] };
114+
if (nftPlugin) {
115+
nftPlugin.excludeFiles = nftPlugin.excludeFiles || [];
116+
nftPlugin.excludeFiles.push(path.join(__dirname, 'withSentryConfig.js'));
117+
} else {
118+
__DEBUG_BUILD__ &&
119+
logger.warn(
120+
'Unable to exclude Sentry build-time helpers from nft files. Could not find `TraceEntryPointsPlugin`.',
121+
);
122+
}
103123
}
104124

105125
// The SDK uses syntax (ES6 and ES6+ features like object spread) which isn't supported by older browsers. For users

0 commit comments

Comments
 (0)