Skip to content

Commit bb04960

Browse files
authored
feat(rspeedy/core): use process.features.typescript (#481)
<!-- Thank you for submitting a pull request! We appreciate the time and effort you have invested in making these changes. Please ensure that you provide enough information to allow others to review your pull request. Upon submission, your pull request will be automatically assigned with reviewers. If you want to learn more about contributing to this project, please visit: https://github.com/lynx-family/lynx-stack/blob/main/CONTRIBUTING.md. --> ## Summary <!-- Can you explain the reasoning behind implementing this change? What problem or issue does this pull request resolve? --> Node.js v23.6 has unflagged the `--experimental-strip-types` feature (nodejs/node#56350). Instead of relying solely on this flag, it’s recommended to also check the standard `process.feature.typescript` to confirm native TypeScript support. > [!NOTE] > It will be backport to v22 soon. See: nodejs/node#57298 <!-- It would be helpful if you could provide any relevant context, such as GitHub issues or related discussions. --> ## Checklist <!--- Check and mark with an "x" --> - [x] Tests updated (or not required). - [x] Documentation updated (or not required).
1 parent ea83ac1 commit bb04960

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

.changeset/slick-pianos-beg.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@lynx-js/rspeedy": patch
3+
---
4+
5+
Support Node.js v23.6+ native TypeScript.
6+
7+
See [Node.js - TypeScript](https://nodejs.org/api/typescript.html) for more details.

packages/rspeedy/core/src/config/loadConfig.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ function shouldUseNativeImport(configPath: string): boolean {
150150
}
151151

152152
function hasNativeTSSupport(): boolean {
153+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
154+
if (process.features.typescript) {
155+
// This is added in Node.js v22.10.
156+
// 1. Node.js v22.10+ with --experimental-transform-types or --experimental-strip-types
157+
// 2. Node.js v23.6+
158+
return true
159+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
160+
} else if (process.features.typescript === false) {
161+
// 1. Node.js v22.10+ without --experimental-transform-types or --experimental-strip-types
162+
// 2. Node.js v23.6+ with --no-experimental-strip-types
163+
return false
164+
}
165+
166+
// Node.js < v22.10
153167
const { NODE_OPTIONS } = process.env
154168

155169
if (!NODE_OPTIONS) {
@@ -164,3 +178,7 @@ function isJavaScriptPath(configPath: string): boolean {
164178
const ext = extname(configPath)
165179
return ['.js', '.mjs', '.cjs'].includes(ext)
166180
}
181+
182+
export function TEST_ONLY_hasNativeTSSupport(): boolean {
183+
return hasNativeTSSupport()
184+
}

packages/rspeedy/core/test/config/loadConfig.test.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { mkdtemp, writeFile } from 'node:fs/promises'
55
import { tmpdir } from 'node:os'
66
import { join } from 'node:path'
77

8-
import { describe, expect, test } from 'vitest'
8+
import { beforeEach, describe, expect, test, vi } from 'vitest'
99

10-
import { loadConfig } from '../../src/config/loadConfig.js'
10+
import {
11+
TEST_ONLY_hasNativeTSSupport as hasNativeTSSupport,
12+
loadConfig,
13+
} from '../../src/config/loadConfig.js'
1114
import type { Config } from '../../src/index.js'
1215

1316
describe('Config - loadConfig', () => {
@@ -307,3 +310,57 @@ describe('Config - loadConfig', () => {
307310
})
308311
})
309312
})
313+
314+
describe('hasNativeTSSupport', () => {
315+
const process: {
316+
env: Record<string, string>
317+
features: { typescript?: false | 'strip' | 'transform' | undefined }
318+
} = {
319+
env: {},
320+
features: {},
321+
}
322+
323+
beforeEach(() => {
324+
process.env = {}
325+
process.features = {}
326+
vi.stubGlobal('process', process)
327+
328+
return () => {
329+
vi.unstubAllGlobals()
330+
}
331+
})
332+
333+
test('without features.typescript', () => {
334+
expect(hasNativeTSSupport()).toBe(false)
335+
})
336+
337+
test('with features.typescript: "transform"', () => {
338+
process.features.typescript = 'transform'
339+
expect(hasNativeTSSupport()).toBe(true)
340+
})
341+
342+
test('with features.typescript: "strip"', () => {
343+
process.features.typescript = 'strip'
344+
expect(hasNativeTSSupport()).toBe(true)
345+
})
346+
347+
test('with features.typescript: false', () => {
348+
process.features.typescript = false
349+
expect(hasNativeTSSupport()).toBe(false)
350+
})
351+
352+
test('with features.typescript: undefined', () => {
353+
process.features.typescript = undefined
354+
expect(hasNativeTSSupport()).toBe(false)
355+
})
356+
357+
test('with NODE_OPTIONS: --experimental-transform-types', () => {
358+
process.env['NODE_OPTIONS'] = '--experimental-transform-types'
359+
expect(hasNativeTSSupport()).toBe(true)
360+
})
361+
362+
test('with NODE_OPTIONS: --experimental-strip-types', () => {
363+
process.env['NODE_OPTIONS'] = '--experimental-strip-types'
364+
expect(hasNativeTSSupport()).toBe(true)
365+
})
366+
})

website/docs/en/guide/cli.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ Just like [Rush](https://rushstack.io/), Rspeedy implements a "version selector"
1818

1919
## Using Node.js TypeScript support
2020

21-
If the version of Node.js you are using supports the [--experimental-transform-types](https://nodejs.org/api/cli.html#--experimental-transform-types)(v22.7.0) or [--experimental-strip-types](https://nodejs.org/api/cli.html#--experimental-strip-types)(v22.6.0) flag, you can use the built-in TS transformation of Node.js.
21+
If the version of Node.js you are using supports TypeScript:
22+
23+
1. Node.js >= v23.6
24+
1. Node.js >= v22.6 with [--experimental-strip-types](https://nodejs.org/api/cli.html#--experimental-strip-types)
25+
1. Node.js >= v22.7 with [--experimental-transform-types](https://nodejs.org/api/cli.html#--experimental-transform-types)
26+
27+
you can use the built-in TS transformation of Node.js.
2228

2329
```json title="package.json"
2430
{
2531
"build": "NODE_OPTIONS=--experimental-transform-types rspeedy build"
2632
}
2733
```
2834

35+
See [Node.js - TypeScript](https://nodejs.org/api/typescript.html) for more details.
36+
2937
## rspeedy -h
3038

3139
To view all available CLI commands, run the following command in the project directory:

website/docs/zh/guide/cli.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ npm install --global @lynx-js/rspeedy
1818

1919
## 使用 Node.js 的 TypeScript 支持
2020

21-
如果你使用的 Node.js 版本支持 [--experimental-transform-types](https://nodejs.org/api/cli.html#--experimental-transform-types)(v22.7.0)或 [--experimental-strip-types](https://nodejs.org/api/cli.html#--experimental-strip-types)(v22.6.0)标志,可以使用 Node.js 内置的 TS 转换功能。
21+
如果你使用的 Node.js 版本支持 TypeScript:
22+
23+
1. Node.js >= v23.6
24+
1. Node.js >= v22.6 使用 [--experimental-strip-types](https://nodejs.org/api/cli.html#--experimental-strip-types)
25+
1. Node.js >= v22.7 使用 [--experimental-transform-types](https://nodejs.org/api/cli.html#--experimental-transform-types)
26+
27+
可以使用 Node.js 内置的 TypeScript 转换功能。
2228

2329
```json title="package.json"
2430
{

0 commit comments

Comments
 (0)