Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit ed55de7

Browse files
authored
Merge pull request #314 from noverby/compiler-fix-import-map
compiler: properly handle import map subdirs
2 parents f318f83 + 604d8c1 commit ed55de7

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

compiler/src/import_map.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,20 @@ impl ImportMap {
3636
let mut imports = IndexMap::new();
3737
let mut scopes = IndexMap::new();
3838
for (k, v) in map.imports.iter() {
39-
if v.starts_with("./") {
40-
imports.insert(
41-
k.into(),
39+
let alias = if v.starts_with("./") {
40+
let path = if v.ends_with("/") {
4241
RelativePath::new(v)
4342
.normalize()
44-
.to_path(Path::new("/"))
45-
.to_slash()
46-
.unwrap()
47-
.into(),
48-
);
43+
.to_relative_path_buf()
44+
.join("/")
45+
} else {
46+
RelativePath::new(v).normalize().to_relative_path_buf()
47+
};
48+
format!("/{}", path)
4949
} else {
50-
imports.insert(k.into(), v.into());
51-
}
50+
v.into()
51+
};
52+
imports.insert(k.into(), alias);
5253
}
5354
for (k, v) in map.scopes.iter() {
5455
let mut map = IndexMap::new();
@@ -118,6 +119,8 @@ mod tests {
118119
let mut scope_imports: SpecifierHashMap = HashMap::new();
119120
imports.insert("@/".into(), "./".into());
120121
imports.insert("~/".into(), "./".into());
122+
imports.insert("comps/".into(), "./components/".into());
123+
imports.insert("lib".into(), "./lib/mod.ts".into());
121124
imports.insert("react".into(), "https://esm.sh/react".into());
122125
imports.insert("react-dom/".into(), "https://esm.sh/react-dom/".into());
123126
imports.insert(
@@ -135,6 +138,11 @@ mod tests {
135138
import_map.resolve("/pages/index.tsx", "~/components/logo.tsx"),
136139
"/components/logo.tsx"
137140
);
141+
assert_eq!(
142+
import_map.resolve("/pages/index.tsx", "comps/logo.tsx"),
143+
"/components/logo.tsx"
144+
);
145+
assert_eq!(import_map.resolve("/pages/index.tsx", "lib"), "/lib/mod.ts");
138146
assert_eq!(
139147
import_map.resolve("/app.tsx", "react"),
140148
"https://esm.sh/react"

0 commit comments

Comments
 (0)