Skip to content

Commit 8092194

Browse files
authored
fix(nextjs): Differentiate between webpack 4 and 5 in server builds (#3878)
In order to cut down on the number of unnecessary files which get uploaded by the Sentry webpack plugin during nextjs builds, we're quite specific about which filepaths to scan. The change which introduced that specificity[1] missed the fact that under webpack 4, `.next/server/chunks` is not generated. This leads the Sentry webpack plugin to throw an error when it tries to upload it, even though nothing is actually amiss. This PR fixes that, by checking the webpack version before deciding which files to upload with the plugin. [1] #3845
1 parent dbe98a1 commit 8092194

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66

7+
fix(nextjs): Differentiate between webpack 4 and 5 in server builds (#FIXME!!)
8+
79
## 6.11.0
810

911
- feat(nextjs): Allow for TypeScript user config files (#3847)

packages/nextjs/src/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type BuildContext = {
5454
buildId: string;
5555
dir: string;
5656
config: Partial<NextConfigObject>;
57+
webpack: { version: string };
5758
};
5859

5960
/**

packages/nextjs/src/config/webpack.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,18 @@ function getWebpackPluginOptions(
242242
buildContext: BuildContext,
243243
userPluginOptions: Partial<SentryWebpackPluginOptions>,
244244
): SentryWebpackPluginOptions {
245-
const { isServer, dir: projectDir, buildId, dev: isDev, config: nextConfig } = buildContext;
245+
const { isServer, dir: projectDir, buildId, dev: isDev, config: nextConfig, webpack } = buildContext;
246246

247+
const isWebpack5 = webpack.version.startsWith('5');
247248
const isServerless = nextConfig.target === 'experimental-serverless-trace';
248249
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
249250

250251
const serverInclude = isServerless
251252
? [{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' }]
252-
: [
253-
{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' },
254-
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
255-
];
253+
: [{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }].concat(
254+
isWebpack5 ? [{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }] : [],
255+
);
256+
256257
const clientInclude = [{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/_next/static/chunks/pages' }];
257258

258259
const defaultPluginOptions = dropUndefinedKeys({

packages/nextjs/test/config.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const baseBuildContext = {
8989
buildId: 'sItStAyLiEdOwN',
9090
dir: '/Users/Maisey/projects/squirrelChasingSimulator',
9191
config: { target: 'server' as const },
92+
webpack: { version: '5.4.15' },
9293
};
9394
const serverBuildContext = { isServer: true, ...baseBuildContext };
9495
const clientBuildContext = { isServer: false, ...baseBuildContext };
@@ -389,7 +390,21 @@ describe('Sentry webpack plugin config', () => {
389390
]);
390391
});
391392

392-
it('has the correct value when building serverful server bundles', async () => {
393+
it('has the correct value when building serverful server bundles using webpack 4', async () => {
394+
const finalWebpackConfig = await materializeFinalWebpackConfig({
395+
userNextConfig,
396+
incomingWebpackConfig: serverWebpackConfig,
397+
incomingWebpackBuildContext: { ...serverBuildContext, webpack: { version: '4.15.13' } },
398+
});
399+
400+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
401+
402+
expect(sentryWebpackPlugin.options?.include).toEqual([
403+
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
404+
]);
405+
});
406+
407+
it('has the correct value when building serverful server bundles using webpack 5', async () => {
393408
const finalWebpackConfig = await materializeFinalWebpackConfig({
394409
userNextConfig,
395410
incomingWebpackConfig: serverWebpackConfig,
@@ -399,8 +414,8 @@ describe('Sentry webpack plugin config', () => {
399414
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
400415

401416
expect(sentryWebpackPlugin.options?.include).toEqual([
402-
{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' },
403417
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
418+
{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' },
404419
]);
405420
});
406421
});

0 commit comments

Comments
 (0)