7
7
RegExpPrototypeExec,
8
8
SafeMap,
9
9
StringPrototypeCodePointAt,
10
+ StringPrototypeIndexOf,
11
+ StringPrototypeMatch,
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,11 @@ 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 ) {
110
- return null ;
105
+ const match = StringPrototypeMatch ( content , kSourceURLMagicComment ) ?. at ( - 1 ) ;
106
+ if ( match == null ) {
107
+ return null ;
111
108
}
112
- let sourceURL = matchSourceURL . groups . sourceURL ;
109
+ let sourceURL = StringPrototypeSubstring ( match , StringPrototypeIndexOf ( match , '=' ) + 1 ) ;
113
110
if ( sourceURL != null && RegExpPrototypeExec ( kLeadingProtocol , sourceURL ) === null ) {
114
111
sourceURL = pathToFileURL ( sourceURL ) . href ;
115
112
}
@@ -125,17 +122,11 @@ function extractSourceURLMagicComment(content) {
125
122
* @returns {string | null } source map url or null if not present
126
123
*/
127
124
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 ) {
136
- return null ;
125
+ const match = StringPrototypeMatch ( content , kSourceMappingURLMagicComment ) ?. at ( - 1 ) ;
126
+ if ( match == null ) {
127
+ return null ;
137
128
}
138
- return lastMatch . groups . sourceMappingURL ;
129
+ return StringPrototypeSubstring ( match , StringPrototypeIndexOf ( match , '=' ) + 1 ) ;
139
130
}
140
131
141
132
/**
0 commit comments