|
| 1 | +#!/usr/bin/env sh |
| 2 | +# SPDX-FileCopyrightText: 2024 Robin Vobruba <[email protected]> |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | + |
| 5 | +set -eu |
| 6 | + |
| 7 | +script_path="$(readlink -f "$0")" |
| 8 | +script_dir="$(dirname "$script_path")" |
| 9 | +script_name="$(basename "$script_path")" |
| 10 | + |
| 11 | +src_dir="$script_dir/.." |
| 12 | +build_dir="$script_dir/../public" |
| 13 | +js_file="htmlpreview.js" |
| 14 | +js_minified="$build_dir/$js_file" |
| 15 | +index_combined="$build_dir/index_combined.html" |
| 16 | +index_combined_minified="$build_dir/index_combined_minified.html" |
| 17 | +index_final="$build_dir/index.html" |
| 18 | + |
| 19 | +print_help() { |
| 20 | + |
| 21 | + echo "$script_name - Creates the release version of this tool." |
| 22 | + echo "This minifies the JavaScript file, inserts it into the HTML (in-line)," |
| 23 | + echo "and then also minifies the HTML and CSS," |
| 24 | + echo "leaving the final result at:" |
| 25 | + echo "$index_final" |
| 26 | +} |
| 27 | + |
| 28 | +# read command-line args |
| 29 | +i=1 |
| 30 | +while [ "$i" -lt "$#" ] |
| 31 | +do |
| 32 | + arg="$(eval "echo \$$i")" |
| 33 | + |
| 34 | + case "$arg" in |
| 35 | + -h|--help) |
| 36 | + shift "$i" |
| 37 | + print_help |
| 38 | + exit 0 |
| 39 | + ;; |
| 40 | + *) # non-/unknown option |
| 41 | + i=$((i + 1)) |
| 42 | + ;; |
| 43 | + esac |
| 44 | +done |
| 45 | + |
| 46 | +mkdir -p "$build_dir" |
| 47 | + |
| 48 | +# Minify the JavaScript |
| 49 | +npx --no-install \ |
| 50 | + uglify-js \ |
| 51 | + --compress \ |
| 52 | + --output "$js_minified" \ |
| 53 | + -- \ |
| 54 | + "$src_dir/$js_file" |
| 55 | + |
| 56 | +# Inserts the minified JavaScript into a copy of index.html, |
| 57 | +# thus combininig everything into a single file. |
| 58 | +awk ' |
| 59 | + NR==FNR { |
| 60 | + a[n++] = $0 |
| 61 | + next |
| 62 | + } |
| 63 | + /<script src="'"$js_file"'"><\/script>/ { |
| 64 | + print "<script>" |
| 65 | + for (i = 0; i < n; ++i) { |
| 66 | + print a[i] |
| 67 | + } |
| 68 | + print "</script>" |
| 69 | + next |
| 70 | + } |
| 71 | + 1' \ |
| 72 | + "$js_minified" \ |
| 73 | + "$src_dir/index.html" \ |
| 74 | + > "$index_combined" |
| 75 | + |
| 76 | +# Minifies the HTML and CSS in the above combined file. |
| 77 | +# NOTE While this tool (`@minify-html/node`) is also able to |
| 78 | +# minify contained JavaScript, it introduces bugs, |
| 79 | +# which is why we minify it before combining with |
| 80 | +# `uglify-js` above. |
| 81 | +npx --no-install \ |
| 82 | + @minify-html/node \ |
| 83 | + --output "$index_combined_minified" \ |
| 84 | + --minify-css \ |
| 85 | + --keep-closing-tags \ |
| 86 | + --keep-spaces-between-attributes \ |
| 87 | + --do-not-minify-doctype \ |
| 88 | + "$index_combined" |
| 89 | + |
| 90 | +# Marks the combined & minified version as the one to be used. |
| 91 | +rm -f "$index_final" |
| 92 | +ln -s "$index_combined_minified" "$index_final" |
0 commit comments