@@ -41,8 +41,8 @@ const esmSourceMapCache = new SafeMap();
4141// The generated sources is not mutable, so we can use a Map without memory concerns:
4242const generatedSourceMapCache = new SafeMap ( ) ;
4343const kLeadingProtocol = / ^ \w + : \/ \/ / ;
44- const kSourceMappingURLMagicComment = / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = (?< sourceMappingURL > [ ^ \s ] + ) / ;
45- const kSourceURLMagicComment = / \/ [ * / ] # \s + s o u r c e U R L = (?< sourceURL > [ ^ \s ] + ) / ;
44+ const kSourceMappingURLMagicComment = / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = (?< sourceMappingURL > [ ^ \s ] + ) / g ;
45+ const kSourceURLMagicComment = / \/ [ * / ] # \s + s o u r c e U R L = (?< sourceURL > [ ^ \s ] + ) / g ;
4646
4747const { fileURLToPath, pathToFileURL, URL } = require ( 'internal/url' ) ;
4848let SourceMap ;
@@ -80,11 +80,14 @@ function setSourceMapsEnabled(val) {
8080}
8181
8282function extractSourceURLMagicComment ( content ) {
83- const matchSourceURL = RegExpPrototypeExec (
84- kSourceURLMagicComment ,
85- content
86- ) ;
87- if ( matchSourceURL === null ) {
83+ let match ;
84+ let matchSourceURL ;
85+ // A while loop is used here to get the last occurrence of sourceURL.
86+ // This is needed so that we don't match sourceURL in string literals.
87+ while ( ( match = RegExpPrototypeExec ( kSourceURLMagicComment , content ) ) ) {
88+ matchSourceURL = match ;
89+ }
90+ if ( matchSourceURL == null ) {
8891 return null ;
8992 }
9093 let sourceURL = matchSourceURL . groups . sourceURL ;
@@ -104,13 +107,21 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
104107 debug ( err ) ;
105108 return ;
106109 }
107- const match = RegExpPrototypeExec ( kSourceMappingURLMagicComment , content ) ;
110+
111+ let match ;
112+ let lastMatch ;
113+ // A while loop is used here to get the last occurrence of sourceMappingURL.
114+ // This is needed so that we don't match sourceMappingURL in string literals.
115+ while ( ( match = RegExpPrototypeExec ( kSourceMappingURLMagicComment , content ) ) ) {
116+ lastMatch = match ;
117+ }
118+
108119 if ( sourceURL === undefined ) {
109120 sourceURL = extractSourceURLMagicComment ( content ) ;
110121 }
111- if ( match ) {
112- const data = dataFromUrl ( filename , match . groups . sourceMappingURL ) ;
113- const url = data ? null : match . groups . sourceMappingURL ;
122+ if ( lastMatch ) {
123+ const data = dataFromUrl ( filename , lastMatch . groups . sourceMappingURL ) ;
124+ const url = data ? null : lastMatch . groups . sourceMappingURL ;
114125 if ( cjsModuleInstance ) {
115126 cjsSourceMapCache . set ( cjsModuleInstance , {
116127 filename,
0 commit comments