Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: TS compiler and module graph #5817

Merged
merged 20 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 76 additions & 45 deletions cli/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::deno_dir;
use crate::file_fetcher::SourceFileFetcher;
use crate::flags;
use crate::http_cache;
use crate::import_map::ImportMap;
use crate::lockfile::Lockfile;
use crate::module_graph::ModuleGraphLoader;
use crate::msg;
use crate::permissions::Permissions;
use crate::tsc::CompiledModule;
Expand Down Expand Up @@ -89,73 +91,102 @@ impl GlobalState {
Ok(GlobalState(Arc::new(inner)))
}

pub async fn fetch_compiled_module(
pub async fn prepare_module_load(
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
&self,
module_specifier: ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
target_lib: TargetLib,
permissions: Permissions,
is_dyn_import: bool,
maybe_import_map: Option<ImportMap>,
) -> Result<(), ErrBox> {
let module_specifier = module_specifier.clone();

// TODO(ry) Try to lift compile_lock as high up in the call stack for
// sanity.
let compile_lock = self.compile_lock.lock().await;

let mut module_graph_loader = ModuleGraphLoader::new(
self.file_fetcher.clone(),
maybe_import_map,
permissions.clone(),
is_dyn_import,
false,
);
module_graph_loader
.add_to_graph(&module_specifier, maybe_referrer)
.await?;
let module_graph = module_graph_loader.get_graph();

let out = self
.file_fetcher
.fetch_cached_source_file(&module_specifier, permissions.clone())
.expect("Source file not found");

// Check if we need to compile files
let needs_compilation = match out.media_type {
msg::MediaType::TypeScript
| msg::MediaType::TSX
| msg::MediaType::JSX => true,
msg::MediaType::JavaScript => self.ts_compiler.compile_js,
_ => false,
};

if needs_compilation {
self
.ts_compiler
.compile_module_graph(
self.clone(),
&out,
target_lib,
permissions,
module_graph,
)
.await?;
}
ry marked this conversation as resolved.
Show resolved Hide resolved

drop(compile_lock);

Ok(())
}

// TODO(bartlomieju): this method doesn't need to be async anymore
pub async fn fetch_compiled_module(
&self,
module_specifier: ModuleSpecifier,
_maybe_referrer: Option<ModuleSpecifier>,
) -> Result<CompiledModule, ErrBox> {
let state1 = self.clone();
let state2 = self.clone();
let module_specifier = module_specifier.clone();

let out = self
.file_fetcher
.fetch_source_file(&module_specifier, maybe_referrer, permissions.clone())
.await?;
.fetch_cached_source_file(&module_specifier, Permissions::allow_all())
.expect("Cached source file doesn't exist");

// TODO(ry) Try to lift compile_lock as high up in the call stack for
// sanity.
let compile_lock = self.compile_lock.lock().await;

let compiled_module = match out.media_type {
// Check if we need to compile files
let needs_compilation = match out.media_type {
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
msg::MediaType::TypeScript
| msg::MediaType::TSX
| msg::MediaType::JSX => {
state1
.ts_compiler
.compile(state1.clone(), &out, target_lib, permissions, is_dyn_import)
.await
}
msg::MediaType::JavaScript => {
if state1.ts_compiler.compile_js {
state2
.ts_compiler
.compile(
state1.clone(),
&out,
target_lib,
permissions,
is_dyn_import,
)
.await
} else {
if let Some(types_url) = out.types_url.clone() {
let types_specifier = ModuleSpecifier::from(types_url);
state1
.file_fetcher
.fetch_source_file(
&types_specifier,
Some(module_specifier.clone()),
permissions.clone(),
)
.await
.ok();
};

Ok(CompiledModule {
code: String::from_utf8(out.source_code.clone())?,
name: out.url.to_string(),
})
}
}
_ => Ok(CompiledModule {
| msg::MediaType::JSX => true,
msg::MediaType::JavaScript => self.ts_compiler.compile_js,
_ => false,
};

let compiled_module = if needs_compilation {
state1.ts_compiler.get_compiled_module(&out.url)?
} else {
CompiledModule {
code: String::from_utf8(out.source_code.clone())?,
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
name: out.url.to_string(),
}),
}?;
}
};

drop(compile_lock);

if let Some(ref lockfile) = state2.lockfile {
Expand Down
55 changes: 44 additions & 11 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,29 @@ async fn print_file_info(
);

let module_specifier_ = module_specifier.clone();
let maybe_import_map = match global_state.flags.import_map_path.as_ref() {
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
None => None,
Some(file_path) => {
if !global_state.flags.unstable {
exit_unstable("--importmap")
}
Some(ImportMap::load(file_path)?)
}
};
global_state
.clone()
.fetch_compiled_module(
module_specifier_,
.prepare_module_load(
module_specifier_.clone(),
None,
TargetLib::Main,
Permissions::allow_all(),
false,
maybe_import_map,
)
.await?;
global_state
.clone()
.fetch_compiled_module(module_specifier_, None)
.await?;

if out.media_type == msg::MediaType::TypeScript
|| (out.media_type == msg::MediaType::JavaScript
Expand Down Expand Up @@ -388,14 +401,15 @@ 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");
Expand All @@ -413,18 +427,37 @@ async fn bundle_command(

let global_state = GlobalState::new(flags)?;

let bundle_result = tsc::bundle(
// TODO(bartlomieju): probably should respect --quiet flag
eprintln!("Bundling {}", module_specifier.to_string());

let output = tsc::bundle(
&global_state,
compiler_config,
module_name,
module_specifier,
maybe_import_map,
out_file,
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() {
eprintln!("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): do we really need to show this info? (it doesn't respect --quiet flag)
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
// TODO(bartlomieju): add "humanFileSize" method
eprintln!("{} bytes emitted.", output_len);
} else {
println!("{}", output_string);
}

Ok(())
}

async fn doc_command(
Expand Down
15 changes: 6 additions & 9 deletions cli/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::op_error::OpError;
use crate::permissions::Permissions;
use crate::swc_util::analyze_dependencies_and_references;
use crate::swc_util::TsReferenceKind;
use crate::tsc::get_available_libs;
use crate::tsc::AVAILABLE_LIBS;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
use futures::stream::FuturesUnordered;
Expand Down Expand Up @@ -138,8 +138,9 @@ impl ModuleGraphLoader {
pub async fn add_to_graph(
&mut self,
specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
) -> Result<(), ErrBox> {
self.download_module(specifier.clone(), None)?;
self.download_module(specifier.clone(), maybe_referrer)?;
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved

loop {
let (specifier, source_file) =
Expand Down Expand Up @@ -238,10 +239,8 @@ impl ModuleGraphLoader {
imports.push(import_descriptor);
}

let available_libs = get_available_libs();

for ref_desc in ref_descs {
if available_libs.contains(&ref_desc.specifier) {
if AVAILABLE_LIBS.contains(&ref_desc.specifier.as_str()) {
continue;
}

Expand Down Expand Up @@ -465,10 +464,8 @@ impl ModuleGraphLoader {
imports.push(import_descriptor);
}

let available_libs = get_available_libs();

for ref_desc in ref_descs {
if available_libs.contains(&ref_desc.specifier) {
if AVAILABLE_LIBS.contains(&ref_desc.specifier.as_str()) {
continue;
}

Expand Down Expand Up @@ -537,7 +534,7 @@ mod tests {
false,
false,
);
graph_loader.add_to_graph(&module_specifier).await?;
graph_loader.add_to_graph(&module_specifier, None).await?;
Ok(graph_loader.get_graph())
}

Expand Down
Loading