Skip to content

Commit

Permalink
fix: support yarn pnp with node 20 (#175)
Browse files Browse the repository at this point in the history
Co-authored-by: JounQin <[email protected]>
  • Loading branch information
fmal and JounQin authored Jul 14, 2024
1 parent e45bfaf commit e99bf89
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-rice-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: support yarn pnp with node 20
29 changes: 23 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash } from 'node:crypto'
import fs from 'node:fs'
import { createRequire } from 'node:module'
import module from 'node:module'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import {
Expand Down Expand Up @@ -53,6 +53,8 @@ const {
SYNCKIT_TS_RUNNER,
} = process.env

const IS_NODE_20 = Number(process.versions.node.split('.')[0]) >= 20

export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined

/* istanbul ignore next */
Expand Down Expand Up @@ -133,7 +135,7 @@ export function createSyncFn<T extends AnyAsyncFn<R>, R = unknown>(

const cjsRequire =
typeof require === 'undefined'
? createRequire(import.meta.url)
? module.createRequire(import.meta.url)
: /* istanbul ignore next */ require

const dataUrl = (code: string) =>
Expand Down Expand Up @@ -252,6 +254,8 @@ const setupTsRunner = (
}
}

let resolvedPnpLoaderPath: string | undefined

/* istanbul ignore if -- https://github.com/facebook/jest/issues/5274 */
if (process.versions.pnp) {
const nodeOptions = NODE_OPTIONS?.split(/\s+/)
Expand All @@ -275,8 +279,15 @@ const setupTsRunner = (
// Transform path to file URL because nodejs does not accept
// absolute Windows paths in the --experimental-loader option.
// https://github.com/un-ts/synckit/issues/123
const experimentalLoader = pathToFileURL(pnpLoaderPath).toString()
execArgv = ['--experimental-loader', experimentalLoader, ...execArgv]
resolvedPnpLoaderPath = pathToFileURL(pnpLoaderPath).toString()

if (!IS_NODE_20) {
execArgv = [
'--experimental-loader',
resolvedPnpLoaderPath,
...execArgv,
]
}
}
}
}
Expand All @@ -288,6 +299,7 @@ const setupTsRunner = (
tsRunner,
tsUseEsm,
workerPath,
pnpLoaderPath: resolvedPnpLoaderPath,
execArgv,
}
}
Expand Down Expand Up @@ -428,6 +440,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
tsUseEsm,
tsRunner: finalTsRunner,
workerPath: finalWorkerPath,
pnpLoaderPath,
execArgv: finalExecArgv,
} = setupTsRunner(workerPath, { execArgv, tsRunner })

Expand Down Expand Up @@ -501,7 +514,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
: workerPathUrl,
{
eval: useEval,
workerData: { sharedBuffer, workerPort },
workerData: { sharedBuffer, workerPort, pnpLoaderPath },
transferList: [workerPort, ...transferList],
execArgv: finalExecArgv,
},
Expand Down Expand Up @@ -561,7 +574,11 @@ export function runAsWorker<
return
}

const { workerPort, sharedBuffer } = workerData as WorkerData
const { workerPort, sharedBuffer, pnpLoaderPath } = workerData as WorkerData

if (pnpLoaderPath && IS_NODE_20) {
module.register(pnpLoaderPath)
}

const sharedBufferView = new Int32Array(sharedBuffer, 0, 1)

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface MainToWorkerMessage<T extends unknown[]> {
export interface WorkerData {
sharedBuffer: SharedArrayBuffer
workerPort: MessagePort
pnpLoaderPath: string | undefined
}

export interface DataMessage<T> {
Expand Down

0 comments on commit e99bf89

Please sign in to comment.