@@ -37,6 +37,7 @@ const DATA_FETCHING_FUNCTIONS = {
37
37
38
38
type LoaderOptions = {
39
39
projectDir : string ;
40
+ pageRegex : RegExp ;
40
41
} ;
41
42
42
43
/**
@@ -108,7 +109,7 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
108
109
}
109
110
110
111
// We know one or the other will be defined, depending on the version of webpack being used
111
- const { projectDir } = 'getOptions' in this ? this . getOptions ( ) : this . query ;
112
+ const { projectDir, pageRegex } = 'getOptions' in this ? this . getOptions ( ) : this . query ;
112
113
113
114
// In the following branch we will proxy the user's file. This means we return code (basically an entirely new file)
114
115
// that re - exports all the user file's originial export, but with a "sentry-proxy-loader" query in the module
@@ -170,12 +171,23 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
170
171
path . relative ( projectDir , this . resourcePath ) ,
171
172
) ;
172
173
174
+ // Get the route name from this page's filepath
175
+ let route = this . resourcePath . match ( pageRegex ) ?. [ 2 ] ;
176
+ if ( ! route ) {
177
+ logger . warn ( `Unable to wrap code from page ${ this . resourcePath } , because the route regex has no matches.` ) ;
178
+ return userCode ;
179
+ }
180
+
173
181
// Fill in template placeholders
174
182
let injectedCode = modifiedTemplateCode ;
175
183
for ( const { placeholder, alias } of Object . values ( DATA_FETCHING_FUNCTIONS ) ) {
176
184
injectedCode = injectedCode . replace ( new RegExp ( placeholder , 'g' ) , alias ) ;
177
185
}
178
186
187
+ // Any route ending in '/index' will correspond to the root of that directory, '/'.
188
+ route = route . replace ( / i n d e x $ / , '' ) ;
189
+ injectedCode = injectedCode . replace ( '__FILEPATH__' , route ) ;
190
+
179
191
return `${ modifiedUserCode } \n${ injectedCode } ` ;
180
192
}
181
193
}
0 commit comments