summaryrefslogtreecommitdiff
path: root/cli/resolver.rs
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2021-10-11 08:26:22 +1100
committerGitHub <noreply@github.com>2021-10-11 08:26:22 +1100
commita7baf5f2bbb50dc0cb571de141b800b9155faca7 (patch)
tree4bebaabd1d3ed4595e8a388e0fae559bb5558974 /cli/resolver.rs
parent5a8a989b7815023f33a1e3183a55cc8999af5d98 (diff)
refactor: integrate deno_graph into CLI (#12369)
Diffstat (limited to 'cli/resolver.rs')
-rw-r--r--cli/resolver.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/cli/resolver.rs b/cli/resolver.rs
new file mode 100644
index 000000000..d3427c58b
--- /dev/null
+++ b/cli/resolver.rs
@@ -0,0 +1,35 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+use deno_core::error::AnyError;
+use deno_core::ModuleSpecifier;
+use deno_graph::source::Resolver;
+use import_map::ImportMap;
+
+/// Wraps an import map to be used when building a deno_graph module graph.
+/// This is done to avoid having `import_map` be a direct dependency of
+/// `deno_graph`.
+#[derive(Debug)]
+pub(crate) struct ImportMapResolver<'a>(&'a ImportMap);
+
+impl<'a> ImportMapResolver<'a> {
+ pub fn new(import_map: &'a ImportMap) -> Self {
+ Self(import_map)
+ }
+
+ pub fn as_resolver(&'a self) -> &'a dyn Resolver {
+ self
+ }
+}
+
+impl Resolver for ImportMapResolver<'_> {
+ fn resolve(
+ &self,
+ specifier: &str,
+ referrer: &ModuleSpecifier,
+ ) -> Result<ModuleSpecifier, AnyError> {
+ self
+ .0
+ .resolve(specifier, referrer.as_str())
+ .map_err(|err| err.into())
+ }
+}