diff --git a/CHANGELOG.md b/CHANGELOG.md index 90ecc94db888..c748f2015acb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott +fix(nextjs): Differentiate between webpack 4 and 5 in server builds (#FIXME!!) + ## 6.11.0 - feat(nextjs): Allow for TypeScript user config files (#3847) diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 0a4adb8dad1c..b577268f41cb 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -54,6 +54,7 @@ export type BuildContext = { buildId: string; dir: string; config: Partial; + webpack: { version: string }; }; /** diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 643b17623d39..a4807dccc53b 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -242,17 +242,18 @@ function getWebpackPluginOptions( buildContext: BuildContext, userPluginOptions: Partial, ): SentryWebpackPluginOptions { - const { isServer, dir: projectDir, buildId, dev: isDev, config: nextConfig } = buildContext; + const { isServer, dir: projectDir, buildId, dev: isDev, config: nextConfig, webpack } = buildContext; + const isWebpack5 = webpack.version.startsWith('5'); const isServerless = nextConfig.target === 'experimental-serverless-trace'; const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties')); const serverInclude = isServerless ? [{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' }] - : [ - { paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }, - { paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }, - ]; + : [{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }].concat( + isWebpack5 ? [{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }] : [], + ); + const clientInclude = [{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/_next/static/chunks/pages' }]; const defaultPluginOptions = dropUndefinedKeys({ diff --git a/packages/nextjs/test/config.test.ts b/packages/nextjs/test/config.test.ts index 636266cd404b..29e575452ed6 100644 --- a/packages/nextjs/test/config.test.ts +++ b/packages/nextjs/test/config.test.ts @@ -89,6 +89,7 @@ const baseBuildContext = { buildId: 'sItStAyLiEdOwN', dir: '/Users/Maisey/projects/squirrelChasingSimulator', config: { target: 'server' as const }, + webpack: { version: '5.4.15' }, }; const serverBuildContext = { isServer: true, ...baseBuildContext }; const clientBuildContext = { isServer: false, ...baseBuildContext }; @@ -389,7 +390,21 @@ describe('Sentry webpack plugin config', () => { ]); }); - it('has the correct value when building serverful server bundles', async () => { + it('has the correct value when building serverful server bundles using webpack 4', async () => { + const finalWebpackConfig = await materializeFinalWebpackConfig({ + userNextConfig, + incomingWebpackConfig: serverWebpackConfig, + incomingWebpackBuildContext: { ...serverBuildContext, webpack: { version: '4.15.13' } }, + }); + + const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType; + + expect(sentryWebpackPlugin.options?.include).toEqual([ + { paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }, + ]); + }); + + it('has the correct value when building serverful server bundles using webpack 5', async () => { const finalWebpackConfig = await materializeFinalWebpackConfig({ userNextConfig, incomingWebpackConfig: serverWebpackConfig, @@ -399,8 +414,8 @@ describe('Sentry webpack plugin config', () => { const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType; expect(sentryWebpackPlugin.options?.include).toEqual([ - { paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }, { paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }, + { paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }, ]); }); });