Skip to content

Commit

Permalink
fix: better compatibility on Node 14 (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 16, 2022
1 parent 784f249 commit 396e964
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-socks-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: better compatibility on Node 14
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ coverage
dist
lib
CHANGELOG.md
!/.github
!/.*.cjs
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{
"extends": "@1stg"
"extends": "@1stg",
"overrides": [
{
"files": ".github/FUNDING.yml",
"rules": {
"unicorn/filename-case": "off"
}
}
]
}
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
os:
Expand All @@ -31,7 +32,7 @@ jobs:
cache: yarn

- name: Install Dependencies
run: yarn --frozen-lockfile --ignore-engines
run: yarn --frozen-lockfile

- name: Build, Lint and Test
run: yarn run-s build lint test
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/pkg-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Package Size Report
uses: pkg-size/action@v1
env:
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"lint:tsc": "tsc --noEmit",
"prepare": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prerelease": "yarn build",
"release": "clean-publish && changeset publish",
"release": "changeset publish",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"typecov": "type-coverage"
},
Expand All @@ -92,7 +92,6 @@
"@changesets/cli": "^2.23.2",
"@types/jest": "^28.1.5",
"@types/node": "^18.0.4",
"clean-publish": "^4.0.1",
"deasync": "^0.1.27",
"esbuild-register": "^3.3.3",
"esbuild-runner": "^2.2.1",
Expand Down
65 changes: 39 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const DEFAULT_EXEC_ARGV = SYNCKIT_EXEC_ARV?.split(',') || []
export const DEFAULT_TS_RUNNER = (SYNCKIT_TS_RUNNER ||
TsRunner.TsNode) as TsRunner

export const MTS_SUPPORTED_NODE_VERSION = 16

const syncFnCache = new Map<string, AnyFn>()

