Skip to content

Commit

Permalink
fix(cli): allow remapping to locals for import map (#8262)
Browse files Browse the repository at this point in the history
Fixes #7723
  • Loading branch information
kitsonk authored Nov 7, 2020
1 parent 4f67f0c commit 88c4d31
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
34 changes: 33 additions & 1 deletion cli/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?
Expand All @@ -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(),
Expand Down Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions cli/tests/module_graph/file_tests-b-mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = "b";
3 changes: 3 additions & 0 deletions cli/tests/module_graph/file_tests-importremap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as a from "https://deno.land/x/a/mod.ts";

console.log(a);
1 change: 1 addition & 0 deletions cli/tests/module_graph/https_deno.land-x-a-mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as b from "../b/mod.js";

0 comments on commit 88c4d31

Please sign in to comment.