2022-02-26 09:41:14 +03:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2021-10-25 10:09:00 +03:00
|
|
|
extern crate swc_node_base;
|
|
|
|
|
2022-01-04 23:12:52 +03:00
|
|
|
use std::{env::args, fs, path::Path};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
use swc_common::{sync::Lrc, Mark, SourceMap};
|
2021-09-16 07:23:33 +03:00
|
|
|
use swc_ecma_codegen::text_writer::JsWriter;
|
|
|
|
use swc_ecma_minifier::{
|
|
|
|
optimize,
|
2022-02-19 13:31:54 +03:00
|
|
|
option::{ExtraOptions, MangleOptions, MinifyOptions},
|
2021-09-16 07:23:33 +03:00
|
|
|
};
|
2022-02-26 09:41:14 +03:00
|
|
|
use swc_ecma_parser::parse_file_as_module;
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::{fixer::fixer, resolver};
|
2021-09-16 07:23:33 +03:00
|
|
|
use swc_ecma_visit::FoldWith;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let file = args().nth(1).expect("should provide a path to file");
|
|
|
|
|
|
|
|
eprintln!("File: {}", file);
|
|
|
|
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
let fm = cm.load_file(Path::new(&file)).expect("failed to load file");
|
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2021-09-16 07:23:33 +03:00
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let program = parse_file_as_module(
|
|
|
|
&fm,
|
2021-09-16 07:23:33 +03:00
|
|
|
Default::default(),
|
|
|
|
Default::default(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.map_err(|err| {
|
|
|
|
err.into_diagnostic(&handler).emit();
|
|
|
|
})
|
2022-04-26 10:38:50 +03:00
|
|
|
.map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false)))
|
2022-02-26 09:41:14 +03:00
|
|
|
.unwrap();
|
2021-09-16 07:23:33 +03:00
|
|
|
|
|
|
|
let output = optimize(
|
|
|
|
program,
|
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
compress: Some(Default::default()),
|
2022-02-19 13:31:54 +03:00
|
|
|
mangle: Some(MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-09-16 07:23:33 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
2022-04-26 10:38:50 +03:00
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
|
|
|
},
|
2021-09-16 07:23:33 +03:00
|
|
|
);
|
|
|
|
|
2021-10-25 10:09:00 +03:00
|
|
|
let output = output.fold_with(&mut fixer(None));
|
2021-09-16 07:23:33 +03:00
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
let code = print(cm, &[output], true);
|
2022-01-04 23:12:52 +03:00
|
|
|
|
|
|
|
fs::write("output.js", code.as_bytes()).expect("failed to write output");
|
2021-09-16 07:23:33 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print<N: swc_ecma_codegen::Node>(cm: Lrc<SourceMap>, nodes: &[N], minify: bool) -> String {
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut emitter = swc_ecma_codegen::Emitter {
|
2022-05-15 08:07:21 +03:00
|
|
|
cfg: swc_ecma_codegen::Config {
|
|
|
|
minify,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2021-09-16 07:23:33 +03:00
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
2022-01-13 17:06:11 +03:00
|
|
|
wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)),
|
2021-09-16 07:23:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for n in nodes {
|
|
|
|
n.emit_with(&mut emitter).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
}
|