Skip to content

Commit

Permalink
recognize when //# sourceMappingURL is percent-encoded (#1330)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cspotcode committed May 23, 2021
1 parent 6b8323e commit 16b66eb
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/index.ts
Expand Up @@ -1318,20 +1318,46 @@ function updateOutput(
'utf8'
).toString('base64');
const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`;
// Expected form: `//# sourceMappingURL=foo.js.map` for input file foo.tsx
const sourceMapLength =
/*//# sourceMappingURL=*/ 21 +
/*foo.tsx*/ basename(fileName).length -
/*.tsx*/ extname(fileName).length +
/*.js*/ getExtension(fileName).length +
/*.map*/ 4;
// Only rewrite if existing directive exists, to support compilers that do not append a sourcemap directive
return (
(outputText.slice(-sourceMapLength, -sourceMapLength + 21) ===
'//# sourceMappingURL='
? outputText.slice(0, -sourceMapLength)
: outputText) + sourceMapContent
);
// Expected form: `//# sourceMappingURL=foo bar.js.map` or `//# sourceMappingURL=foo%20bar.js.map` for input file "foo bar.tsx"
// Percent-encoding behavior added in TS 4.1.1: https://github.com/microsoft/TypeScript/issues/40951
const prefix = '//# sourceMappingURL=';
const prefixLength = prefix.length;
const baseName = /*foo.tsx*/ basename(fileName);
const extName = /*.tsx*/ extname(fileName);
const extension = /*.js*/ getExtension(fileName);
const sourcemapFilename =
baseName.slice(0, -extName.length) + extension + '.map';
const sourceMapLengthWithoutPercentEncoding =
prefixLength + sourcemapFilename.length;
/*
* Only rewrite if existing directive exists at the location we expect, to support:
* a) compilers that do not append a sourcemap directive
* b) situations where we did the math wrong
* Not ideal, but appending our sourcemap *after* a pre-existing sourcemap still overrides, so the end-user is happy.
*/
if (
outputText.substr(-sourceMapLengthWithoutPercentEncoding, prefixLength) ===
prefix
) {
return (
outputText.slice(0, -sourceMapLengthWithoutPercentEncoding) +
sourceMapContent
);
}
// 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);
const sourceMapLengthWithPercentEncoding =
prefixLength + encodeURI(sourcemapFilename).length;
if (
outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) ===
prefix
) {
return (
outputText.slice(0, -sourceMapLengthWithPercentEncoding) +
sourceMapContent
);
}

return `${outputText}\n${sourceMapContent}`;
}

/**
Expand Down

0 comments on commit 16b66eb

Please sign in to comment.