File tree Expand file tree Collapse file tree 3 files changed +46
-5
lines changed Expand file tree Collapse file tree 3 files changed +46
-5
lines changed Original file line number Diff line number Diff 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
6265export type EntryPropertyFunction = ( ) => Promise < EntryPropertyObject > ;
6366
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
44
55import {
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 ;
Original file line number Diff line number Diff 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 } ) ;
You can’t perform that action at this time.
0 commit comments