@@ -133,18 +133,55 @@ impl DenoDir {
133133 self : & DenoDir ,
134134 module_name : & str ,
135135 filename : & str ,
136- ) -> DenoResult < String > {
136+ ) -> DenoResult < CodeFetchOutput > {
137137 if is_remote ( module_name) {
138- self . fetch_remote_source ( module_name, filename)
138+ let try_extension = |ext| {
139+ let module_name = format ! ( "{}{}" , module_name, ext) ;
140+ let filename = format ! ( "{}{}" , filename, ext) ;
141+ let source_code = self . fetch_remote_source ( & module_name, & filename) ?;
142+ return Ok ( CodeFetchOutput {
143+ module_name : module_name. to_string ( ) ,
144+ filename : filename. to_string ( ) ,
145+ source_code,
146+ maybe_output_code : None ,
147+ } ) ;
148+ } ;
149+ if module_name. ends_with ( ".ts" ) || module_name. ends_with ( ".js" ) {
150+ return try_extension ( "" ) ;
151+ }
152+ println ! ( "Trying {}.ts..." , module_name) ;
153+ if let Ok ( v) = try_extension ( ".ts" ) {
154+ return Ok ( v) ;
155+ }
156+ println ! ( "Trying {}.js..." , module_name) ;
157+ try_extension ( ".js" )
139158 } else if module_name. starts_with ( ASSET_PREFIX ) {
140159 panic ! ( "Asset resolution should be done in JS, not Rust." ) ;
141160 } else {
142161 assert_eq ! (
143162 module_name, filename,
144163 "if a module isn't remote, it should have the same filename"
145164 ) ;
146- let src = fs:: read_to_string ( Path :: new ( filename) ) ?;
147- Ok ( src)
165+ let try_extension = |ext| {
166+ let module_name = format ! ( "{}{}" , module_name, ext) ;
167+ let filename = format ! ( "{}{}" , filename, ext) ;
168+ let source_code = fs:: read_to_string ( Path :: new ( & filename) ) ?;
169+ return Ok ( CodeFetchOutput {
170+ module_name : module_name. to_string ( ) ,
171+ filename : filename. to_string ( ) ,
172+ source_code,
173+ maybe_output_code : None ,
174+ } ) ;
175+ } ;
176+ if module_name. ends_with ( ".ts" ) || module_name. ends_with ( ".js" ) {
177+ return try_extension ( "" ) ;
178+ }
179+ println ! ( "Trying {}.ts..." , module_name) ;
180+ if let Ok ( v) = try_extension ( ".ts" ) {
181+ return Ok ( v) ;
182+ }
183+ println ! ( "Trying {}.js..." , module_name) ;
184+ try_extension ( ".js" )
148185 }
149186 }
150187
@@ -161,16 +198,7 @@ impl DenoDir {
161198 let ( module_name, filename) =
162199 self . resolve_module ( module_specifier, containing_file) ?;
163200
164- let result = self
165- . get_source_code ( module_name. as_str ( ) , filename. as_str ( ) )
166- . and_then ( |source_code| {
167- Ok ( CodeFetchOutput {
168- module_name,
169- filename,
170- source_code,
171- maybe_output_code : None ,
172- } )
173- } ) ;
201+ let result = self . get_source_code ( module_name. as_str ( ) , filename. as_str ( ) ) ;
174202 let out = match result {
175203 Err ( err) => {
176204 if err. kind ( ) == ErrorKind :: NotFound {
0 commit comments