mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
4d1a14ca7f
This commits adds auto-discovery of "package.json" file when running "deno run" and "deno task" subcommands. In case of "deno run" the "package.json" is being looked up starting from the directory of the script that is being run, stopping early if "deno.json(c)" file is found (ie. FS tree won't be traversed "up" from "deno.json"). When "package.json" is discovered the "--node-modules-dir" flag is implied, leading to creation of local "node_modules/" directory - we did that, because most tools relying on "package.json" will expect "node_modules/" directory to be present (eg. Vite). Additionally "dependencies" and "devDependencies" specified in the "package.json" are downloaded on startup. This is a stepping stone to supporting bare specifier imports, but the actual integration will be done in a follow up commit. --------- Co-authored-by: David Sherret <dsherret@gmail.com>
76 lines
2.3 KiB
Rust
76 lines
2.3 KiB
Rust
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use deno_core::error::AnyError;
|
|
use deno_core::ModuleSpecifier;
|
|
use deno_graph::source::Resolver;
|
|
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
|
|
use import_map::ImportMap;
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
|
|
use crate::args::JsxImportSourceConfig;
|
|
use deno_graph::npm::NpmPackageReq;
|
|
|
|
/// A resolver that takes care of resolution, taking into account loaded
|
|
/// import map, JSX settings.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct CliGraphResolver {
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
// TODO(bartlomieju): actually use in `resolver`, once
|
|
// deno_graph refactors and upgrades land.
|
|
#[allow(dead_code)]
|
|
maybe_package_json_deps: Option<HashMap<String, NpmPackageReq>>,
|
|
maybe_default_jsx_import_source: Option<String>,
|
|
maybe_jsx_import_source_module: Option<String>,
|
|
}
|
|
|
|
impl CliGraphResolver {
|
|
pub fn new(
|
|
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
maybe_package_json_deps: Option<HashMap<String, NpmPackageReq>>,
|
|
) -> 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),
|
|
maybe_package_json_deps,
|
|
}
|
|
}
|
|
|
|
pub fn as_graph_resolver(&self) -> &dyn Resolver {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Resolver for CliGraphResolver {
|
|
fn default_jsx_import_source(&self) -> Option<String> {
|
|
self.maybe_default_jsx_import_source.clone()
|
|
}
|
|
|
|
fn jsx_import_source_module(&self) -> &str {
|
|
self
|
|
.maybe_jsx_import_source_module
|
|
.as_deref()
|
|
.unwrap_or(DEFAULT_JSX_IMPORT_SOURCE_MODULE)
|
|
}
|
|
|
|
fn resolve(
|
|
&self,
|
|
specifier: &str,
|
|
referrer: &ModuleSpecifier,
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
// TODO(bartlomieju): actually use `maybe_package_json_deps` here, once
|
|
// deno_graph refactors and upgrades land.
|
|
if let Some(import_map) = &self.maybe_import_map {
|
|
import_map
|
|
.resolve(specifier, referrer)
|
|
.map_err(|err| err.into())
|
|
} else {
|
|
deno_graph::resolve_import(specifier, referrer).map_err(|err| err.into())
|
|
}
|
|
}
|
|
}
|