OK, look, I hate SWC and I really don't want to use it if I can help it, but the other options are worse.
33 lines
1 KiB
Rust
33 lines
1 KiB
Rust
use deno_ast::{parse_module, MediaType, ParseParams, SourceTextInfo};
|
|
use oden_js::{Error, Result};
|
|
|
|
pub fn transpile_to_javascript(path: &str, input: String) -> Result<String> {
|
|
let text_info = SourceTextInfo::new(input.into());
|
|
let parsed_source = parse_module(ParseParams {
|
|
specifier: path.to_string(),
|
|
media_type: MediaType::TypeScript,
|
|
text_info,
|
|
capture_tokens: true,
|
|
maybe_syntax: None,
|
|
scope_analysis: false,
|
|
})
|
|
.map_err(|e| {
|
|
let position = e.display_position();
|
|
Error::ParseError(
|
|
path.into(),
|
|
format!(
|
|
"{} at {}:{}:{}",
|
|
e.message(),
|
|
path,
|
|
position.line_number,
|
|
position.column_number
|
|
),
|
|
)
|
|
})?;
|
|
|
|
let options: deno_ast::EmitOptions = Default::default();
|
|
let transpiled = parsed_source
|
|
.transpile(&options)
|
|
.map_err(|e| Error::ParseError(path.into(), e.to_string()))?;
|
|
return Ok(transpiled.text);
|
|
}
|