export interface SynckitOptions {
Expand Down Expand Up @@ -128,7 +130,7 @@ const setupTsRunner = (
workerPath: string,
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
) => {
const ext = path.extname(workerPath)
let ext = path.extname(workerPath)

if (
!/[/\\]node_modules[/\\]/.test(workerPath) &&
Expand All @@ -150,8 +152,12 @@ const setupTsRunner = (
break
}
const found = tryExtensions(workPathWithoutExt, extensions)
if (found && (!ext || found !== workPathWithoutExt)) {
let differentExt: boolean | undefined
if (found && (!ext || (differentExt = found !== workPathWithoutExt))) {
workerPath = found
if (differentExt) {
ext = path.extname(workerPath)
}
}
}

Expand Down Expand Up @@ -212,6 +218,7 @@ const setupTsRunner = (
}
}

// eslint-disable-next-line sonarjs/cognitive-complexity
function startWorkerThread<R, T extends AnyAsyncFn<R>>(
workerPath: string,
{
Expand All @@ -224,6 +231,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
const { port1: mainPort, port2: workerPort } = new MessageChannel()

const {
isTs,
ext,
tsUseEsm,
workerPath: finalWorkerPath,
Expand All @@ -232,36 +240,41 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(

const workerPathUrl = pathToFileURL(finalWorkerPath)

if (
/\.[cm]ts$/.test(finalWorkerPath) &&
(
[
// https://github.com/egoist/esbuild-register/issues/79
TsRunner.EsbuildRegister,
// https://github.com/folke/esbuild-runner/issues/67
TsRunner.EsbuildRunner,
] as TsRunner[]
).includes(tsRunner)
) {
throw new Error(
`${tsRunner} is not supported for ${ext} files yet, you can try [tsx](https://github.com/esbuild-kit/tsx) instead`,
)
if (/\.[cm]ts$/.test(finalWorkerPath)) {
const isTsxSupported =
!tsUseEsm ||
Number.parseFloat(process.versions.node) >= MTS_SUPPORTED_NODE_VERSION
/* istanbul ignore if */
if (
(
[
// https://github.com/egoist/esbuild-register/issues/79
TsRunner.EsbuildRegister,
// https://github.com/folke/esbuild-runner/issues/67
TsRunner.EsbuildRunner,
.../* istanbul ignore next */ (isTsxSupported ? [] : [TsRunner.TSX]),
] as TsRunner[]
).includes(tsRunner)
) {
throw new Error(
`${tsRunner} is not supported for ${ext} files yet` +
(isTsxSupported
? ', you can try [tsx](https://github.com/esbuild-kit/tsx) instead'
: ''),
)
}
}

const useEval = isTs && !tsUseEsm

const worker = new Worker(
tsUseEsm &&
(
[
TsRunner.TsNode,
// https://github.com/egoist/esbuild-register/issues/79
// TsRunner.EsbuildRegister,
// https://github.com/folke/esbuild-runner/issues/67
// TsRunner.EsbuildRunner
] as TsRunner[]
).includes(tsRunner)
tsUseEsm && tsRunner === TsRunner.TsNode
? dataUrl(`import '${String(workerPathUrl)}'`)
: useEval
? `require('${finalWorkerPath.replace(/\\/g, '\\\\')}')`
: workerPathUrl,
{
eval: useEval,
workerData: { workerPort },
transferList: [workerPort],
execArgv: finalExecArgv,
Expand Down
27 changes: 17 additions & 10 deletions test/ts-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { jest } from '@jest/globals'
import { _dirname } from './helpers.js'
import type { AsyncWorkerFn } from './types.js'

import { TsRunner } from 'synckit'
import { MTS_SUPPORTED_NODE_VERSION, TsRunner } from 'synckit'

beforeEach(() => {
jest.resetModules()
Expand All @@ -27,14 +27,12 @@ it(TsRunner.EsbuildRegister, async () => {

expect(() =>
createSyncFn<AsyncWorkerFn>(
path.resolve(_dirname, 'esbuild-register-error.worker.mts'),
path.resolve(_dirname, 'esbuild-register-error.worker.mjs'),
{
tsRunner: TsRunner.EsbuildRegister,
},
),
).toThrowErrorMatchingInlineSnapshot(
`"esbuild-register is not supported for .mts files yet, you can try [tsx](https://github.com/esbuild-kit/tsx) instead"`,
)
).toThrowError('esbuild-register is not supported for .mts files yet')
})

it(TsRunner.EsbuildRunner, async () => {
Expand All @@ -51,18 +49,27 @@ it(TsRunner.EsbuildRunner, async () => {

expect(() =>
createSyncFn<AsyncWorkerFn>(
path.resolve(_dirname, 'esbuild-runner-error.worker.mts'),
path.resolve(_dirname, 'esbuild-runner-error.worker.mjs'),
{
tsRunner: TsRunner.EsbuildRunner,
},
),
).toThrowErrorMatchingInlineSnapshot(
`"esbuild-runner is not supported for .mts files yet, you can try [tsx](https://github.com/esbuild-kit/tsx) instead"`,
)
).toThrowError('esbuild-runner is not supported for .mts files yet')
})

it(TsRunner.TSX, async () => {
const { createSyncFn } = await import('synckit')

if (Number.parseFloat(process.versions.node) < MTS_SUPPORTED_NODE_VERSION) {
// eslint-disable-next-line jest/no-conditional-expect
expect(() =>
createSyncFn<AsyncWorkerFn>(path.resolve(_dirname, 'tsx.worker.mjs'), {
tsRunner: TsRunner.TSX,
}),
).toThrowError('tsx is not supported for .mts files yet')
return
}

const syncFn = createSyncFn<AsyncWorkerFn>(
path.resolve(_dirname, 'tsx.worker.mjs'),
{
Expand All @@ -79,7 +86,7 @@ it('unknown ts runner', async () => {

expect(() =>
// @ts-expect-error
createSyncFn<AsyncWorkerFn>(path.resolve(_dirname, 'worker.ts'), {
createSyncFn<AsyncWorkerFn>(path.resolve(_dirname, 'worker.js'), {
tsRunner: 'unknown',
}),
).toThrowErrorMatchingInlineSnapshot(`"Unknown ts runner: unknown"`)
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3685,16 +3685,6 @@ cjs-module-lexer@^1.0.0:
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz#9f84ba3244a512f3a54e5277e8eef4c489864e40"
integrity sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==

clean-publish@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/clean-publish/-/clean-publish-4.0.1.tgz#8492fa1ef9b5d5f8e26cbca76c849a2be14543a3"
integrity sha512-6v0bh5kQD5FDlxBgXDVNNc6KmAB7iIP/GHD91q9xsGVZT5XB9Y8TNqB7dL5u9PTZlBeLpBw+A1AseRlEEJLSWA==
dependencies:
cross-spawn "^7.0.3"
fast-glob "^3.2.11"
lilconfig "^2.0.5"
micromatch "^4.0.5"

clean-regexp@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/clean-regexp/-/clean-regexp-1.0.0.tgz#8df7c7aae51fd36874e8f8d05b9180bc11a3fed7"
Expand Down

0 comments on commit 396e964

Please sign in to comment.