From 60aa1f44341ffbbcb3cc33661dc1c45e8f1f2342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Tue, 18 Oct 2022 21:49:06 +0200 Subject: [PATCH] Add support for ESM --- build.js | 1 + package.json | 4 ++-- src/InstallBinCommand.ts | 20 ++++++++++++++++++-- src/tmpl/bin-wrapper-pnp.sh.in | 2 +- yarn.lock | 22 +++++++++++----------- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/build.js b/build.js index febd6f8..2177790 100755 --- a/build.js +++ b/build.js @@ -15,6 +15,7 @@ const EXTERNALS = [ `clipanion`, `crypto`, `os`, + `url`, ]; const compiler = webpack({ diff --git a/package.json b/package.json index d2adc66..2cc0286 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "@yarnpkg/core": "^3.0.0", "@yarnpkg/fslib": "^2.5.1", "@yarnpkg/plugin-patch": "^3.0.0", - "@yarnpkg/plugin-pnp": "^3.0.0", + "@yarnpkg/plugin-pnp": "^3.1.0", "babel-loader": "^8.1.0", "clipanion": "^3.0.0", "prettier": "2.5.1", @@ -47,7 +47,7 @@ "@yarnpkg/plugin-npm-cli": "3.0.0", "@yarnpkg/plugin-pack": "3.0.0", "@yarnpkg/plugin-patch": "3.0.0", - "@yarnpkg/plugin-pnp": "3.0.1", + "@yarnpkg/plugin-pnp": "3.1.0", "@yarnpkg/plugin-stage": "3.0.0", "@yarnpkg/shell": "3.0.0" } diff --git a/src/InstallBinCommand.ts b/src/InstallBinCommand.ts index cd8c8cf..5020a58 100644 --- a/src/InstallBinCommand.ts +++ b/src/InstallBinCommand.ts @@ -13,6 +13,7 @@ import { import binWrapperNodeModulesTmpl from "./tmpl/bin-wrapper-node-modules.sh.in"; import binWrapperPnpTmpl from "./tmpl/bin-wrapper-pnp.sh.in"; import { renderTmpl } from "./textUtils"; +import { pathToFileURL } from "url"; // Internal command that creates wrappers for binaries. // Used inside the Nix install phase. @@ -82,13 +83,28 @@ export default class InstallBinCommand extends Command { ) { let wrapper; switch (configuration.get(`nodeLinker`)) { - case `pnp`: + case `pnp`: { + const pnpPath = getPnpPath(project); + const nodeOptions = []; + if (await xfs.existsPromise(pnpPath.cjs)) { + nodeOptions.push( + `--require "${npath.fromPortablePath(pnpPath.cjs)}"` + ); + } + if (await xfs.existsPromise(pnpPath.esmLoader)) { + nodeOptions.push( + `--experimental-loader "${ + pathToFileURL(npath.fromPortablePath(pnpPath.esmLoader)).href + }"` + ); + } wrapper = renderTmpl(binWrapperPnpTmpl, { NODE_PATH: process.execPath, - PNP_PATH: getPnpPath(project).cjs, + NODE_OPTIONS: nodeOptions.join(" "), BINARY_PATH: binaryPath, }); break; + } case `node-modules`: wrapper = renderTmpl(binWrapperNodeModulesTmpl, { NODE_PATH: process.execPath, diff --git a/src/tmpl/bin-wrapper-pnp.sh.in b/src/tmpl/bin-wrapper-pnp.sh.in index 011be39..7ee1e3d 100644 --- a/src/tmpl/bin-wrapper-pnp.sh.in +++ b/src/tmpl/bin-wrapper-pnp.sh.in @@ -1,3 +1,3 @@ #!/bin/sh -export NODE_OPTIONS="--require @@PNP_PATH@@" +export NODE_OPTIONS='@@NODE_OPTIONS@@' exec '@@NODE_PATH@@' '@@BINARY_PATH@@' "$@" diff --git a/yarn.lock b/yarn.lock index 0d39d26..95ec21f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1074,22 +1074,22 @@ __metadata: languageName: node linkType: hard -"@yarnpkg/plugin-pnp@npm:3.0.1": - version: 3.0.1 - resolution: "@yarnpkg/plugin-pnp@npm:3.0.1" +"@yarnpkg/plugin-pnp@npm:3.1.0": + version: 3.1.0 + resolution: "@yarnpkg/plugin-pnp@npm:3.1.0" dependencies: "@types/semver": ^7.1.0 - "@yarnpkg/fslib": ^2.5.1 - "@yarnpkg/plugin-stage": ^3.0.0 - "@yarnpkg/pnp": ^3.0.1 + "@yarnpkg/fslib": ^2.6.0 + "@yarnpkg/plugin-stage": ^3.1.0 + "@yarnpkg/pnp": ^3.1.0 clipanion: ^3.0.1 micromatch: ^4.0.2 semver: ^7.1.2 tslib: ^1.13.0 peerDependencies: - "@yarnpkg/cli": ^3.0.1 - "@yarnpkg/core": ^3.0.0 - checksum: 682fdaab91583a6fb662be05e660a76510df94e5d79c42f51dd19db9d5e3310b29d91111e5a8ed9fc6e382e2b581a9e0342f37852e2af96c98d33c50d9331bd4 + "@yarnpkg/cli": ^3.1.0 + "@yarnpkg/core": ^3.1.0 + checksum: 9ffd901263f60c83a116a38e8bb76a9196185b36fde514c95b0336affc879308099db12ae9bd20b8f70f3253791f1127bf4f87ced5a62d2b4529a02115347f6d languageName: node linkType: hard @@ -1124,7 +1124,7 @@ __metadata: languageName: node linkType: hard -"@yarnpkg/pnp@npm:^3.0.1, @yarnpkg/pnp@npm:^3.1.0": +"@yarnpkg/pnp@npm:^3.1.0": version: 3.1.0 resolution: "@yarnpkg/pnp@npm:3.1.0" dependencies: @@ -3307,7 +3307,7 @@ __metadata: "@yarnpkg/core": ^3.0.0 "@yarnpkg/fslib": ^2.5.1 "@yarnpkg/plugin-patch": ^3.0.0 - "@yarnpkg/plugin-pnp": ^3.0.0 + "@yarnpkg/plugin-pnp": ^3.1.0 babel-loader: ^8.1.0 clipanion: ^3.0.0 prettier: 2.5.1