From 88c4d3145b1e964387be487090bb6ec0a572b90f Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Sat, 7 Nov 2020 15:04:22 +1100 Subject: [PATCH] fix(cli): allow remapping to locals for import map (#8262) Fixes #7723 --- cli/module_graph.rs | 34 ++++++++++++++++++- cli/tests/module_graph/file_tests-b-mod.js | 1 + .../module_graph/file_tests-importremap.ts | 3 ++ .../module_graph/https_deno.land-x-a-mod.ts | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cli/tests/module_graph/file_tests-b-mod.js create mode 100644 cli/tests/module_graph/file_tests-importremap.ts create mode 100644 cli/tests/module_graph/https_deno.land-x-a-mod.ts diff --git a/cli/module_graph.rs b/cli/module_graph.rs index 9b3d79a9435421..4d0a4419ff307a 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -441,7 +441,9 @@ impl Module { } else { None }; + let mut remapped_import = false; let specifier = if let Some(module_specifier) = maybe_resolve { + remapped_import = true; module_specifier } else { ModuleSpecifier::resolve_import(specifier, self.specifier.as_str())? @@ -462,9 +464,11 @@ impl Module { ); } - // Disallow a remote URL from trying to import a local URL + // Disallow a remote URL from trying to import a local URL, unless it is a + // remapped import via the import map if (referrer_scheme == "https" || referrer_scheme == "http") && !(specifier_scheme == "https" || specifier_scheme == "http") + && !remapped_import { return Err( GraphError::InvalidLocalImport(specifier.clone(), location).into(), @@ -2211,6 +2215,34 @@ pub mod tests { } } + #[tokio::test] + async fn test_graph_import_map_remote_to_local() { + let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); + let fixtures = c.join("tests/module_graph"); + let maybe_import_map = Some( + ImportMap::from_json( + "file:///tests/importmap.json", + r#"{ + "imports": { + "https://deno.land/x/b/mod.js": "./b/mod.js" + } + } + "#, + ) + .expect("could not parse import map"), + ); + let handler = Rc::new(RefCell::new(MockSpecifierHandler { + fixtures, + ..Default::default() + })); + let mut builder = GraphBuilder::new(handler, maybe_import_map, None); + let specifier = + ModuleSpecifier::resolve_url_or_path("file:///tests/importremap.ts") + .expect("could not resolve module"); + builder.add(&specifier, false).await.expect("could not add"); + builder.get_graph(); + } + #[tokio::test] async fn test_graph_with_lockfile() { let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); diff --git a/cli/tests/module_graph/file_tests-b-mod.js b/cli/tests/module_graph/file_tests-b-mod.js new file mode 100644 index 00000000000000..59d1689930e55d --- /dev/null +++ b/cli/tests/module_graph/file_tests-b-mod.js @@ -0,0 +1 @@ +export const b = "b"; diff --git a/cli/tests/module_graph/file_tests-importremap.ts b/cli/tests/module_graph/file_tests-importremap.ts new file mode 100644 index 00000000000000..17f0126739bc97 --- /dev/null +++ b/cli/tests/module_graph/file_tests-importremap.ts @@ -0,0 +1,3 @@ +import * as a from "https://deno.land/x/a/mod.ts"; + +console.log(a); diff --git a/cli/tests/module_graph/https_deno.land-x-a-mod.ts b/cli/tests/module_graph/https_deno.land-x-a-mod.ts new file mode 100644 index 00000000000000..1e334d399f4b85 --- /dev/null +++ b/cli/tests/module_graph/https_deno.land-x-a-mod.ts @@ -0,0 +1 @@ +export * as b from "../b/mod.js";