Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Oct 2, 2021
1 parent 0390071 commit f01c528
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "import_map"
version = "0.3.0"
version = "0.3.1"
authors = ["the Deno authors"]
edition = "2018"
license = "MIT"
Expand Down
104 changes: 104 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Serialize;
use serde_json::Map;
use serde_json::Value;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt;
Expand Down Expand Up @@ -516,6 +517,50 @@ impl ImportMap {
Some(referrer.to_string()),
))
}

/// This is a non-standard method that allows to add
/// more "imports" mappings to already existing import map.
///
///
pub fn update_imports(
&mut self,
imports: HashMap<String, String>,
) -> Result<Vec<String>, ImportMapError> {
let mut diagnostics = vec![];

for (key, value) in imports.iter() {
if let Some(import_value) = self.imports.get(key) {
diagnostics.push(format!(
"\"{}\" already exists and is mapped to {:?}",
key, import_value
));
continue;
}

let address_url =
match ImportMap::try_url_like_specifier(value, &self.base_url) {
Some(url) => url,
None => {
diagnostics.push(format!(
"Invalid address \"{}\" for the specifier key {:?}.",
value, key
));
continue;
}
};
self.imports.insert(key.to_string(), Some(address_url));
}

// Sort in longest and alphabetical order.
self.imports.sort_by(|k1, _v1, k2, _v2| match k1.cmp(k2) {
Ordering::Greater => Ordering::Less,
Ordering::Less => Ordering::Greater,
// JSON guarantees that there can't be duplicate keys
Ordering::Equal => unreachable!(),
});

Ok(diagnostics)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -862,4 +907,63 @@ mod tests {
"https://esm.sh/[email protected]#fragment"
);
}

#[test]
fn update_imports() {
let json_map = r#"{
"imports": {
"fs": "https://example.com/1"
}
}"#;
let mut import_map =
ImportMap::from_json("https://deno.land", json_map).unwrap();
let mut mappings = HashMap::new();
mappings.insert(
"assert".to_string(),
"https://deno.land/std/node/assert.ts".to_string(),
);
mappings.insert(
"child_process".to_string(),
"https://deno.land/std/node/child_process.ts".to_string(),
);
mappings.insert(
"fs".to_string(),
"https://deno.land/std/node/fs.ts".to_string(),
);
mappings.insert(
"url".to_string(),
"https://deno.land/std/node/url.ts".to_string(),
);
let diagnostics = import_map.update_imports(mappings).unwrap();
assert_eq!(diagnostics.len(), 1);
assert!(diagnostics[0].contains("fs"));
assert_eq!(
import_map
.resolve("assert", "http://deno.land")
.unwrap()
.as_str(),
"https://deno.land/std/node/assert.ts"
);
assert_eq!(
import_map
.resolve("child_process", "http://deno.land")
.unwrap()
.as_str(),
"https://deno.land/std/node/child_process.ts"
);
assert_eq!(
import_map
.resolve("fs", "http://deno.land")
.unwrap()
.as_str(),
"https://example.com/1"
);
assert_eq!(
import_map
.resolve("url", "http://deno.land")
.unwrap()
.as_str(),
"https://deno.land/std/node/url.ts"
);
}
}

0 comments on commit f01c528

Please sign in to comment.