From 57cad539457dff7fc273bed5ecaf08bd3dc40d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 26 Oct 2020 14:03:03 +0100 Subject: refactor(cli): rewrite Deno.transpileOnly() to use SWC (#8090) Co-authored-by: Kitson Kelly --- cli/ops/runtime_compiler.rs | 59 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 6 deletions(-) (limited to 'cli/ops/runtime_compiler.rs') diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index b01469fa9..5ceb90316 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -1,12 +1,17 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +use crate::ast; +use crate::colors; +use crate::media_type::MediaType; use crate::permissions::Permissions; use crate::tsc::runtime_bundle; use crate::tsc::runtime_compile; -use crate::tsc::runtime_transpile; +use crate::tsc_config; use deno_core::error::AnyError; use deno_core::futures::FutureExt; +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::OpState; @@ -71,16 +76,58 @@ struct TranspileArgs { options: Option, } +#[derive(Debug, Serialize)] +struct RuntimeTranspileEmit { + source: String, + map: Option, +} + async fn op_transpile( state: Rc>, args: Value, _data: BufVec, ) -> Result { - super::check_unstable2(&state, "Deno.transpile"); + super::check_unstable2(&state, "Deno.transpileOnly"); let args: TranspileArgs = serde_json::from_value(args)?; - let cli_state = super::global_state2(&state); - let program_state = cli_state.clone(); - let result = - runtime_transpile(program_state, &args.sources, &args.options).await?; + + 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 = 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)?; Ok(result) } -- cgit v1.2.3