Skip to content

Commit face997

Browse files
refactor: leverage process.features.typescript instead of node version (#452)
* refactor: leverage process.features.typescript instead of node version * fix: features.typescript check * fix: supportNativeTypeScript check * docs: unreachable code
1 parent bf7dfac commit face997

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

lib/runtime.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,20 @@ Object.defineProperties(runtime, {
117117
runtime.tsx ||
118118
runtime.esbuild ||
119119
runtime.tsimp ||
120-
runtime.nodeVersion >= 23
120+
runtime.supportNativeTypeScript
121121
)
122122
return cache.supportTypeScript
123123
}
124124
},
125+
supportNativeTypeScript: {
126+
get () {
127+
cache.supportNativeTypeScript ??=
128+
/* c8 ignore start - The right-hand part of the branch is unreachable on v20 for the moment */
129+
process.features.typescript !== undefined && process.features.typescript !== false
130+
/* c8 ignore end */
131+
return cache.supportNativeTypeScript
132+
}
133+
},
125134
forceESM: {
126135
get () {
127136
cache.forceESM ??= (
@@ -132,12 +141,6 @@ Object.defineProperties(runtime, {
132141
return cache.forceESM
133142
}
134143
},
135-
nodeVersion: {
136-
get () {
137-
cache.nodeVersion ??= Number(process.version.split('.')[0].slice(1))
138-
return cache.nodeVersion
139-
},
140-
},
141144
})
142145

143146
module.exports = runtime

scripts/unit-typescript-native-type-stripping.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
const { exec } = require('node:child_process')
44
const runtime = require('../lib/runtime')
55

6+
if (runtime.supportNativeTypeScript) {
7+
common()
8+
}
9+
610
function common () {
711
const args = ['node', 'test/typescript-common/index.ts']
812
const child = exec(args.join(' '), {
@@ -24,7 +28,3 @@ function esm () {
2428
child.stderr.pipe(process.stderr)
2529
child.once('close', process.exit)
2630
}
27-
28-
if (runtime.nodeVersion >= 23) {
29-
common()
30-
}

scripts/unit-typescript-tsx.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict'
22

33
const { exec } = require('node:child_process')
4-
const runtime = require('../lib/runtime')
4+
5+
const nodeVersion = Number(process.version.split('.')[0].slice(1))
56

67
const args = [
78
'npx',
8-
runtime.nodeVersion >= 18 ? '--node-options=--import=tsx' : '',
9+
nodeVersion >= 18 ? '--node-options=--import=tsx' : '',
910
'tsnd',
1011
'test/typescript/basic.ts'
1112
]

test/commonjs/error.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const Fastify = require('fastify')
66

77
const runtime = require('../../lib/runtime')
88

9-
const typeStrippingEnabled = runtime.nodeVersion >= 23
10-
119
describe('independent module support - unexpected token', function () {
1210
const app = Fastify()
1311

@@ -52,7 +50,7 @@ describe('independent module support - cannot import plugin typescript', functio
5250
})
5351

5452
it('should return cannot import plugin typescript error', async function () {
55-
if (typeStrippingEnabled) {
53+
if (runtime.supportNativeTypeScript) {
5654
assert.doesNotThrow(() => app.ready())
5755
} else {
5856
await assert.rejects(app.ready(), Error, /cannot import plugin.*typescript/i)

test/issues/369/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Issue 369 tests', async function () {
2424
dir: path.join(__dirname, 'invalid-index-type'),
2525
autoHooks: true,
2626
})
27-
if (runtime.nodeVersion >= 23) {
27+
if (runtime.supportNativeTypeScript) {
2828
assert.doesNotThrow(() => app.ready())
2929
} else {
3030
await assert.rejects(

test/typescript-common/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use script'
22

3-
const t = require('tap')
3+
const { test: t } = require('node:test')
44
const fastify = require('fastify')
55

66
const basicApp = require('./basic/app.ts')

0 commit comments

Comments
 (0)