From 0c444935f7b8403b406b7b39d2b2e8aee45e4b81 Mon Sep 17 00:00:00 2001 From: Filip Malinowski Date: Thu, 20 Jun 2024 18:01:17 +0200 Subject: [PATCH 1/2] fix: support yarn pnp with node 20 https://github.com/un-ts/synckit/issues/174 --- src/index.ts | 29 +++++++++++++++++++++++------ src/types.ts | 1 + 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8ef71b1f4..df193d1a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -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 */ @@ -133,7 +135,7 @@ export function createSyncFn, R = unknown>( const cjsRequire = typeof require === 'undefined' - ? createRequire(import.meta.url) + ? module.createRequire(import.meta.url) : /* istanbul ignore next */ require const dataUrl = (code: string) => @@ -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+/) @@ -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, + ] + } } } } @@ -288,6 +299,7 @@ const setupTsRunner = ( tsRunner, tsUseEsm, workerPath, + pnpLoaderPath: resolvedPnpLoaderPath, execArgv, } } @@ -428,6 +440,7 @@ function startWorkerThread>( tsUseEsm, tsRunner: finalTsRunner, workerPath: finalWorkerPath, + pnpLoaderPath, execArgv: finalExecArgv, } = setupTsRunner(workerPath, { execArgv, tsRunner }) @@ -501,7 +514,7 @@ function startWorkerThread>( : workerPathUrl, { eval: useEval, - workerData: { sharedBuffer, workerPort }, + workerData: { sharedBuffer, workerPort, pnpLoaderPath }, transferList: [workerPort, ...transferList], execArgv: finalExecArgv, }, @@ -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) diff --git a/src/types.ts b/src/types.ts index 79dfdb4c0..b3fd7516a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,6 +28,7 @@ export interface MainToWorkerMessage { export interface WorkerData { sharedBuffer: SharedArrayBuffer workerPort: MessagePort + pnpLoaderPath: string | undefined } export interface DataMessage { From bd607595c13c133df7544bb5281fd2e29ddc52ba Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 14 Jul 2024 20:12:32 +0800 Subject: [PATCH 2/2] Create polite-rice-fold.md --- .changeset/polite-rice-fold.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/polite-rice-fold.md diff --git a/.changeset/polite-rice-fold.md b/.changeset/polite-rice-fold.md new file mode 100644 index 000000000..94136a683 --- /dev/null +++ b/.changeset/polite-rice-fold.md @@ -0,0 +1,5 @@ +--- +"synckit": patch +--- + +fix: support yarn pnp with node 20