Skip to content

Commit 67b7685

Browse files
authored
fix(nextjs): Fix conflict with other libraries modifying webpack entry property (#3703)
Next's code ensuring compatibility between the old `main.js` entry point and the new `main` entry point was causing the code we inject into `main` to be dropped if another library put anything into `main.js`. This merges them both into `main`, avoiding the `main.js` question all together. More details in the PR description.
1 parent d324ed1 commit 67b7685

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

packages/nextjs/src/config/types.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ export type WebpackEntryProperty = EntryPropertyObject | EntryPropertyFunction;
5454
// Each value in that object is either a string representing a single entry point, an array of such strings, or an
5555
// object containing either of those, along with other configuration options. In that third case, the entry point(s) are
5656
// listed under the key `import`.
57-
export type EntryPropertyObject =
58-
| { [key: string]: string }
59-
| { [key: string]: Array<string> }
60-
| { [key: string]: EntryPointObject }; // only in webpack 5
57+
export type EntryPropertyObject = {
58+
[key: string]:
59+
| string
60+
| Array<string>
61+
// only in webpack 5
62+
| EntryPointObject;
63+
};
6164

6265
export type EntryPropertyFunction = () => Promise<EntryPropertyObject>;
6366

packages/nextjs/src/config/webpack.ts

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
44

55
import {
66
BuildContext,
7+
EntryPointObject,
78
EntryPropertyObject,
89
ExportedNextConfig,
910
SentryWebpackPluginOptions,
@@ -151,6 +152,23 @@ async function addSentryToEntryProperty(
151152
// On the client, it's sufficient to inject it into the `main` JS code, which is included in every browser page.
152153
else {
153154
addFileToExistingEntryPoint(newEntryProperty, 'main', SENTRY_CLIENT_CONFIG_FILE);
155+
156+
// To work around a bug in nextjs, we need to ensure that the `main.js` entry is empty (otherwise it'll choose that
157+
// over `main` and we'll lose the change we just made). In case some other library has put something into it, copy
158+
// its contents over before emptying it out. See
159+
// https://github.com/getsentry/sentry-javascript/pull/3696#issuecomment-863363803.)
160+
const mainjsValue = newEntryProperty['main.js'];
161+
if (Array.isArray(mainjsValue) && mainjsValue.length > 0) {
162+
const mainValue = newEntryProperty.main;
163+
164+
// copy the `main.js` entries over
165+
newEntryProperty.main = Array.isArray(mainValue)
166+
? [...mainjsValue, ...mainValue]
167+
: { ...(mainValue as EntryPointObject), import: [...mainjsValue, ...(mainValue as EntryPointObject).import] };
168+
169+
// nuke the entries
170+
newEntryProperty['main.js'] = [];
171+
}
154172
}
155173

156174
return newEntryProperty;

packages/nextjs/test/config.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,27 @@ describe('webpack config', () => {
212212
});
213213

214214
expect(finalWebpackConfig.entry).toEqual(
215-
expect.objectContaining({ main: expect.arrayContaining(['./sentry.client.config.js']) }),
215+
expect.objectContaining({ main: ['./src/index.ts', './sentry.client.config.js'] }),
216+
);
217+
});
218+
219+
// see https://github.com/getsentry/sentry-javascript/pull/3696#issuecomment-863363803
220+
it('handles non-empty `main.js` entry point', async () => {
221+
const finalWebpackConfig = await materializeFinalWebpackConfig({
222+
userNextConfig,
223+
userSentryWebpackPluginConfig,
224+
incomingWebpackConfig: {
225+
...clientWebpackConfig,
226+
entry: () => Promise.resolve({ main: './src/index.ts', 'main.js': ['sitLieDownRollOver.config.js'] }),
227+
},
228+
incomingWebpackBuildContext: { ...buildContext, isServer: false },
229+
});
230+
231+
expect(finalWebpackConfig.entry).toEqual(
232+
expect.objectContaining({
233+
main: ['sitLieDownRollOver.config.js', './src/index.ts', './sentry.client.config.js'],
234+
'main.js': [],
235+
}),
216236
);
217237
});
218238
});

0 commit comments

Comments
 (0)