-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add npm specifiers to the graph #232
Conversation
…directs the unresolved npm specifier to a resolved module
…NodeId` back to CLI later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/graph.rs
Outdated
for (specifier, npm_ref, maybe_range) in infos { | ||
if !self.graph.module_slots.contains_key(&specifier) { | ||
if let Some(npm_resolver) = &self.npm_resolver { | ||
// todo: cache resolutions to ensure they always resolve the same | ||
let resolution = npm_resolver.resolve_npm(&npm_ref.req); | ||
match resolution { | ||
Ok(pkg_id) => { | ||
specifier_resolutions | ||
.insert(specifier.clone(), pkg_id.clone()); | ||
let pkg_id_ref = NpmPackageNvReference { | ||
nv: pkg_id, | ||
sub_path: npm_ref.sub_path, | ||
}; | ||
let resolved_specifier = pkg_id_ref.as_specifier(); | ||
if resolved_specifier != specifier { | ||
self | ||
.graph | ||
.redirects | ||
.insert(specifier, resolved_specifier.clone()); | ||
} | ||
self.graph.module_slots.insert( | ||
resolved_specifier.clone(), | ||
ModuleSlot::Module(Module::Npm(NpmModule { | ||
specifier: resolved_specifier, | ||
nv_reference: pkg_id_ref, | ||
})), | ||
); | ||
} | ||
Err(err) => { | ||
self.graph.module_slots.insert( | ||
specifier.clone(), | ||
ModuleSlot::Err(ModuleGraphError::ResolutionError( | ||
ResolutionError::ResolverError { | ||
error: Arc::new(err), | ||
specifier: specifier.to_string(), | ||
// this should always be set, | ||
range: maybe_range.unwrap_or_else(|| Range { | ||
specifier, | ||
start: Position::zeroed(), | ||
end: Position::zeroed(), | ||
}), | ||
}, | ||
)), | ||
); | ||
} | ||
} | ||
} else { | ||
self.graph.module_slots.insert( | ||
specifier.clone(), | ||
ModuleSlot::Err(ModuleGraphError::LoadingErr( | ||
specifier, | ||
maybe_range, | ||
Arc::new(anyhow::anyhow!( | ||
"npm specifiers are not supported in this environment" | ||
)), | ||
)), | ||
); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very nice, consider factoring the whole body of while
into a helper function, could be much easier to grep with fewer indents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about that, but got lazy. Will update soon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a lot of work to do this properly. I'm going to skip on this for now and add a todo
This changes npm specifiers to be handled by deno_graph and resolved to an npm package name and version when the specifier is encountered. It also slightly changes how npm specifier resolution occurs—previously it would collect all the npm specifiers and resolve them all at once, but now it resolves them on the fly as they are encountered in the module graph. denoland/deno_graph#232 --------- Co-authored-by: Bartek Iwańczuk <[email protected]>
We're going to rely on integration tests in the CLI for this atm (opened #241 for the future).
Closes #230