forked from HSLdevcom/digitransit-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Precompress and optimize static files during build and serve them to …
…the browsers. - optimize png images with pngquant (quality 95) and advpng - optimize svg images with svgo - create precompressed variants of js, css, svg files: gz using zopfli and br using brotli Currently using forked versions of expressjs/send/negotiator while waiting for the PRs to go through: - jshttp/negotiator#49 - pillarjs/send#108
- Loading branch information
Mikko Tiihonen
authored and
Mikko Tiihonen
committed
Nov 23, 2016
1 parent
aeb1502
commit 737e59d
Showing
5 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [ -x "$(command -v brotli)" ]; then | ||
BROTLI=brotli | ||
elif [ -x "$(command -v bro)" ]; then | ||
BROTLI=bro | ||
fi | ||
if [ -x "$(command -v zopfli)" ]; then | ||
ZOPFLI=zopfli | ||
fi | ||
if [ -x "$(command -v advpng)" ]; then | ||
ADVPNG=advpng | ||
fi | ||
|
||
# settings | ||
ZOPFLI_ITERATIONS=15 | ||
BROTLI_LEVEL=20 | ||
PNG_QUALITY=95 | ||
PNG_ZOPFLI_ITERATIONS=15 | ||
SKIP_COMPRESS_REGEXP="\.map$" | ||
|
||
[ -f "$1" ] || exit 1 | ||
|
||
[[ "$1" =~ $SKIP_COMPRESS_REGEXP ]] && exit 0 | ||
|
||
TMPDIR="$(mktemp -d /tmp/precompress-XXXXXXXXXX)" | ||
function cleanup { | ||
rm -rf "$TMPDIR" | ||
} | ||
trap cleanup EXIT | ||
|
||
if [[ "$1" =~ \.png$ ]]; then | ||
TMPFILE="$TMPDIR/image.png" | ||
if pngquant --speed 1 --quality $PNG_QUALITY - < "$1" > "$TMPFILE"; then :; else | ||
[[ $? != 99 ]] && exit 1 | ||
fi | ||
[ -n "$ADVPNG" ] && $ADVPNG --recompress --shrink-insane --iter=$PNG_ZOPFLI_ITERATIONS --quiet "$TMPFILE" || exit 1 | ||
mv "$TMPFILE" "$1" || exit 2 | ||
else | ||
if [[ "$1" =~ \.svg$ ]]; then | ||
export PATH+=:$(dirname "$0")/node_modules/.bin | ||
svgo --multipass --disable removeUselessDefs --disable=cleanupIDs --config '{"useShortTags":false}' --output "$TMPDIR/file.svg" "$1" && | ||
mv "$TMPDIR/file.svg" "$1" || exit 3 | ||
fi | ||
[ -n "$ZOPFLI" ] && $ZOPFLI -i$ZOPFLI_ITERATIONS "$1" -c > "$TMPDIR/file.gz" && mv "$TMPDIR/file.gz" "$1.gz" || exit 4 | ||
[ -n "$BROTLI" ] && $BROTLI --quality $BROTLI_LEVEL --input "$1" --output "$TMPDIR/file.br" && mv "$TMPDIR/file.br" "$1.br" || exit 5 | ||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [ ! -d "$1" ]; then | ||
exit 1 | ||
fi | ||
|
||
if [ $(uname) == Linux ]; then | ||
NUMCPUS=$(grep -c "^processor" /proc/cpuinfo) | ||
else | ||
NUMCPUS=$(sysctl -n hw.ncpu) | ||
fi | ||
|
||
find "$1" -type f -and -not -name "*.gz" -and -not -name "*.br" -print0 | xargs -0 -n1 -P$NUMCPUS nice ./precompress-file.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters