@@ -139,7 +139,9 @@ function extractSourceMapURLMagicComment(content) {
139
139
}
140
140
141
141
/**
142
- * Caches the source map if it is present in the content, with the given filename, moduleInstance, and sourceURL.
142
+ * Caches the source map, with the given filename, moduleInstance, sourceURL and sourceMapURL.
143
+ * This function does not automatically extract the source map from the content. The caller should either
144
+ * extract the source map from the content via V8 API or use {@link extractSourceURLMagicComment} explicitly.
143
145
* @param {string } filename - the actual filename
144
146
* @param {string } content - the actual source content
145
147
* @param {import('internal/modules/cjs/loader').Module | ModuleWrap } moduleInstance - a module instance that
@@ -162,20 +164,13 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
162
164
return ;
163
165
}
164
166
165
- if ( sourceMapURL === undefined ) {
166
- sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
167
- }
168
-
169
167
// Bail out when there is no source map url.
170
168
if ( typeof sourceMapURL !== 'string' ) {
171
169
return ;
172
170
}
173
171
174
- // FIXME: callers should obtain sourceURL from v8 and pass it
175
- // rather than leaving it undefined and extract by regex.
176
- if ( sourceURL === undefined ) {
177
- sourceURL = extractSourceURLMagicComment ( content ) ;
178
- }
172
+ // Normalize the sourceURL to a file URL if it is a path.
173
+ sourceURL = normalizeReferrerURL ( sourceURL ) ;
179
174
180
175
const data = dataFromUrl ( filename , sourceMapURL ) ;
181
176
// `data` could be null if the source map is invalid.
@@ -192,9 +187,6 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
192
187
193
188
if ( isGeneratedSource ) {
194
189
generatedSourceMapCache . set ( filename , entry ) ;
195
- if ( sourceURL ) {
196
- generatedSourceMapCache . set ( sourceURL , entry ) ;
197
- }
198
190
return ;
199
191
}
200
192
// If it is not a generated source, we assume we are in a "cjs/esm"
@@ -215,8 +207,14 @@ function maybeCacheGeneratedSourceMap(content) {
215
207
if ( sourceURL === null ) {
216
208
return ;
217
209
}
210
+ const sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
211
+ if ( sourceMapURL === null ) {
212
+ return ;
213
+ }
214
+
218
215
try {
219
- maybeCacheSourceMap ( sourceURL , content , null , true , sourceURL ) ;
216
+ // Use the sourceURL as the filename, and do not create a duplicate entry.
217
+ maybeCacheSourceMap ( sourceURL , content , null , true , undefined /** no duplicated sourceURL */ , sourceMapURL ) ;
220
218
} catch ( err ) {
221
219
// This can happen if the filename is not a valid URL.
222
220
// If we fail to cache the source map, we should not fail the whole process.
0 commit comments