5
5
JSONParse,
6
6
ObjectFreeze,
7
7
RegExpPrototypeExec,
8
+ RegExpPrototypeSymbolMatch,
8
9
SafeMap,
9
10
StringPrototypeCodePointAt,
11
+ StringPrototypeIndexOf,
10
12
StringPrototypeSplit,
13
+ StringPrototypeSubstring,
11
14
} = primordials ;
12
15
13
16
// See https://tc39.es/ecma426/ for SourceMap V3 specification.
@@ -37,8 +40,8 @@ const getModuleSourceMapCache = getLazy(() => {
37
40
// source url magic comments.
38
41
const generatedSourceMapCache = new SafeMap ( ) ;
39
42
const kLeadingProtocol = / ^ \w + : \/ \/ / ;
40
- const kSourceMappingURLMagicComment = / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = (?< sourceMappingURL > [ ^ \s ] + ) / g;
41
- const kSourceURLMagicComment = / \/ [ * / ] # \s + s o u r c e U R L = (?< sourceURL > [ ^ \s ] + ) / g;
43
+ const kSourceMappingURLMagicComment = / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = [ ^ \s ] + / g;
44
+ const kSourceURLMagicComment = / \/ [ * / ] # \s + s o u r c e U R L = [ ^ \s ] + / g;
42
45
43
46
const { isAbsolute } = require ( 'path' ) ;
44
47
const { fileURLToPath, pathToFileURL, URL , URLParse } = require ( 'internal/url' ) ;
@@ -99,17 +102,12 @@ function setSourceMapsSupport(enabled, options = kEmptyObject) {
99
102
* @returns {string | null } source url or null if not present
100
103
*/
101
104
function extractSourceURLMagicComment ( content ) {
102
- let match ;
103
- let matchSourceURL ;
104
- // A while loop is used here to get the last occurrence of sourceURL.
105
- // This is needed so that we don't match sourceURL in string literals.
106
- while ( ( match = RegExpPrototypeExec ( kSourceURLMagicComment , content ) ) ) {
107
- matchSourceURL = match ;
108
- }
109
- if ( matchSourceURL == null ) {
105
+ const matches = RegExpPrototypeSymbolMatch ( kSourceURLMagicComment , content ) ;
106
+ if ( matches == null || matches . length === 0 ) {
110
107
return null ;
111
108
}
112
- let sourceURL = matchSourceURL . groups . sourceURL ;
109
+ const match = matches [ matches . length - 1 ] ;
110
+ let sourceURL = StringPrototypeSubstring ( match , StringPrototypeIndexOf ( match , '=' ) + 1 ) ;
113
111
if ( sourceURL != null && RegExpPrototypeExec ( kLeadingProtocol , sourceURL ) === null ) {
114
112
sourceURL = pathToFileURL ( sourceURL ) . href ;
115
113
}
@@ -125,17 +123,12 @@ function extractSourceURLMagicComment(content) {
125
123
* @returns {string | null } source map url or null if not present
126
124
*/
127
125
function extractSourceMapURLMagicComment ( content ) {
128
- let match ;
129
- let lastMatch ;
130
- // A while loop is used here to get the last occurrence of sourceMappingURL.
131
- // This is needed so that we don't match sourceMappingURL in string literals.
132
- while ( ( match = RegExpPrototypeExec ( kSourceMappingURLMagicComment , content ) ) ) {
133
- lastMatch = match ;
134
- }
135
- if ( lastMatch == null ) {
126
+ const matches = RegExpPrototypeSymbolMatch ( kSourceMappingURLMagicComment , content ) ;
127
+ if ( matches == null || matches . length === 0 ) {
136
128
return null ;
137
129
}
138
- return lastMatch . groups . sourceMappingURL ;
130
+ const match = matches [ matches . length - 1 ] ;
131
+ return StringPrototypeSubstring ( match , StringPrototypeIndexOf ( match , '=' ) + 1 ) ;
139
132
}
140
133
141
134
/**
0 commit comments