File tree 3 files changed +46
-5
lines changed
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;
54
54
// Each value in that object is either a string representing a single entry point, an array of such strings, or an
55
55
// object containing either of those, along with other configuration options. In that third case, the entry point(s) are
56
56
// 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
+ } ;
61
64
62
65
export type EntryPropertyFunction = ( ) => Promise < EntryPropertyObject > ;
63
66
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
4
4
5
5
import {
6
6
BuildContext ,
7
+ EntryPointObject ,
7
8
EntryPropertyObject ,
8
9
ExportedNextConfig ,
9
10
SentryWebpackPluginOptions ,
@@ -151,6 +152,23 @@ async function addSentryToEntryProperty(
151
152
// On the client, it's sufficient to inject it into the `main` JS code, which is included in every browser page.
152
153
else {
153
154
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
+ }
154
172
}
155
173
156
174
return newEntryProperty ;
Original file line number Diff line number Diff line change @@ -212,7 +212,27 @@ describe('webpack config', () => {
212
212
} ) ;
213
213
214
214
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
+ } ) ,
216
236
) ;
217
237
} ) ;
218
238
} ) ;
You can’t perform that action at this time.
0 commit comments