1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

fix(npm): conditional exports with --node-modules-dir (#17111)

This commit fixes conditional exports in `require()` implementation
if `--node-modules-dir` flag is used.
This commit is contained in:
Bartek Iwańczuk 2022-12-19 17:09:54 +01:00 committed by GitHub
parent d1f6b51990
commit 383d40a33b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 38 additions and 3 deletions

View file

@ -102,6 +102,15 @@ mod npm {
http_server: true, http_server: true,
}); });
itest!(conditional_exports_node_modules_dir {
args:
"run --allow-read --node-modules-dir $TESTDATA/npm/conditional_exports/main.js",
output: "npm/conditional_exports/main.out",
envs: env_vars_for_npm_tests(),
http_server: true,
temp_cwd: true,
});
itest!(dual_cjs_esm { itest!(dual_cjs_esm {
args: "run -A --quiet npm/dual_cjs_esm/main.ts", args: "run -A --quiet npm/dual_cjs_esm/main.ts",
output: "npm/dual_cjs_esm/main.out", output: "npm/dual_cjs_esm/main.out",

View file

@ -2,8 +2,10 @@ import mod from "npm:@denotest/conditional-exports";
import client from "npm:@denotest/conditional-exports/client"; import client from "npm:@denotest/conditional-exports/client";
import clientFoo from "npm:@denotest/conditional-exports/client/foo"; import clientFoo from "npm:@denotest/conditional-exports/client/foo";
import clientBar from "npm:@denotest/conditional-exports/client/bar"; import clientBar from "npm:@denotest/conditional-exports/client/bar";
import supportsESM from "npm:supports-esm";
console.log(mod); console.log(mod);
console.log(client); console.log(client);
console.log(clientFoo); console.log(clientFoo);
console.log(clientBar); console.log(clientBar);
console.log(supportsESM);

View file

@ -1,6 +1,13 @@
Download http://localhost:4545/npm/registry/@denotest/conditional-exports Download http://localhost:4545/npm/registry/@denotest/conditional-exports
Download http://localhost:4545/npm/registry/supports-esm
Download http://localhost:4545/npm/registry/has-package-exports
Download http://localhost:4545/npm/registry/@ljharb/has-package-exports-patterns
Download http://localhost:4545/npm/registry/@denotest/conditional-exports/1.0.0.tgz Download http://localhost:4545/npm/registry/@denotest/conditional-exports/1.0.0.tgz
Download http://localhost:4545/npm/registry/@ljharb/has-package-exports-patterns/has-package-exports-patterns-0.0.2.tgz
Download http://localhost:4545/npm/registry/has-package-exports/has-package-exports-1.3.0.tgz
Download http://localhost:4545/npm/registry/supports-esm/supports-esm-1.0.0.tgz
{ hello: "from esm" } { hello: "from esm" }
{ hello: "from esm client" } { hello: "from esm client" }
{ hello: "from esm client foo" } { hello: "from esm client foo" }
{ hello: "from esm client bar" } { hello: "from esm client bar" }
true

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
{"_id":"supports-esm","_rev":"1-c2e0f7841e94f7fd5efa579ca9ab52c9","name":"supports-esm","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"supports-esm","version":"1.0.0","description":"Detect at runtime if Node.js supports ECMAScript modules","main":"index.js","exports":"./index.js","dependencies":{"has-package-exports":"^1.1.0"},"devDependencies":{"semver":"^7.3.2"},"scripts":{"test":"node test.js"},"repository":{"type":"git","url":"git+https://github.com/targos/supports-esm.git"},"keywords":["ESM","support","ES Modules"],"author":{"name":"Michaël Zasso","email":"targos@protonmail.com"},"license":"MIT","bugs":{"url":"https://github.com/targos/supports-esm/issues"},"homepage":"https://github.com/targos/supports-esm#readme","gitHead":"433e6107ae208a642fdf27dc0862d79a7c8b9145","_id":"supports-esm@1.0.0","_nodeVersion":"14.2.0","_npmVersion":"6.14.4","dist":{"integrity":"sha512-96Am8CDqUaC0I2+C/swJ0yEvM8ZnGn4unoers/LSdE4umhX7mELzqyLzx3HnZAluq5PXIsGMKqa7NkqaeHMPcg==","shasum":"7cc567747d0745e2b77b331c9b9cae13cf4dc60e","tarball":"http://localhost:4545/npm/registry/supports-esm/supports-esm-1.0.0.tgz","fileCount":4,"unpackedSize":3489,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJev7mjCRA9TVsSAnZWagAANe8P/0TWu/Gj/Nq6fNoK/ZSd\nAthgVBhz9pIPiLe0W3ACsb7nPUoJYykAA10N+Bzscb2S6dKLCuByxUtdiv+M\nOk10z2q+E/FRhOTPsXBRSvQdEBhKD1A1/diCGQTo9MGGchWOtwi+vlNDQUf5\nK6iNyNxjFenVj+t56A97LSQmtHq1I1bbzBa4zBO11mpDDVsTGCDnnSO0TFDO\npBBYmuMEVaHz/Yt7eIoBCQR5+DvIwgpbDrGrV0L25kT8VmZRxqRXOBFJNO2C\nYE2VlE7SZhxRwkRDyonJ3RZJzFCw9NSJTUJwXk8zRNdpv+6XB+YLj0OBgQ28\nGYHqnsJJ5kETM+y4oONU4aKWi+hn6/EteVAAaBkYE1E5NSJoOh5OEE90PytX\nY1vVWn7Yqip+fbw0QrSpgSXj0rfui26MRvCTL1wBZ7Y3phkBB/c2rLmQa7RO\nO+hbRkjk9cyzBzNXe4r2Zv2eKwn6TZNIBPT/EcYaHPnuKn4X4DnPgEB+bP2G\nqWa1C5lQ9VVt3xYYJE961uec+hAlKlllkKu4587rqvZ+OGFqi9AnIXagCBU4\n7LmAsbd/RRhlZcgTAv29qFwPf/nEgjHkcKvyJBLpmq+G9c2o3LM+oVwbZU8D\n2iQ2oyJ2ui8NavomlNxqZkoaocuKER7qgUEJWBB3aTUREhSpFZ1EK3Flm546\netuD\r\n=evMx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDDiId5jvQO53FtGea7llGowJaurzOK+xWkjyCE0wA6hgIgd1KdPWDd/ofGUvBGP1Hqqdr5365vjkYyVNxnOUDbWCo="}]},"maintainers":[{"name":"targos","email":"targos+npm@protonmail.com"}],"_npmUser":{"name":"targos","email":"targos+npm@protonmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/supports-esm_1.0.0_1589623203010_0.7300584811356978"},"_hasShrinkwrap":false}},"time":{"created":"2020-05-16T10:00:03.010Z","1.0.0":"2020-05-16T10:00:03.202Z","modified":"2022-05-19T01:59:46.323Z"},"maintainers":[{"name":"targos","email":"targos+npm@protonmail.com"}],"description":"Detect at runtime if Node.js supports ECMAScript modules","homepage":"https://github.com/targos/supports-esm#readme","keywords":["ESM","support","ES Modules"],"repository":{"type":"git","url":"git+https://github.com/targos/supports-esm.git"},"author":{"name":"Michaël Zasso","email":"targos@protonmail.com"},"bugs":{"url":"https://github.com/targos/supports-esm/issues"},"license":"MIT","readme":"# supports-esm\n\n[![NPM version][npm-image]][npm-url]\n[![build status][ci-image]][ci-url]\n[![npm download][download-image]][download-url]\n[![License][license-image]][license-url]\n\nDetect at runtime if Node.js supports ECMAScript modules.\n\n## Installation\n\n`$ npm install supports-esm`\n\n## Usage\n\nThis library exports `true` if the current Node.js version is considered to have\nreasonable support for ESM features, `false` otherwise.\n\nVersion 1.x returns `true` if the following features are all present:\n\n- Dynamic imports\n- \"exports\" field in `package.json`, including conditional exports\n- Package self-reference\n\nOne notable use case is to conditionally execute an ESM or CommonJS entrypoint\nfrom a \"bin\" script, such as a command-line interface authored in ESM and\ntranspiled to CommonJS for backwards compatibility:\n\n```js\n'use strict';\n\nconst supportsESM = require('supports-esm');\n\nif (supportsESM) {\n import('../src/cli.js').catch((error) => {\n console.error(error);\n process.exit(1);\n });\n} else {\n require('../build-cjs/cli.js');\n}\n```\n\nThis snippet works in all Node.js versions >= 10.0.0.\n\n[npm-image]: https://img.shields.io/npm/v/supports-esm.svg\n[npm-url]: https://npmjs.org/package/supports-esm\n[ci-image]: https://github.com/targos/supports-esm/workflows/Test/badge.svg?branch=master\n[ci-url]: https://github.com/targos/supports-esm/actions?query=workflow%3ATest\n[download-image]: https://img.shields.io/npm/dm/supports-esm.svg\n[download-url]: https://npmjs.org/package/supports-esm\n[license-image]: https://img.shields.io/npm/l/supports-esm.svg\n[license-url]: LICENSE\n","readmeFilename":"README.md"}

Binary file not shown.

View file

@ -303,7 +303,12 @@
// 1. name/.* // 1. name/.*
// 2. @scope/name/.* // 2. @scope/name/.*
const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/; const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/;
function resolveExports(modulesPath, request, parentPath) { function resolveExports(
modulesPath,
request,
parentPath,
usesLocalNodeModulesDir,
) {
// The implementation's behavior is meant to mirror resolution in ESM. // The implementation's behavior is meant to mirror resolution in ESM.
const [, name, expansion = ""] = const [, name, expansion = ""] =
StringPrototypeMatch(request, EXPORTS_PATTERN) || []; StringPrototypeMatch(request, EXPORTS_PATTERN) || [];
@ -312,6 +317,7 @@
} }
return core.ops.op_require_resolve_exports( return core.ops.op_require_resolve_exports(
usesLocalNodeModulesDir,
modulesPath, modulesPath,
request, request,
name, name,
@ -349,7 +355,12 @@
if (curPath && stat(curPath) < 1) continue; if (curPath && stat(curPath) < 1) continue;
if (!absoluteRequest) { if (!absoluteRequest) {
const exportsResolved = resolveExports(curPath, request, parentPath); const exportsResolved = resolveExports(
curPath,
request,
parentPath,
usesLocalNodeModulesDir,
);
if (exportsResolved) { if (exportsResolved) {
return exportsResolved; return exportsResolved;
} }

View file

@ -542,6 +542,7 @@ pub fn op_require_as_file_path(file_or_url: String) -> String {
#[op] #[op]
fn op_require_resolve_exports( fn op_require_resolve_exports(
state: &mut OpState, state: &mut OpState,
uses_local_node_modules_dir: bool,
modules_path: String, modules_path: String,
_request: String, _request: String,
name: String, name: String,
@ -550,7 +551,9 @@ fn op_require_resolve_exports(
) -> Result<Option<String>, AnyError> { ) -> Result<Option<String>, AnyError> {
let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone(); let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone();
let pkg_path = if resolver.in_npm_package(&PathBuf::from(&modules_path)) { let pkg_path = if resolver.in_npm_package(&PathBuf::from(&modules_path))
&& !uses_local_node_modules_dir
{
modules_path modules_path
} else { } else {
path_resolve(vec![modules_path, name]) path_resolve(vec![modules_path, name])