Skip to content

Commit

Permalink
update workflow. fix #41
Browse files Browse the repository at this point in the history
  • Loading branch information
matubu committed Sep 10, 2024
1 parent bcac4f1 commit 43eaa7a
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions .github/workflows/build.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
#!/bin/bash

set -e

in_dir=./src
out_dir=./dist

esbuild_system=esbuild-linux-64
[ "$(uname)" == "Darwin" ] && esbuild_system=esbuild-darwin-64

esbuild_version=0.14.28
esbuild=$esbuild_system-$esbuild_version
esbuild_bin=./package/bin/esbuild
esbuild_options="--bundle --minify"

minify_file () {
# arguments:
# 1- input file
# minify a file from in_dir/... to out_dir/...

set -e

minify_js () {
file=$1
out=${file/${in_dir}/${out_dir}}
out=${out/.ts/.js}
mkdir -p $(dirname $out)
$esbuild_bin $file $esbuild_options --format=esm --target=es2019 --outfile=$out
npx esbuild $file $esbuild_options --format=esm --target=es2020 --outfile=$out

[[ $file == *.css ]] && return
node=${file/${in_dir}/"${out_dir}/node"}
mkdir -p $(dirname $node)
$esbuild_bin $file $esbuild_options --format=cjs --platform=node --target=node12 --outfile=$node
npx esbuild $file $esbuild_options --format=cjs --platform=node --target=node12 --outfile=$node
}

[ ! -f $esbuild.tgz ] && curl -O https://registry.npmjs.org/$esbuild_system/-/$esbuild.tgz
[ ! -f $esbuild_bin ] && tar xf ./$esbuild.tgz

rm -rf $out_dir

for file in $(find $in_dir \( -name "*.js" -o -name "*.css" \) -type f); do
minify_file $file &
for file in $(find $in_dir \( -name "*.ts" \) -type f); do
minify_js $file &
done

minify_css () {
file=$1

out=${file/${in_dir}/${out_dir}}
mkdir -p $(dirname $out)

# need to run two times for rules to be properly merged together
npx lightningcss --minify --bundle --targets '>= 2%' $file -o $out
npx lightningcss --minify --bundle --targets '>= 2%' $out -o $out
}

for file in $(find $in_dir \( -name "*.css" \) -type f); do
minify_css $file &
done


for pid in $(jobs -p); do
wait $pid || (wait && echo -e "\n\033[91mError: could not minify some files\033[0m" && exit 1)
done
Expand All @@ -47,4 +50,4 @@ npx -p typescript tsc --outDir dist/node

echo '{"type": "commonjs"}' > dist/node/package.json

exit 0
exit 0

0 comments on commit 43eaa7a

Please sign in to comment.