Skip to content

Commit 16b66eb

Browse files
authored
recognize when //# sourceMappingURL is percent-encoded (#1330)
* recognize when //# sourceMappingURL is percent-encoded * lint-fix * dedupe const value * fix * use encodeURI instead of url.format because it seems closer to what TS does; it percent-encodes the square brackets in [eval].ts * remove unused import
1 parent 6b8323e commit 16b66eb

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

src/index.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,20 +1318,46 @@ function updateOutput(
13181318
'utf8'
13191319
).toString('base64');
13201320
const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`;
1321-
// Expected form: `//# sourceMappingURL=foo.js.map` for input file foo.tsx
1322-
const sourceMapLength =
1323-
/*//# sourceMappingURL=*/ 21 +
1324-
/*foo.tsx*/ basename(fileName).length -
1325-
/*.tsx*/ extname(fileName).length +
1326-
/*.js*/ getExtension(fileName).length +
1327-
/*.map*/ 4;
1328-
// Only rewrite if existing directive exists, to support compilers that do not append a sourcemap directive
1329-
return (
1330-
(outputText.slice(-sourceMapLength, -sourceMapLength + 21) ===
1331-
'//# sourceMappingURL='
1332-
? outputText.slice(0, -sourceMapLength)
1333-
: outputText) + sourceMapContent
1334-
);
1321+
// Expected form: `//# sourceMappingURL=foo bar.js.map` or `//# sourceMappingURL=foo%20bar.js.map` for input file "foo bar.tsx"
1322+
// Percent-encoding behavior added in TS 4.1.1: https://github.com/microsoft/TypeScript/issues/40951
1323+
const prefix = '//# sourceMappingURL=';
1324+
const prefixLength = prefix.length;
1325+
const baseName = /*foo.tsx*/ basename(fileName);
1326+
const extName = /*.tsx*/ extname(fileName);
1327+
const extension = /*.js*/ getExtension(fileName);
1328+
const sourcemapFilename =
1329+
baseName.slice(0, -extName.length) + extension + '.map';
1330+
const sourceMapLengthWithoutPercentEncoding =
1331+
prefixLength + sourcemapFilename.length;
1332+
/*
1333+
* Only rewrite if existing directive exists at the location we expect, to support:
1334+
* a) compilers that do not append a sourcemap directive
1335+
* b) situations where we did the math wrong
1336+
* Not ideal, but appending our sourcemap *after* a pre-existing sourcemap still overrides, so the end-user is happy.
1337+
*/
1338+
if (
1339+
outputText.substr(-sourceMapLengthWithoutPercentEncoding, prefixLength) ===
1340+
prefix
1341+
) {
1342+
return (
1343+
outputText.slice(0, -sourceMapLengthWithoutPercentEncoding) +
1344+
sourceMapContent
1345+
);
1346+
}
1347+
// If anyone asks why we're not using URL, the URL equivalent is: `u = new URL('http://d'); u.pathname = "/" + sourcemapFilename; return u.pathname.slice(1);
1348+
const sourceMapLengthWithPercentEncoding =
1349+
prefixLength + encodeURI(sourcemapFilename).length;
1350+
if (
1351+
outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) ===
1352+
prefix
1353+
) {
1354+
return (
1355+
outputText.slice(0, -sourceMapLengthWithPercentEncoding) +
1356+
sourceMapContent
1357+
);
1358+
}
1359+
1360+
return `${outputText}\n${sourceMapContent}`;
13351361
}
13361362

13371363
/**

0 commit comments

Comments
 (0)