Skip to content

Commit

Permalink
Cache import map deps
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Apr 29, 2024
1 parent e066b79 commit 01b8985
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
37 changes: 37 additions & 0 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::cache::CodeCache;
use crate::cache::ModuleInfoCache;
use crate::cache::ParsedSourceCache;
use crate::emit::Emitter;
use crate::factory::CliFactory;
use crate::graph_util::graph_lock_or_exit;
use crate::graph_util::CreateGraphOptions;
use crate::graph_util::ModuleGraphBuilder;
Expand Down Expand Up @@ -64,6 +65,42 @@ use std::rc::Rc;
use std::str;
use std::sync::Arc;

pub async fn load_top_level_deps(factory: &CliFactory) -> Result<(), AnyError> {
let npm_resolver = factory.npm_resolver().await?;
if let Some(npm_resolver) = npm_resolver.as_managed() {
npm_resolver.ensure_top_level_package_json_install().await?;
npm_resolver.resolve_pending().await?;
}
// cache as many entries in the import map as we can
if let Some(import_map) = factory.maybe_import_map().await? {
let roots = import_map
.imports()
.entries()
.filter_map(|entry| {
if entry.key.ends_with('/') {
None
} else if let Some(value) = entry.value {
Some(value.clone())
} else {
None
}
})
.collect();
factory
.module_load_preparer()
.await?
.prepare_module_load(
roots,
false,
factory.cli_options().ts_type_lib_window(),
deno_runtime::permissions::PermissionsContainer::allow_all(),
)
.await?;
}

Ok(())
}

pub struct ModuleLoadPreparer {
options: Arc<CliOptions>,
graph_container: Arc<ModuleGraphContainer>,
Expand Down
8 changes: 3 additions & 5 deletions cli/tools/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,10 @@ async fn install_local(
if let Some(add_flags) = maybe_add_flags {
return super::registry::add(flags, add_flags).await;
}

let factory = CliFactory::from_flags(flags)?;
let npm_resolver = factory.npm_resolver().await?;
if let Some(npm_resolver) = npm_resolver.as_managed() {
npm_resolver.ensure_top_level_package_json_install().await?;
npm_resolver.resolve_pending().await?;
}
crate::module_loader::load_top_level_deps(&factory).await?;

Ok(())
}

Expand Down
7 changes: 2 additions & 5 deletions cli/tools/registry/pm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,8 @@ pub async fn add(flags: Flags, add_flags: AddFlags) -> Result<(), AnyError> {

// make a new CliFactory to pick up the updated config file
let cli_factory = CliFactory::from_flags(flags)?;
let npm_resolver = cli_factory.npm_resolver().await?;
if let Some(npm_resolver) = npm_resolver.as_managed() {
npm_resolver.ensure_top_level_package_json_install().await?;
npm_resolver.resolve_pending().await?;
}
// cache deps
crate::module_loader::load_top_level_deps(&cli_factory).await?;

Ok(())
}
Expand Down

0 comments on commit 01b8985

Please sign in to comment.