2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-12-31 16:43:54 -05:00
|
|
|
use crate::import_map::ImportMap;
|
2020-11-02 14:41:20 -05:00
|
|
|
use crate::module_graph::BundleType;
|
|
|
|
use crate::module_graph::EmitOptions;
|
|
|
|
use crate::module_graph::GraphBuilder;
|
2020-12-11 12:49:26 -05:00
|
|
|
use crate::program_state::ProgramState;
|
2020-11-01 21:51:56 -05:00
|
|
|
use crate::specifier_handler::FetchHandler;
|
|
|
|
use crate::specifier_handler::MemoryHandler;
|
|
|
|
use crate::specifier_handler::SpecifierHandler;
|
|
|
|
|
2020-12-31 16:43:54 -05:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-11-01 21:51:56 -05:00
|
|
|
use deno_core::error::Context;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2020-10-26 09:03:03 -04:00
|
|
|
use deno_core::serde_json::json;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json::Value;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-11-01 21:51:56 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-29 23:17:17 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::collections::HashMap;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-12-29 23:17:17 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2020-01-21 11:50:06 -05:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
2020-12-31 16:43:54 -05:00
|
|
|
super::reg_json_async(rt, "op_emit", op_emit);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
2020-12-31 16:43:54 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
enum RuntimeBundleType {
|
|
|
|
#[serde(rename = "esm")]
|
|
|
|
Esm,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
2020-01-21 11:50:06 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-12-31 16:43:54 -05:00
|
|
|
struct EmitArgs {
|
|
|
|
bundle: Option<RuntimeBundleType>,
|
|
|
|
check: Option<bool>,
|
|
|
|
compiler_options: Option<HashMap<String, Value>>,
|
|
|
|
import_map: Option<Value>,
|
|
|
|
import_map_path: Option<String>,
|
|
|
|
root_specifier: String,
|
2020-01-21 11:50:06 -05:00
|
|
|
sources: Option<HashMap<String, String>>,
|
|
|
|
}
|
|
|
|
|
2020-12-31 16:43:54 -05:00
|
|
|
async fn op_emit(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-01-21 11:50:06 -05:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_data: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-12-31 16:43:54 -05:00
|
|
|
deno_runtime::ops::check_unstable2(&state, "Deno.emit");
|
|
|
|
let args: EmitArgs = serde_json::from_value(args)?;
|
2020-12-11 12:49:26 -05:00
|
|
|
let program_state = state.borrow().borrow::<Arc<ProgramState>>().clone();
|
2020-11-01 21:51:56 -05:00
|
|
|
let runtime_permissions = {
|
2020-09-19 19:17:35 -04:00
|
|
|
let state = state.borrow();
|
|
|
|
state.borrow::<Permissions>().clone()
|
|
|
|
};
|
2020-12-31 16:43:54 -05:00
|
|
|
// when we are actually resolving modules without provided sources, we should
|
|
|
|
// treat the root module as a dynamic import so that runtime permissions are
|
|
|
|
// applied.
|
|
|
|
let mut is_dynamic = false;
|
2020-12-29 23:17:17 -05:00
|
|
|
let handler: Arc<Mutex<dyn SpecifierHandler>> =
|
2020-11-01 21:51:56 -05:00
|
|
|
if let Some(sources) = args.sources {
|
2020-12-31 16:43:54 -05:00
|
|
|
is_dynamic = true;
|
2020-12-29 23:17:17 -05:00
|
|
|
Arc::new(Mutex::new(MemoryHandler::new(sources)))
|
2020-11-01 21:51:56 -05:00
|
|
|
} else {
|
2020-12-29 23:17:17 -05:00
|
|
|
Arc::new(Mutex::new(FetchHandler::new(
|
2020-11-01 21:51:56 -05:00
|
|
|
&program_state,
|
|
|
|
runtime_permissions,
|
|
|
|
)?))
|
|
|
|
};
|
2020-12-31 16:43:54 -05:00
|
|
|
let maybe_import_map = if let Some(import_map_str) = args.import_map_path {
|
|
|
|
let import_map_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&import_map_str).context(
|
|
|
|
format!("Bad file path (\"{}\") for import map.", import_map_str),
|
|
|
|
)?;
|
|
|
|
let import_map_url = import_map_specifier.as_url();
|
|
|
|
let import_map = if let Some(value) = args.import_map {
|
|
|
|
ImportMap::from_json(&import_map_url.to_string(), &value.to_string())?
|
|
|
|
} else {
|
|
|
|
ImportMap::load(&import_map_str)?
|
|
|
|
};
|
|
|
|
Some(import_map)
|
|
|
|
} else if args.import_map.is_some() {
|
|
|
|
return Err(generic_error("An importMap was specified, but no importMapPath was provided, which is required."));
|
2020-08-28 11:08:24 -04:00
|
|
|
} else {
|
2020-12-31 16:43:54 -05:00
|
|
|
None
|
|
|
|
};
|
|
|
|
let mut builder = GraphBuilder::new(handler, maybe_import_map, None);
|
|
|
|
let root_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&args.root_specifier)?;
|
|
|
|
builder.add(&root_specifier, is_dynamic).await?;
|
|
|
|
let bundle_type = match args.bundle {
|
|
|
|
Some(RuntimeBundleType::Esm) => BundleType::Esm,
|
|
|
|
_ => BundleType::None,
|
2020-08-28 11:08:24 -04:00
|
|
|
};
|
2020-12-31 16:43:54 -05:00
|
|
|
let graph = builder.get_graph();
|
2020-11-01 21:51:56 -05:00
|
|
|
let debug = program_state.flags.log_level == Some(log::Level::Debug);
|
2020-12-31 16:43:54 -05:00
|
|
|
let (files, result_info) = graph.emit(EmitOptions {
|
2020-11-01 21:51:56 -05:00
|
|
|
bundle_type,
|
2020-12-31 16:43:54 -05:00
|
|
|
check: args.check.unwrap_or(true),
|
2020-11-01 21:51:56 -05:00
|
|
|
debug,
|
2020-12-31 16:43:54 -05:00
|
|
|
maybe_user_config: args.compiler_options,
|
2020-11-01 21:51:56 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(json!({
|
|
|
|
"diagnostics": result_info.diagnostics,
|
2020-12-31 16:43:54 -05:00
|
|
|
"files": files,
|
|
|
|
"ignoredOptions": result_info.maybe_ignored_options,
|
|
|
|
"stats": result_info.stats,
|
2020-11-01 21:51:56 -05:00
|
|
|
}))
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|