mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 21:24:48 +00:00
refactor(cli): migrate runtime compile/bundle to new infrastructure (#8192)
Fixes #8060
This commit is contained in:
parent
3558769d46
commit
fdcc78500c
23 changed files with 852 additions and 2770 deletions
|
@ -3,17 +3,23 @@
|
|||
use crate::ast;
|
||||
use crate::colors;
|
||||
use crate::media_type::MediaType;
|
||||
use crate::module_graph2::BundleType;
|
||||
use crate::module_graph2::EmitOptions;
|
||||
use crate::module_graph2::GraphBuilder2;
|
||||
use crate::permissions::Permissions;
|
||||
use crate::tsc::runtime_bundle;
|
||||
use crate::tsc::runtime_compile;
|
||||
use crate::specifier_handler::FetchHandler;
|
||||
use crate::specifier_handler::MemoryHandler;
|
||||
use crate::specifier_handler::SpecifierHandler;
|
||||
use crate::tsc_config;
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::futures::FutureExt;
|
||||
use deno_core::error::Context;
|
||||
use deno_core::serde::Serialize;
|
||||
use deno_core::serde_json;
|
||||
use deno_core::serde_json::json;
|
||||
use deno_core::serde_json::Value;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use deno_core::OpState;
|
||||
use serde::Deserialize;
|
||||
use std::cell::RefCell;
|
||||
|
@ -39,35 +45,53 @@ async fn op_compile(
|
|||
args: Value,
|
||||
_data: BufVec,
|
||||
) -> Result<Value, AnyError> {
|
||||
super::check_unstable2(&state, "Deno.compile");
|
||||
let args: CompileArgs = serde_json::from_value(args)?;
|
||||
let cli_state = super::global_state2(&state);
|
||||
let program_state = cli_state.clone();
|
||||
let permissions = {
|
||||
if args.bundle {
|
||||
super::check_unstable2(&state, "Deno.bundle");
|
||||
} else {
|
||||
super::check_unstable2(&state, "Deno.compile");
|
||||
}
|
||||
let program_state = super::global_state2(&state);
|
||||
let runtime_permissions = {
|
||||
let state = state.borrow();
|
||||
state.borrow::<Permissions>().clone()
|
||||
};
|
||||
let fut = if args.bundle {
|
||||
runtime_bundle(
|
||||
&program_state,
|
||||
permissions,
|
||||
&args.root_name,
|
||||
&args.sources,
|
||||
&args.options,
|
||||
)
|
||||
.boxed_local()
|
||||
let handler: Rc<RefCell<dyn SpecifierHandler>> =
|
||||
if let Some(sources) = args.sources {
|
||||
Rc::new(RefCell::new(MemoryHandler::new(sources)))
|
||||
} else {
|
||||
Rc::new(RefCell::new(FetchHandler::new(
|
||||
&program_state,
|
||||
runtime_permissions,
|
||||
)?))
|
||||
};
|
||||
let mut builder = GraphBuilder2::new(handler, None, None);
|
||||
let specifier = ModuleSpecifier::resolve_url_or_path(&args.root_name)
|
||||
.context("The root specifier is invalid.")?;
|
||||
builder.add(&specifier, false).await?;
|
||||
let graph = builder.get_graph();
|
||||
let bundle_type = if args.bundle {
|
||||
BundleType::Esm
|
||||
} else {
|
||||
runtime_compile(
|
||||
&program_state,
|
||||
permissions,
|
||||
&args.root_name,
|
||||
&args.sources,
|
||||
&args.options,
|
||||
)
|
||||
.boxed_local()
|
||||
BundleType::None
|
||||
};
|
||||
let result = fut.await?;
|
||||
Ok(result)
|
||||
let debug = program_state.flags.log_level == Some(log::Level::Debug);
|
||||
let maybe_user_config: Option<HashMap<String, Value>> =
|
||||
if let Some(options) = args.options {
|
||||
Some(serde_json::from_str(&options)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (emitted_files, result_info) = graph.emit(EmitOptions {
|
||||
bundle_type,
|
||||
debug,
|
||||
maybe_user_config,
|
||||
})?;
|
||||
|
||||
Ok(json!({
|
||||
"emittedFiles": emitted_files,
|
||||
"diagnostics": result_info.diagnostics,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue