pages/bin/deploy
George 64cf57fab3
Added support to PurgeCSS (#1562)
Tackled #1329 with [PurgeCSS](https://purgecss.com/).

Being talking with @varuniyer about using
[jekyll-uncss](https://github.com/episource/jekyll-uncss) to reduce css
file sizes by ditching unused classes. This approach have 3 main
problems:

1 - have some limitations as pointed
[here](https://github.com/alshedivat/al-folio/issues/1329#issuecomment-1546517327)
2 - last update to
[jekyll-uncss](https://github.com/episource/jekyll-uncss) was about 3
years ago, so it might have a few issues
3 - [uncss](https://github.com/uncss/uncss) haven't seem a new release
in a while, currently [lacking
maintenance](https://github.com/uncss/uncss/issues/459), and using some
deprecated libraries as seem here:

```
npm install -g uncss
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated w3c-hr-time@1.0.2: Use your platform's native performance.now() and performance.timeOrigin.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
```

I thought about giving PurgeCSS a go, since it has been more [actively
maintaned](https://github.com/FullHuman/purgecss), but
[jekyll-purgecss](https://github.com/mhanberg/jekyll-purgecss) haven't.

For this, I needed to change to use some local libraries instead of
getting them via CDN. The good news is that it is quite effective in
reducing css file sizes. Comparing dir sizes with `du -hs
_site/assets/css/`:

| current | minify | PurgeCSS | PurgeCSS + minify |
| ------- | ------ | -------- | ----------------- |
| 1,1M    | 988K   | 456K     | 420K              |

---------

Signed-off-by: George Araujo <george.gcac@gmail.com>
2023-09-28 14:22:42 -03:00

120 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run this script to deploy the app to Github Pages
# Parse cmd arguments
SRC_BRANCH="master"
DEPLOY_BRANCH="gh-pages"
USAGE_MSG="usage: deploy [-h|--help] [-u|--user] [-s|--src SRC_BRANCH] [-d|--deploy DEPLOY_BRANCH] [--verbose] [--no-push]"
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
echo $USAGE_MSG
exit 0
;;
-u|--user)
;;
-s|--src)
SRC_BRANCH="$2"
shift
;;
-d|--deploy)
DEPLOY_BRANCH="$2"
shift
;;
--verbose)
set -x
;;
--no-push)
NO_PUSH="--no-push"
;;
*)
echo "Option $1 is unknown." >&2
echo $USAGE_MSG >&2
exit 1
;;
esac
shift
done
# Exit if any subcommand fails
set -e
echo "Deploying..."
echo "Source branch: $SRC_BRANCH"
echo "Deploy branch: $DEPLOY_BRANCH"
read -r -p "Do you want to proceed? [y/N] " response
if [[ ! $response =~ ^([yY][eE][sS]|[yY])+$ ]]
then
echo "Aborting."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
# Check if there are any uncommitted changes
if ! git diff-index --quiet HEAD --; then
echo "Changes to the following files are uncommitted:"
git diff-index --name-only HEAD --
echo "Please commit the changes before proceeding."
echo "Aborting."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
# Check if there are any untracked files
if ! test -z "$(git ls-files --exclude-standard --others)"; then
echo "There are untracked files:"
git ls-files --exclude-standard --others
echo "Please commit those files or stash them before proceeding."
echo "Aborting."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
# Switch to source branch (creates it if necessary from the current branch)
if [ `git branch | grep $SRC_BRANCH | tr ' ' '\n' | tail -1` ]
then
git checkout $SRC_BRANCH
else
git checkout -b $SRC_BRANCH
fi
# Checkout DEPLOY_BRANCH branch
if [ `git branch | grep $DEPLOY_BRANCH` ]
then
git branch -D $DEPLOY_BRANCH
fi
git checkout -b $DEPLOY_BRANCH
# Export JEKYLL_ENV=production
export JEKYLL_ENV=production
# Build site
bundle exec jekyll build --lsi
# Purge unused css
purgecss -c purgecss.config.js
# Delete and move files
find . -maxdepth 1 ! -name '_site' ! -name '.git' ! -name 'CNAME' ! -name '.gitignore' -exec rm -rf {} \;
mv _site/* .
rm -R _site/
# Create `.nojekyll` file (bypass GitHub Pages Jekyll processing)
touch .nojekyll
# Push to DEPLOY_BRANCH
git add -fA
git commit --allow-empty -m "$(git log -1 --pretty=%B) [ci skip]"
[[ ${NO_PUSH} ]] || git push -f -q origin $DEPLOY_BRANCH
# Move back to SRC_BRANCH
git checkout $SRC_BRANCH
echo "Deployed successfully!"
exit 0