Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nextjs): Differentiate between webpack 4 and 5 in server builds #3878

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type BuildContext = {
buildId: string;
dir: string;
config: Partial<NextConfigObject>;
webpack: { version: string };
};

/**
Expand Down
11 changes: 6 additions & 5 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,18 @@ function getWebpackPluginOptions(
buildContext: BuildContext,
userPluginOptions: Partial<SentryWebpackPluginOptions>,
): 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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we an use the webpack 5 check to simplify the logic on how we add entry points with addFileToExistingEntryPoint (instead of having the various if else conditions)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a bad idea, but once I sat down to try to do it, I realized that though knowing it's webpack 4 would narrow down the possible formats, knowing it's webpack 5 wouldn't, because it can take all of the webpack 4 formats along with its own new formats. So just because of webpack 5 I think we need all the if/elses. Unless you're seeing something I'm not?

In any case, I'm going to merge this and we can handle that, if we do, in a later PR.

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({
Expand Down
19 changes: 17 additions & 2 deletions packages/nextjs/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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,
Expand All @@ -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' },
]);
});
});
Expand Down