@@ -99,20 +99,59 @@ export function constructWebpackConfigFunction(
99
99
100
100
if ( isServer ) {
101
101
if ( userSentryOptions . autoInstrumentServerFunctions !== false ) {
102
- const pagesDir = newConfig . resolve ?. alias ?. [ 'private-next-pages' ] as string ;
102
+ let pagesDirPath : string ;
103
+ if (
104
+ fs . existsSync ( path . join ( projectDir , 'pages' ) ) &&
105
+ fs . lstatSync ( path . join ( projectDir , 'pages' ) ) . isDirectory ( )
106
+ ) {
107
+ pagesDirPath = path . join ( projectDir , 'pages' ) ;
108
+ } else {
109
+ pagesDirPath = path . join ( projectDir , 'src' , 'pages' ) ;
110
+ }
111
+
112
+ const middlewareJsPath = path . join ( pagesDirPath , '..' , 'middleware.js' ) ;
113
+ const middlewareTsPath = path . join ( pagesDirPath , '..' , 'middleware.ts' ) ;
103
114
104
115
// Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161
105
116
const pageExtensions = userNextConfig . pageExtensions || [ 'tsx' , 'ts' , 'jsx' , 'js' ] ;
117
+ const dotPrefixedPageExtensions = pageExtensions . map ( ext => `.${ ext } ` ) ;
106
118
const pageExtensionRegex = pageExtensions . map ( escapeStringForRegex ) . join ( '|' ) ;
107
119
108
120
// It is very important that we insert our loader at the beginning of the array because we expect any sort of transformations/transpilations (e.g. TS -> JS) to already have happened.
109
121
newConfig . module . rules . unshift ( {
110
- test : new RegExp ( `^${ escapeStringForRegex ( pagesDir ) } .*\\.(${ pageExtensionRegex } )$` ) ,
122
+ test : resourcePath => {
123
+ // We generally want to apply the loader to all API routes, pages and to the middleware file.
124
+
125
+ // `resourcePath` may be an absolute path or a path relative to the context of the webpack config
126
+ let absoluteResourcePath : string ;
127
+ if ( path . isAbsolute ( resourcePath ) ) {
128
+ absoluteResourcePath = resourcePath ;
129
+ } else {
130
+ absoluteResourcePath = path . join ( projectDir , resourcePath ) ;
131
+ }
132
+ const normalizedAbsoluteResourcePath = path . normalize ( absoluteResourcePath ) ;
133
+
134
+ if (
135
+ // Match everything inside pages/ with the appropriate file extension
136
+ normalizedAbsoluteResourcePath . startsWith ( pagesDirPath ) &&
137
+ dotPrefixedPageExtensions . some ( ext => normalizedAbsoluteResourcePath . endsWith ( ext ) )
138
+ ) {
139
+ return true ;
140
+ } else if (
141
+ // Match middleware.js and middleware.ts
142
+ normalizedAbsoluteResourcePath === middlewareJsPath ||
143
+ normalizedAbsoluteResourcePath === middlewareTsPath
144
+ ) {
145
+ return true ;
146
+ } else {
147
+ return false ;
148
+ }
149
+ } ,
111
150
use : [
112
151
{
113
152
loader : path . resolve ( __dirname , 'loaders/wrappingLoader.js' ) ,
114
153
options : {
115
- pagesDir,
154
+ pagesDir : pagesDirPath ,
116
155
pageExtensionRegex,
117
156
excludeServerRoutes : userSentryOptions . excludeServerRoutes ,
118
157
} ,
0 commit comments