Skip to content

Commit cffcefa

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

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ export { SentryWebpackPlugin };
2121
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
2222
// TODO: drop merged keys from override check? `includeDefaults` option?
2323

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

91-
checkWebpackPluginOverrides(defaultSentryWebpackPluginOptions, userSentryWebpackPluginOptions);
92-
9379
newConfig.plugins = newConfig.plugins || [];
9480
newConfig.plugins.push(
9581
// @ts-ignore Our types for the plugin are messed up somehow - TS wants this to be `SentryWebpackPlugin.default`,
9682
// but that's not actually a thing
97-
new SentryWebpackPlugin({
98-
dryRun: buildContext.dev,
99-
release: getSentryRelease(buildContext.buildId),
100-
...defaultSentryWebpackPluginOptions,
101-
...userSentryWebpackPluginOptions,
102-
}),
83+
new SentryWebpackPlugin(getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions)),
10384
);
10485
}
10586

@@ -248,3 +229,46 @@ function checkWebpackPluginOverrides(
248229
function shouldAddSentryToEntryPoint(entryPointName: string): boolean {
249230
return entryPointName === 'pages/_app' || entryPointName.includes('pages/api');
250231
}
232+
233+
/**
234+
* Combine default and user-provided SentryWebpackPlugin options, accounting for whether we're building server files or
235+
* client files.
236+
*
237+
* @param buildContext Nexjs-provided data about the current build
238+
* @param userPluginOptions User-provided SentryWebpackPlugin options
239+
* @returns Final set of combined options
240+
*/
241+
function getWebpackPluginOptions(
242+
buildContext: BuildContext,
243+
userPluginOptions: Partial<SentryWebpackPluginOptions>,
244+
): SentryWebpackPluginOptions {
245+
const { isServer, dir: projectDir, buildId, dev: isDev } = buildContext;
246+
247+
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
248+
249+
const serverInclude = [
250+
{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' },
251+
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
252+
{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' },
253+
];
254+
const clientInclude = [{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/_next/static/chunks/pages' }];
255+
256+
const defaultPluginOptions = dropUndefinedKeys({
257+
include: isServer ? serverInclude : clientInclude,
258+
ignore: [],
259+
url: process.env.SENTRY_URL,
260+
org: process.env.SENTRY_ORG,
261+
project: process.env.SENTRY_PROJECT,
262+
authToken: process.env.SENTRY_AUTH_TOKEN,
263+
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
264+
stripPrefix: ['webpack://_N_E/'],
265+
urlPrefix: `~/_next`,
266+
entries: shouldAddSentryToEntryPoint,
267+
release: getSentryRelease(buildId),
268+
dryRun: isDev,
269+
});
270+
271+
checkWebpackPluginOverrides(defaultPluginOptions, userPluginOptions);
272+
273+
return { ...defaultPluginOptions, ...userPluginOptions };
274+
}

0 commit comments

Comments
 (0)