2021-10-15 06:49:37 +03:00
|
|
|
/// Use memory allocator
|
|
|
|
extern crate swc_node_base;
|
|
|
|
|
|
|
|
use std::{env, fs, path::Path, time::Instant};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-10-15 06:49:37 +03:00
|
|
|
use swc_common::input::SourceFileInput;
|
|
|
|
use swc_ecma_ast::*;
|
|
|
|
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
|
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
|
|
|
|
2022-03-14 06:52:59 +03:00
|
|
|
fn parse_and_gen(entry: &Path) {
|
2021-10-15 06:49:37 +03:00
|
|
|
testing::run_test2(false, |cm, _| {
|
|
|
|
let fm = cm.load_file(entry).unwrap();
|
|
|
|
|
|
|
|
let lexer = Lexer::new(
|
|
|
|
Syntax::Typescript(Default::default()),
|
|
|
|
EsVersion::latest(),
|
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let m = parser
|
|
|
|
.parse_module()
|
|
|
|
.expect("failed to parse input as a module");
|
|
|
|
|
2022-03-14 06:52:59 +03:00
|
|
|
let code = {
|
|
|
|
let mut buf = vec![];
|
2021-10-15 06:49:37 +03:00
|
|
|
|
2022-03-14 06:52:59 +03:00
|
|
|
{
|
|
|
|
let mut emitter = Emitter {
|
|
|
|
cfg: swc_ecma_codegen::Config {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
|
|
|
wr: JsWriter::new(cm, "\n", &mut buf, None),
|
|
|
|
};
|
2021-10-15 06:49:37 +03:00
|
|
|
|
2022-03-14 06:52:59 +03:00
|
|
|
emitter.emit_module(&m).unwrap();
|
|
|
|
}
|
2021-10-15 06:49:37 +03:00
|
|
|
|
2022-03-14 06:52:59 +03:00
|
|
|
String::from_utf8_lossy(&buf).to_string()
|
|
|
|
};
|
2021-10-15 06:49:37 +03:00
|
|
|
|
2023-03-14 07:56:21 +03:00
|
|
|
fs::write("output.js", code).unwrap();
|
2021-10-15 06:49:37 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("failed to process a module");
|
|
|
|
}
|
|
|
|
|
2022-04-11 21:45:58 +03:00
|
|
|
/// Usage: ./scripts/instruments path/to/input/file
|
2021-10-15 06:49:37 +03:00
|
|
|
fn main() {
|
|
|
|
let main_file = env::args().nth(1).unwrap();
|
|
|
|
|
|
|
|
let start = Instant::now();
|
2022-03-14 06:52:59 +03:00
|
|
|
parse_and_gen(Path::new(&main_file));
|
2021-10-15 06:49:37 +03:00
|
|
|
let dur = start.elapsed();
|
|
|
|
println!("Took {:?}", dur);
|
|
|
|
}
|