From ea97eae5e25933fd4524d0bf96adbf2712060df4 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 22 May 2021 18:20:07 -0400 Subject: [PATCH 1/6] recognize when //# sourceMappingURL is percent-encoded --- src/index.ts | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index e8d44384d..f3ed23fac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { relative, basename, extname, resolve, dirname, join } from 'path'; import { Module } from 'module'; import * as util from 'util'; -import { fileURLToPath } from 'url'; +import { fileURLToPath, format as urlFormat } from 'url'; import sourceMapSupport = require('source-map-support'); import { BaseError } from 'make-error'; @@ -1318,20 +1318,30 @@ 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(-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) == '//# sourceMappingURL=') { + return outputText.slice(0, -sourceMapLengthWithoutPercentEncoding) + sourceMapContent; + } + const sourceMapLengthWithPercentEncoding = prefixLength + urlFormat(sourcemapFilename).length; + if(outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) === '//# sourceMappingURL=') { + return outputText.slice(0, -sourceMapLengthWithPercentEncoding) + sourceMapContent; + } + + return `${ outputText }\n${ sourceMapContent }`; } /** From 6fd11c80f856d9687f6ea5c53496f710f9d32cd7 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 22 May 2021 18:29:00 -0400 Subject: [PATCH 2/6] lint-fix --- src/index.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index f3ed23fac..e8b1b0d40 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1325,23 +1325,38 @@ function updateOutput( const baseName = /*foo.tsx*/ basename(fileName); const extName = /*.tsx*/ extname(fileName); const extension = /*.js*/ getExtension(fileName); - const sourcemapFilename = baseName.slice(-extName.length) + extension + '.map'; - const sourceMapLengthWithoutPercentEncoding = prefixLength + sourcemapFilename.length; + const sourcemapFilename = + baseName.slice(-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) == '//# sourceMappingURL=') { - return outputText.slice(0, -sourceMapLengthWithoutPercentEncoding) + sourceMapContent; + if ( + outputText.substr(-sourceMapLengthWithoutPercentEncoding, prefixLength) == + '//# sourceMappingURL=' + ) { + return ( + outputText.slice(0, -sourceMapLengthWithoutPercentEncoding) + + sourceMapContent + ); } - const sourceMapLengthWithPercentEncoding = prefixLength + urlFormat(sourcemapFilename).length; - if(outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) === '//# sourceMappingURL=') { - return outputText.slice(0, -sourceMapLengthWithPercentEncoding) + sourceMapContent; + const sourceMapLengthWithPercentEncoding = + prefixLength + urlFormat(sourcemapFilename).length; + if ( + outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) === + '//# sourceMappingURL=' + ) { + return ( + outputText.slice(0, -sourceMapLengthWithPercentEncoding) + + sourceMapContent + ); } - return `${ outputText }\n${ sourceMapContent }`; + return `${outputText}\n${sourceMapContent}`; } /** From 9ef8c0e8568c052126432550ff7702398e1078d3 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 22 May 2021 18:44:04 -0400 Subject: [PATCH 3/6] dedupe const value --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index e8b1b0d40..db12b53c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1336,8 +1336,8 @@ function updateOutput( * Not ideal, but appending our sourcemap *after* a pre-existing sourcemap still overrides, so the end-user is happy. */ if ( - outputText.substr(-sourceMapLengthWithoutPercentEncoding, prefixLength) == - '//# sourceMappingURL=' + outputText.substr(-sourceMapLengthWithoutPercentEncoding, prefixLength) === + prefix ) { return ( outputText.slice(0, -sourceMapLengthWithoutPercentEncoding) + @@ -1348,7 +1348,7 @@ function updateOutput( prefixLength + urlFormat(sourcemapFilename).length; if ( outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) === - '//# sourceMappingURL=' + prefix ) { return ( outputText.slice(0, -sourceMapLengthWithPercentEncoding) + From f66316f85dfd1ba232df1e1d199016dbd53d3c6d Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 22 May 2021 21:11:22 -0400 Subject: [PATCH 4/6] fix --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index db12b53c9..d05436327 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1326,7 +1326,7 @@ function updateOutput( const extName = /*.tsx*/ extname(fileName); const extension = /*.js*/ getExtension(fileName); const sourcemapFilename = - baseName.slice(-extName.length) + extension + '.map'; + baseName.slice(0, -extName.length) + extension + '.map'; const sourceMapLengthWithoutPercentEncoding = prefixLength + sourcemapFilename.length; /* From c837b75f309910970521fc39beb633b1619d09fc Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 22 May 2021 21:24:50 -0400 Subject: [PATCH 5/6] use encodeURI instead of url.format because it seems closer to what TS does; it percent-encodes the square brackets in [eval].ts --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index d05436327..8f7b6dd1d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1344,8 +1344,9 @@ function updateOutput( 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 + urlFormat(sourcemapFilename).length; + prefixLength + encodeURI(sourcemapFilename).length; if ( outputText.substr(-sourceMapLengthWithPercentEncoding, prefixLength) === prefix From e813ef7666fc594d4ccb484bd99693a927132fe1 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 22 May 2021 22:03:30 -0400 Subject: [PATCH 6/6] remove unused import --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 8f7b6dd1d..9aea226d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { relative, basename, extname, resolve, dirname, join } from 'path'; import { Module } from 'module'; import * as util from 'util'; -import { fileURLToPath, format as urlFormat } from 'url'; +import { fileURLToPath } from 'url'; import sourceMapSupport = require('source-map-support'); import { BaseError } from 'make-error';