2020-01-21 11:50:06 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-10-26 09:03:03 -04:00
|
|
|
use crate::ast;
|
|
|
|
use crate::colors;
|
|
|
|
use crate::media_type::MediaType;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-06-19 06:27:15 -04:00
|
|
|
use crate::tsc::runtime_bundle;
|
2020-05-08 10:18:00 -04:00
|
|
|
use crate::tsc::runtime_compile;
|
2020-10-26 09:03:03 -04:00
|
|
|
use crate::tsc_config;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
2020-10-26 09:03:03 -04:00
|
|
|
use deno_core::serde::Serialize;
|
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-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
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-01-21 11:50:06 -05:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_async(rt, "op_compile", op_compile);
|
|
|
|
super::reg_json_async(rt, "op_transpile", op_transpile);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CompileArgs {
|
|
|
|
root_name: String,
|
|
|
|
sources: Option<HashMap<String, String>>,
|
|
|
|
bundle: bool,
|
|
|
|
options: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_compile(
|
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-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.compile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: CompileArgs = serde_json::from_value(args)?;
|
2020-09-26 14:26:51 -04:00
|
|
|
let cli_state = super::global_state2(&state);
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = cli_state.clone();
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = {
|
|
|
|
let state = state.borrow();
|
|
|
|
state.borrow::<Permissions>().clone()
|
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
let fut = if args.bundle {
|
|
|
|
runtime_bundle(
|
2020-10-13 07:35:35 -04:00
|
|
|
&program_state,
|
2020-08-28 11:08:24 -04:00
|
|
|
permissions,
|
|
|
|
&args.root_name,
|
|
|
|
&args.sources,
|
|
|
|
&args.options,
|
|
|
|
)
|
|
|
|
.boxed_local()
|
|
|
|
} else {
|
|
|
|
runtime_compile(
|
2020-10-13 07:35:35 -04:00
|
|
|
&program_state,
|
2020-08-28 11:08:24 -04:00
|
|
|
permissions,
|
|
|
|
&args.root_name,
|
|
|
|
&args.sources,
|
|
|
|
&args.options,
|
|
|
|
)
|
|
|
|
.boxed_local()
|
|
|
|
};
|
|
|
|
let result = fut.await?;
|
|
|
|
Ok(result)
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct TranspileArgs {
|
|
|
|
sources: HashMap<String, String>,
|
|
|
|
options: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:03:03 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct RuntimeTranspileEmit {
|
|
|
|
source: String,
|
|
|
|
map: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_transpile(
|
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-10-26 09:03:03 -04:00
|
|
|
super::check_unstable2(&state, "Deno.transpileOnly");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: TranspileArgs = serde_json::from_value(args)?;
|
2020-10-26 09:03:03 -04:00
|
|
|
|
|
|
|
let mut compiler_options = tsc_config::TsConfig::new(json!({
|
|
|
|
"checkJs": true,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
|
|
|
"inlineSourceMap": false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
let user_options: HashMap<String, Value> = if let Some(options) = args.options
|
|
|
|
{
|
|
|
|
serde_json::from_str(&options)?
|
|
|
|
} else {
|
|
|
|
HashMap::new()
|
|
|
|
};
|
|
|
|
let maybe_ignored_options =
|
|
|
|
compiler_options.merge_user_config(&user_options)?;
|
|
|
|
// TODO(@kitsonk) these really should just be passed back to the caller
|
|
|
|
if let Some(ignored_options) = maybe_ignored_options {
|
|
|
|
info!("{}: {}", colors::yellow("warning"), ignored_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
let emit_options: ast::EmitOptions = compiler_options.into();
|
|
|
|
let mut emit_map = HashMap::new();
|
|
|
|
|
|
|
|
for (specifier, source) in args.sources {
|
|
|
|
let media_type = MediaType::from(&specifier);
|
|
|
|
let parsed_module = ast::parse(&specifier, &source, &media_type)?;
|
|
|
|
let (source, maybe_source_map) = parsed_module.transpile(&emit_options)?;
|
|
|
|
|
|
|
|
emit_map.insert(
|
|
|
|
specifier.to_string(),
|
|
|
|
RuntimeTranspileEmit {
|
|
|
|
source,
|
|
|
|
map: maybe_source_map,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let result = serde_json::to_value(emit_map)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(result)
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|