From baee1f6575e9b0049ca5053f723b5874857f827a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 00:18:05 +0200 Subject: [PATCH 1/3] ImportMap::update_imports --- src/lib.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9ee580c..f7ddcc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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, + ) -> Result, 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)] @@ -862,4 +907,63 @@ mod tests { "https://esm.sh/postcss-modules@4.2.2#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" + ); + } } From 670df2390ea9c42191b5921ccd205874d436cab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 15:56:25 +0200 Subject: [PATCH 2/3] lint --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f7ddcc0..1736d96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -538,7 +538,7 @@ impl ImportMap { } let address_url = - match ImportMap::try_url_like_specifier(&value, &self.base_url) { + match ImportMap::try_url_like_specifier(value, &self.base_url) { Some(url) => url, None => { diagnostics.push(format!( From 8276e37ca6cc5d6c9e17f3330a924d047b99ee6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 15:58:56 +0200 Subject: [PATCH 3/3] v0.3.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf9f6a7..aa5452a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,7 +43,7 @@ dependencies = [ [[package]] name = "import_map" -version = "0.3.0" +version = "0.3.1" dependencies = [ "indexmap", "log", diff --git a/Cargo.toml b/Cargo.toml index 0c6bf6c..355e6a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "import_map" -version = "0.3.0" +version = "0.3.1" authors = ["the Deno authors"] edition = "2018" license = "MIT"