11// Copyright 2020 the Aleph.js authors. All rights reserved. MIT license.
22
33use indexmap:: IndexMap ;
4+ use path_slash:: PathBufExt ;
5+ use relative_path:: RelativePath ;
46use serde:: Deserialize ;
5- use std:: collections:: HashMap ;
7+ use std:: { collections:: HashMap , path :: Path } ;
68
79type SpecifierHashMap = HashMap < String , String > ;
810type SpecifierMap = IndexMap < String , String > ;
@@ -36,12 +38,36 @@ impl ImportMap {
3638 let mut imports: SpecifierMap = IndexMap :: new ( ) ;
3739 let mut scopes = IndexMap :: new ( ) ;
3840 for ( k, v) in map. imports . iter ( ) {
39- imports. insert ( k. into ( ) , v. into ( ) ) ;
41+ if k. eq ( "@/" ) || k. eq ( "~/" ) {
42+ imports. insert (
43+ k. into ( ) ,
44+ RelativePath :: new ( v)
45+ . normalize ( )
46+ . to_path ( Path :: new ( "/" ) )
47+ . to_slash ( )
48+ . unwrap ( )
49+ . into ( ) ,
50+ ) ;
51+ } else {
52+ imports. insert ( k. into ( ) , v. into ( ) ) ;
53+ }
4054 }
4155 for ( k, v) in map. scopes . iter ( ) {
4256 let mut map: SpecifierMap = IndexMap :: new ( ) ;
4357 for ( k, v) in v. iter ( ) {
44- map. insert ( k. into ( ) , v. into ( ) ) ;
58+ if k. eq ( "@/" ) || k. eq ( "~/" ) {
59+ imports. insert (
60+ k. into ( ) ,
61+ RelativePath :: new ( v)
62+ . normalize ( )
63+ . to_path ( Path :: new ( "/" ) )
64+ . to_slash ( )
65+ . unwrap ( )
66+ . into ( ) ,
67+ ) ;
68+ } else {
69+ map. insert ( k. into ( ) , v. into ( ) ) ;
70+ }
4571 }
4672 scopes. insert ( k. into ( ) , map) ;
4773 }
@@ -92,6 +118,8 @@ mod tests {
92118 let mut imports: SpecifierHashMap = HashMap :: new ( ) ;
93119 let mut scopes: HashMap < String , SpecifierHashMap > = HashMap :: new ( ) ;
94120 let mut scope_imports: SpecifierHashMap = HashMap :: new ( ) ;
121+ imports. insert ( "@/" . into ( ) , "./" . into ( ) ) ;
122+ imports. insert ( "~/" . into ( ) , "./" . into ( ) ) ;
95123 imports. insert ( "react" . into ( ) , "https://esm.sh/react" . into ( ) ) ;
96124 imports. insert ( "react-dom/" . into ( ) , "https://esm.sh/react-dom/" . into ( ) ) ;
97125 imports. insert (
@@ -102,15 +130,23 @@ mod tests {
102130 scopes. insert ( "/scope/" . into ( ) , scope_imports) ;
103131 let import_map = ImportMap :: from_hashmap ( ImportHashMap { imports, scopes } ) ;
104132 assert_eq ! (
105- import_map. resolve( "./app.tsx" , "react" ) ,
133+ import_map. resolve( "/pages/index.tsx" , "@/components/logo.tsx" ) ,
134+ "/components/logo.tsx"
135+ ) ;
136+ assert_eq ! (
137+ import_map. resolve( "/pages/index.tsx" , "~/components/logo.tsx" ) ,
138+ "/components/logo.tsx"
139+ ) ;
140+ assert_eq ! (
141+ import_map. resolve( "/app.tsx" , "react" ) ,
106142 "https://esm.sh/react"
107143 ) ;
108144 assert_eq ! (
109- import_map. resolve( ". /app.tsx" , "https://deno.land/x/aleph/mod.ts" ) ,
145+ import_map. resolve( "/app.tsx" , "https://deno.land/x/aleph/mod.ts" ) ,
110146 "http://localhost:2020/mod.ts"
111147 ) ;
112148 assert_eq ! (
113- import_map. resolve( ". /renderer.ts" , "react-dom/server" ) ,
149+ import_map. resolve( "/framework/react /renderer.ts" , "react-dom/server" ) ,
114150 "https://esm.sh/react-dom/server"
115151 ) ;
116152 assert_eq ! (
0 commit comments