Skip to content

Commit b3fd2b9

Browse files
committed
fix: find the *last* sourceMappingURL in the source document.
This fixes odd behaviour for source files that contain the literal string '//# sourceMappingURL=' somewhere in the actual source code.
1 parent e2dd940 commit b3fd2b9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

source-map-support.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ function retrieveSourceMapURL(source) {
7272

7373
// Get the URL of the source map
7474
fileData = retrieveFile(source);
75-
var match = /\/\/[#@]\s*sourceMappingURL=([^'"]+)\s*$/m.exec(fileData);
76-
if (!match) return null;
77-
return match[1];
75+
var re = /\/\/[#@]\s*sourceMappingURL=([^'"]+)\s*$/mg;
76+
// Keep executing the search to find the *last* sourceMappingURL to avoid
77+
// picking up sourceMappingURLs from comments, strings, etc.
78+
var lastMatch, match;
79+
while (match = re.exec(fileData)) lastMatch = match;
80+
if (!lastMatch) return null;
81+
return lastMatch[1];
7882
};
7983

8084
// Can be overridden by the retrieveSourceMap option to install. Takes a

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ it('sourcesContent with data URL', function() {
250250
]);
251251
});
252252

253+
it('finds the last sourceMappingURL', function() {
254+
compareStackTrace(createMultiLineSourceMapWithSourcesContent(), [
255+
'//# sourceMappingURL=missing.map.js', // NB: compareStackTrace adds another source mapping.
256+
'throw new Error("test");'
257+
], [
258+
'Error: test',
259+
/^ at Object\.exports\.test \(.*\/original.js:1002:5\)$/
260+
]);
261+
});
262+
253263
it('default options', function(done) {
254264
compareStdout(done, createSingleLineSourceMap(), [
255265
'',

0 commit comments

Comments
 (0)