feat(dbg-swc): Add a command to diff semantics (#4671)

This commit is contained in:
Donny/강동윤 2022-05-16 02:05:39 +09:00 committed by GitHub
parent e3ef5f8578
commit e3cda264bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 28 deletions

View File

@ -2,4 +2,4 @@
set -eu set -eu
# cargo run -- minify ensure-size --no-terser $1 > list.txt # cargo run -- minify ensure-size --no-terser $1 > list.txt
cat list.txt | xargs -L 1 cargo run --release -- minify reduce cat list.txt | xargs -L 1 cargo run --release -- minify reduce --mode semantics

View File

@ -63,7 +63,7 @@ fn main() -> Result<()> {
let cm = Arc::new(SourceMap::default()); 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( return try_with_handler(
cm.clone(), cm.clone(),
HandlerOpts { HandlerOpts {
@ -75,33 +75,54 @@ fn main() -> Result<()> {
HANDLER.set(handler, || { 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)?; let terser_output = get_terser_output("input.js".as_ref(), true, true)?;
if swc_output.len() > terser_output.len() { if swc_output.len() > terser_output.len() {
return Ok(()); 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")
}) })
}) })
}, },

View File

@ -7,13 +7,22 @@ use std::{
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use clap::Args; use clap::{ArgEnum, Args};
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
use swc_common::SourceMap; use swc_common::SourceMap;
#[derive(Debug, Args)] #[derive(Debug, Args)]
pub struct ReduceCommand { pub struct ReduceCommand {
pub path: PathBuf, pub path: PathBuf,
#[clap(long, arg_enum)]
pub mode: ReduceMode,
}
#[derive(Debug, Clone, Copy, ArgEnum)]
pub enum ReduceMode {
Size,
Semantics,
} }
impl ReduceCommand { impl ReduceCommand {
@ -21,7 +30,14 @@ impl ReduceCommand {
fs::copy(&self.path, "input.js").context("failed to copy")?; fs::copy(&self.path, "input.js").context("failed to copy")?;
let mut c = Command::new("creduce"); 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()?; let exe = current_exe()?;
c.arg(&exe); c.arg(&exe);
c.arg("input.js"); c.arg("input.js");