@@ -139,7 +139,9 @@ function extractSourceMapURLMagicComment(content) {
139139}
140140
141141/**
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.
143145 * @param {string } filename - the actual filename
144146 * @param {string } content - the actual source content
145147 * @param {import('internal/modules/cjs/loader').Module | ModuleWrap } moduleInstance - a module instance that
@@ -162,19 +164,15 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
162164 return ;
163165 }
164166
165- if ( sourceMapURL === undefined ) {
166- sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
167- }
168-
169167 // Bail out when there is no source map url.
170168 if ( typeof sourceMapURL !== 'string' ) {
171169 return ;
172170 }
173171
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 ) ;
172+ if ( sourceURL !== undefined ) {
173+ // SourceURL magic comment content might be a file path or URL .
174+ // Normalize the sourceURL to be a file URL if it is a file path.
175+ sourceURL = normalizeReferrerURL ( sourceURL ) ;
178176 }
179177
180178 const data = dataFromUrl ( filename , sourceMapURL ) ;
@@ -215,8 +213,13 @@ function maybeCacheGeneratedSourceMap(content) {
215213 if ( sourceURL === null ) {
216214 return ;
217215 }
216+ const sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
217+ if ( sourceMapURL === null ) {
218+ return ;
219+ }
218220 try {
219- maybeCacheSourceMap ( sourceURL , content , null , true , sourceURL ) ;
221+ // Use the sourceURL as the filename, and do not create a duplicate entry.
222+ maybeCacheSourceMap ( sourceURL , content , null , true , undefined /** no duplicated sourceURL */ , sourceMapURL ) ;
220223 } catch ( err ) {
221224 // This can happen if the filename is not a valid URL.
222225 // If we fail to cache the source map, we should not fail the whole process.
0 commit comments