diff --git a/crates/dbg-swc/scripts/check-size.sh b/crates/dbg-swc/scripts/check-size.sh index f9fdcecd572..67c0a4971e7 100755 --- a/crates/dbg-swc/scripts/check-size.sh +++ b/crates/dbg-swc/scripts/check-size.sh @@ -2,4 +2,4 @@ set -eu # cargo run -- minify ensure-size --no-terser $1 > list.txt -cat list.txt | xargs -L 1 cargo run --release -- minify reduce \ No newline at end of file +cat list.txt | xargs -L 1 cargo run --release -- minify reduce --mode semantics \ No newline at end of file diff --git a/crates/dbg-swc/src/main.rs b/crates/dbg-swc/src/main.rs index f90867b5208..19770368379 100644 --- a/crates/dbg-swc/src/main.rs +++ b/crates/dbg-swc/src/main.rs @@ -63,7 +63,7 @@ fn main() -> Result<()> { let cm = Arc::new(SourceMap::default()); - if env::var("CREDUCE_COMPARE").unwrap_or_default() == "1" { + if let Ok(mode) = env::var("CREDUCE_COMPARE") { return try_with_handler( cm.clone(), HandlerOpts { @@ -75,33 +75,54 @@ fn main() -> Result<()> { HANDLER.set(handler, || { // - let m = get_minified(cm.clone(), "input.js".as_ref(), true, true)?; + if mode == "SIZE" { + let m = get_minified(cm.clone(), "input.js".as_ref(), true, true)?; - let swc_output = print_js(cm.clone(), &m.module, true)?; + let swc_output = print_js(cm.clone(), &m.module, true)?; - let terser_output = get_terser_output("input.js".as_ref(), true, true)?; - if swc_output.len() > terser_output.len() { - return Ok(()); + let terser_output = get_terser_output("input.js".as_ref(), true, true)?; + if swc_output.len() > terser_output.len() { + return Ok(()); + } + + // We only care about length, so we can replace it. + // + // We target es5, but esbuild does not support it + let swc_output = swc_output.replace("\\n", "_"); + + let esbuild_output = get_esbuild_output("input.js".as_ref(), true)?; + + if swc_output.len() > esbuild_output.len() { + return Ok(()); + } + + println!( + "swc size = {}, esbuild size = {}", + swc_output.len(), + esbuild_output.len() + ); + + bail!("We don't care about this file") + } else if mode == "SEMANTICS" { + let m = get_minified(cm.clone(), "input.js".as_ref(), true, false)?; + + let swc_output = print_js(cm.clone(), &m.module, true)?; + + let terser_output = + get_terser_output("input.js".as_ref(), true, false)?; + + if swc_output.len() <= 1000 || terser_output.len() <= 1000 { + bail!("We don't care about this file because it's too small") + } + + if swc_output.trim() == terser_output.trim() { + bail!("We don't care about this file") + } + + Ok(()) + } else { + unreachable!("Unknown mode `{}`", mode) } - - // We only care about length, so we can replace it. - // - // We target es5, but esbuild does not support it - let swc_output = swc_output.replace("\\n", "_"); - - let esbuild_output = get_esbuild_output("input.js".as_ref(), true)?; - - if swc_output.len() > esbuild_output.len() { - return Ok(()); - } - - println!( - "swc size = {}, esbuild size = {}", - swc_output.len(), - esbuild_output.len() - ); - - bail!("We don't care about this file") }) }) }, diff --git a/crates/dbg-swc/src/minify/reduce.rs b/crates/dbg-swc/src/minify/reduce.rs index e196fc8bf34..1bf4f76383a 100644 --- a/crates/dbg-swc/src/minify/reduce.rs +++ b/crates/dbg-swc/src/minify/reduce.rs @@ -7,13 +7,22 @@ use std::{ }; use anyhow::{Context, Result}; -use clap::Args; +use clap::{ArgEnum, Args}; use sha1::{Digest, Sha1}; use swc_common::SourceMap; #[derive(Debug, Args)] pub struct ReduceCommand { pub path: PathBuf, + + #[clap(long, arg_enum)] + pub mode: ReduceMode, +} + +#[derive(Debug, Clone, Copy, ArgEnum)] +pub enum ReduceMode { + Size, + Semantics, } impl ReduceCommand { @@ -21,7 +30,14 @@ impl ReduceCommand { fs::copy(&self.path, "input.js").context("failed to copy")?; let mut c = Command::new("creduce"); - c.env("CREDUCE_COMPARE", "1"); + + c.env( + "CREDUCE_COMPARE", + match self.mode { + ReduceMode::Size => "SIZE", + ReduceMode::Semantics => "SEMANTICS", + }, + ); let exe = current_exe()?; c.arg(&exe); c.arg("input.js");