Skip to content

fix: reintroduce source-map support #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@
"rxjs": "^6.5.4",
"simple-git": "^2.0.0",
"slash": "^3.0.0",
"source-map-support": "^0.5.19",
"strip-ansi": "^6.0.0",
"ts-morph": "^7.1.0",
"ts-node": "^8.10.1",
"ts-node": "^8.10.2",
"tslib": "^2.0.0",
"type-fest": "^0.16.0",
"typescript": "3.9.x"
Expand All @@ -77,6 +78,7 @@
"@types/lodash": "4.14.152",
"@types/node": "13.13.9",
"@types/parse-json": "^4.0.0",
"@types/source-map-support": "^0.5.2",
"@types/which": "^1.3.2",
"cross-env": "^7.0.2",
"docsify": "4.11.3",
Expand Down
69 changes: 68 additions & 1 deletion src/lib/tsc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Either, left, right } from 'fp-ts/lib/Either'
import * as fs from 'fs-jetpack'
import * as path from 'path'
import { addHook } from 'pirates'
import slash from 'slash'
import * as sourceMapSupport from 'source-map-support'
import * as ts from 'typescript'
import { Layout } from './layout'
import { rootLogger } from './nexus-logger'
Expand Down Expand Up @@ -141,20 +144,84 @@ function createTSError(diagnostics: ReadonlyArray<ts.Diagnostic>) {
* This is strictly about transpilation, no type checking is done.
*/
export function registerTypeScriptTranspile(compilerOptions?: ts.CompilerOptions) {
const outputCache = new Map<
string,
{
content: string
}
>()
const options = compilerOptions ?? {}
sourceMapSupport.install({
environment: 'node',
retrieveFile(path) {
return outputCache.get(slash(path))?.content || ''
},
})

/**
* Get the extension for a transpiled file.
*/
const getExtension =
options.jsx === ts.JsxEmit.Preserve
? (path: string) => (/\.[tj]sx$/.test(path) ? '.jsx' : '.js')
: (_: string) => '.js'

addHook(
(source, fileName) => {
const transpiled = ts.transpileModule(source, {
reportDiagnostics: true,
fileName,
compilerOptions,
compilerOptions: {
...options,
sourceMap: true,
},
})

if (transpiled.diagnostics && transpiled.diagnostics.length > 0) {
throw createTSError(transpiled.diagnostics)
}

const normalizedFileName = slash(fileName)
const output = updateOutput(
transpiled.outputText,
normalizedFileName,
transpiled.sourceMapText!,
getExtension
)
outputCache.set(normalizedFileName, { content: output })

return transpiled.outputText
},
{ exts: ['.ts'] }
)
}

/**
* Update the output remapping the source map.
* Taken from ts-node
*/
function updateOutput(
outputText: string,
fileName: string,
sourceMap: string,
getExtension: (fileName: string) => string
) {
const base64Map = Buffer.from(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64')
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`
const sourceMapLength =
`${path.basename(fileName)}.map`.length + (getExtension(fileName).length - path.extname(fileName).length)

return outputText.slice(0, -sourceMapLength) + sourceMapContent
}

/**
* Update the source map contents for improved output.
* Taken from ts-node
*/
function updateSourceMap(sourceMapText: string, fileName: string) {
const sourceMap = JSON.parse(sourceMapText)
sourceMap.file = fileName
sourceMap.sources = [fileName]
delete sourceMap.sourceRoot
return JSON.stringify(sourceMap)
}
18 changes: 13 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,14 @@
"@types/express-serve-static-core" "*"
"@types/mime" "*"

"@types/source-map-support@^0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@types/source-map-support/-/source-map-support-0.5.2.tgz#35e7446d470e63ec3fff4d7eceee0bc40efad239"
integrity sha512-krvWmwQ2Pzr+Yp8tKjhKC9UguRNg1ev9mNdlVVpVJvU9iynulYZsx3ydf1SPzNNxzhmbWAOAIw5hMWhAMDc2NA==
dependencies:
"@types/node" "*"
source-map "^0.6.0"

"@types/stack-utils@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
Expand Down Expand Up @@ -5776,7 +5784,7 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"

source-map-support@^0.5.17:
source-map-support@^0.5.17, source-map-support@^0.5.19:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
Expand Down Expand Up @@ -6229,10 +6237,10 @@ ts-morph@^7.1.0:
"@ts-morph/common" "~0.5.0"
code-block-writer "^10.1.0"

ts-node@^8.10.1:
version "8.10.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.1.tgz#77da0366ff8afbe733596361d2df9a60fc9c9bd3"
integrity sha512-bdNz1L4ekHiJul6SHtZWs1ujEKERJnHs4HxN7rjTyyVOFf3HaJ6sLqe6aPG62XTzAB/63pKRh5jTSWL0D7bsvw==
ts-node@^8.10.2:
version "8.10.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.2.tgz#eee03764633b1234ddd37f8db9ec10b75ec7fb8d"
integrity sha512-ISJJGgkIpDdBhWVu3jufsWpK3Rzo7bdiIXJjQc0ynKxVOVcg2oIrf2H2cejminGrptVc6q6/uynAHNCuWGbpVA==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
Expand Down