Skip to content

Commit dd869c6

Browse files
committed
fix: only fallback to ts-node when it's available
close #128
1 parent b7be102 commit dd869c6

File tree

11 files changed

+5487
-5382
lines changed

11 files changed

+5487
-5382
lines changed

.codesandbox/ci.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"node": "16",
2+
"node": "18",
33
"sandboxes": []
44
}

.github/workflows/ci.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@ jobs:
1313
- 14
1414
- 16
1515
- 18
16+
- 20
1617
os:
1718
- macos-latest
1819
- ubuntu-latest
1920
- windows-latest
20-
exclude:
21-
- node: 18
22-
os: windows-latest
2321
runs-on: ${{ matrix.os }}
2422
steps:
2523
- name: Checkout Repo
26-
uses: actions/checkout@v3
24+
uses: actions/checkout@v4
2725

2826
- name: Setup Node.js ${{ matrix.node }}
29-
uses: actions/setup-node@v3
27+
uses: actions/setup-node@v4
3028
with:
3129
node-version: ${{ matrix.node }}
3230
cache: yarn
3331

3432
- name: Install Dependencies
35-
run: yarn --frozen-lockfile
33+
run: yarn --frozen-lockfile --ignore-engines
3634

3735
- name: Build, Lint and Test
3836
run: yarn run-s build lint test

.github/workflows/codeql.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ name: 'CodeQL'
22

33
on:
44
push:
5-
branches: ['main']
5+
branches:
6+
- main
67
pull_request:
7-
branches: ['main']
8+
branches:
9+
- main
810
schedule:
911
- cron: '3 2 * * 6'
1012

@@ -20,11 +22,12 @@ jobs:
2022
strategy:
2123
fail-fast: false
2224
matrix:
23-
language: [javascript]
25+
language:
26+
- javascript
2427

2528
steps:
2629
- name: Checkout
27-
uses: actions/checkout@v3
30+
uses: actions/checkout@v4
2831

2932
- name: Initialize CodeQL
3033
uses: github/codeql-action/init@v2

.github/workflows/pkg-size.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
steps:
1414
- name: Checkout
15-
uses: actions/checkout@v3
15+
uses: actions/checkout@v4
1616

1717
- name: Package Size Report
1818
uses: pkg-size/action@v1

.github/workflows/release.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout Repo
14-
uses: actions/checkout@v3
14+
uses: actions/checkout@v4
1515
with:
1616
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
1717
fetch-depth: 0
1818

19-
- name: Setup Node.js 16
20-
uses: actions/setup-node@v3
19+
- name: Setup Node.js LTS
20+
uses: actions/setup-node@v4
2121
with:
22-
node-version: 16
22+
node-version: --lts
2323
cache: yarn
2424

2525
- name: Install Dependencies

package.json

+30-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
88
"funding": "https://opencollective.com/unts",
99
"license": "MIT",
10-
"packageManager": "yarn@1.22.19",
10+
"packageManager": "yarn@1.22.21",
1111
"engines": {
1212
"node": "^14.18.0 || >=16.0.0"
1313
},
@@ -52,34 +52,34 @@
5252
"typecov": "type-coverage"
5353
},
5454
"dependencies": {
55-
"@pkgr/utils": "^2.3.1",
56-
"tslib": "^2.5.0"
55+
"@pkgr/utils": "^2.4.2",
56+
"tslib": "^2.6.2"
5757
},
5858
"devDependencies": {
59-
"@1stg/lib-config": "^10.2.1",
60-
"@changesets/changelog-github": "^0.4.6",
61-
"@changesets/cli": "^2.26.0",
62-
"@swc-node/register": "^1.5.5",
63-
"@types/jest": "^28.1.8",
64-
"@types/node": "^18.7.13",
59+
"@1stg/lib-config": "^12.0.0",
60+
"@changesets/changelog-github": "^0.5.0",
61+
"@changesets/cli": "^2.27.1",
62+
"@swc-node/register": "^1.6.8",
63+
"@types/jest": "^29.5.10",
64+
"@types/node": "^20.10.1",
6565
"clean-pkg-json": "^1.2.0",
66-
"deasync": "^0.1.28",
67-
"esbuild-register": "^3.3.3",
68-
"esbuild-runner": "^2.2.1",
69-
"execa": "^6.1.0",
70-
"jest": "^29.0.1",
71-
"patch-package": "^6.4.7",
66+
"deasync": "^0.1.29",
67+
"esbuild-register": "^3.5.0",
68+
"esbuild-runner": "^2.2.2",
69+
"execa": "^8.0.1",
70+
"jest": "^29.7.0",
71+
"patch-package": "^8.0.0",
7272
"sync-threads": "^1.0.1",
7373
"ts-expect": "^1.3.0",
74-
"ts-jest": "^28.0.8",
74+
"ts-jest": "^29.1.1",
7575
"ts-node": "^10.9.1",
76-
"tsx": "^3.12.2",
77-
"type-coverage": "^2.22.0",
78-
"typescript": "^4.9.4",
79-
"yarn-deduplicate": "^6.0.0"
76+
"tsx": "^4.6.1",
77+
"type-coverage": "^2.27.0",
78+
"typescript": "^5.3.2",
79+
"yarn-deduplicate": "^6.0.2"
8080
},
8181
"resolutions": {
82-
"prettier": "^2.7.1",
82+
"prettier": "^2.8.8",
8383
"synckit": "link:."
8484
},
8585
"commitlint": {
@@ -96,13 +96,16 @@
9696
"^(\\.{1,2}/.*)\\.js$": "$1",
9797
"^synckit$": "<rootDir>/src"
9898
},
99-
"globals": {
100-
"ts-jest": {
101-
"useESM": true,
102-
"tsconfig": {
103-
"importHelpers": false
99+
"transform": {
100+
"^.+\\.tsx?$": [
101+
"ts-jest",
102+
{
103+
"useESM": true,
104+
"tsconfig": {
105+
"importHelpers": false
106+
}
104107
}
105-
}
108+
]
106109
}
107110
},
108111
"prettier": "@1stg/prettier-config",

src/index.ts

+39-52
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
parentPort,
1212
} from 'node:worker_threads'
1313

