From 383d40a33bee842a7462fb157f5faca263598d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 19 Dec 2022 17:09:54 +0100 Subject: fix(npm): conditional exports with --node-modules-dir (#17111) This commit fixes conditional exports in `require()` implementation if `--node-modules-dir` flag is used. --- ext/node/02_require.js | 15 +++++++++++++-- ext/node/lib.rs | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'ext') diff --git a/ext/node/02_require.js b/ext/node/02_require.js index 2174ff8a9..0f11253f2 100644 --- a/ext/node/02_require.js +++ b/ext/node/02_require.js @@ -303,7 +303,12 @@ // 1. name/.* // 2. @scope/name/.* 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. const [, name, expansion = ""] = StringPrototypeMatch(request, EXPORTS_PATTERN) || []; @@ -312,6 +317,7 @@ } return core.ops.op_require_resolve_exports( + usesLocalNodeModulesDir, modulesPath, request, name, @@ -349,7 +355,12 @@ if (curPath && stat(curPath) < 1) continue; if (!absoluteRequest) { - const exportsResolved = resolveExports(curPath, request, parentPath); + const exportsResolved = resolveExports( + curPath, + request, + parentPath, + usesLocalNodeModulesDir, + ); if (exportsResolved) { return exportsResolved; } diff --git a/ext/node/lib.rs b/ext/node/lib.rs index b2443db0b..e8ff3c549 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -542,6 +542,7 @@ pub fn op_require_as_file_path(file_or_url: String) -> String { #[op] fn op_require_resolve_exports( state: &mut OpState, + uses_local_node_modules_dir: bool, modules_path: String, _request: String, name: String, @@ -550,7 +551,9 @@ fn op_require_resolve_exports( ) -> Result, AnyError> { let resolver = state.borrow::>().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 } else { path_resolve(vec![modules_path, name]) -- cgit v1.2.3