diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-05-29 16:32:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-29 16:32:15 +0200 |
commit | ad6d2a7734aafb4a64837abc6abd1d1d0fb20017 (patch) | |
tree | 4c0e8714384bc47211a4b68953a925fb54b7a015 /cli/main.rs | |
parent | b97459b5ae3918aae21f0c02342fd7c18189ad3e (diff) |
refactor: TS compiler and module graph (#5817)
This PR addresses many problems with module graph loading
introduced in #5029, as well as many long standing issues.
"ModuleGraphLoader" has been wired to "ModuleLoader" implemented
on "State" - that means that dependency analysis and fetching is done
before spinning up TS compiler worker.
Basic dependency tracking for TS compilation has been implemented.
Errors caused by import statements are now annotated with import
location.
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/main.rs')
-rw-r--r-- | cli/main.rs | 65 |
1 files changed, 40 insertions, 25 deletions
diff --git a/cli/main.rs b/cli/main.rs index 16ca41fe4..1e90729d1 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -72,12 +72,10 @@ use crate::file_fetcher::SourceFile; use crate::file_fetcher::SourceFileFetcher; use crate::fs as deno_fs; use crate::global_state::GlobalState; -use crate::import_map::ImportMap; use crate::msg::MediaType; use crate::op_error::OpError; use crate::ops::io::get_stdio; use crate::permissions::Permissions; -use crate::state::exit_unstable; use crate::state::State; use crate::tsc::TargetLib; use crate::worker::MainWorker; @@ -156,7 +154,13 @@ fn create_main_worker( global_state: GlobalState, main_module: ModuleSpecifier, ) -> Result<MainWorker, ErrBox> { - let state = State::new(global_state, None, main_module, false)?; + let state = State::new( + global_state.clone(), + None, + main_module, + global_state.maybe_import_map.clone(), + false, + )?; let mut worker = MainWorker::new( "main".to_string(), @@ -220,16 +224,21 @@ async fn print_file_info( ); let module_specifier_ = module_specifier.clone(); + global_state - .clone() - .fetch_compiled_module( - module_specifier_, + .prepare_module_load( + module_specifier_.clone(), None, TargetLib::Main, Permissions::allow_all(), false, + global_state.maybe_import_map.clone(), ) .await?; + global_state + .clone() + .fetch_compiled_module(module_specifier_, None) + .await?; if out.media_type == msg::MediaType::TypeScript || (out.media_type == msg::MediaType::JavaScript @@ -393,43 +402,49 @@ async fn bundle_command( source_file: String, out_file: Option<PathBuf>, ) -> Result<(), ErrBox> { - let mut module_name = ModuleSpecifier::resolve_url_or_path(&source_file)?; - let url = module_name.as_url(); + let mut module_specifier = + ModuleSpecifier::resolve_url_or_path(&source_file)?; + let url = module_specifier.as_url(); // TODO(bartlomieju): fix this hack in ModuleSpecifier if url.scheme() == "file" { let a = deno_fs::normalize_path(&url.to_file_path().unwrap()); let u = Url::from_file_path(a).unwrap(); - module_name = ModuleSpecifier::from(u) + module_specifier = ModuleSpecifier::from(u) } debug!(">>>>> bundle START"); let compiler_config = tsc::CompilerConfig::load(flags.config_path.clone())?; - let maybe_import_map = match flags.import_map_path.as_ref() { - None => None, - Some(file_path) => { - if !flags.unstable { - exit_unstable("--importmap") - } - Some(ImportMap::load(file_path)?) - } - }; - let global_state = GlobalState::new(flags)?; - let bundle_result = tsc::bundle( + info!("Bundling {}", module_specifier.to_string()); + + let output = tsc::bundle( &global_state, compiler_config, - module_name, - maybe_import_map, - out_file, + module_specifier, + global_state.maybe_import_map.clone(), global_state.flags.unstable, ) - .await; + .await?; debug!(">>>>> bundle END"); - bundle_result + + let output_string = fmt::format_text(&output)?; + + if let Some(out_file_) = out_file.as_ref() { + info!("Emitting bundle to {:?}", out_file_); + let output_bytes = output_string.as_bytes(); + let output_len = output_bytes.len(); + deno_fs::write_file(out_file_, output_bytes, 0o666)?; + // TODO(bartlomieju): add "humanFileSize" method + info!("{} bytes emitted.", output_len); + } else { + println!("{}", output_string); + } + + Ok(()) } async fn doc_command( |