2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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;
|
2022-11-02 10:47:02 -04:00
|
|
|
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
|
2021-10-10 17:26:22 -04:00
|
|
|
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-11-25 17:00:28 -05:00
|
|
|
use crate::args::JsxImportSourceConfig;
|
2022-08-24 13:36:05 -04:00
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
/// A resolver that takes care of resolution, taking into account loaded
|
|
|
|
/// import map, JSX settings.
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct CliResolver {
|
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
|
|
maybe_default_jsx_import_source: Option<String>,
|
|
|
|
maybe_jsx_import_source_module: Option<String>,
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
impl CliResolver {
|
|
|
|
pub fn maybe_new(
|
|
|
|
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
|
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
|
|
) -> Option<Self> {
|
|
|
|
if maybe_jsx_import_source_config.is_some() || maybe_import_map.is_some() {
|
|
|
|
Some(Self {
|
|
|
|
maybe_import_map,
|
|
|
|
maybe_default_jsx_import_source: maybe_jsx_import_source_config
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|c| c.default_specifier.clone()),
|
|
|
|
maybe_jsx_import_source_module: maybe_jsx_import_source_config
|
|
|
|
.map(|c| c.module),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
2022-01-31 17:33:57 -05:00
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
pub fn with_import_map(import_map: Arc<ImportMap>) -> Self {
|
|
|
|
Self::maybe_new(None, Some(import_map)).unwrap()
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
pub fn as_graph_resolver(&self) -> &dyn Resolver {
|
2021-11-08 20:26:39 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:47:02 -04:00
|
|
|
impl Resolver for CliResolver {
|
2022-08-24 13:36:05 -04:00
|
|
|
fn default_jsx_import_source(&self) -> Option<String> {
|
2022-11-02 10:47:02 -04:00
|
|
|
self.maybe_default_jsx_import_source.clone()
|
2022-08-24 13:36:05 -04:00
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
2022-11-02 10:47:02 -04:00
|
|
|
self
|
|
|
|
.maybe_jsx_import_source_module
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(DEFAULT_JSX_IMPORT_SOURCE_MODULE)
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-01-31 17:33:57 -05:00
|
|
|
) -> ResolveResponse {
|
2022-11-02 10:47:02 -04:00
|
|
|
if let Some(import_map) = &self.maybe_import_map {
|
|
|
|
match import_map.resolve(specifier, referrer) {
|
|
|
|
Ok(resolved_specifier) => {
|
|
|
|
ResolveResponse::Specifier(resolved_specifier)
|
|
|
|
}
|
|
|
|
Err(err) => ResolveResponse::Err(err.into()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match resolve_import(specifier, referrer.as_str()) {
|
2022-01-31 17:33:57 -05:00
|
|
|
Ok(specifier) => ResolveResponse::Specifier(specifier),
|
|
|
|
Err(err) => ResolveResponse::Err(err.into()),
|
2022-11-02 10:47:02 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
}
|