Skip to content

Commit 71612f5

Browse files
lobsterkatieLms24
authored andcommitted
move userNextConfig.sentry into a closure in constructWebpackConfigFunction
1 parent 016fc1b commit 71612f5

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,
@@ -37,6 +38,7 @@ export { SentryWebpackPlugin };
3738
export function constructWebpackConfigFunction(
3839
userNextConfig: Partial<NextConfigObject> = {},
3940
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
41+
userSentryOptions: UserSentryOptions = {},
4042
): WebpackConfigFunction {
4143
// Will be called by nextjs and passed its default webpack configuration and context data about the build (whether
4244
// we're building server or client, whether we're in dev, what version of webpack we're using, etc). Note that
@@ -122,9 +124,7 @@ export function constructWebpackConfigFunction(
122124
// with the `--ignore-scripts` option, this will be blocked and the missing binary will cause an error when users
123125
// try to build their apps.)
124126
ensureCLIBinaryExists() &&
125-
(isServer
126-
? !userNextConfig.sentry?.disableServerWebpackPlugin
127-
: !userNextConfig.sentry?.disableClientWebpackPlugin);
127+
(isServer ? !userSentryOptions.disableServerWebpackPlugin : !userSentryOptions.disableClientWebpackPlugin);
128128

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

144144
newConfig.plugins = newConfig.plugins || [];
145145
newConfig.plugins.push(
146-
new SentryWebpackPlugin(getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions)),
146+
new SentryWebpackPlugin(
147+
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
148+
),
147149
);
148150
}
149151

@@ -381,6 +383,7 @@ function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean):
381383
export function getWebpackPluginOptions(
382384
buildContext: BuildContext,
383385
userPluginOptions: Partial<SentryWebpackPluginOptions>,
386+
userSentryOptions: UserSentryOptions,
384387
): SentryWebpackPluginOptions {
385388
const { buildId, isServer, webpack, config: userNextConfig, dev: isDev, dir: projectDir } = buildContext;
386389
const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory
@@ -396,14 +399,14 @@ export function getWebpackPluginOptions(
396399
isWebpack5 ? [{ paths: [`${distDir}/server/chunks/`], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
397400
);
398401

399-
const clientInclude = userNextConfig.sentry?.widenClientFileUpload
402+
const clientInclude = userSentryOptions.widenClientFileUpload
400403
? [{ paths: [`${distDir}/static/chunks`], urlPrefix: `${urlPrefix}/static/chunks` }]
401404
: [{ paths: [`${distDir}/static/chunks/pages`], urlPrefix: `${urlPrefix}/static/chunks/pages` }];
402405

403406
const defaultPluginOptions = dropUndefinedKeys({
404407
include: isServer ? serverInclude : clientInclude,
405408
ignore:
406-
isServer || !userNextConfig.sentry?.widenClientFileUpload
409+
isServer || !userSentryOptions.widenClientFileUpload
407410
? []
408411
: // Widening the upload scope is necessarily going to lead to us uploading files we don't need to (ones which
409412
// 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)