-
-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathlib.rs
70 lines (59 loc) · 1.93 KB
/
lib.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! ECMAScript Minifier
mod compressor;
mod ctx;
mod keep_var;
mod options;
mod peephole;
#[cfg(test)]
mod tester;
use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_mangler::Mangler;
use oxc_semantic::{SemanticBuilder, Stats};
pub use oxc_mangler::MangleOptions;
pub use crate::{compressor::Compressor, options::CompressOptions};
#[derive(Debug, Clone, Copy)]
pub struct MinifierOptions {
pub mangle: Option<MangleOptions>,
pub compress: Option<CompressOptions>,
}
impl Default for MinifierOptions {
fn default() -> Self {
Self { mangle: Some(MangleOptions::default()), compress: Some(CompressOptions::default()) }
}
}
pub struct MinifierReturn {
pub mangler: Option<Mangler>,
}
pub struct Minifier {
options: MinifierOptions,
}
impl Minifier {
pub fn new(options: MinifierOptions) -> Self {
Self { options }
}
pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) -> MinifierReturn {
let stats = if let Some(compress) = self.options.compress {
let semantic = SemanticBuilder::new().build(program).semantic;
let stats = semantic.stats();
let (symbols, scopes) = semantic.into_symbol_table_and_scope_tree();
Compressor::new(allocator, compress)
.build_with_symbols_and_scopes(symbols, scopes, program);
stats
} else {
Stats::default()
};
let mangler = self.options.mangle.map(|options| {
let semantic = SemanticBuilder::new()
.with_stats(stats)
.with_scope_tree_child_ids(true)
.build(program)
.semantic;
let (symbols, scopes) = semantic.into_symbol_table_and_scope_tree();
Mangler::default()
.with_options(options)
.build_with_symbols_and_scopes(symbols, &scopes, program)
});
MinifierReturn { mangler }
}
}