@@ -133,19 +133,41 @@ impl DenoDir {
133133 self : & DenoDir ,
134134 module_name : & str ,
135135 filename : & str ,
136- ) -> DenoResult < String > {
137- if is_remote ( module_name) {
138- self . fetch_remote_source ( module_name, filename)
139- } else if module_name. starts_with ( ASSET_PREFIX ) {
136+ ) -> DenoResult < CodeFetchOutput > {
137+ if module_name. starts_with ( ASSET_PREFIX ) {
140138 panic ! ( "Asset resolution should be done in JS, not Rust." ) ;
141- } else {
142- assert_eq ! (
143- module_name, filename,
144- "if a module isn't remote, it should have the same filename"
145- ) ;
146- let src = fs:: read_to_string ( Path :: new ( filename) ) ?;
147- Ok ( src)
148139 }
140+ let is_module_remote = is_remote ( module_name) ;
141+ let use_extension = |ext| {
142+ let module_name = format ! ( "{}{}" , module_name, ext) ;
143+ let filename = format ! ( "{}{}" , filename, ext) ;
144+ let source_code = if is_module_remote {
145+ self . fetch_remote_source ( & module_name, & filename) ?
146+ } else {
147+ assert_eq ! (
148+ module_name, filename,
149+ "if a module isn't remote, it should have the same filename"
150+ ) ;
151+ fs:: read_to_string ( Path :: new ( & filename) ) ?
152+ } ;
153+ return Ok ( CodeFetchOutput {
154+ module_name : module_name. to_string ( ) ,
155+ filename : filename. to_string ( ) ,
156+ source_code,
157+ maybe_output_code : None ,
158+ } ) ;
159+ } ;
160+ let default_attempt = use_extension ( "" ) ;
161+ if default_attempt. is_ok ( ) {
162+ return default_attempt;
163+ }
164+ debug ! ( "Trying {}.ts..." , module_name) ;
165+ let ts_attempt = use_extension ( ".ts" ) ;
166+ if ts_attempt. is_ok ( ) {
167+ return ts_attempt;
168+ }
169+ debug ! ( "Trying {}.js..." , module_name) ;
170+ use_extension ( ".js" )
149171 }
150172
151173 pub fn code_fetch (
@@ -161,16 +183,7 @@ impl DenoDir {
161183 let ( module_name, filename) =
162184 self . resolve_module ( module_specifier, containing_file) ?;
163185
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- } ) ;
186+ let result = self . get_source_code ( module_name. as_str ( ) , filename. as_str ( ) ) ;
174187 let out = match result {
175188 Err ( err) => {
176189 if err. kind ( ) == ErrorKind :: NotFound {
@@ -420,6 +433,48 @@ fn test_code_fetch() {
420433 //println!("code_fetch_output {:?}", code_fetch_output);
421434}
422435
436+ #[ test]
437+ fn test_code_fetch_no_ext ( ) {
438+ let ( _temp_dir, deno_dir) = test_setup ( ) ;
439+
440+ let cwd = std:: env:: current_dir ( ) . unwrap ( ) ;
441+ let cwd_string = String :: from ( cwd. to_str ( ) . unwrap ( ) ) + "/" ;
442+
443+ // Assuming cwd is the deno repo root.
444+ let module_specifier = "./js/main" ;
445+ let containing_file = cwd_string. as_str ( ) ;
446+ let r = deno_dir. code_fetch ( module_specifier, containing_file) ;
447+ assert ! ( r. is_ok( ) ) ;
448+
449+ // Test .ts extension
450+ // Assuming cwd is the deno repo root.
451+ let module_specifier = "./js/main" ;
452+ let containing_file = cwd_string. as_str ( ) ;
453+ let r = deno_dir. code_fetch ( module_specifier, containing_file) ;
454+ assert ! ( r. is_ok( ) ) ;
455+ let code_fetch_output = r. unwrap ( ) ;
456+ // could only test .ends_with to avoid include local abs path
457+ assert ! ( code_fetch_output. module_name. ends_with( "/js/main.ts" ) ) ;
458+ assert ! ( code_fetch_output. filename. ends_with( "/js/main.ts" ) ) ;
459+ assert ! ( code_fetch_output. source_code. len( ) > 10 ) ;
460+
461+ // Test .js extension
462+ // Assuming cwd is the deno repo root.
463+ let module_specifier = "./js/mock_builtin" ;
464+ let containing_file = cwd_string. as_str ( ) ;
465+ let r = deno_dir. code_fetch ( module_specifier, containing_file) ;
466+ assert ! ( r. is_ok( ) ) ;
467+ let code_fetch_output = r. unwrap ( ) ;
468+ // could only test .ends_with to avoid include local abs path
469+ assert ! (
470+ code_fetch_output
471+ . module_name
472+ . ends_with( "/js/mock_builtin.js" )
473+ ) ;
474+ assert ! ( code_fetch_output. filename. ends_with( "/js/mock_builtin.js" ) ) ;
475+ assert ! ( code_fetch_output. source_code. len( ) > 10 ) ;
476+ }
477+
423478#[ test]
424479fn test_src_file_to_url_1 ( ) {
425480 let ( _temp_dir, deno_dir) = test_setup ( ) ;
0 commit comments