2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-10-11 08:26:22 +11:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2023-01-24 14:23:19 +01:00
|
|
|
use deno_core::error::AnyError;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_core::futures::future;
|
|
|
|
use deno_core::futures::future::LocalBoxFuture;
|
|
|
|
use deno_core::futures::FutureExt;
|
2021-10-11 08:26:22 +11:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::npm::NpmPackageNv;
|
|
|
|
use deno_graph::npm::NpmPackageReq;
|
|
|
|
use deno_graph::source::NpmResolver;
|
2021-10-11 08:26:22 +11:00
|
|
|
use deno_graph::source::Resolver;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::source::UnknownBuiltInNodeModuleError;
|
2022-11-02 15:47:02 +01:00
|
|
|
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_runtime::deno_node::is_builtin_node_module;
|
2021-10-11 08:26:22 +11:00
|
|
|
use import_map::ImportMap;
|
2023-02-20 19:14:06 +01:00
|
|
|
use std::collections::HashMap;
|
2021-11-09 12:26:39 +11:00
|
|
|
use std::sync::Arc;
|
2021-10-11 08:26:22 +11:00
|
|
|
|
2022-11-25 17:00:28 -05:00
|
|
|
use crate::args::JsxImportSourceConfig;
|
2023-02-22 14:15:25 -05:00
|
|
|
use crate::npm::NpmRegistryApi;
|
|
|
|
use crate::npm::NpmResolution;
|
2022-08-24 19:36:05 +02:00
|
|
|
|
2022-11-02 15:47:02 +01:00
|
|
|
/// A resolver that takes care of resolution, taking into account loaded
|
|
|
|
/// import map, JSX settings.
|
2023-02-22 14:15:25 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2023-02-15 11:30:54 -05:00
|
|
|
pub struct CliGraphResolver {
|
2022-11-02 15:47:02 +01:00
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
2023-02-20 19:14:06 +01:00
|
|
|
maybe_package_json_deps: Option<HashMap<String, NpmPackageReq>>,
|
2022-11-02 15:47:02 +01:00
|
|
|
maybe_default_jsx_import_source: Option<String>,
|
|
|
|
maybe_jsx_import_source_module: Option<String>,
|
2023-02-22 14:15:25 -05:00
|
|
|
no_npm: bool,
|
|
|
|
npm_registry_api: NpmRegistryApi,
|
|
|
|
npm_resolution: NpmResolution,
|
|
|
|
sync_download_semaphore: Option<Arc<tokio::sync::Semaphore>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CliGraphResolver {
|
|
|
|
fn default() -> Self {
|
|
|
|
// This is not ideal, but necessary for the LSP. In the future, we should
|
|
|
|
// refactor the LSP and force this to be initialized.
|
|
|
|
let npm_registry_api = NpmRegistryApi::new_uninitialized();
|
|
|
|
let npm_resolution =
|
|
|
|
NpmResolution::new(npm_registry_api.clone(), None, None);
|
|
|
|
Self {
|
|
|
|
maybe_import_map: Default::default(),
|
|
|
|
maybe_default_jsx_import_source: Default::default(),
|
|
|
|
maybe_jsx_import_source_module: Default::default(),
|
|
|
|
no_npm: false,
|
|
|
|
npm_registry_api,
|
|
|
|
npm_resolution,
|
|
|
|
maybe_package_json_deps: Default::default(),
|
|
|
|
sync_download_semaphore: Self::create_sync_download_semaphore(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 08:26:22 +11:00
|
|
|
}
|
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
impl CliGraphResolver {
|
|
|
|
pub fn new(
|
2022-11-02 15:47:02 +01:00
|
|
|
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
|
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
2023-02-22 14:15:25 -05:00
|
|
|
no_npm: bool,
|
|
|
|
npm_registry_api: NpmRegistryApi,
|
|
|
|
npm_resolution: NpmResolution,
|
2023-02-20 19:14:06 +01:00
|
|
|
maybe_package_json_deps: Option<HashMap<String, NpmPackageReq>>,
|
2023-02-15 11:30:54 -05: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),
|
2023-02-22 14:15:25 -05:00
|
|
|
no_npm,
|
|
|
|
npm_registry_api,
|
|
|
|
npm_resolution,
|
2023-02-20 19:14:06 +01:00
|
|
|
maybe_package_json_deps,
|
2023-02-22 14:15:25 -05:00
|
|
|
sync_download_semaphore: Self::create_sync_download_semaphore(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_sync_download_semaphore() -> Option<Arc<tokio::sync::Semaphore>> {
|
|
|
|
if crate::npm::should_sync_download() {
|
|
|
|
Some(Arc::new(tokio::sync::Semaphore::new(1)))
|
|
|
|
} else {
|
|
|
|
None
|
2022-02-01 09:33:57 +11:00
|
|
|
}
|
2021-10-11 08:26:22 +11:00
|
|
|
}
|
2021-11-09 12:26:39 +11:00
|
|
|
|
2022-11-02 15:47:02 +01:00
|
|
|
pub fn as_graph_resolver(&self) -> &dyn Resolver {
|
2021-11-09 12:26:39 +11:00
|
|
|
self
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
|
|
|
|
pub fn as_graph_npm_resolver(&self) -> &dyn NpmResolver {
|
|
|
|
self
|
|
|
|
}
|
2021-11-09 12:26:39 +11:00
|
|
|
}
|
|
|
|
|
2023-02-15 11:30:54 -05:00
|
|
|
impl Resolver for CliGraphResolver {
|
2022-08-24 19:36:05 +02:00
|
|
|
fn default_jsx_import_source(&self) -> Option<String> {
|
2022-11-02 15:47:02 +01:00
|
|
|
self.maybe_default_jsx_import_source.clone()
|
2022-08-24 19:36:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 12:26:39 +11:00
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
2022-11-02 15:47:02 +01:00
|
|
|
self
|
|
|
|
.maybe_jsx_import_source_module
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(DEFAULT_JSX_IMPORT_SOURCE_MODULE)
|
2021-11-09 12:26:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2023-01-24 14:23:19 +01:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2022-11-02 15:47:02 +01:00
|
|
|
if let Some(import_map) = &self.maybe_import_map {
|
2023-02-22 23:21:05 +01:00
|
|
|
return import_map
|
2023-01-24 14:23:19 +01:00
|
|
|
.resolve(specifier, referrer)
|
2023-02-22 23:21:05 +01:00
|
|
|
.map_err(|err| err.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(deps) = self.maybe_package_json_deps.as_ref() {
|
|
|
|
if let Some(req) = deps.get(specifier) {
|
|
|
|
return Ok(ModuleSpecifier::parse(&format!("npm:{req}")).unwrap());
|
|
|
|
}
|
2022-11-02 15:47:02 +01:00
|
|
|
}
|
2023-02-22 23:21:05 +01:00
|
|
|
|
|
|
|
deno_graph::resolve_import(specifier, referrer).map_err(|err| err.into())
|
2021-11-09 12:26:39 +11:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
|
|
|
|
impl NpmResolver for CliGraphResolver {
|
|
|
|
fn resolve_builtin_node_module(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Option<String>, UnknownBuiltInNodeModuleError> {
|
|
|
|
if specifier.scheme() != "node" {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let module_name = specifier.path().to_string();
|
|
|
|
if is_builtin_node_module(&module_name) {
|
|
|
|
Ok(Some(module_name))
|
|
|
|
} else {
|
|
|
|
Err(UnknownBuiltInNodeModuleError { module_name })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_and_cache_npm_package_info(
|
|
|
|
&self,
|
|
|
|
package_name: &str,
|
|
|
|
) -> LocalBoxFuture<'static, Result<(), String>> {
|
|
|
|
if self.no_npm {
|
|
|
|
// return it succeeded and error at the import site below
|
|
|
|
return Box::pin(future::ready(Ok(())));
|
|
|
|
}
|
|
|
|
// this will internally cache the package information
|
|
|
|
let package_name = package_name.to_string();
|
|
|
|
let api = self.npm_registry_api.clone();
|
|
|
|
let mut maybe_sync_download_semaphore =
|
|
|
|
self.sync_download_semaphore.clone();
|
|
|
|
async move {
|
|
|
|
let result = if let Some(semaphore) = maybe_sync_download_semaphore.take()
|
|
|
|
{
|
|
|
|
let _permit = semaphore.acquire().await.unwrap();
|
|
|
|
api.package_info(&package_name).await
|
|
|
|
} else {
|
|
|
|
api.package_info(&package_name).await
|
|
|
|
};
|
|
|
|
result.map(|_| ()).map_err(|err| format!("{err:#}"))
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_npm(
|
|
|
|
&self,
|
|
|
|
package_req: &NpmPackageReq,
|
|
|
|
) -> Result<NpmPackageNv, AnyError> {
|
|
|
|
if self.no_npm {
|
|
|
|
bail!("npm specifiers were requested; but --no-npm is specified")
|
|
|
|
}
|
|
|
|
self
|
|
|
|
.npm_resolution
|
|
|
|
.resolve_package_req_for_deno_graph(package_req)
|
|
|
|
}
|
|
|
|
}
|