1
1
/* eslint-disable max-lines */
2
2
import { getSentryRelease } from '@sentry/node' ;
3
- import { arrayify , dropUndefinedKeys , escapeStringForRegex , logger } from '@sentry/utils' ;
3
+ import { arrayify , dropUndefinedKeys , escapeStringForRegex , logger , stringMatchesSomePattern } from '@sentry/utils' ;
4
4
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin' ;
5
5
import * as chalk from 'chalk' ;
6
6
import * as fs from 'fs' ;
@@ -91,7 +91,11 @@ export function constructWebpackConfigFunction(
91
91
use : [
92
92
{
93
93
loader : path . resolve ( __dirname , 'loaders/proxyLoader.js' ) ,
94
- options : { pagesDir, pageExtensionRegex } ,
94
+ options : {
95
+ pagesDir,
96
+ pageExtensionRegex,
97
+ excludeServerRoutes : userSentryOptions . excludeServerRoutes ,
98
+ } ,
95
99
} ,
96
100
] ,
97
101
} ) ;
@@ -135,7 +139,7 @@ export function constructWebpackConfigFunction(
135
139
// will call the callback which will call `f` which will call `x.y`... and on and on. Theoretically this could also
136
140
// be fixed by using `bind`, but this is way simpler.)
137
141
const origEntryProperty = newConfig . entry ;
138
- newConfig . entry = async ( ) => addSentryToEntryProperty ( origEntryProperty , buildContext ) ;
142
+ newConfig . entry = async ( ) => addSentryToEntryProperty ( origEntryProperty , buildContext , userSentryOptions ) ;
139
143
140
144
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
141
145
if ( shouldEnableWebpackPlugin ( buildContext , userSentryOptions ) ) {
@@ -248,6 +252,7 @@ function findTranspilationRules(rules: WebpackModuleRule[] | undefined, projectD
248
252
async function addSentryToEntryProperty (
249
253
currentEntryProperty : WebpackEntryProperty ,
250
254
buildContext : BuildContext ,
255
+ userSentryOptions : UserSentryOptions ,
251
256
) : Promise < EntryPropertyObject > {
252
257
// The `entry` entry in a webpack config can be a string, array of strings, object, or function. By default, nextjs
253
258
// sets it to an async function which returns the promise of an object of string arrays. Because we don't know whether
@@ -268,8 +273,18 @@ async function addSentryToEntryProperty(
268
273
269
274
// inject into all entry points which might contain user's code
270
275
for ( const entryPointName in newEntryProperty ) {
271
- if ( shouldAddSentryToEntryPoint ( entryPointName , isServer ) ) {
276
+ if ( shouldAddSentryToEntryPoint ( entryPointName , isServer , userSentryOptions . excludeServerRoutes ) ) {
272
277
addFilesToExistingEntryPoint ( newEntryProperty , entryPointName , filesToInject ) ;
278
+ } else {
279
+ if (
280
+ isServer &&
281
+ // If the user has asked to exclude pages, confirm for them that it's worked
282
+ userSentryOptions . excludeServerRoutes &&
283
+ // We always skip these, so it's not worth telling the user that we've done so
284
+ ! [ 'pages/_app' , 'pages/_document' ] . includes ( entryPointName )
285
+ ) {
286
+ __DEBUG_BUILD__ && logger . log ( `Skipping Sentry injection for ${ entryPointName . replace ( / ^ p a g e s / , '' ) } ` ) ;
287
+ }
273
288
}
274
289
}
275
290
@@ -377,13 +392,21 @@ function checkWebpackPluginOverrides(
377
392
*
378
393
* @param entryPointName The name of the entry point in question
379
394
* @param isServer Whether or not this function is being called in the context of a server build
395
+ * @param excludeServerRoutes A list of excluded serverside entrypoints provided by the user
380
396
* @returns `true` if sentry code should be injected, and `false` otherwise
381
397
*/
382
- function shouldAddSentryToEntryPoint ( entryPointName : string , isServer : boolean ) : boolean {
398
+ function shouldAddSentryToEntryPoint (
399
+ entryPointName : string ,
400
+ isServer : boolean ,
401
+ excludeServerRoutes : Array < string | RegExp > = [ ] ,
402
+ ) : boolean {
383
403
// On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
384
404
if ( isServer ) {
385
405
const entryPointRoute = entryPointName . replace ( / ^ p a g e s / , '' ) ;
386
406
if (
407
+ // User-specified pages to skip. (Note: For ease of use, `excludeServerRoutes` is specified in terms of routes,
408
+ // which don't have the `pages` prefix.)
409
+ stringMatchesSomePattern ( entryPointRoute , excludeServerRoutes , true ) ||
387
410
// All non-API pages contain both of these components, and we don't want to inject more than once, so as long as
388
411
// we're doing the individual pages, it's fine to skip these. (Note: Even if a given user doesn't have either or
389
412
// both of these in their `pages/` folder, they'll exist as entrypoints because nextjs will supply default
@@ -462,7 +485,8 @@ export function getWebpackPluginOptions(
462
485
configFile : hasSentryProperties ? 'sentry.properties' : undefined ,
463
486
stripPrefix : [ 'webpack://_N_E/' ] ,
464
487
urlPrefix,
465
- entries : ( entryPointName : string ) => shouldAddSentryToEntryPoint ( entryPointName , isServer ) ,
488
+ entries : ( entryPointName : string ) =>
489
+ shouldAddSentryToEntryPoint ( entryPointName , isServer , userSentryOptions . excludeServerRoutes ) ,
466
490
release : getSentryRelease ( buildId ) ,
467
491
dryRun : isDev ,
468
492
} ) ;
0 commit comments