use std::collections::HashMap;
use std::path::{Path, PathBuf};
use super::{
build_inner, normalize_path, stdlib, stdlib_module_name, PackageContext, ParsedSourceRetention,
};
use crate::ModuleGraph;
pub fn read_module_source(path: &Path) -> Option<String> {
if let Some(stdlib_module) = stdlib_module_name(path) {
return stdlib::get_stdlib_source(stdlib_module).map(ToString::to_string);
}
MODULE_DISK_READS.with(|reads| reads.set(reads.get() + 1));
std::fs::read_to_string(path).ok()
}
thread_local! {
static MODULE_DISK_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
pub fn module_source_disk_reads() -> usize {
MODULE_DISK_READS.with(std::cell::Cell::get)
}
pub fn build_with_source(file: &Path, source: &str) -> ModuleGraph {
let file = normalize_path(file);
let source_overrides = HashMap::from([(file.clone(), source.to_string())]);
build_inner(
&[file],
ParsedSourceRetention::None,
Some(&source_overrides),
PackageContext::Project,
None,
)
.graph
}
pub fn build_with_sources(file: &Path, sources: &HashMap<PathBuf, String>) -> ModuleGraph {
let file = normalize_path(file);
let source_overrides: HashMap<PathBuf, String> = sources
.iter()
.map(|(path, source)| (normalize_path(path), source.clone()))
.collect();
build_inner(
&[file],
ParsedSourceRetention::None,
Some(&source_overrides),
PackageContext::Project,
None,
)
.graph
}