Skip to content

Commit 9e9a7bb

Browse files
committed
move userNextConfig.sentry into a closure in constructWebpackConfigFunction
1 parent 87eace1 commit 9e9a7bb

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

packages/nextjs/src/config/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,33 @@ export function withSentryConfig(
1717
if (typeof userNextConfig === 'function') {
1818
return function (phase: string, defaults: { defaultConfig: NextConfigObject }): Partial<NextConfigObject> {
1919
const materializedUserNextConfig = userNextConfig(phase, defaults);
20+
21+
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`, so grab and then remove the `sentry`
22+
// property there. Where we actually need it is in the webpack config function we're going to create, so pass it
23+
// to `constructWebpackConfigFunction` so that it will be in the created function's closure.
24+
const { sentry: userSentryOptions } = materializedUserNextConfig;
25+
delete materializedUserNextConfig.sentry;
26+
2027
return {
2128
...materializedUserNextConfig,
22-
webpack: constructWebpackConfigFunction(materializedUserNextConfig, userSentryWebpackPluginOptions),
29+
webpack: constructWebpackConfigFunction(
30+
materializedUserNextConfig,
31+
userSentryWebpackPluginOptions,
32+
userSentryOptions,
33+
),
2334
};
2435
};
2536
}
2637

2738
// Otherwise, we can just merge their config with ours and return an object.
39+
40+
// Prevent nextjs from getting mad about having a non-standard config property in `userNextConfig`. (See note above
41+
// for a more thorough explanation of what we're doing here.)
42+
const { sentry: userSentryOptions } = userNextConfig;
43+
delete userNextConfig.sentry;
44+
2845
return {
2946
...userNextConfig,
30-
webpack: constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions),
47+
webpack: constructWebpackConfigFunction(userNextConfig, userSentryWebpackPluginOptions, userSentryOptions),
3148
};
3249
}

packages/nextjs/src/config/webpack.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
EntryPropertyObject,
1111
NextConfigObject,
1212
SentryWebpackPluginOptions,
13+
UserSentryOptions,
1314
WebpackConfigFunction,
1415
WebpackConfigObject,
1516
WebpackEntryProperty,
@@ -36,6 +37,7 @@ export { SentryWebpackPlugin };
3637
export function constructWebpackConfigFunction(
3738
userNextConfig: Partial<NextConfigObject> = {},
3839
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
40+
userSentryOptions: UserSentryOptions = {},
3941
): WebpackConfigFunction {
4042
// Will be called by nextjs and passed its default webpack configuration and context data about the build (whether
4143
// we're building server or client, whether we're in dev, what version of webpack we're using, etc). Note that
@@ -93,9 +95,7 @@ export function constructWebpackConfigFunction(
9395
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
9496
// try to build their apps.)
9597
ensureCLIBinaryExists() &&
96-
(isServer
97-
? !userNextConfig.sentry?.disableServerWebpackPlugin
98-
: !userNextConfig.sentry?.disableClientWebpackPlugin);
98+
(isServer ? !userSentryOptions.disableServerWebpackPlugin : !userSentryOptions.disableClientWebpackPlugin);
9999

100100
if (enableWebpackPlugin) {
101101
// TODO Handle possibility that user is using `SourceMapDevToolPlugin` (see
@@ -109,12 +109,14 @@ export function constructWebpackConfigFunction(
109109
// the browser won't look for them and throw errors into the console when it can't find them. Because this is a
110110
// front-end-only problem, and because `sentry-cli` handles sourcemaps more reliably with the comment than
111111
// without, the option to use `hidden-source-map` only applies to the client-side build.
112-
newConfig.devtool = userNextConfig.sentry?.hideSourceMaps && !isServer ? 'hidden-source-map' : 'source-map';
112+
newConfig.devtool = userSentryOptions.hideSourceMaps && !isServer ? 'hidden-source-map' : 'source-map';
113113
}
114114

115115
newConfig.plugins = newConfig.plugins || [];
116116
newConfig.plugins.push(
117-
new SentryWebpackPlugin(getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions)),
117+
new SentryWebpackPlugin(
118+
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
119+
),
118120
);
119121
}
120122

@@ -286,6 +288,7 @@ function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean):
286288
export function getWebpackPluginOptions(
287289
buildContext: BuildContext,
288290
userPluginOptions: Partial<SentryWebpackPluginOptions>,
291+
userSentryOptions: UserSentryOptions,
289292
): SentryWebpackPluginOptions {
290293
const { buildId, isServer, webpack, config: userNextConfig, dev: isDev, dir: projectDir } = buildContext;
291294
const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory
@@ -301,14 +304,14 @@ export function getWebpackPluginOptions(
301304
isWebpack5 ? [{ paths: [`${distDir}/server/chunks/`], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
302305
);
303306

304-
const clientInclude = userNextConfig.sentry?.widenClientFileUpload
307+
const clientInclude = userSentryOptions.widenClientFileUpload
305308
? [{ paths: [`${distDir}/static/chunks`], urlPrefix: `${urlPrefix}/static/chunks` }]
306309
: [{ paths: [`${distDir}/static/chunks/pages`], urlPrefix: `${urlPrefix}/static/chunks/pages` }];
307310

308311
const defaultPluginOptions = dropUndefinedKeys({
309312
include: isServer ? serverInclude : clientInclude,
310313
ignore:
311-
isServer || !userNextConfig.sentry?.widenClientFileUpload
314+
isServer || !userSentryOptions.widenClientFileUpload
312315
? []
313316
: // Widening the upload scope is necessarily going to lead to us uploading files we don't need to (ones which
314317
// don't include any user code). In order to lessen that where we can, exclude the internal nextjs files we know

0 commit comments

Comments
 (0)