Skip to content

Commit 3faae6c

Browse files
authored
fix: reintroduce source-map support (#1195)
1 parent 3ec8a85 commit 3faae6c

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@
6060
"rxjs": "^6.5.4",
6161
"simple-git": "^2.0.0",
6262
"slash": "^3.0.0",
63+
"source-map-support": "^0.5.19",
6364
"strip-ansi": "^6.0.0",
6465
"ts-morph": "^7.1.0",
65-
"ts-node": "^8.10.1",
66+
"ts-node": "^8.10.2",
6667
"tslib": "^2.0.0",
6768
"type-fest": "^0.16.0",
6869
"typescript": "3.9.x"
@@ -77,6 +78,7 @@
7778
"@types/lodash": "4.14.152",
7879
"@types/node": "13.13.9",
7980
"@types/parse-json": "^4.0.0",
81+
"@types/source-map-support": "^0.5.2",
8082
"@types/which": "^1.3.2",
8183
"cross-env": "^7.0.2",
8284
"docsify": "4.11.3",

src/lib/tsc.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Either, left, right } from 'fp-ts/lib/Either'
22
import * as fs from 'fs-jetpack'
3+
import * as path from 'path'
34
import { addHook } from 'pirates'
5+
import slash from 'slash'
6+
import * as sourceMapSupport from 'source-map-support'
47
import * as ts from 'typescript'
58
import { Layout } from './layout'
69
import { rootLogger } from './nexus-logger'
@@ -141,20 +144,84 @@ function createTSError(diagnostics: ReadonlyArray<ts.Diagnostic>) {
141144
* This is strictly about transpilation, no type checking is done.
142145
*/
143146
export function registerTypeScriptTranspile(compilerOptions?: ts.CompilerOptions) {
147+
const outputCache = new Map<
148+
string,
149+
{
150+
content: string
151+
}
152+
>()
153+
const options = compilerOptions ?? {}
154+
sourceMapSupport.install({
155+
environment: 'node',
156+
retrieveFile(path) {
157+
return outputCache.get(slash(path))?.content || ''
158+
},
159+
})
160+
161+
/**
162+
* Get the extension for a transpiled file.
163+
*/
164+
const getExtension =
165+
options.jsx === ts.JsxEmit.Preserve
166+
? (path: string) => (/\.[tj]sx$/.test(path) ? '.jsx' : '.js')
167+
: (_: string) => '.js'
168+
144169
addHook(
145170
(source, fileName) => {
146171
const transpiled = ts.transpileModule(source, {
147172
reportDiagnostics: true,
148173
fileName,
149-
compilerOptions,
174+
compilerOptions: {
175+
...options,
176+
sourceMap: true,
177+
},
150178
})
151179

152180
if (transpiled.diagnostics && transpiled.diagnostics.length > 0) {
153181
throw createTSError(transpiled.diagnostics)
154182
}
155183

184+
const normalizedFileName = slash(fileName)
185+
const output = updateOutput(
186+
transpiled.outputText,
187+
normalizedFileName,
188+
transpiled.sourceMapText!,
189+
getExtension
190+
)
191+
outputCache.set(normalizedFileName, { content: output })
192+
156193
return transpiled.outputText
157194
},
158195
{ exts: ['.ts'] }
159196
)
160197
}
198+
199+
/**
200+
* Update the output remapping the source map.
201+
* Taken from ts-node
202+
*/
203+
function updateOutput(
204+
outputText: string,
205+
fileName: string,
206+
sourceMap: string,
207+
getExtension: (fileName: string) => string
208+
) {
209+
const base64Map = Buffer.from(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64')
210+
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`
211+
const sourceMapLength =
212+
`${path.basename(fileName)}.map`.length + (getExtension(fileName).length - path.extname(fileName).length)
213+
214+
return outputText.slice(0, -sourceMapLength) + sourceMapContent
215+
}
216+
217+
/**
218+
* Update the source map contents for improved output.
219+
* Taken from ts-node
220+
*/
221+
function updateSourceMap(sourceMapText: string, fileName: string) {
222+
const sourceMap = JSON.parse(sourceMapText)
223+
sourceMap.file = fileName
224+
sourceMap.sources = [fileName]
225+
delete sourceMap.sourceRoot
226+
return JSON.stringify(sourceMap)
227+
}

yarn.lock

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,14 @@
10051005
"@types/express-serve-static-core" "*"
10061006
"@types/mime" "*"
10071007

1008+
"@types/source-map-support@^0.5.2":
1009+
version "0.5.2"
1010+
resolved "https://registry.yarnpkg.com/@types/source-map-support/-/source-map-support-0.5.2.tgz#35e7446d470e63ec3fff4d7eceee0bc40efad239"
1011+
integrity sha512-krvWmwQ2Pzr+Yp8tKjhKC9UguRNg1ev9mNdlVVpVJvU9iynulYZsx3ydf1SPzNNxzhmbWAOAIw5hMWhAMDc2NA==
1012+
dependencies:
1013+
"@types/node" "*"
1014+
source-map "^0.6.0"
1015+
10081016
"@types/stack-utils@^1.0.1":
10091017
version "1.0.1"
10101018
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
@@ -5776,7 +5784,7 @@ source-map-resolve@^0.5.0:
57765784
source-map-url "^0.4.0"
57775785
urix "^0.1.0"
57785786

5779-
source-map-support@^0.5.17:
5787+
source-map-support@^0.5.17, source-map-support@^0.5.19:
57805788
version "0.5.19"
57815789
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
57825790
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
@@ -6229,10 +6237,10 @@ ts-morph@^7.1.0:
62296237
"@ts-morph/common" "~0.5.0"
62306238
code-block-writer "^10.1.0"
62316239

6232-
ts-node@^8.10.1:
6233-
version "8.10.1"
6234-
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.1.tgz#77da0366ff8afbe733596361d2df9a60fc9c9bd3"
6235-
integrity sha512-bdNz1L4ekHiJul6SHtZWs1ujEKERJnHs4HxN7rjTyyVOFf3HaJ6sLqe6aPG62XTzAB/63pKRh5jTSWL0D7bsvw==
6240+
ts-node@^8.10.2:
6241+
version "8.10.2"
6242+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.2.tgz#eee03764633b1234ddd37f8db9ec10b75ec7fb8d"
6243+
integrity sha512-ISJJGgkIpDdBhWVu3jufsWpK3Rzo7bdiIXJjQc0ynKxVOVcg2oIrf2H2cejminGrptVc6q6/uynAHNCuWGbpVA==
62366244
dependencies:
62376245
arg "^4.1.0"
62386246
diff "^4.0.1"

0 commit comments

Comments
 (0)