Skip to content

Commit 680947c

Browse files
committed
lib: don't match sourceMappingURL in strings
Prior to this change `sourceMappingURL` in string where being matched by the RegExp which caused sourcemaps not be loaded when using the `--enable-source-maps` flag. This commit changes the RegExp to match the last occurrence. Fixes: #44654
1 parent aa64ff6 commit 680947c

5 files changed

+39
-11
lines changed

lib/internal/source_map/source_map_cache.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const esmSourceMapCache = new SafeMap();
4141
// The generated sources is not mutable, so we can use a Map without memory concerns:
4242
const generatedSourceMapCache = new SafeMap();
4343
const kLeadingProtocol = /^\w+:\/\//;
44-
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/;
45-
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/;
44+
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/g;
45+
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
4646

4747
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
4848
let SourceMap;
@@ -80,11 +80,12 @@ function setSourceMapsEnabled(val) {
8080
}
8181

8282
function extractSourceURLMagicComment(content) {
83-
const matchSourceURL = RegExpPrototypeExec(
84-
kSourceURLMagicComment,
85-
content
86-
);
87-
if (matchSourceURL === null) {
83+
let match;
84+
let matchSourceURL;
85+
while ((match = RegExpPrototypeExec(kSourceURLMagicComment, content))) {
86+
matchSourceURL = match;
87+
}
88+
if (matchSourceURL == null) {
8889
return null;
8990
}
9091
let sourceURL = matchSourceURL.groups.sourceURL;
@@ -104,13 +105,19 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
104105
debug(err);
105106
return;
106107
}
107-
const match = RegExpPrototypeExec(kSourceMappingURLMagicComment, content);
108+
109+
let match;
110+
let lastMatch;
111+
while ((match = RegExpPrototypeExec(kSourceMappingURLMagicComment, content))) {
112+
lastMatch = match;
113+
}
114+
108115
if (sourceURL === undefined) {
109116
sourceURL = extractSourceURLMagicComment(content);
110117
}
111-
if (match) {
112-
const data = dataFromUrl(filename, match.groups.sourceMappingURL);
113-
const url = data ? null : match.groups.sourceMappingURL;
118+
if (lastMatch) {
119+
const data = dataFromUrl(filename, lastMatch.groups.sourceMappingURL);
120+
const url = data ? null : lastMatch.groups.sourceMappingURL;
114121
if (cjsModuleInstance) {
115122
cjsSourceMapCache.set(cjsModuleInstance, {
116123
filename,

test/fixtures/source-map/typescript-sourcemapping_url_string.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-map/typescript-sourcemapping_url_string.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Flags: --enable-source-maps
2+
3+
'use strict';
4+
require('../common');
5+
Error.stackTraceLimit = 2;
6+
7+
try {
8+
require('../fixtures/source-map/typescript-sourcemapping_url_string');
9+
} catch (err) {
10+
setTimeout(() => {
11+
console.info(err);
12+
}, 10);
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Error: an exception.
2+
at *typescript-sourcemapping_url_string.ts:3:7*
3+
at Module._compile (node:internal/modules/cjs/loader:1129:14)

0 commit comments

Comments
 (0)