dbg_swc/es/minifier/
compare_opts.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
use std::{path::PathBuf, sync::Arc};

use anyhow::{Context, Result};
use clap::Args;
use swc_common::SourceMap;
use swc_ecma_minifier::option::{CompressOptions, MangleOptions};

use crate::util::{
    all_js_files,
    minifier::{get_minified, get_minified_with_opts},
    print_js,
};

/// [Experimental] Internal command to check the size difference caused by
/// options.
#[derive(Debug, Args)]
pub struct CompareOptsCommand {
    pub path: PathBuf,
}

impl CompareOptsCommand {
    pub fn run(self, cm: Arc<SourceMap>) -> Result<()> {
        let files = all_js_files(&self.path)?;

        let mut default_sum = 0;
        let mut new_sum = 0;

        for file in files {
            let default_record = get_minified(cm.clone(), &file, true, true)?;
            let default_code = print_js(cm.clone(), &default_record.module, true)
                .context("failed to convert ast to code")?;

            eprintln!("default: {} bytes", default_code.as_bytes().len());
            default_sum += default_code.as_bytes().len();

            let new_record = get_minified_with_opts(
                cm.clone(),
                &file,
                Some(CompressOptions {
                    keep_classnames: true,
                    keep_fnames: true,
                    ..Default::default()
                }),
                Some(MangleOptions {
                    keep_class_names: true,
                    keep_fn_names: true,
                    ..Default::default()
                }),
            )?;
            let new_code = print_js(cm.clone(), &new_record.module, true)
                .context("failed to convert ast to code")?;

            eprintln!("new: {} bytes", new_code.as_bytes().len());
            new_sum += new_code.as_bytes().len();
        }

        eprintln!("default (total): {} bytes", default_sum);
        eprintln!("new (total): {} bytes", new_sum);

        Ok(())
    }
}