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

feat: add support for yarn pnp #1819

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ npm-debug.log*
.idea

# MacOs
.DS_Store
.DS_Store

# Yarn
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
.serverless
.serverless-offline
.webpack
coverage
coverage
.pnp.cjs
yarn-4.1.1.cjs
4 changes: 2 additions & 2 deletions package-lock.json

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

Original file line number Diff line number Diff line change
@@ -1,25 +1,75 @@
import { MessageChannel, Worker } from "node:worker_threads"
import { join } from "desm"
import { pathToFileURL } from "node:url"
import { versions, env as nodeEnv } from "node:process"
import { createRequire } from "node:module"
import { statSync } from "node:fs"

const IS_NODE_20 = Number(versions.node.split(".")[0]) >= 20

const isFile = (path) => {
try {
return !!statSync(path, { throwIfNoEntry: false })?.isFile()
} catch {
/* istanbul ignore next */
return false
}
}

export default class WorkerThreadRunner {
#workerThread = null

constructor(funOptions, env) {
const { codeDir, functionKey, handler, servicePath, timeout } = funOptions
// Resolve the PnP loader path if Yarn PnP is in use
let pnpLoaderPath
let execArgv
if (versions.pnp) {
const nodeOptions = nodeEnv.NODE_OPTIONS?.split(/\s+/)
const cjsRequire = createRequire(import.meta.url)
let pnpApiPath
try {
/** @see https://github.com/facebook/jest/issues/9543 */
pnpApiPath = cjsRequire.resolve("pnpapi")
} catch {
/* empty */
}
if (
pnpApiPath &&
!nodeOptions?.some(
(option, index) =>
["-r", "--require"].includes(option) &&
pnpApiPath === cjsRequire.resolve(nodeOptions[index + 1]),
)
) {
execArgv = ["-r", pnpApiPath]
// eslint-disable-next-line no-underscore-dangle
const _pnpLoaderPath = join(pnpApiPath, "../.pnp.loader.mjs")
if (isFile(_pnpLoaderPath)) {
pnpLoaderPath = pathToFileURL(_pnpLoaderPath).toString()
if (!IS_NODE_20) {
execArgv = ["--experimental-loader", pnpLoaderPath, ...execArgv]
}
}
}
}

const workerOptions = {
env,
execArgv,
workerData: {
codeDir,
functionKey,
handler,
pnpLoaderPath,
servicePath,
timeout,
},
}

this.#workerThread = new Worker(
join(import.meta.url, "workerThreadHelper.js"),
{
// don't pass process.env from the main process!
env,
workerData: {
codeDir,
functionKey,
handler,
servicePath,
timeout,
},
},
workerOptions,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { env } from "node:process"
import { env, versions } from "node:process"
import { parentPort, workerData } from "node:worker_threads"
import { register } from "node:module"
import InProcessRunner from "../in-process-runner/index.js"

const { codeDir, functionKey, handler, servicePath, timeout } = workerData
const IS_NODE_20 = Number(versions.node.split(".")[0]) >= 20

const { codeDir, functionKey, handler, servicePath, timeout, pnpLoaderPath } =
workerData

if (pnpLoaderPath && IS_NODE_20) {
register(pnpLoaderPath)
}

const inProcessRunner = new InProcessRunner(
{
Expand Down
8 changes: 8 additions & 0 deletions tests/installYarnModules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { execa } from "execa"

export default function installYarnModules(dirPath) {
return execa("yarn", ["--immutable"], {
cwd: dirPath,
stdio: "inherit",
})
}
Loading