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-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-10-26 09:03:03 -04:00
|
|
|
use crate::tsc_config;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-12-11 12:49:26 -05:00
|
|
|
use std::sync::Arc;
|
2020-11-01 21:51:56 -05:00
|
|
|
|
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-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-11-01 21:51:56 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
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-01-21 11:50:06 -05:00
|
|
|
let args: CompileArgs = serde_json::from_value(args)?;
|
2020-11-01 21:51:56 -05:00
|
|
|
if args.bundle {
|
2020-12-13 13:45:53 -05:00
|
|
|
deno_runtime::ops::check_unstable2(&state, "Deno.bundle");
|
2020-11-01 21:51:56 -05:00
|
|
|
} else {
|
2020-12-13 13:45:53 -05:00
|
|
|
deno_runtime::ops::check_unstable2(&state, "Deno.compile");
|
2020-11-01 21:51:56 -05:00
|
|
|
}
|
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-11-01 21:51:56 -05:00
|
|
|
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,
|
|
|
|
)?))
|
|
|
|
};
|
2020-11-02 14:41:20 -05:00
|
|
|
let mut builder = GraphBuilder::new(handler, None, None);
|
2020-11-01 21:51:56 -05:00
|
|
|
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
|
2020-08-28 11:08:24 -04:00
|
|
|
} else {
|
2020-11-01 21:51:56 -05:00
|
|
|
BundleType::None
|
2020-08-28 11:08:24 -04:00
|
|
|
};
|
2020-11-01 21:51:56 -05:00
|
|
|
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,
|
|
|
|
}))
|
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-12-13 13:45:53 -05:00
|
|
|
deno_runtime::ops::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
|
|
|
}
|