/
transform_error.rs
33 lines (28 loc) · 1.06 KB
/
transform_error.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::sync::Arc;
use anyhow::Context;
use swc::{self, try_with_handler};
use swc_common::{errors::ColorConfig, FileName, SourceMap, GLOBALS};
fn main() {
let cm = Arc::<SourceMap>::default();
let c = swc::Compiler::new(cm.clone());
let output = GLOBALS
.set(&Default::default(), || {
try_with_handler(
cm.clone(),
swc::HandlerOpts {
// Auto is default, but for it's an example.
// You can use the env var named `NO_COLOR` to control this.
color: ColorConfig::Auto,
skip_filename: false,
},
|handler| {
let fm =
cm.new_source_file(FileName::Custom("foo.js".into()), "this ?= foo".into());
c.process_js_file(fm, handler, &Default::default())
.context("failed to process file")
},
)
})
.expect("anyhow will give you good error message");
println!("{}", output.code);
}