Skip to content

Commit ca81ae0

Browse files
committed
use cli for webpack
1 parent 84c2e1a commit ca81ae0

File tree

6 files changed

+117
-20
lines changed

6 files changed

+117
-20
lines changed

packages/nextjs/src/config/getBuildPluginOptions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export function getBuildPluginOptions({
8181
);
8282

8383
// File deletion for webpack client builds
84-
if (sentryBuildOptions.sourcemaps?.deleteSourcemapsAfterUpload) {
84+
// If the user has opted into using the experimental hook, we delete the source maps in the hook instead
85+
if (sentryBuildOptions.sourcemaps?.deleteSourcemapsAfterUpload && !useRunAfterProductionCompileHook) {
8586
filesToDeleteAfterUpload.push(
8687
// We only care to delete client bundle source maps because they would be the ones being served.
8788
// Removing the server source maps crashes Vercel builds for (thus far) unknown reasons:
@@ -94,8 +95,7 @@ export function getBuildPluginOptions({
9495
}
9596
}
9697

97-
// If the user has opted into using the experimental hook, we skip sourcemaps and release management in the plugin
98-
// to avoid double sourcemap uploads.
98+
// If the user has opted into using the experimental hook, we skip sourcemap uploads in the plugin
9999
const shouldSkipSourcemapsUpload = useRunAfterProductionCompileHook && buildTool.startsWith('webpack');
100100

101101
return {
@@ -116,7 +116,7 @@ export function getBuildPluginOptions({
116116
silent: sentryBuildOptions.silent,
117117
url: sentryBuildOptions.sentryUrl,
118118
sourcemaps: {
119-
disable: sentryBuildOptions.sourcemaps?.disable || shouldSkipSourcemapsUpload,
119+
disable: shouldSkipSourcemapsUpload ? true : (sentryBuildOptions.sourcemaps?.disable ?? false),
120120
rewriteSources(source) {
121121
if (source.startsWith('webpack://_N_E/')) {
122122
return source.replace('webpack://_N_E/', '');

packages/nextjs/src/config/webpack.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ let showedMissingGlobalErrorWarningMsg = false;
4040
* @param userSentryOptions The user's SentryWebpackPlugin config, as passed to `withSentryConfig`
4141
* @returns The function to set as the nextjs config's `webpack` value
4242
*/
43-
export function constructWebpackConfigFunction(
44-
userNextConfig: NextConfigObject = {},
45-
userSentryOptions: SentryBuildOptions = {},
46-
releaseName: string | undefined,
47-
routeManifest: RouteManifest | undefined,
48-
nextJsVersion: string | undefined,
49-
): WebpackConfigFunction {
43+
export function constructWebpackConfigFunction({
44+
userNextConfig = {},
45+
userSentryOptions = {},
46+
releaseName,
47+
routeManifest,
48+
nextJsVersion,
49+
useRunAfterProductionCompileHook,
50+
}: {
51+
userNextConfig: NextConfigObject;
52+
userSentryOptions: SentryBuildOptions;
53+
releaseName: string | undefined;
54+
routeManifest: RouteManifest | undefined;
55+
nextJsVersion: string | undefined;
56+
useRunAfterProductionCompileHook: boolean | undefined;
57+
}): WebpackConfigFunction {
5058
// Will be called by nextjs and passed its default webpack configuration and context data about the build (whether
5159
// we're building server or client, whether we're in dev, what version of webpack we're using, etc). Note that
5260
// `incomingConfig` and `buildContext` are referred to as `config` and `options` in the nextjs docs.
@@ -420,6 +428,7 @@ export function constructWebpackConfigFunction(
420428
releaseName,
421429
distDirAbsPath,
422430
buildTool,
431+
useRunAfterProductionCompileHook,
423432
}),
424433
);
425434

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,14 @@ function getFinalConfigObject(
368368
webpack:
369369
isTurbopack || userSentryOptions.disableSentryWebpackConfig
370370
? incomingUserNextConfigObject.webpack // just return the original webpack config
371-
: constructWebpackConfigFunction(
372-
incomingUserNextConfigObject,
371+
: constructWebpackConfigFunction({
372+
userNextConfig: incomingUserNextConfigObject,
373373
userSentryOptions,
374374
releaseName,
375375
routeManifest,
376376
nextJsVersion,
377-
),
377+
useRunAfterProductionCompileHook: userSentryOptions._experimental?.useRunAfterProductionCompileHook,
378+
}),
378379
...(isTurbopackSupported && isTurbopack
379380
? {
380381
turbopack: constructTurbopackConfig({

packages/nextjs/test/config/getBuildPluginOptions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ describe('getBuildPluginOptions', () => {
618618
});
619619

620620
expect(result.sourcemaps).toMatchObject({
621-
disable: undefined,
621+
disable: false,
622622
assets: ['/path/to/.next/**'],
623623
ignore: [],
624624
filesToDeleteAfterUpload: undefined,

packages/nextjs/test/config/testUtils.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export async function materializeFinalWebpackConfig(options: {
5656
incomingWebpackConfig: WebpackConfigObject;
5757
incomingWebpackBuildContext: BuildContext;
5858
sentryBuildTimeOptions?: SentryBuildOptions;
59+
releaseName?: string;
60+
routeManifest?: any;
61+
nextJsVersion?: string;
62+
useRunAfterProductionCompileHook?: boolean;
5963
}): Promise<WebpackConfigObjectWithModuleRules> {
6064
const { exportedNextConfig, incomingWebpackConfig, incomingWebpackBuildContext } = options;
6165

@@ -66,11 +70,14 @@ export async function materializeFinalWebpackConfig(options: {
6670
: exportedNextConfig;
6771

6872
// get the webpack config function we'd normally pass back to next
69-
const webpackConfigFunction = constructWebpackConfigFunction(
70-
materializedUserNextConfig,
71-
options.sentryBuildTimeOptions,
72-
undefined,
73-
);
73+
const webpackConfigFunction = constructWebpackConfigFunction({
74+
userNextConfig: materializedUserNextConfig,
75+
userSentryOptions: options.sentryBuildTimeOptions || {},
76+
releaseName: options.releaseName,
77+
routeManifest: options.routeManifest,
78+
nextJsVersion: options.nextJsVersion,
79+
useRunAfterProductionCompileHook: options.useRunAfterProductionCompileHook ?? options.sentryBuildTimeOptions?._experimental?.useRunAfterProductionCompileHook,
80+
});
7481

7582
// call it to get concrete values for comparison
7683
const finalWebpackConfigValue = webpackConfigFunction(incomingWebpackConfig, incomingWebpackBuildContext);

packages/nextjs/test/config/webpack/constructWebpackConfig.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,86 @@ describe('constructWebpackConfigFunction()', () => {
8787
getBuildPluginOptionsSpy.mockRestore();
8888
});
8989

90+
it('passes useRunAfterProductionCompileHook to getBuildPluginOptions when enabled', async () => {
91+
const getBuildPluginOptionsSpy = vi.spyOn(getBuildPluginOptionsModule, 'getBuildPluginOptions');
92+
vi.spyOn(core, 'loadModule').mockImplementation(() => ({
93+
sentryWebpackPlugin: () => ({
94+
_name: 'sentry-webpack-plugin',
95+
}),
96+
}));
97+
98+
await materializeFinalWebpackConfig({
99+
exportedNextConfig,
100+
incomingWebpackConfig: serverWebpackConfig,
101+
incomingWebpackBuildContext: serverBuildContext,
102+
sentryBuildTimeOptions: {
103+
_experimental: {
104+
useRunAfterProductionCompileHook: true,
105+
},
106+
},
107+
});
108+
109+
expect(getBuildPluginOptionsSpy).toHaveBeenCalledWith(
110+
expect.objectContaining({
111+
useRunAfterProductionCompileHook: true,
112+
}),
113+
);
114+
115+
getBuildPluginOptionsSpy.mockRestore();
116+
});
117+
118+
it('passes useRunAfterProductionCompileHook to getBuildPluginOptions when disabled', async () => {
119+
const getBuildPluginOptionsSpy = vi.spyOn(getBuildPluginOptionsModule, 'getBuildPluginOptions');
120+
vi.spyOn(core, 'loadModule').mockImplementation(() => ({
121+
sentryWebpackPlugin: () => ({
122+
_name: 'sentry-webpack-plugin',
123+
}),
124+
}));
125+
126+
await materializeFinalWebpackConfig({
127+
exportedNextConfig,
128+
incomingWebpackConfig: serverWebpackConfig,
129+
incomingWebpackBuildContext: serverBuildContext,
130+
sentryBuildTimeOptions: {
131+
_experimental: {
132+
useRunAfterProductionCompileHook: false,
133+
},
134+
},
135+
});
136+
137+
expect(getBuildPluginOptionsSpy).toHaveBeenCalledWith(
138+
expect.objectContaining({
139+
useRunAfterProductionCompileHook: false,
140+
}),
141+
);
142+
143+
getBuildPluginOptionsSpy.mockRestore();
144+
});
145+
146+
it('passes useRunAfterProductionCompileHook as undefined when not specified', async () => {
147+
const getBuildPluginOptionsSpy = vi.spyOn(getBuildPluginOptionsModule, 'getBuildPluginOptions');
148+
vi.spyOn(core, 'loadModule').mockImplementation(() => ({
149+
sentryWebpackPlugin: () => ({
150+
_name: 'sentry-webpack-plugin',
151+
}),
152+
}));
153+
154+
await materializeFinalWebpackConfig({
155+
exportedNextConfig,
156+
incomingWebpackConfig: serverWebpackConfig,
157+
incomingWebpackBuildContext: serverBuildContext,
158+
sentryBuildTimeOptions: {},
159+
});
160+
161+
expect(getBuildPluginOptionsSpy).toHaveBeenCalledWith(
162+
expect.objectContaining({
163+
useRunAfterProductionCompileHook: undefined,
164+
}),
165+
);
166+
167+
getBuildPluginOptionsSpy.mockRestore();
168+
});
169+
90170
it('preserves unrelated webpack config options', async () => {
91171
const finalWebpackConfig = await materializeFinalWebpackConfig({
92172
exportedNextConfig,

0 commit comments

Comments
 (0)