@@ -97,7 +97,21 @@ function extractSourceURLMagicComment(content) {
97
97
return sourceURL ;
98
98
}
99
99
100
- function maybeCacheSourceMap ( filename , content , cjsModuleInstance , isGeneratedSource , sourceURL ) {
100
+ function extractSourceMapURLMagicComment ( content ) {
101
+ let match ;
102
+ let lastMatch ;
103
+ // A while loop is used here to get the last occurrence of sourceMappingURL.
104
+ // This is needed so that we don't match sourceMappingURL in string literals.
105
+ while ( ( match = RegExpPrototypeExec ( kSourceMappingURLMagicComment , content ) ) ) {
106
+ lastMatch = match ;
107
+ }
108
+ if ( lastMatch == null ) {
109
+ return null ;
110
+ }
111
+ return lastMatch . groups . sourceMappingURL ;
112
+ }
113
+
114
+ function maybeCacheSourceMap ( filename , content , cjsModuleInstance , isGeneratedSource , sourceURL , sourceMapURL ) {
101
115
const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
102
116
if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
103
117
try {
@@ -108,52 +122,52 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
108
122
return ;
109
123
}
110
124
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 ;
125
+ if ( sourceMapURL === undefined ) {
126
+ sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
127
+ }
128
+
129
+ // Bail out when there is no source map url.
130
+ if ( typeof sourceMapURL !== 'string' ) {
131
+ return ;
117
132
}
118
133
119
134
if ( sourceURL === undefined ) {
120
135
sourceURL = extractSourceURLMagicComment ( content ) ;
121
136
}
122
- if ( lastMatch ) {
123
- const data = dataFromUrl ( filename , lastMatch . groups . sourceMappingURL ) ;
124
- const url = data ? null : lastMatch . groups . sourceMappingURL ;
125
- if ( cjsModuleInstance ) {
126
- cjsSourceMapCache . set ( cjsModuleInstance , {
127
- filename,
128
- lineLengths : lineLengths ( content ) ,
129
- data,
130
- url,
131
- sourceURL,
132
- } ) ;
133
- } else if ( isGeneratedSource ) {
134
- const entry = {
135
- lineLengths : lineLengths ( content ) ,
136
- data,
137
- url,
138
- sourceURL
139
- } ;
140
- generatedSourceMapCache . set ( filename , entry ) ;
141
- if ( sourceURL ) {
142
- generatedSourceMapCache . set ( sourceURL , entry ) ;
143
- }
144
- } else {
145
- // If there is no cjsModuleInstance and is not generated source assume we are in a
146
- // "modules/esm" context.
147
- const entry = {
148
- lineLengths : lineLengths ( content ) ,
149
- data,
150
- url,
151
- sourceURL,
152
- } ;
153
- esmSourceMapCache . set ( filename , entry ) ;
154
- if ( sourceURL ) {
155
- esmSourceMapCache . set ( sourceURL , entry ) ;
156
- }
137
+
138
+ const data = dataFromUrl ( filename , sourceMapURL ) ;
139
+ const url = data ? null : sourceMapURL ;
140
+ if ( cjsModuleInstance ) {
141
+ cjsSourceMapCache . set ( cjsModuleInstance , {
142
+ filename,
143
+ lineLengths : lineLengths ( content ) ,
144
+ data,
145
+ url,
146
+ sourceURL,
147
+ } ) ;
148
+ } else if ( isGeneratedSource ) {
149
+ const entry = {
150
+ lineLengths : lineLengths ( content ) ,
151
+ data,
152
+ url,
153
+ sourceURL
154
+ } ;
155
+ generatedSourceMapCache . set ( filename , entry ) ;
156
+ if ( sourceURL ) {
157
+ generatedSourceMapCache . set ( sourceURL , entry ) ;
158
+ }
159
+ } else {
160
+ // If there is no cjsModuleInstance and is not generated source assume we are in a
161
+ // "modules/esm" context.
162
+ const entry = {
163
+ lineLengths : lineLengths ( content ) ,
164
+ data,
165
+ url,
166
+ sourceURL,
167
+ } ;
168
+ esmSourceMapCache . set ( filename , entry ) ;
169
+ if ( sourceURL ) {
170
+ esmSourceMapCache . set ( sourceURL , entry ) ;
157
171
}
158
172
}
159
173
}
0 commit comments