Skip to content

Commit 43c842a

Browse files
committed
refactor: use the new options path but fallback to toplevel options
1 parent c7b5f46 commit 43c842a

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

packages/nextjs/src/config/getBuildPluginOptions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function createReleaseConfig(
205205
vcsRemote: sentryBuildOptions.release?.vcsRemote,
206206
setCommits: sentryBuildOptions.release?.setCommits,
207207
deploy: sentryBuildOptions.release?.deploy,
208-
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.release,
208+
...sentryBuildOptions.webpack?.unstable_sentryWebpackPluginOptions?.release,
209209
};
210210
}
211211

@@ -241,7 +241,7 @@ export function getBuildPluginOptions({
241241
const normalizedDistDirAbsPath = normalizePathForGlob(distDirAbsPath);
242242

243243
const loggerPrefix = LOGGER_PREFIXES[buildTool];
244-
const widenClientFileUpload = sentryBuildOptions.widenClientFileUpload ?? false;
244+
const widenClientFileUpload = sentryBuildOptions.webpack?.widenClientFileUpload ?? false;
245245
const deleteSourcemapsAfterUpload = sentryBuildOptions.sourcemaps?.deleteSourcemapsAfterUpload ?? false;
246246

247247
const sourcemapUploadAssets = createSourcemapUploadAssetPatterns(
@@ -272,8 +272,8 @@ export function getBuildPluginOptions({
272272
reactComponentAnnotation: buildTool.startsWith('after-production-compile')
273273
? undefined
274274
: {
275-
...sentryBuildOptions.reactComponentAnnotation,
276-
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.reactComponentAnnotation,
275+
...sentryBuildOptions.webpack?.reactComponentAnnotation,
276+
...sentryBuildOptions.webpack?.unstable_sentryWebpackPluginOptions?.reactComponentAnnotation,
277277
},
278278
silent: sentryBuildOptions.silent,
279279
url: sentryBuildOptions.sentryUrl,
@@ -283,7 +283,7 @@ export function getBuildPluginOptions({
283283
assets: sentryBuildOptions.sourcemaps?.assets ?? sourcemapUploadAssets,
284284
ignore: sentryBuildOptions.sourcemaps?.ignore ?? sourcemapUploadIgnore,
285285
filesToDeleteAfterUpload,
286-
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.sourcemaps,
286+
...sentryBuildOptions.webpack?.unstable_sentryWebpackPluginOptions?.sourcemaps,
287287
},
288288
release: createReleaseConfig(releaseName, sentryBuildOptions),
289289
bundleSizeOptimizations: {
@@ -295,6 +295,6 @@ export function getBuildPluginOptions({
295295
metaFramework: 'nextjs',
296296
},
297297
},
298-
...sentryBuildOptions.unstable_sentryWebpackPluginOptions,
298+
...sentryBuildOptions.webpack?.unstable_sentryWebpackPluginOptions,
299299
};
300300
}

packages/nextjs/src/config/webpack.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function constructWebpackConfigFunction({
145145
appDir: appDirPath,
146146
pagesDir: pagesDirPath,
147147
pageExtensionRegex,
148-
excludeServerRoutes: userSentryOptions.excludeServerRoutes,
148+
excludeServerRoutes: userSentryOptions.webpack?.excludeServerRoutes,
149149
nextjsRequestAsyncStorageModulePath: getRequestAsyncStorageModuleLocation(
150150
projectDir,
151151
rawNewConfig.resolve?.modules,
@@ -220,7 +220,7 @@ export function constructWebpackConfigFunction({
220220
);
221221
};
222222

223-
if (isServer && userSentryOptions.autoInstrumentServerFunctions !== false) {
223+
if (isServer && userSentryOptions.webpack?.autoInstrumentServerFunctions !== false) {
224224
// It is very important that we insert our loaders at the beginning of the array because we expect any sort of transformations/transpilations (e.g. TS -> JS) to already have happened.
225225

226226
// Wrap pages
@@ -239,7 +239,7 @@ export function constructWebpackConfigFunction({
239239

240240
let vercelCronsConfig: VercelCronsConfig = undefined;
241241
try {
242-
if (process.env.VERCEL && userSentryOptions.automaticVercelMonitors) {
242+
if (process.env.VERCEL && userSentryOptions.webpack?.automaticVercelMonitors) {
243243
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
244244
vercelCronsConfig = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'vercel.json'), 'utf8')).crons;
245245
if (vercelCronsConfig) {
@@ -277,7 +277,7 @@ export function constructWebpackConfigFunction({
277277

278278
// Wrap middleware
279279
const canWrapStandaloneMiddleware = userNextConfig.output !== 'standalone' || !major || major < 16;
280-
if ((userSentryOptions.autoInstrumentMiddleware ?? true) && canWrapStandaloneMiddleware) {
280+
if ((userSentryOptions.webpack?.autoInstrumentMiddleware ?? true) && canWrapStandaloneMiddleware) {
281281
newConfig.module.rules.unshift({
282282
test: isMiddlewareResource,
283283
use: [
@@ -293,7 +293,7 @@ export function constructWebpackConfigFunction({
293293
}
294294
}
295295

296-
if (isServer && userSentryOptions.autoInstrumentAppDirectory !== false) {
296+
if (isServer && userSentryOptions.webpack?.autoInstrumentAppDirectory !== false) {
297297
// Wrap server components
298298
newConfig.module.rules.unshift({
299299
test: isServerComponentResource,
@@ -431,7 +431,7 @@ export function constructWebpackConfigFunction({
431431
}
432432
}
433433

434-
if (userSentryOptions.disableLogger) {
434+
if (userSentryOptions.webpack?.treeshake?.debugLogs) {
435435
newConfig.plugins = newConfig.plugins || [];
436436
newConfig.plugins.push(
437437
new buildContext.webpack.DefinePlugin({

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,84 @@ function generateRandomTunnelRoute(): string {
9898
return `/${randomString}`;
9999
}
100100

101+
/**
102+
* Migrates deprecated top-level webpack options to the new `webpack.*` path for backward compatibility.
103+
* The new path takes precedence over deprecated options. This mutates the userSentryOptions object.
104+
*/
105+
function migrateDeprecatedWebpackOptions(userSentryOptions: SentryBuildOptions): void {
106+
// Initialize webpack options if not present
107+
userSentryOptions.webpack = userSentryOptions.webpack || {};
108+
109+
const webpack = userSentryOptions.webpack;
110+
111+
const withDeprecatedFallback = <T>(newValue: T | undefined, deprecatedValue: T | undefined): T | undefined => {
112+
return newValue ?? deprecatedValue;
113+
};
114+
115+
/* eslint-disable deprecation/deprecation */
116+
// Migrate each deprecated option to the new path, but only if the new path isn't already set
117+
webpack.autoInstrumentServerFunctions = withDeprecatedFallback(
118+
webpack.autoInstrumentServerFunctions,
119+
userSentryOptions.autoInstrumentServerFunctions,
120+
);
121+
122+
webpack.autoInstrumentMiddleware = withDeprecatedFallback(
123+
webpack.autoInstrumentMiddleware,
124+
userSentryOptions.autoInstrumentMiddleware,
125+
);
126+
127+
webpack.autoInstrumentAppDirectory = withDeprecatedFallback(
128+
webpack.autoInstrumentAppDirectory,
129+
userSentryOptions.autoInstrumentAppDirectory,
130+
);
131+
132+
webpack.excludeServerRoutes = withDeprecatedFallback(
133+
webpack.excludeServerRoutes,
134+
userSentryOptions.excludeServerRoutes,
135+
);
136+
137+
webpack.widenClientFileUpload = withDeprecatedFallback(
138+
webpack.widenClientFileUpload,
139+
userSentryOptions.widenClientFileUpload,
140+
);
141+
142+
webpack.unstable_sentryWebpackPluginOptions = withDeprecatedFallback(
143+
webpack.unstable_sentryWebpackPluginOptions,
144+
userSentryOptions.unstable_sentryWebpackPluginOptions,
145+
);
146+
147+
webpack.disableSentryConfig = withDeprecatedFallback(
148+
webpack.disableSentryConfig,
149+
userSentryOptions.disableSentryWebpackConfig,
150+
);
151+
152+
// Handle treeshake.debugLogs specially since it's nested
153+
if (userSentryOptions.disableLogger !== undefined) {
154+
webpack.treeshake = webpack.treeshake || {};
155+
webpack.treeshake.debugLogs = withDeprecatedFallback(webpack.treeshake.debugLogs, userSentryOptions.disableLogger);
156+
}
157+
158+
webpack.automaticVercelMonitors = withDeprecatedFallback(
159+
webpack.automaticVercelMonitors,
160+
userSentryOptions.automaticVercelMonitors,
161+
);
162+
163+
webpack.reactComponentAnnotation = withDeprecatedFallback(
164+
webpack.reactComponentAnnotation,
165+
userSentryOptions.reactComponentAnnotation,
166+
);
167+
/* eslint-enable deprecation/deprecation */
168+
}
169+
101170
// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
102171
// `webpack` property
103172
function getFinalConfigObject(
104173
incomingUserNextConfigObject: NextConfigObject,
105174
userSentryOptions: SentryBuildOptions,
106175
): NextConfigObject {
176+
// Migrate deprecated webpack options to new webpack path for backward compatibility
177+
migrateDeprecatedWebpackOptions(userSentryOptions);
178+
107179
// Only determine a release name if release creation is not explicitly disabled
108180
// This prevents injection of Git commit hashes that break build determinism
109181
const shouldCreateRelease = userSentryOptions.release?.create !== false;
@@ -363,7 +435,7 @@ function getFinalConfigObject(
363435
],
364436
},
365437
}),
366-
...(isWebpack && !userSentryOptions.disableSentryWebpackConfig
438+
...(isWebpack && !userSentryOptions.webpack?.disableSentryConfig
367439
? {
368440
webpack: constructWebpackConfigFunction({
369441
userNextConfig: incomingUserNextConfigObject,

0 commit comments

Comments
 (0)