From 057fca4196263dcfc05e6161cb727ec4888d7e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 10 Jan 2022 16:59:21 +0900 Subject: [PATCH] chore(es/minifier): Add scripts to extract tests automatically (#3212) --- crates/swc_ecma_minifier/.gitignore | 5 +- .../scripts/_/reduce/compare.js | 2 +- .../scripts/cdnjs/download-all.sh | 9 +++ .../scripts/inputs/remove-useless.sh | 56 +++++++++++++++++++ .../swc_ecma_minifier/scripts/reduce-all.sh | 19 ++++++- crates/swc_ecma_minifier/scripts/reduce.sh | 34 ++++++----- .../compress/fixture/reduced/config.json | 4 ++ 7 files changed, 110 insertions(+), 19 deletions(-) create mode 100755 crates/swc_ecma_minifier/scripts/cdnjs/download-all.sh create mode 100755 crates/swc_ecma_minifier/scripts/inputs/remove-useless.sh create mode 100644 crates/swc_ecma_minifier/tests/compress/fixture/reduced/config.json diff --git a/crates/swc_ecma_minifier/.gitignore b/crates/swc_ecma_minifier/.gitignore index 30500fe8eee..bf66a2080f7 100644 --- a/crates/swc_ecma_minifier/.gitignore +++ b/crates/swc_ecma_minifier/.gitignore @@ -6,4 +6,7 @@ private/ # Used for reducing /input.js /output.js -*.orig \ No newline at end of file +*.orig + + +creduce_bug_* \ No newline at end of file diff --git a/crates/swc_ecma_minifier/scripts/_/reduce/compare.js b/crates/swc_ecma_minifier/scripts/_/reduce/compare.js index 014a037bd1c..2c7e1ed7577 100755 --- a/crates/swc_ecma_minifier/scripts/_/reduce/compare.js +++ b/crates/swc_ecma_minifier/scripts/_/reduce/compare.js @@ -8,7 +8,7 @@ let [ast1, ast2] = process.argv.slice(2) // skip node executable and script filename .map(file => readFileSync(file, 'utf-8')) // read given files as strings .map(code => { - console.log(code); + // console.log(code); return parseModule(code) }); // parse into ASTs diff --git a/crates/swc_ecma_minifier/scripts/cdnjs/download-all.sh b/crates/swc_ecma_minifier/scripts/cdnjs/download-all.sh new file mode 100755 index 00000000000..5f20d35ae65 --- /dev/null +++ b/crates/swc_ecma_minifier/scripts/cdnjs/download-all.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -eu + +# Work in .data directory +cd .data + +git clone https://github.com/cdnjs/cdnjs.git --depth 1 || true + +cd cdnjs \ No newline at end of file diff --git a/crates/swc_ecma_minifier/scripts/inputs/remove-useless.sh b/crates/swc_ecma_minifier/scripts/inputs/remove-useless.sh new file mode 100755 index 00000000000..d967bf8eeba --- /dev/null +++ b/crates/swc_ecma_minifier/scripts/inputs/remove-useless.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +set -eu + +dir="${1-"./inputs/"}" + +echo "Removing useless files in $dir" +find $dir -type f \( \ + -name "*.html" -o \ + -name "*.xml" -o \ + -name "*.aac" -o \ + -name "*.avif" -o \ + -name "*.ogg" -o \ + -name "*.mp3" -o \ + -name "*.mp4" -o \ + -name "*.wav" -o \ + -name "*.bat" -o \ + -name "*.cur" -o \ + -name "*.db" -o \ + -name "*.diff" -o \ + -name "*.donotoptimizepng" -o \ + -name "*.gif" -o \ + -name "*.svg" -o \ + -name "*.css" -o \ + -name "*.png" -o \ + -name "*.json" -o \ + -name "*.woff" -o \ + -name "*.woff2" -o \ + -name "*.otf" -o \ + -name "*.ttf" -o \ + -name "*.eot" -o \ + -name "*.flow" -o \ + -name "*.md" -o \ + -name "*.less" -o \ + -name "*.sass" -o \ + -name "*.scss" -o \ + -name "*.ico" -o \ + -name "*.sh" -o \ + -name "*.jpg" -o \ + -name "*.webp" -o \ + -name "*.jpeg" -o \ + -name "*.gz" -o \ + -name "*.gzip" -o \ + -name "*.zip" -o \ + -name "*.woff" -o \ + -name "*.wasm" -o \ + -name "*.wast" -o \ + -name "*.tsbuildinfo" \ + \) \ + -delete + + +echo "Removing empty directories in $dir" + +find $dir -type d -empty -delete + +echo "Reducing js files in $dir" \ No newline at end of file diff --git a/crates/swc_ecma_minifier/scripts/reduce-all.sh b/crates/swc_ecma_minifier/scripts/reduce-all.sh index bf97d50ba21..793c63a64d5 100755 --- a/crates/swc_ecma_minifier/scripts/reduce-all.sh +++ b/crates/swc_ecma_minifier/scripts/reduce-all.sh @@ -1,7 +1,20 @@ #!/usr/bin/env bash +# +# Usage: ./scrtips/reduce-all.sh path/to/minifier-tasklist +# +# e.g: +# - ./scrtips/reduce-all.sh ~/projects/minifier-tasklist +# - ./scrtips/reduce-all.sh ~/projects/minifier-tasklist/react +# +# set -eu +dir="$1" -find ./inputs/ -type f -name '*.js' -print0 | xargs -0 -I {} sh -c '\ - echo "Processing {}" - ./scripts/reduce.sh {}' \ No newline at end of file +echo "Reducing javascript files in $dir" + +find $dir -type f -name '*.js' -print0 | xargs -0 -P 4 -I {} sh -c './scripts/reduce.sh {}' || true + +echo "Removing empty directories in $dir" + +find $dir -type d -empty -delete diff --git a/crates/swc_ecma_minifier/scripts/reduce.sh b/crates/swc_ecma_minifier/scripts/reduce.sh index a01941f1087..d97f02c8588 100755 --- a/crates/swc_ecma_minifier/scripts/reduce.sh +++ b/crates/swc_ecma_minifier/scripts/reduce.sh @@ -8,32 +8,38 @@ then exit fi -cp $1 input.js - SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) -# Build swc minifier -export MINIFY=$(cargo profile bin-path --example minifier) +echo "Reducing $1" +ls -al $1 -echo "Using $MINIFY" +# Build swc minifier +export MINIFY=$(cargo profile bin-path --release --example minifier) + +wd="$(mktemp -d)" +echo "Using $MINIFY; dir = $wd" + +cp $1 "$wd/input.js" +dir="$(dirname $1)" # Verify that we can run `creduce` $SCRIPT_DIR/_/reduce/compare.sh -creduce "$SCRIPT_DIR/_/reduce/compare.sh" input.js - -REDUCED_SIZE=$(wc -c <"input.js") +(cd $wd && creduce "$SCRIPT_DIR/_/reduce/compare.sh" "$wd/input.js") +REDUCED_SIZE=$(wc -c <"$wd/input.js") +hash=$(sha1sum < "$wd/input.js") +echo "Hash is $hash" + echo "Reduced size is $REDUCED_SIZE bytes" -if [[ "$1" == *"inputs"* && $REDUCED_SIZE -le 3 ]]; then +if [[ $REDUCED_SIZE -le 3 ]]; then echo "Removing $1" - git rm --force $1 - git commit -m 'Remove useless input' - - find ./inputs -type d -empty -delete - + rm -rf $1 ./scripts/_/notify.sh "Removed $1" + (cd $dir && git commit -m "Remove a file as it didn't break anything" $1) else + # mkdir -p "$SCRIPT_DIR/../tests/compress/fixture/reduced/$hash" + # cp "$wd/input.js" "$SCRIPT_DIR/../tests/compress/fixture/reduced/$hash/input.js" ./scripts/_/notify.sh "Found errornous input" fi diff --git a/crates/swc_ecma_minifier/tests/compress/fixture/reduced/config.json b/crates/swc_ecma_minifier/tests/compress/fixture/reduced/config.json new file mode 100644 index 00000000000..d6dd7e6ba8d --- /dev/null +++ b/crates/swc_ecma_minifier/tests/compress/fixture/reduced/config.json @@ -0,0 +1,4 @@ +{ + "defaults": true, + "toplevel": false +}