Skip to content

Commit

Permalink
fix: pass yarn PnP experimental loader to worker if it exists (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: JounQin <admin@1stg.me>
  • Loading branch information
Noah and JounQin committed Aug 10, 2022
1 parent 0f5cbfb commit b1308ac
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/weak-yaks-remain.md
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: pass yarn PnP experimental loader to worker if it exists
13 changes: 13 additions & 0 deletions src/index.ts
@@ -1,3 +1,4 @@
import fs from 'node:fs'
import { createRequire } from 'node:module'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
Expand Down Expand Up @@ -127,6 +128,14 @@ const cjsRequire =
const dataUrl = (code: string) =>
new URL(`data:text/javascript,${encodeURIComponent(code)}`)

export const isFile = (path: string) => {
try {
return fs.statSync(path).isFile()
} catch {
return false
}
}

const setupTsRunner = (
workerPath: string,
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
Expand Down Expand Up @@ -223,6 +232,10 @@ const setupTsRunner = (
!execArgv.includes(pnpApiPath)
) {
execArgv = ['-r', pnpApiPath, ...execArgv]
const pnpLoaderPath = path.resolve(pnpApiPath, '../.pnp.loader.mjs')
if (isFile(pnpLoaderPath)) {
execArgv = ['--experimental-loader', pnpLoaderPath, ...execArgv]
}
}
}

Expand Down
273 changes: 273 additions & 0 deletions test/fixtures/yarn-pnp/.pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/yarn-pnp/.yarnrc.yml
@@ -1,3 +1,3 @@
nodeLinker: pnp

pnpEnableEsmLoader: true
yarnPath: .yarn/releases/yarn-3.2.2.cjs
11 changes: 11 additions & 0 deletions test/utils.spec.ts
@@ -0,0 +1,11 @@
import { fileURLToPath } from 'node:url'

import { _dirname } from './helpers'

import { isFile } from 'synckit'

test('utils', () => {
expect(isFile(_dirname)).toBe(false)
expect(isFile('non-existed')).toBe(false)
expect(isFile(fileURLToPath(import.meta.url))).toBe(true)
})

0 comments on commit b1308ac

Please sign in to comment.