Skip to content

Minify assets before deploy

We can minify our assets before the deployment. There's a tool written in Goland called Minify. The folks at Docker use this script:

#!/bin/sh

TARGET="$1"
VER="$2"

if [ -z "$TARGET" -o -z "$VER" ]; then
  echo "Usage: $0 <target> <ver>"
  echo "Either <target> or <ver> is missing. Exiting."
  exit 1
fi

if ! [ -d "$TARGET" ]; then
  echo "Target directory $TARGET does not exist. Exiting."
  exit 1
fi

# Minify assets. This benefits both the compressed, and uncompressed versions
printf "Optimizing "

printf "html..."; /scripts/minify -r --type=html --match=\.html -o ${TARGET}/ ${TARGET} || true
printf "css..." ; /scripts/minify -r --type=css  --match=\.css  -o ${TARGET}/ ${TARGET} || true
printf "json..."; /scripts/minify -r --type=json --match=\.json -o ${TARGET}/ ${TARGET} || true

echo "done."

And add this to the builder Docker image

# Build minifier utility
FROM golang:1.9-alpine AS minifier
RUN apk add --no-cache git
RUN go get -d github.com/tdewolff/minify/cmd/minify \
 && go build -v -o /minify github.com/tdewolff/minify/cmd/minify
Edited by Achilleas Pipinellis