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

refactor(npm): add referrer when resolving npm package sub path from deno module (#20800)

Adds a `referrer` parameter to this function instead of using a fake
one.
This commit is contained in:
David Sherret 2023-10-05 16:18:29 -04:00 committed by GitHub
parent 7a01799f49
commit 820e93e3e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 29 deletions

View file

@ -1587,9 +1587,10 @@ fn node_resolve_npm_req_ref(
.and_then(|package_folder| {
npm
.node_resolver
.resolve_npm_reference(
.resolve_package_subpath_from_deno_module(
&package_folder,
npm_req_ref.sub_path(),
referrer,
NodeResolutionMode::Types,
&PermissionsContainer::allow_all(),
)

View file

@ -512,6 +512,7 @@ impl ModuleLoader for CliModuleLoader {
.resolve_package_sub_path(
&package_folder,
module.nv_reference.sub_path(),
referrer,
permissions,
)
.with_context(|| {
@ -726,6 +727,7 @@ impl CliNodeResolver {
.resolve_package_sub_path(
&package_folder,
req_ref.sub_path(),
referrer,
permissions,
)
.with_context(|| format!("Could not resolve '{}'.", req_ref))
@ -735,14 +737,18 @@ impl CliNodeResolver {
&self,
package_folder: &Path,
sub_path: Option<&str>,
referrer: &ModuleSpecifier,
permissions: &PermissionsContainer,
) -> Result<ModuleSpecifier, AnyError> {
self.handle_node_resolve_result(self.node_resolver.resolve_npm_reference(
package_folder,
sub_path,
NodeResolutionMode::Execution,
permissions,
))
self.handle_node_resolve_result(
self.node_resolver.resolve_package_subpath_from_deno_module(
package_folder,
sub_path,
referrer,
NodeResolutionMode::Execution,
permissions,
),
)
}
fn handle_node_resolve_result(

View file

@ -586,7 +586,7 @@ fn op_resolve(
let maybe_result = match resolved_dep {
Some(ResolutionResolved { specifier, .. }) => {
resolve_graph_specifier_types(specifier, state)?
resolve_graph_specifier_types(specifier, &referrer, state)?
}
_ => resolve_non_graph_specifier_types(&specifier, &referrer, state)?,
};
@ -629,6 +629,7 @@ fn op_resolve(
fn resolve_graph_specifier_types(
specifier: &ModuleSpecifier,
referrer: &ModuleSpecifier,
state: &State,
) -> Result<Option<(ModuleSpecifier, MediaType)>, AnyError> {
let graph = &state.graph;
@ -663,12 +664,14 @@ fn resolve_graph_specifier_types(
.as_managed()
.unwrap() // should never be byonm because it won't create Module::Npm
.resolve_pkg_folder_from_deno_module(module.nv_reference.nv())?;
let maybe_resolution = npm.node_resolver.resolve_npm_reference(
&package_folder,
module.nv_reference.sub_path(),
NodeResolutionMode::Types,
&PermissionsContainer::allow_all(),
)?;
let maybe_resolution =
npm.node_resolver.resolve_package_subpath_from_deno_module(
&package_folder,
module.nv_reference.sub_path(),
referrer,
NodeResolutionMode::Types,
&PermissionsContainer::allow_all(),
)?;
Ok(Some(NodeResolution::into_specifier_and_media_type(
maybe_resolution,
)))
@ -721,12 +724,14 @@ fn resolve_non_graph_specifier_types(
let package_folder = npm
.npm_resolver
.resolve_pkg_folder_from_deno_module_req(npm_req_ref.req(), referrer)?;
let maybe_resolution = node_resolver.resolve_npm_reference(
&package_folder,
npm_req_ref.sub_path(),
NodeResolutionMode::Types,
&PermissionsContainer::allow_all(),
)?;
let maybe_resolution = node_resolver
.resolve_package_subpath_from_deno_module(
&package_folder,
npm_req_ref.sub_path(),
referrer,
NodeResolutionMode::Types,
&PermissionsContainer::allow_all(),
)?;
Ok(Some(NodeResolution::into_specifier_and_media_type(
maybe_resolution,
)))

View file

@ -571,12 +571,19 @@ impl CliMainWorkerFactory {
return Ok(None);
}
let Some(resolution) = self.shared.node_resolver.resolve_npm_reference(
package_folder,
sub_path,
NodeResolutionMode::Execution,
permissions,
)?
// use a fake referrer since a real one doesn't exist
let referrer =
ModuleSpecifier::from_directory_path(package_folder).unwrap();
let Some(resolution) = self
.shared
.node_resolver
.resolve_package_subpath_from_deno_module(
package_folder,
sub_path,
&referrer,
NodeResolutionMode::Execution,
permissions,
)?
else {
return Ok(None);
};

View file

@ -325,15 +325,15 @@ impl NodeResolver {
Ok(resolved)
}
pub fn resolve_npm_reference(
pub fn resolve_package_subpath_from_deno_module(
&self,
package_dir: &Path,
package_subpath: Option<&str>,
referrer: &ModuleSpecifier,
mode: NodeResolutionMode,
permissions: &dyn NodePermissions,
) -> Result<Option<NodeResolution>, AnyError> {
let package_json_path = package_dir.join("package.json");
let referrer = ModuleSpecifier::from_directory_path(package_dir).unwrap();
let package_json =
self.load_package_json(permissions, package_json_path.clone())?;
let node_module_kind = NodeModuleKind::Esm;
@ -344,7 +344,7 @@ impl NodeResolver {
.resolve_package_subpath(
&package_json,
&package_subpath,
&referrer,
referrer,
node_module_kind,
DEFAULT_CONDITIONS,
mode,