Skip to content

Commit 4df2de2

Browse files
authored
feat: resolve_export_value_urls (#38)
1 parent 9d8f58a commit 4df2de2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/lib.rs

+56
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,24 @@ impl ConfigFile {
725725
self.json.unstable.iter().any(|v| v == name)
726726
}
727727

728+
/// Resolve the export values in a config file to their URLs.
729+
pub fn resolve_export_value_urls(&self) -> Result<Vec<Url>, AnyError> {
730+
let exports_config = self
731+
.to_exports_config()
732+
.with_context(|| {
733+
format!("Failed to parse exports at {}", self.specifier)
734+
})?
735+
.into_map();
736+
let mut exports = Vec::with_capacity(exports_config.len());
737+
for (_, value) in exports_config {
738+
let entry_point = self.specifier.join(&value).with_context(|| {
739+
format!("Failed to join {} with {}", self.specifier, value)
740+
})?;
741+
exports.push(entry_point);
742+
}
743+
Ok(exports)
744+
}
745+
728746
pub fn to_exports_config(&self) -> Result<ExportsConfig, AnyError> {
729747
fn has_extension(value: &str) -> bool {
730748
let search_text = &value[value.rfind('/').unwrap_or(0)..];
@@ -2140,6 +2158,44 @@ mod tests {
21402158
);
21412159
}
21422160

2161+
#[test]
2162+
fn resolve_export_value_urls() {
2163+
fn get_exports(config_text: &str) -> Vec<String> {
2164+
let config_dir = Url::parse("file:///deno/").unwrap();
2165+
let config_specifier = config_dir.join("tsconfig.json").unwrap();
2166+
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
2167+
config_file
2168+
.resolve_export_value_urls()
2169+
.unwrap()
2170+
.into_iter()
2171+
.map(|u| u.to_string())
2172+
.collect()
2173+
}
2174+
2175+
// no exports
2176+
assert_eq!(get_exports("{}"), Vec::<String>::new());
2177+
// string export
2178+
assert_eq!(
2179+
get_exports(r#"{ "exports": "./mod.ts" }"#),
2180+
vec!["file:///deno/mod.ts".to_string()]
2181+
);
2182+
// map export
2183+
assert_eq!(
2184+
get_exports(r#"{ "exports": { "./export": "./mod.ts" } }"#),
2185+
vec!["file:///deno/mod.ts".to_string()]
2186+
);
2187+
// multiple
2188+
assert_eq!(
2189+
get_exports(
2190+
r#"{ "exports": { "./export": "./mod.ts", "./other": "./other.ts" } }"#
2191+
),
2192+
vec![
2193+
"file:///deno/mod.ts".to_string(),
2194+
"file:///deno/other.ts".to_string(),
2195+
]
2196+
);
2197+
}
2198+
21432199
#[test]
21442200
fn test_empty_workspaces() {
21452201
let config_text = r#"{

0 commit comments

Comments
 (0)