Skip to content

Commit

Permalink
Automatically activate node source map when enabled in tsconfig.json (#…
Browse files Browse the repository at this point in the history
…33)

* feat: set --enable-source-maps when sourceMap is active in tsconfig.json

* feat: enable source map when watch is active

* refactor: move source map activation in a dedicated function

* test: remove unusefull assertion in sourceMap.test.js
  • Loading branch information
Palaxx committed May 21, 2024
1 parent 50e315f commit 2c8349f
Show file tree
Hide file tree
Showing 6 changed files with 4,824 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fixtures/ts-esm-source-map/src/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function add (x: number, y: number): number {
return x + y
}
7 changes: 7 additions & 0 deletions fixtures/ts-esm-source-map/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../src/add.js'
import { strictEqual } from 'node:assert'

test('add', () => {
strictEqual(add(1, 2), 5)
})
24 changes: 24 additions & 0 deletions fixtures/ts-esm-source-map/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"outDir": "dist",
"sourceMap": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"strict": true,
"resolveJsonModule": true,
"removeComments": true,
"newLine": "lf",
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"lib": [
"ESNext"
],
"incremental": true
}
}
11 changes: 11 additions & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ function deferred () {
return { resolve, reject, promise }
}

function enableSourceMapSupport (tsconfig) {
if (!tsconfig?.compilerOptions?.sourceMap) {
return
}

process.env.NODE_OPTIONS = (process.env.NODE_OPTIONS || '') + ' --enable-source-maps'
}
export default async function runWithTypeScript (config) {
// This is a hack to override
// https://github.com/nodejs/node/commit/d5c9adf3df
Expand Down Expand Up @@ -58,6 +65,8 @@ export default async function runWithTypeScript (config) {
prefix = join(dirname(tsconfigPath), outDir)
}

enableSourceMapSupport(tsconfig)

if (tscPath) {
// This will throw if we cannot find the `tsc` binary
await access(tscPath)
Expand Down Expand Up @@ -116,6 +125,8 @@ export default async function runWithTypeScript (config) {
if (config['post-compile'] && tsconfigPath) {
const tsconfig = JSON.parse(await readFile(tsconfigPath))
outDir = tsconfig.compilerOptions.outDir

enableSourceMapSupport(tsconfig)
}
let start = Date.now()
tscChild = execa('node', [tscPath, ...typescriptCliArgs], { cwd })
Expand Down
Loading

0 comments on commit 2c8349f

Please sign in to comment.