2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 21:26:22 +00:00
|
|
|
|
2023-01-24 13:23:19 +00:00
|
|
|
use deno_core::error::AnyError;
|
2021-10-10 21:26:22 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::source::Resolver;
|
2022-11-02 14:47:02 +00:00
|
|
|
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
|
2021-10-10 21:26:22 +00:00
|
|
|
use import_map::ImportMap;
|
2021-11-09 01:26:39 +00:00
|
|
|
use std::sync::Arc;
|
2021-10-10 21:26:22 +00:00
|
|
|
|
2022-11-25 22:00:28 +00:00
|
|
|
use crate::args::JsxImportSourceConfig;
|
2022-08-24 17:36:05 +00:00
|
|
|
|
2022-11-02 14:47:02 +00:00
|
|
|
/// A resolver that takes care of resolution, taking into account loaded
|
|
|
|
/// import map, JSX settings.
|
|
|
|
#[derive(Debug, Clone, Default)]
|
2023-02-15 16:30:54 +00:00
|
|
|
pub struct CliGraphResolver {
|
2022-11-02 14:47:02 +00:00
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
|
|
maybe_default_jsx_import_source: Option<String>,
|
|
|
|
maybe_jsx_import_source_module: Option<String>,
|
2021-10-10 21:26:22 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 16:30:54 +00:00
|
|
|
impl CliGraphResolver {
|
|
|
|
pub fn new(
|
2022-11-02 14:47:02 +00:00
|
|
|
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
|
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
2023-02-15 16:30:54 +00:00
|
|
|
) -> Self {
|
|
|
|
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),
|
2022-01-31 22:33:57 +00:00
|
|
|
}
|
2021-10-10 21:26:22 +00:00
|
|
|
}
|
2021-11-09 01:26:39 +00:00
|
|
|
|
2022-11-02 14:47:02 +00:00
|
|
|
pub fn as_graph_resolver(&self) -> &dyn Resolver {
|
2021-11-09 01:26:39 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:30:54 +00:00
|
|
|
impl Resolver for CliGraphResolver {
|
2022-08-24 17:36:05 +00:00
|
|
|
fn default_jsx_import_source(&self) -> Option<String> {
|
2022-11-02 14:47:02 +00:00
|
|
|
self.maybe_default_jsx_import_source.clone()
|
2022-08-24 17:36:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:26:39 +00:00
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
2022-11-02 14:47:02 +00:00
|
|
|
self
|
|
|
|
.maybe_jsx_import_source_module
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(DEFAULT_JSX_IMPORT_SOURCE_MODULE)
|
2021-11-09 01:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2023-01-24 13:23:19 +00:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2022-11-02 14:47:02 +00:00
|
|
|
if let Some(import_map) = &self.maybe_import_map {
|
2023-01-24 13:23:19 +00:00
|
|
|
import_map
|
|
|
|
.resolve(specifier, referrer)
|
|
|
|
.map_err(|err| err.into())
|
2022-11-02 14:47:02 +00:00
|
|
|
} else {
|
2023-02-15 16:30:54 +00:00
|
|
|
deno_graph::resolve_import(specifier, referrer).map_err(|err| err.into())
|
2022-11-02 14:47:02 +00:00
|
|
|
}
|
2021-11-09 01:26:39 +00:00
|
|
|
}
|
|
|
|
}
|