2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
use deno_core::resolve_import;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::source::ResolveResponse;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_graph::source::Resolver;
|
|
|
|
use import_map::ImportMap;
|
2021-11-08 20:26:39 -05:00
|
|
|
use std::sync::Arc;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2022-08-24 13:36:05 -04:00
|
|
|
use crate::args::config_file::JsxImportSourceConfig;
|
|
|
|
|
2021-10-10 17:26:22 -04: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-08 20:26:39 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct ImportMapResolver(Arc<ImportMap>);
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
impl ImportMapResolver {
|
|
|
|
pub fn new(import_map: Arc<ImportMap>) -> Self {
|
2021-10-10 17:26:22 -04:00
|
|
|
Self(import_map)
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
pub fn as_resolver(&self) -> &dyn Resolver {
|
2021-10-10 17:26:22 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
impl Resolver for ImportMapResolver {
|
2021-10-10 17:26:22 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-01-31 17:33:57 -05:00
|
|
|
) -> ResolveResponse {
|
|
|
|
match self.0.resolve(specifier, referrer) {
|
2022-06-14 10:05:37 -04:00
|
|
|
Ok(resolved_specifier) => ResolveResponse::Specifier(resolved_specifier),
|
2022-01-31 17:33:57 -05:00
|
|
|
Err(err) => ResolveResponse::Err(err.into()),
|
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct JsxResolver {
|
2022-08-24 13:36:05 -04:00
|
|
|
default_jsx_import_source: Option<String>,
|
2021-11-08 20:26:39 -05:00
|
|
|
jsx_import_source_module: String,
|
|
|
|
maybe_import_map_resolver: Option<ImportMapResolver>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JsxResolver {
|
|
|
|
pub fn new(
|
2022-08-24 13:36:05 -04:00
|
|
|
jsx_import_source_config: JsxImportSourceConfig,
|
2021-11-08 20:26:39 -05:00
|
|
|
maybe_import_map_resolver: Option<ImportMapResolver>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2022-08-24 13:36:05 -04:00
|
|
|
default_jsx_import_source: jsx_import_source_config.default_specifier,
|
|
|
|
jsx_import_source_module: jsx_import_source_config.module,
|
2021-11-08 20:26:39 -05:00
|
|
|
maybe_import_map_resolver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_resolver(&self) -> &dyn Resolver {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver for JsxResolver {
|
2022-08-24 13:36:05 -04:00
|
|
|
fn default_jsx_import_source(&self) -> Option<String> {
|
|
|
|
self.default_jsx_import_source.clone()
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
|
|
|
self.jsx_import_source_module.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-01-31 17:33:57 -05:00
|
|
|
) -> ResolveResponse {
|
2021-11-08 20:26:39 -05:00
|
|
|
self.maybe_import_map_resolver.as_ref().map_or_else(
|
2022-01-31 17:33:57 -05:00
|
|
|
|| match resolve_import(specifier, referrer.as_str()) {
|
|
|
|
Ok(specifier) => ResolveResponse::Specifier(specifier),
|
|
|
|
Err(err) => ResolveResponse::Err(err.into()),
|
|
|
|
},
|
2021-11-08 20:26:39 -05:00
|
|
|
|r| r.resolve(specifier, referrer),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|