Skip to content

Commit

Permalink
feat(pnp): support the --conditions flag (#5228)
Browse files Browse the repository at this point in the history
* feat(pnp): support the `--conditions` flag

* test: check `NODE_OPTIONS` works
  • Loading branch information
merceyz committed Jan 31, 2023
1 parent 872661b commit 9ae23b2
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@

# Minimize the diff with upstream
packages/yarnpkg-pnp/sources/node/**
packages/yarnpkg-pnp/sources/loader/node-options*
8 changes: 8 additions & 0 deletions .pnp.cjs

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

Binary file not shown.
27 changes: 27 additions & 0 deletions .yarn/versions/e4478040.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
56 changes: 56 additions & 0 deletions packages/acceptance-tests/pkg-tests-specs/sources/pnp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2126,4 +2126,60 @@ describe(`Plug'n'Play`, () => {
},
),
);

test(
`it should respect user provided conditions`,
makeTemporaryEnv(
{
imports: {
'#foo': {
custom: `./custom.js`,
default: `./404.js`,
},
},
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});

await xfs.writeFilePromise(ppath.join(path, `custom.js`), `console.log('foo')`);
await xfs.writeFilePromise(ppath.join(path, `index.js`), `require('#foo')`);

await expect(run(`node`, `--conditions`, `custom`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `foo\n`,
stderr: ``,
});

await expect(run(`node`, `-C`, `custom`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `foo\n`,
stderr: ``,
});

await expect(
run(`node`, `./index.js`, {
env: {
NODE_OPTIONS: `--conditions custom`,
},
}),
).resolves.toMatchObject({
code: 0,
stdout: `foo\n`,
stderr: ``,
});

await expect(
run(`node`, `./index.js`, {
env: {
NODE_OPTIONS: `-C custom`,
},
}),
).resolves.toMatchObject({
code: 0,
stdout: `foo\n`,
stderr: ``,
});
},
),
);
});
1 change: 1 addition & 0 deletions packages/yarnpkg-pnp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^11.0.1",
"@yarnpkg/libzip": "workspace:^",
"arg": "^5.0.2",
"esbuild": "npm:esbuild-wasm@^0.15.5",
"resolve.exports": "^1.1.0",
"rollup": "^2.59.0",
Expand Down
10 changes: 7 additions & 3 deletions packages/yarnpkg-pnp/sources/loader/makeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {packageImportsResolve}
import {PackageInformation, PackageLocator, PnpApi, RuntimeState, PhysicalPackageLocator, DependencyTarget, ResolveToUnqualifiedOptions, ResolveUnqualifiedOptions, ResolveRequestOptions} from '../types';

import {ErrorCode, makeError, getPathForDisplay} from './internalTools';
import {getOptionValue} from './node-options.js';
import * as nodeUtils from './nodeUtils';

export type MakeApiOptions = {
Expand Down Expand Up @@ -193,7 +194,12 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
return false;
}

const defaultExportsConditions = new Set([`default`, `node`, `require`]);
const defaultExportsConditions = new Set([
`default`,
`node`,
`require`,
...getOptionValue(`--conditions`),
]);

/**
* Implements the node resolution for the "exports" field
Expand Down Expand Up @@ -234,8 +240,6 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
let resolvedExport;
try {
resolvedExport = resolveExport(pkgJson, ppath.normalize(subpath), {
// TODO: implement support for the --conditions flag
// Waiting on https://github.com/nodejs/node/issues/36935
// @ts-expect-error - Type should be Iterable<string>
conditions,
unsafe: true,
Expand Down
1 change: 1 addition & 0 deletions packages/yarnpkg-pnp/sources/loader/node-options.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function getOptionValue(key: '--conditions'): string[]
134 changes: 134 additions & 0 deletions packages/yarnpkg-pnp/sources/loader/node-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Adapted from https://github.com/TypeStrong/ts-node/blob/97f9afd046b66a0fe05a7d76e7a32f94b872016f/dist-raw/node-options.js
// which has the following license:

/**
@license
The MIT License (MIT)
Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// It adapts code from Node.js with the following license:

/**
@license
Copyright Joyent, Inc. and other Node contributors.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// Replacement for node's internal 'internal/options' module

import arg from 'arg';

export function getOptionValue(opt) {
parseOptions();
return options[opt];
}

let options;
function parseOptions() {
if (!options) {
options = {
'--conditions': [],
...parseArgv(getNodeOptionsEnvArgv()),
...parseArgv(process.execArgv),
};
}
}

function parseArgv(argv) {
return arg(
{
'--conditions': [String],
'-C': '--conditions',
},
{
argv,
permissive: true,
}
);
}

function getNodeOptionsEnvArgv() {
const errors = [];
const envArgv = ParseNodeOptionsEnvVar(process.env.NODE_OPTIONS || '', errors);
if (errors.length !== 0) {
// TODO: handle errors somehow
}
return envArgv;
}

// Direct JS port of C implementation: https://github.com/nodejs/node/blob/67ba825037b4082d5d16f922fb9ce54516b4a869/src/node_options.cc#L1024-L1063
function ParseNodeOptionsEnvVar(node_options, errors) {
const env_argv = [];

let is_in_string = false;
let will_start_new_arg = true;
for (let index = 0; index < node_options.length; ++index) {
let c = node_options[index];

// Backslashes escape the following character
if (c === '\\' && is_in_string) {
if (index + 1 === node_options.length) {
errors.push('invalid value for NODE_OPTIONS ' + '(invalid escape)\n');
return env_argv;
} else {
c = node_options[++index];
}
} else if (c === ' ' && !is_in_string) {
will_start_new_arg = true;
continue;
} else if (c === '"') {
is_in_string = !is_in_string;
continue;
}

if (will_start_new_arg) {
env_argv.push(c);
will_start_new_arg = false;
} else {
env_argv[env_argv.length - 1] += c;
}
}

if (is_in_string) {
errors.push('invalid value for NODE_OPTIONS ' + '(unterminated string)\n');
}
return env_argv;
}
6 changes: 4 additions & 2 deletions scripts/setup-ts-execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ require(`@babel/register`)({
extensions: [`.tsx`, `.ts`, `.js`],
only: [
p => {
if (p?.endsWith(`.js`))
return /packages(\\|\/)yarnpkg-pnp(\\|\/)sources(\\|\/)node/.test(p);
if (p?.endsWith(`.js`)) {
const normalizedP = p.replace(/\\/g, `/`);
return normalizedP.includes(`packages/yarnpkg-pnp/sources/node`) || normalizedP.endsWith(`packages/yarnpkg-pnp/sources/loader/node-options.js`);
}

return true;
},
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6300,6 +6300,7 @@ __metadata:
"@types/node": ^13.7.0
"@yarnpkg/fslib": "workspace:^"
"@yarnpkg/libzip": "workspace:^"
arg: ^5.0.2
esbuild: "npm:esbuild-wasm@^0.15.5"
resolve.exports: ^1.1.0
rollup: ^2.59.0
Expand Down Expand Up @@ -6813,6 +6814,13 @@ __metadata:
languageName: node
linkType: hard

"arg@npm:^5.0.2":
version: 5.0.2
resolution: "arg@npm:5.0.2"
checksum: 6c69ada1a9943d332d9e5382393e897c500908d91d5cb735a01120d5f71daf1b339b7b8980cbeaba8fd1afc68e658a739746179e4315a26e8a28951ff9930078
languageName: node
linkType: hard

"argparse@npm:^1.0.7":
version: 1.0.10
resolution: "argparse@npm:1.0.10"
Expand Down

0 comments on commit 9ae23b2

Please sign in to comment.