Skip to content

Commit 88c4d31

Browse files
authored
fix(cli): allow remapping to locals for import map (#8262)
Fixes #7723
1 parent 4f67f0c commit 88c4d31

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

cli/module_graph.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ impl Module {
441441
} else {
442442
None
443443
};
444+
let mut remapped_import = false;
444445
let specifier = if let Some(module_specifier) = maybe_resolve {
446+
remapped_import = true;
445447
module_specifier
446448
} else {
447449
ModuleSpecifier::resolve_import(specifier, self.specifier.as_str())?
@@ -462,9 +464,11 @@ impl Module {
462464
);
463465
}
464466

465-
// Disallow a remote URL from trying to import a local URL
467+
// Disallow a remote URL from trying to import a local URL, unless it is a
468+
// remapped import via the import map
466469
if (referrer_scheme == "https" || referrer_scheme == "http")
467470
&& !(specifier_scheme == "https" || specifier_scheme == "http")
471+
&& !remapped_import
468472
{
469473
return Err(
470474
GraphError::InvalidLocalImport(specifier.clone(), location).into(),
@@ -2211,6 +2215,34 @@ pub mod tests {
22112215
}
22122216
}
22132217

2218+
#[tokio::test]
2219+
async fn test_graph_import_map_remote_to_local() {
2220+
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
2221+
let fixtures = c.join("tests/module_graph");
2222+
let maybe_import_map = Some(
2223+
ImportMap::from_json(
2224+
"file:///tests/importmap.json",
2225+
r#"{
2226+
"imports": {
2227+
"https://deno.land/x/b/mod.js": "./b/mod.js"
2228+
}
2229+
}
2230+
"#,
2231+
)
2232+
.expect("could not parse import map"),
2233+
);
2234+
let handler = Rc::new(RefCell::new(MockSpecifierHandler {
2235+
fixtures,
2236+
..Default::default()
2237+
}));
2238+
let mut builder = GraphBuilder::new(handler, maybe_import_map, None);
2239+
let specifier =
2240+
ModuleSpecifier::resolve_url_or_path("file:///tests/importremap.ts")
2241+
.expect("could not resolve module");
2242+
builder.add(&specifier, false).await.expect("could not add");
2243+
builder.get_graph();
2244+
}
2245+
22142246
#[tokio::test]
22152247
async fn test_graph_with_lockfile() {
22162248
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = "b";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as a from "https://deno.land/x/a/mod.ts";
2+
3+
console.log(a);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as b from "../b/mod.js";

0 commit comments

Comments
 (0)