Skip to main content

harn_modules/
standalone.rs

1use std::collections::HashMap;
2use std::path::Path;
3
4use super::{build_inner, normalize_path, ModuleGraph, ParsedSourceRetention};
5
6/// Whether module resolution may consult package metadata discovered around
7/// the seed files. Standalone callers retain relative and standard-library
8/// imports while making package resolution independent of ambient projects.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10pub(super) enum PackageContext {
11    #[default]
12    Project,
13    Standalone,
14}
15
16/// Build a module graph using caller-owned source without discovering package
17/// metadata around the entry file.
18pub fn build_with_standalone_source(file: &Path, source: &str) -> ModuleGraph {
19    let file = normalize_path(file);
20    let source_overrides = HashMap::from([(file.clone(), source.to_string())]);
21    build_inner(
22        &[file],
23        ParsedSourceRetention::None,
24        Some(&source_overrides),
25        PackageContext::Standalone,
26    )
27    .graph
28}