@@ -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,21 +164,11 @@ 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 ) ;
178- }
179-
180172 const data = dataFromUrl ( filename , sourceMapURL ) ;
181173 // `data` could be null if the source map is invalid.
182174 // In this case, create a cache entry with null data with source url for test coverage.
@@ -192,9 +184,6 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
192184
193185 if ( isGeneratedSource ) {
194186 generatedSourceMapCache . set ( filename , entry ) ;
195- if ( sourceURL ) {
196- generatedSourceMapCache . set ( sourceURL , entry ) ;
197- }
198187 return ;
199188 }
200189 // If it is not a generated source, we assume we are in a "cjs/esm"
@@ -215,8 +204,13 @@ function maybeCacheGeneratedSourceMap(content) {
215204 if ( sourceURL === null ) {
216205 return ;
217206 }
207+ const sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
208+ if ( sourceMapURL === null ) {
209+ return ;
210+ }
218211 try {
219- maybeCacheSourceMap ( sourceURL , content , null , true , sourceURL ) ;
212+ // Use the sourceURL as the filename, and do not create a duplicate entry.
213+ maybeCacheSourceMap ( sourceURL , content , null , true , undefined /** no duplicated sourceURL */ , sourceMapURL ) ;
220214 } catch ( err ) {
221215 // This can happen if the filename is not a valid URL.
222216 // If we fail to cache the source map, we should not fail the whole process.
0 commit comments