Skip to content

Commit

Permalink
exclude build-time files
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Nov 22, 2022
1 parent b945fef commit c1d8af4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/nextjs/src/config/types.ts
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion packages/nextjs/src/config/webpack.ts
Expand Up @@ -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,
Expand All @@ -16,6 +18,7 @@ import {
WebpackConfigObject,
WebpackEntryProperty,
WebpackModuleRule,
WebpackPluginInstance,
} from './types';

export { SentryWebpackPlugin };
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c1d8af4

Please sign in to comment.