Skip to content

Commit e5a0fa1

Browse files
author
Luca Forstner
authored
fix: Make sourceMappingURL heuristic more resilient (#378)
1 parent 2735252 commit e5a0fa1

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

packages/bundler-plugin-core/src/debug-id-upload.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,35 @@ async function determineSourceMapPathFromBundle(
298298
const sourceMappingUrlMatch = bundleSource.match(/^\s*\/\/# sourceMappingURL=(.*)$/m);
299299
if (sourceMappingUrlMatch) {
300300
const sourceMappingUrl = path.normalize(sourceMappingUrlMatch[1] as string);
301-
if (path.isAbsolute(sourceMappingUrl)) {
302-
return sourceMappingUrl;
301+
302+
let isUrl;
303+
let isSupportedUrl;
304+
try {
305+
const url = new URL(sourceMappingUrl);
306+
isUrl = true;
307+
isSupportedUrl = url.protocol === "file:";
308+
} catch {
309+
isUrl = false;
310+
isSupportedUrl = false;
311+
}
312+
313+
let absoluteSourceMapPath;
314+
if (isSupportedUrl) {
315+
absoluteSourceMapPath = sourceMappingUrl;
316+
} else if (isUrl) {
317+
return;
318+
} else if (path.isAbsolute(sourceMappingUrl)) {
319+
absoluteSourceMapPath = sourceMappingUrl;
303320
} else {
304-
return path.join(path.dirname(bundlePath), sourceMappingUrl);
321+
absoluteSourceMapPath = path.join(path.dirname(bundlePath), sourceMappingUrl);
322+
}
323+
324+
try {
325+
// Check if the file actually exists
326+
await util.promisify(fs.access)(absoluteSourceMapPath);
327+
return absoluteSourceMapPath;
328+
} catch (e) {
329+
// noop
305330
}
306331
}
307332

0 commit comments

Comments
 (0)