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