2022-01-08 03:09:52 +00:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 21:26:22 +00:00
|
|
|
|
2021-11-09 01:26:39 +00:00
|
|
|
use deno_core::resolve_import;
|
2021-10-10 21:26:22 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-01-31 22:33:57 +00:00
|
|
|
use deno_graph::source::ResolveResponse;
|
2021-10-10 21:26:22 +00:00
|
|
|
use deno_graph::source::Resolver;
|
|
|
|
use import_map::ImportMap;
|
2021-11-09 01:26:39 +00:00
|
|
|
use std::sync::Arc;
|
2021-10-10 21:26:22 +00:00
|
|
|
|
|
|
|
/// 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`.
|
2021-11-09 01:26:39 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct ImportMapResolver(Arc<ImportMap>);
|
2021-10-10 21:26:22 +00:00
|
|
|
|
2021-11-09 01:26:39 +00:00
|
|
|
impl ImportMapResolver {
|
|
|
|
pub fn new(import_map: Arc<ImportMap>) -> Self {
|
2021-10-10 21:26:22 +00:00
|
|
|
Self(import_map)
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:26:39 +00:00
|
|
|
pub fn as_resolver(&self) -> &dyn Resolver {
|
2021-10-10 21:26:22 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:26:39 +00:00
|
|
|
impl Resolver for ImportMapResolver {
|
2021-10-10 21:26:22 +00:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-01-31 22:33:57 +00:00
|
|
|
) -> ResolveResponse {
|
|
|
|
match self.0.resolve(specifier, referrer) {
|
|
|
|
Ok(specifier) => ResolveResponse::Specifier(specifier),
|
|
|
|
Err(err) => ResolveResponse::Err(err.into()),
|
|
|
|
}
|
2021-10-10 21:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 01:26:39 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub(crate) struct JsxResolver {
|
|
|
|
jsx_import_source_module: String,
|
|
|
|
maybe_import_map_resolver: Option<ImportMapResolver>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JsxResolver {
|
|
|
|
pub fn new(
|
|
|
|
jsx_import_source_module: String,
|
|
|
|
maybe_import_map_resolver: Option<ImportMapResolver>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
jsx_import_source_module,
|
|
|
|
maybe_import_map_resolver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_resolver(&self) -> &dyn Resolver {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver for JsxResolver {
|
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
|
|
|
self.jsx_import_source_module.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-01-31 22:33:57 +00:00
|
|
|
) -> ResolveResponse {
|
2021-11-09 01:26:39 +00:00
|
|
|
self.maybe_import_map_resolver.as_ref().map_or_else(
|
2022-01-31 22:33:57 +00:00
|
|
|
|| match resolve_import(specifier, referrer.as_str()) {
|
|
|
|
Ok(specifier) => ResolveResponse::Specifier(specifier),
|
|
|
|
Err(err) => ResolveResponse::Err(err.into()),
|
|
|
|
},
|
2021-11-09 01:26:39 +00:00
|
|
|
|r| r.resolve(specifier, referrer),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|