1
1
// Copyright 2020 the Aleph.js authors. All rights reserved. MIT license.
2
2
3
3
use indexmap:: IndexMap ;
4
+ use path_slash:: PathBufExt ;
5
+ use relative_path:: RelativePath ;
4
6
use serde:: Deserialize ;
5
- use std:: collections:: HashMap ;
7
+ use std:: { collections:: HashMap , path :: Path } ;
6
8
7
9
type SpecifierHashMap = HashMap < String , String > ;
8
10
type SpecifierMap = IndexMap < String , String > ;
@@ -36,12 +38,36 @@ impl ImportMap {
36
38
let mut imports: SpecifierMap = IndexMap :: new ( ) ;
37
39
let mut scopes = IndexMap :: new ( ) ;
38
40
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
+ }
40
54
}
41
55
for ( k, v) in map. scopes . iter ( ) {
42
56
let mut map: SpecifierMap = IndexMap :: new ( ) ;
43
57
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
+ }
45
71
}
46
72
scopes. insert ( k. into ( ) , map) ;
47
73
}
@@ -92,6 +118,8 @@ mod tests {
92
118
let mut imports: SpecifierHashMap = HashMap :: new ( ) ;
93
119
let mut scopes: HashMap < String , SpecifierHashMap > = HashMap :: new ( ) ;
94
120
let mut scope_imports: SpecifierHashMap = HashMap :: new ( ) ;
121
+ imports. insert ( "@/" . into ( ) , "./" . into ( ) ) ;
122
+ imports. insert ( "~/" . into ( ) , "./" . into ( ) ) ;
95
123
imports. insert ( "react" . into ( ) , "https://esm.sh/react" . into ( ) ) ;
96
124
imports. insert ( "react-dom/" . into ( ) , "https://esm.sh/react-dom/" . into ( ) ) ;
97
125
imports. insert (
@@ -102,15 +130,23 @@ mod tests {
102
130
scopes. insert ( "/scope/" . into ( ) , scope_imports) ;
103
131
let import_map = ImportMap :: from_hashmap ( ImportHashMap { imports, scopes } ) ;
104
132
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" ) ,
106
142
"https://esm.sh/react"
107
143
) ;
108
144
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" ) ,
110
146
"http://localhost:2020/mod.ts"
111
147
) ;
112
148
assert_eq ! (
113
- import_map. resolve( ". /renderer.ts" , "react-dom/server" ) ,
149
+ import_map. resolve( "/framework/react /renderer.ts" , "react-dom/server" ) ,
114
150
"https://esm.sh/react-dom/server"
115
151
) ;
116
152
assert_eq ! (
0 commit comments