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
use emit::emit_js;
use error::SyntaxError;
use lex::Lexer;
use minify::minify_js;
use parse::{parser::Parser, toplevel::parse_top_level};
use std::io::{self, Write};

mod ast;
mod char;
mod emit;
mod error;
mod lex;
mod minify;
mod num;
mod operator;
mod parse;
#[cfg(test)]
mod serialise;
mod source;
mod symbol;
mod token;
mod update;
mod util;

#[derive(Debug)]
pub enum MinifyError {
    Syntax(SyntaxError),
    IO(io::Error),
}

/// Minifies UTF-8 JavaScript code, represented as an array of bytes.
///
/// # Arguments
///
/// * `source` - A vector of bytes representing the source code to minify.
/// * `output` - Destination to write minified output JavaScript code.
///
/// # Examples
///
/// ```
/// use std::io::BufWriter;
/// use minify_js::minify;
///
/// let mut code: &[u8] = b"const main = () => { let my_first_variable = 1; };";
/// let mut out = BufWriter::new(Vec::new());
/// minify(code.to_vec(), &mut out).unwrap();
/// assert_eq!(out.get_ref().as_slice(), b"const main=()=>{let a=1}");
/// ```
pub fn minify<T: Write>(source: Vec<u8>, output: &mut T) -> Result<(), MinifyError> {
    let lexer = Lexer::new(source);
    let mut parser = Parser::new(lexer);
    let parsed = parse_top_level(&mut parser).map_err(|err| MinifyError::Syntax(err))?;
    let (mut node_map, mut scope_map) = parser.take();
    minify_js(&mut scope_map, &mut node_map, parsed.top_level_node_id);
    emit_js(output, &node_map, parsed.top_level_node_id).map_err(|err| MinifyError::IO(err))?;
    Ok(())
}