diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-07-17 09:13:22 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-17 09:13:22 -0400 |
commit | f4b9d8586215fc07c28998e5d896fefa876139b7 (patch) | |
tree | 9ee42eb4bb62af04b1c3b049cd179dfa6fe908bb /cli/tools | |
parent | 078def0ff8501bb07f3f286515acd8c6a2181037 (diff) |
fix(workspace): support resolving bare specifiers to npm pkgs within a workspace (#24611)
This makes bare specifiers for npm packages work when inside a
workspace, which emulates the same behaviour as when there's a
node_modules directory. The bare specifier can be overwritten by
specifying an import map entry or package.json dependency entry.
* https://github.com/denoland/deno_config/pull/88
Closes #24605
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/registry/unfurl.rs | 62 | ||||
-rw-r--r-- | cli/tools/vendor/test.rs | 3 |
2 files changed, 51 insertions, 14 deletions
diff --git a/cli/tools/registry/unfurl.rs b/cli/tools/registry/unfurl.rs index 758db0796..63c773f01 100644 --- a/cli/tools/registry/unfurl.rs +++ b/cli/tools/registry/unfurl.rs @@ -75,26 +75,59 @@ impl SpecifierUnfurler { match resolved { MappedResolution::Normal(specifier) | MappedResolution::ImportMap(specifier) => Some(specifier), + MappedResolution::WorkspaceNpmPackage { + target_pkg_json: pkg_json, + pkg_name, + sub_path, + } => { + // todo(#24612): consider warning or error when this is also a jsr package? + ModuleSpecifier::parse(&format!( + "npm:{}{}{}", + pkg_name, + pkg_json + .version + .as_ref() + .map(|v| format!("@^{}", v)) + .unwrap_or_default(), + sub_path + .as_ref() + .map(|s| format!("/{}", s)) + .unwrap_or_default() + )) + .ok() + } MappedResolution::PackageJson { + alias, sub_path, dep_result, .. } => match dep_result { Ok(dep) => match dep { - PackageJsonDepValue::Req(req) => ModuleSpecifier::parse(&format!( - "npm:{}{}", - req, - sub_path - .as_ref() - .map(|s| format!("/{}", s)) - .unwrap_or_default() - )) - .ok(), - PackageJsonDepValue::Workspace(_) => { - log::warn!( - "package.json workspace entries are not implemented yet for publishing." - ); - None + PackageJsonDepValue::Req(pkg_req) => { + // todo(#24612): consider warning or error when this is an npm workspace + // member that's also a jsr package? + ModuleSpecifier::parse(&format!( + "npm:{}{}", + pkg_req, + sub_path + .as_ref() + .map(|s| format!("/{}", s)) + .unwrap_or_default() + )) + .ok() + } + PackageJsonDepValue::Workspace(version_req) => { + // todo(#24612): consider warning or error when this is also a jsr package? + ModuleSpecifier::parse(&format!( + "npm:{}@{}{}", + alias, + version_req, + sub_path + .as_ref() + .map(|s| format!("/{}", s)) + .unwrap_or_default() + )) + .ok() } }, Err(err) => { @@ -401,6 +434,7 @@ mod tests { }), ); let workspace_resolver = WorkspaceResolver::new_raw( + Arc::new(ModuleSpecifier::from_directory_path(&cwd).unwrap()), Some(import_map), vec![Arc::new(package_json)], deno_config::workspace::PackageJsonDepResolution::Enabled, diff --git a/cli/tools/vendor/test.rs b/cli/tools/vendor/test.rs index ac07c47d1..dc851858e 100644 --- a/cli/tools/vendor/test.rs +++ b/cli/tools/vendor/test.rs @@ -234,6 +234,7 @@ impl VendorTestBuilder { let loader = self.loader.clone(); let parsed_source_cache = ParsedSourceCache::default(); let resolver = Arc::new(build_resolver( + output_dir.parent().unwrap(), self.jsx_import_source_config.clone(), self.maybe_original_import_map.clone(), )); @@ -287,6 +288,7 @@ impl VendorTestBuilder { } fn build_resolver( + root_dir: &Path, maybe_jsx_import_source_config: Option<JsxImportSourceConfig>, maybe_original_import_map: Option<ImportMap>, ) -> CliGraphResolver { @@ -295,6 +297,7 @@ fn build_resolver( npm_resolver: None, sloppy_imports_resolver: None, workspace_resolver: Arc::new(WorkspaceResolver::new_raw( + Arc::new(ModuleSpecifier::from_directory_path(root_dir).unwrap()), maybe_original_import_map, Vec::new(), deno_config::workspace::PackageJsonDepResolution::Enabled, |