14-
import { findUp, tryExtensions } from '@pkgr/utils'
14+
import { findUp, isPkgAvailable, tryExtensions } from '@pkgr/utils'
1515

1616
import {
1717
AnyAsyncFn,
@@ -59,8 +59,7 @@ export const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024
5959
/* istanbul ignore next */
6060
export const DEFAULT_EXEC_ARGV = SYNCKIT_EXEC_ARGV?.split(',') || []
6161

62-
export const DEFAULT_TS_RUNNER = (SYNCKIT_TS_RUNNER ||
63-
TsRunner.TsNode) as TsRunner
62+
export const DEFAULT_TS_RUNNER = SYNCKIT_TS_RUNNER as TsRunner | undefined
6463

6564
export const MTS_SUPPORTED_NODE_VERSION = 16
6665

@@ -134,15 +133,15 @@ const dataUrl = (code: string) =>
134133

135134
export const isFile = (path: string) => {
136135
try {
137-
return fs.statSync(path).isFile()
136+
return !!fs.statSync(path, { throwIfNoEntry: false })?.isFile()
138137
} catch {
139138
return false
140139
}
141140
}
142141

143142
const setupTsRunner = (
144143
workerPath: string,
145-
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
144+
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner?: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
146145
) => {
147146
let ext = path.extname(workerPath)
148147

@@ -155,15 +154,18 @@ const setupTsRunner = (
155154
: workerPath
156155
let extensions: string[]
157156
switch (ext) {
158-
case '.cjs':
157+
case '.cjs': {
159158
extensions = ['.cts', '.cjs']
160159
break
161-
case '.mjs':
160+
}
161+
case '.mjs': {
162162
extensions = ['.mts', '.mjs']
163163
break
164-
default:
164+
}
165+
default: {
165166
extensions = ['.ts', '.js']
166167
break
168+
}
167169
}
168170
const found = tryExtensions(workPathWithoutExt, extensions)
169171
let differentExt: boolean | undefined
@@ -188,6 +190,11 @@ const setupTsRunner = (
188190
'module'
189191
}
190192
}
193+
194+
if (tsRunner == null && isPkgAvailable(TsRunner.TsNode)) {
195+
tsRunner = TsRunner.TsNode
196+
}
197+
191198
switch (tsRunner) {
192199
case TsRunner.TsNode: {
193200
if (tsUseEsm) {
@@ -261,6 +268,7 @@ const setupTsRunner = (
261268
return {
262269
ext,
263270
isTs,
271+
tsRunner,
264272
tsUseEsm,
265273
workerPath,
266274
execArgv,
@@ -283,6 +291,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
283291
isTs,
284292
ext,
285293
tsUseEsm,
294+
tsRunner: finalTsRunner,
286295
workerPath: finalWorkerPath,
287296
execArgv: finalExecArgv,
288297
} = setupTsRunner(workerPath, { execArgv, tsRunner })
@@ -293,8 +302,9 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
293302
const isTsxSupported =
294303
!tsUseEsm ||
295304
Number.parseFloat(process.versions.node) >= MTS_SUPPORTED_NODE_VERSION
296-
/* istanbul ignore if */
297-
if (
305+
if (!finalTsRunner) {
306+
throw new Error('No ts runner specified, ts worker path is not supported')
307+
} /* istanbul ignore if */ else if (
298308
(
299309
[
300310
// https://github.com/egoist/esbuild-register/issues/79
@@ -305,10 +315,10 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
305315
TsRunner.SWC,
306316
.../* istanbul ignore next */ (isTsxSupported ? [] : [TsRunner.TSX]),
307317
] as TsRunner[]
308-
).includes(tsRunner)
318+
).includes(finalTsRunner)
309319
) {
310320
throw new Error(
311-
`${tsRunner} is not supported for ${ext} files yet` +
321+
`${finalTsRunner} is not supported for ${ext} files yet` +
312322
(isTsxSupported
313323
? ', you can try [tsx](https://github.com/esbuild-kit/tsx) instead'
314324
: ''),
@@ -322,7 +332,8 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
322332
tsUseEsm && tsRunner === TsRunner.TsNode
323333
? dataUrl(`import '${String(workerPathUrl)}'`)
324334
: useEval
325-
? `require('${finalWorkerPath.replace(/\\/g, '\\\\')}')`
335+
? // eslint-disable-next-line unicorn/prefer-string-replace-all -- compatibility
336+
`require('${finalWorkerPath.replace(/\\/g, '\\\\')}')`
326337
: workerPathUrl,
327338
{
328339
eval: useEval,
@@ -387,46 +398,22 @@ export function runAsWorker<
387398

388399
const { workerPort } = workerData as WorkerData
389400

390-
try {
391-
parentPort!.on(
392-
'message',
393-
({ sharedBuffer, id, args }: MainToWorkerMessage<Parameters<T>>) => {
394-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
395-
;(async () => {
396-
const sharedBufferView = new Int32Array(sharedBuffer)
397-
let msg: WorkerToMainMessage<R>
398-
try {
399-
msg = { id, result: await fn(...args) }
400-
} catch (error: unknown) {
401-
msg = { id, error, properties: extractProperties(error) }
402-
}
403-
workerPort.postMessage(msg)
404-
Atomics.add(sharedBufferView, 0, 1)
405-
Atomics.notify(sharedBufferView, 0)
406-
})()
407-
},
408-
)
409-
410-
/**
411-
* @see https://github.com/un-ts/synckit/issues/94
412-
*
413-
* Starting the worker can fail, due to syntax error, for example. In that case
414-
* we just fail all incoming messages with whatever error message we got.
415-
* Otherwise incoming messages will hang forever waiting for a reply.
416-
*/
417-
} catch (error) {
418-
parentPort!.on(
419-
'message',
420-
({ sharedBuffer, id }: MainToWorkerMessage<Parameters<T>>) => {
401+
parentPort!.on(
402+
'message',
403+
({ sharedBuffer, id, args }: MainToWorkerMessage<Parameters<T>>) => {
404+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
405+
;(async () => {
421406
const sharedBufferView = new Int32Array(sharedBuffer)
422-
workerPort.postMessage({
423-
id,
424-
error,
425-
properties: extractProperties(error),
426-
})
407+
let msg: WorkerToMainMessage<R>
408+
try {
409+
msg = { id, result: await fn(...args) }
410+
} catch (error: unknown) {
411+
msg = { id, error, properties: extractProperties(error) }
412+
}
413+
workerPort.postMessage(msg)
427414
Atomics.add(sharedBufferView, 0, 1)
428415
Atomics.notify(sharedBufferView, 0)
429-
},
430-
)
431-
}
416+
})()
417+
},
418+
)
432419
}

test/fixtures/yarn-pnp.spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ test('yarn-pnp', async () => {
77
nodeOptions: ['-r', './.pnp.cjs'],
88
cwd: resolve('test/fixtures/yarn-pnp'),
99
})
10-
expect(stdout).toBe([1, 2, 5].join(' '))
10+
11+
/**
12+
* @see https://github.com/sindresorhus/execa/issues/587
13+
*/
14+
expect(stdout).toContain('1')
15+
expect(stdout).toContain('2')
16+
expect(stdout).toContain('5')
1117
})

0 commit comments

Comments
 (0)