@@ -62,22 +62,80 @@ fn main() {
62
62
63
63
fn resolve ( wd : & str , specifier : & str , containing_filename : Option < String > ) -> Result < String , ResolveError > {
64
64
if specifier. starts_with ( "/" ) || specifier. starts_with ( "file://" ) {
65
- return Ok ( specifier. to_owned ( ) ) ;
65
+ return Err ( ResolveError :: NotFound ( specifier. to_owned ( ) ) ) ;
66
66
}
67
- let resolver = Resolver :: new ( ResolveOptions {
68
- condition_names : vec ! [ "node" . to_owned( ) , "require" . to_owned( ) ] ,
69
- ..Default :: default ( )
70
- } ) ;
71
- if ( specifier. starts_with ( "./" ) || specifier. starts_with ( "../" ) ) && containing_filename. is_some ( ) {
72
- let containing_filename = containing_filename. unwrap ( ) ;
73
- let containing_dir = Path :: new ( & containing_filename) . parent ( ) . unwrap ( ) ;
74
- let ret = resolver. resolve ( containing_dir, specifier) ?;
75
- return Ok ( ret. path ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) ) ;
67
+ let mut specifier = specifier. to_owned ( ) ;
68
+ if specifier. eq ( "." ) {
69
+ specifier = "./index.js" . to_owned ( ) ;
70
+ }
71
+ if specifier. eq ( ".." ) {
72
+ specifier = "../index.js" . to_owned ( ) ;
73
+ }
74
+ if specifier. starts_with ( "./" ) || specifier. starts_with ( "../" ) {
75
+ if let Some ( containing_filename) = containing_filename {
76
+ let containing_dir = Path :: new ( & containing_filename) . parent ( ) . unwrap ( ) ;
77
+ specifier = containing_dir. join ( specifier) . to_str ( ) . unwrap ( ) . to_owned ( ) ;
78
+ } else {
79
+ return Err ( ResolveError :: NotFound ( specifier. to_owned ( ) ) ) ;
80
+ }
76
81
}
77
82
if specifier. starts_with ( "." ) {
78
83
return Err ( ResolveError :: NotFound ( specifier. to_owned ( ) ) ) ;
79
84
}
80
- let ret = resolver. resolve ( wd, specifier) ?;
85
+
86
+ let fullpath = if specifier. starts_with ( "/" ) {
87
+ specifier. clone ( )
88
+ } else {
89
+ Path :: join ( Path :: new ( wd) , "node_modules/" . to_owned ( ) + specifier. as_str ( ) )
90
+ . to_str ( )
91
+ . unwrap ( )
92
+ . to_owned ( )
93
+ } ;
94
+ if fs:: exists ( & fullpath) . expect ( "Can't check existence of file" ) {
95
+ return Ok ( fullpath) ;
96
+ }
97
+ let maybe_exists = fullpath. to_owned ( ) + ".cjs" ;
98
+ if fs:: exists ( & maybe_exists) . expect ( "Can't check existence of file" ) {
99
+ return Ok ( maybe_exists) ;
100
+ }
101
+ let maybe_exists = fullpath. to_owned ( ) + ".js" ;
102
+ if fs:: exists ( & maybe_exists) . expect ( "Can't check existence of file" ) {
103
+ return Ok ( maybe_exists) ;
104
+ }
105
+ let maybe_exists = fullpath. to_owned ( ) + "/index.cjs" ;
106
+ if fs:: exists ( & maybe_exists) . expect ( "Can't check existence of file" ) {
107
+ return Ok ( maybe_exists) ;
108
+ }
109
+ let maybe_exists = fullpath. to_owned ( ) + "/index.js" ;
110
+ if fs:: exists ( & maybe_exists) . expect ( "Can't check existence of file" ) {
111
+ return Ok ( maybe_exists) ;
112
+ }
113
+ if fullpath. ends_with ( ".js" ) {
114
+ let maybe_exists = fullpath[ ..fullpath. len ( ) - 3 ] . to_owned ( ) + ".cjs" ;
115
+ if fs:: exists ( & maybe_exists) . expect ( "Can't check existence of file" ) {
116
+ return Ok ( maybe_exists) ;
117
+ }
118
+ }
119
+ if fullpath. ends_with ( ".cjs" ) {
120
+ let maybe_exists = fullpath[ ..fullpath. len ( ) - 4 ] . to_owned ( ) + ".js" ;
121
+ if fs:: exists ( & maybe_exists) . expect ( "Can't check existence of file" ) {
122
+ return Ok ( maybe_exists) ;
123
+ }
124
+ }
125
+
126
+ // `/path/to/wd/node_modules/react/index` -> `react/index`
127
+ if specifier. starts_with ( "/" ) {
128
+ let rel_path = Path :: new ( & specifier)
129
+ . strip_prefix ( Path :: new ( wd) . join ( "node_modules" ) )
130
+ . unwrap ( ) ;
131
+ specifier = rel_path. to_str ( ) . unwrap ( ) . to_owned ( ) ;
132
+ }
133
+
134
+ let resolver = Resolver :: new ( ResolveOptions {
135
+ condition_names : vec ! [ "node" . to_owned( ) , "require" . to_owned( ) ] ,
136
+ ..Default :: default ( )
137
+ } ) ;
138
+ let ret = resolver. resolve ( wd, & specifier) ?;
81
139
Ok ( ret. path ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) )
82
140
}
83
141
0 commit comments