@@ -170,18 +170,54 @@ export class SourceProcessor {
170170 }
171171
172172 public async getSourceMapPathFromSource ( source : string , sourcePath : string ) : Promise < string | undefined > {
173- const resolveFile = ( filePath : string ) =>
174- pipe ( filePath , statFile , ( result ) =>
175- ! result . isOk ( ) || result . data . isFile ( )
176- ? filePath
177- : ( path . join ( filePath , path . basename ( sourcePath ) + '.map' ) as string | undefined ) ,
173+ const matchAll = ( str : string , regex : RegExp ) => {
174+ const result : RegExpMatchArray [ ] = [ ] ;
175+ // eslint-disable-next-line no-constant-condition
176+ while ( true ) {
177+ const match = regex . exec ( str ) ;
178+ if ( ! match ) {
179+ return result ;
180+ }
181+ result . push ( match ) ;
182+ }
183+ } ;
184+
185+ const checkFile = ( filePath : string ) =>
186+ pipe ( filePath , statFile , ( result ) => ( result . isOk ( ) && result . data . isFile ( ) ? filePath : undefined ) ) ;
187+
188+ const sourceMapName = path . basename ( sourcePath ) + '.map' ;
189+ const checkFileInDir = ( dir : string ) =>
190+ pipe ( dir , statFile , ( result ) =>
191+ result . isOk ( ) && result . data . isDirectory ( )
192+ ? // If path exists and is a directory, check if file exists in that dir
193+ checkFile ( path . join ( dir , sourceMapName ) )
194+ : // If path does not exist or is not a directory, check if file exists in dir of that path
195+ checkFile ( path . join ( path . dirname ( dir ) , sourceMapName ) ) ,
178196 ) ;
179197
180- return pipe ( source . match ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = ( .+ ) $ / m) , ( match ) =>
181- ! match || ! match [ 1 ]
182- ? undefined
183- : pipe ( match [ 1 ] , ( match ) => path . resolve ( path . dirname ( sourcePath ) , match ) , resolveFile ) ,
184- ) ;
198+ const matches = matchAll ( source , / ^ \s * \/ \/ # s o u r c e M a p p i n g U R L = ( .+ ) $ / gm) ;
199+ if ( ! matches . length ) {
200+ return checkFileInDir ( sourcePath ) ;
201+ }
202+
203+ for ( const match of matches . reverse ( ) ) {
204+ const file = match [ 1 ] ;
205+ if ( ! file ) {
206+ continue ;
207+ }
208+
209+ const fullPath = path . resolve ( path . dirname ( sourcePath ) , file ) ;
210+ if ( await checkFile ( fullPath ) ) {
211+ return fullPath ;
212+ }
213+
214+ const fileInDir = await checkFileInDir ( fullPath ) ;
215+ if ( fileInDir ) {
216+ return fileInDir ;
217+ }
218+ }
219+
220+ return checkFileInDir ( sourcePath ) ;
185221 }
186222
187223 public async addSourcesToSourceMap (
0 commit comments