@@ -140,19 +140,45 @@ export function constructWebpackConfigFunction(
140
140
return path . normalize ( absoluteResourcePath ) ;
141
141
} ;
142
142
143
+ const isPageResource = ( resourcePath : string ) : boolean => {
144
+ const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
145
+ return (
146
+ normalizedAbsoluteResourcePath . startsWith ( pagesDirPath + path . sep ) &&
147
+ ! normalizedAbsoluteResourcePath . startsWith ( apiRoutesPath + path . sep ) &&
148
+ dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
149
+ ) ;
150
+ } ;
151
+
152
+ const isApiRouteResource = ( resourcePath : string ) : boolean => {
153
+ const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
154
+ return (
155
+ normalizedAbsoluteResourcePath . startsWith ( apiRoutesPath + path . sep ) &&
156
+ dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
157
+ ) ;
158
+ } ;
159
+
160
+ const isMiddlewareResource = ( resourcePath : string ) : boolean => {
161
+ const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
162
+ return normalizedAbsoluteResourcePath === middlewareJsPath || normalizedAbsoluteResourcePath === middlewareTsPath ;
163
+ } ;
164
+
165
+ const isServerComponentResource = ( resourcePath : string ) : boolean => {
166
+ const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
167
+
168
+ // ".js, .jsx, or .tsx file extensions can be used for Pages"
169
+ // https://beta.nextjs.org/docs/routing/pages-and-layouts#pages:~:text=.js%2C%20.jsx%2C%20or%20.tsx%20file%20extensions%20can%20be%20used%20for%20Pages.
170
+ return (
171
+ normalizedAbsoluteResourcePath . startsWith ( appDirPath + path . sep ) &&
172
+ ! ! normalizedAbsoluteResourcePath . match ( / [ \\ / ] ( p a g e | l a y o u t | l o a d i n g | h e a d | n o t - f o u n d ) \. ( j s | j s x | t s x ) $ / )
173
+ ) ;
174
+ } ;
175
+
143
176
if ( isServer && userSentryOptions . autoInstrumentServerFunctions !== false ) {
144
177
// It is very important that we insert our loaders at the beginning of the array because we expect any sort of transformations/transpilations (e.g. TS -> JS) to already have happened.
145
178
146
179
// Wrap pages
147
180
newConfig . module . rules . unshift ( {
148
- test : resourcePath => {
149
- const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
150
- return (
151
- normalizedAbsoluteResourcePath . startsWith ( pagesDirPath + path . sep ) &&
152
- ! normalizedAbsoluteResourcePath . startsWith ( apiRoutesPath + path . sep ) &&
153
- dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
154
- ) ;
155
- } ,
181
+ test : isPageResource ,
156
182
use : [
157
183
{
158
184
loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
@@ -190,13 +216,7 @@ export function constructWebpackConfigFunction(
190
216
191
217
// Wrap api routes
192
218
newConfig . module . rules . unshift ( {
193
- test : resourcePath => {
194
- const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
195
- return (
196
- normalizedAbsoluteResourcePath . startsWith ( apiRoutesPath + path . sep ) &&
197
- dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
198
- ) ;
199
- } ,
219
+ test : isApiRouteResource ,
200
220
use : [
201
221
{
202
222
loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
@@ -211,12 +231,7 @@ export function constructWebpackConfigFunction(
211
231
212
232
// Wrap middleware
213
233
newConfig . module . rules . unshift ( {
214
- test : resourcePath => {
215
- const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
216
- return (
217
- normalizedAbsoluteResourcePath === middlewareJsPath || normalizedAbsoluteResourcePath === middlewareTsPath
218
- ) ;
219
- } ,
234
+ test : isMiddlewareResource ,
220
235
use : [
221
236
{
222
237
loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
@@ -232,22 +247,36 @@ export function constructWebpackConfigFunction(
232
247
if ( isServer && userSentryOptions . autoInstrumentAppDirectory !== false ) {
233
248
// Wrap page server components
234
249
newConfig . module . rules . unshift ( {
235
- test : resourcePath => {
236
- const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
250
+ test : isServerComponentResource ,
251
+ use : [
252
+ {
253
+ loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
254
+ options : {
255
+ ...staticWrappingLoaderOptions ,
256
+ wrappingTargetKind : 'server-component' ,
257
+ } ,
258
+ } ,
259
+ ] ,
260
+ } ) ;
261
+ }
237
262
238
- // ".js, .jsx, or .tsx file extensions can be used for Pages"
239
- // https://beta.nextjs.org/docs/routing/pages-and-layouts#pages:~:text=.js%2C%20.jsx%2C%20or%20.tsx%20file%20extensions%20can%20be%20used%20for%20Pages.
263
+ if ( isServer ) {
264
+ // Import the Sentry config in every user file
265
+ newConfig . module . rules . unshift ( {
266
+ test : resourcePath => {
240
267
return (
241
- normalizedAbsoluteResourcePath . startsWith ( appDirPath + path . sep ) &&
242
- ! ! normalizedAbsoluteResourcePath . match ( / [ \\ / ] ( p a g e | l a y o u t | l o a d i n g | h e a d | n o t - f o u n d ) \. ( j s | j s x | t s x ) $ / )
268
+ isPageResource ( resourcePath ) ||
269
+ isApiRouteResource ( resourcePath ) ||
270
+ isMiddlewareResource ( resourcePath ) ||
271
+ isServerComponentResource ( resourcePath )
243
272
) ;
244
273
} ,
245
274
use : [
246
275
{
247
276
loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
248
277
options : {
249
278
...staticWrappingLoaderOptions ,
250
- wrappingTargetKind : 'server-component ' ,
279
+ wrappingTargetKind : 'sentry-init ' ,
251
280
} ,
252
281
} ,
253
282
] ,
0 commit comments