Skip to content

Commit f8818d9

Browse files
committed
create function to calculate default plugin options and merge them with user options
1 parent 6691198 commit f8818d9

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getSentryRelease } from '@sentry/node';
22
import { dropUndefinedKeys, logger } from '@sentry/utils';
33
import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
4+
import * as fs from 'fs';
5+
import * as path from 'path';
46

57
import {
68
BuildContext,
@@ -22,18 +24,6 @@ export { SentryWebpackPlugin };
2224
export const CLIENT_SDK_CONFIG_FILE = './sentry.client.config.js';
2325
export const SERVER_SDK_CONFIG_FILE = './sentry.server.config.js';
2426

25-
const defaultSentryWebpackPluginOptions = dropUndefinedKeys({
26-
url: process.env.SENTRY_URL,
27-
org: process.env.SENTRY_ORG,
28-
project: process.env.SENTRY_PROJECT,
29-
authToken: process.env.SENTRY_AUTH_TOKEN,
30-
configFile: 'sentry.properties',
31-
stripPrefix: ['webpack://_N_E/'],
32-
urlPrefix: `~/_next`,
33-
include: '.next/',
34-
ignore: ['.next/cache', 'server/ssr-module-cache.js', 'static/*/_ssgManifest.js', 'static/*/_buildManifest.js'],
35-
});
36-
3727
/**
3828
* Construct the function which will be used as the nextjs config's `webpack` value.
3929
*
@@ -89,18 +79,11 @@ export function constructWebpackConfigFunction(
8979
newConfig.devtool = 'source-map';
9080
}
9181

92-
checkWebpackPluginOverrides(defaultSentryWebpackPluginOptions, userSentryWebpackPluginOptions);
93-
9482
newConfig.plugins = newConfig.plugins || [];
9583
newConfig.plugins.push(
9684
// @ts-ignore Our types for the plugin are messed up somehow - TS wants this to be `SentryWebpackPlugin.default`,
9785
// but that's not actually a thing
98-
new SentryWebpackPlugin({
99-
dryRun: buildContext.dev,
100-
release: getSentryRelease(buildContext.buildId),
101-
...defaultSentryWebpackPluginOptions,
102-
...userSentryWebpackPluginOptions,
103-
}),
86+
new SentryWebpackPlugin(getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions)),
10487
);
10588
}
10689

@@ -226,3 +209,46 @@ function checkWebpackPluginOverrides(
226209
function shouldAddSentryToEntryPoint(entryPointName: string): boolean {
227210
return entryPointName === 'pages/_app' || entryPointName.includes('pages/api');
228211
}
212+
213+
/**
214+
* Combine default and user-provided SentryWebpackPlugin options, accounting for whether we're building server files or
215+
* client files.
216+
*
217+
* @param buildContext Nexjs-provided data about the current build
218+
* @param userPluginOptions User-provided SentryWebpackPlugin options
219+
* @returns Final set of combined options
220+
*/
221+
function getWebpackPluginOptions(
222+
buildContext: BuildContext,
223+
userPluginOptions: Partial<SentryWebpackPluginOptions>,
224+
): SentryWebpackPluginOptions {
225+
const { isServer, dir: projectDir, buildId, dev: isDev } = buildContext;
226+
227+
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
228+
229+
const serverInclude = [
230+
{ path: '.next/server/chunks/', urlPrefix: '~/_next/server/chunks' },
231+
{ path: '.next/server/pages/', urlPrefix: '~/_next/server/pages' },
232+
{ path: '.next/serverless/', urlPrefix: '~/_next/serverless' },
233+
];
234+
const clientInclude = [{ path: '.next/static/chunks/pages', urlPrefix: '~/_next/static/chunks/pages' }];
235+
236+
const defaultPluginOptions = dropUndefinedKeys({
237+
include: isServer ? serverInclude : clientInclude,
238+
ignore: [],
239+
url: process.env.SENTRY_URL,
240+
org: process.env.SENTRY_ORG,
241+
project: process.env.SENTRY_PROJECT,
242+
authToken: process.env.SENTRY_AUTH_TOKEN,
243+
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
244+
stripPrefix: ['webpack://_N_E/'],
245+
urlPrefix: `~/_next`,
246+
entries: shouldAddSentryToEntryPoint,
247+
release: getSentryRelease(buildId),
248+
dryRun: isDev,
249+
});
250+
251+
checkWebpackPluginOverrides(defaultPluginOptions, userPluginOptions);
252+
253+
return { ...defaultPluginOptions, ...userPluginOptions };
254+
}

0 commit comments

Comments
 (0)