Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support yarn pnp with node 20 #175

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading