Skip to content

Commit b1308ac

Browse files
NoahJounQin
Noah
andauthoredAug 10, 2022
fix: pass yarn PnP experimental loader to worker if it exists (#103)
Co-authored-by: JounQin <admin@1stg.me>
1 parent 0f5cbfb commit b1308ac

File tree

5 files changed

+303
-1
lines changed

5 files changed

+303
-1
lines changed
 

‎.changeset/weak-yaks-remain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"synckit": patch
3+
---
4+
5+
fix: pass yarn PnP experimental loader to worker if it exists

‎src/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs'
12
import { createRequire } from 'node:module'
23
import path from 'node:path'
34
import { pathToFileURL } from 'node:url'
@@ -127,6 +128,14 @@ const cjsRequire =
127128
const dataUrl = (code: string) =>
128129
new URL(`data:text/javascript,${encodeURIComponent(code)}`)
129130

131+
export const isFile = (path: string) => {
132+
try {
133+
return fs.statSync(path).isFile()
134+
} catch {
135+
return false
136+
}
137+
}
138+
130139
const setupTsRunner = (
131140
workerPath: string,
132141
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
@@ -223,6 +232,10 @@ const setupTsRunner = (
223232
!execArgv.includes(pnpApiPath)
224233
) {
225234
execArgv = ['-r', pnpApiPath, ...execArgv]
235+
const pnpLoaderPath = path.resolve(pnpApiPath, '../.pnp.loader.mjs')
236+
if (isFile(pnpLoaderPath)) {
237+
execArgv = ['--experimental-loader', pnpLoaderPath, ...execArgv]
238+
}
226239
}
227240
}
228241

‎test/fixtures/yarn-pnp/.pnp.loader.mjs

+273
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/yarn-pnp/.yarnrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
nodeLinker: pnp
2-
2+
pnpEnableEsmLoader: true
33
yarnPath: .yarn/releases/yarn-3.2.2.cjs

‎test/utils.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fileURLToPath } from 'node:url'
2+
3+
import { _dirname } from './helpers'
4+
5+
import { isFile } from 'synckit'
6+
7+
test('utils', () => {
8+
expect(isFile(_dirname)).toBe(false)
9+
expect(isFile('non-existed')).toBe(false)
10+
expect(isFile(fileURLToPath(import.meta.url))).toBe(true)
11+
})

0 commit comments

Comments
 (0)
Please sign in to comment.