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: pass yarn PnP experimental loader to worker if it exists #103

Merged
merged 4 commits into from Aug 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})