@@ -140,19 +140,45 @@ export function constructWebpackConfigFunction(
140140 return path . normalize ( absoluteResourcePath ) ;
141141 } ;
142142
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+
143176 if ( isServer && userSentryOptions . autoInstrumentServerFunctions !== false ) {
144177 // 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.
145178
146179 // Wrap pages
147180 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 ,
156182 use : [
157183 {
158184 loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
@@ -190,13 +216,7 @@ export function constructWebpackConfigFunction(
190216
191217 // Wrap api routes
192218 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 ,
200220 use : [
201221 {
202222 loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
@@ -211,12 +231,7 @@ export function constructWebpackConfigFunction(
211231
212232 // Wrap middleware
213233 newConfig . module . rules . unshift ( {
214- test : resourcePath => {
215- const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath ( resourcePath ) ;
216- return (
217- normalizedAbsoluteResourcePath === middlewareJsPath || normalizedAbsoluteResourcePath === middlewareTsPath
218- ) ;
219- } ,
234+ test : isMiddlewareResource ,
220235 use : [
221236 {
222237 loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
@@ -232,22 +247,36 @@ export function constructWebpackConfigFunction(
232247 if ( isServer && userSentryOptions . autoInstrumentAppDirectory !== false ) {
233248 // Wrap page server components
234249 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+ }
237262
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 => {
240267 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 )
243272 ) ;
244273 } ,
245274 use : [
246275 {
247276 loader : path . resolve ( __dirname , 'loaders' , 'wrappingLoader.js' ) ,
248277 options : {
249278 ...staticWrappingLoaderOptions ,
250- wrappingTargetKind : 'server-component ' ,
279+ wrappingTargetKind : 'sentry-init ' ,
251280 } ,
252281 } ,
253282 ] ,
0 commit comments