-
Notifications
You must be signed in to change notification settings - Fork 2
/
zip.sh
executable file
·83 lines (70 loc) · 2.52 KB
/
zip.sh
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
#!/bin/bash
set -x
set -e
yarn clean
# Generate latest sprite data
yarn atlas
# TODO: consider optimizing aseprite-json by shortening names.
# build with rollup
NODE_ENV=production yarn build
# compress JS
terser dist/bundle.js --compress --mangle --mangle-props -o dist/bundle.min.js
mv -f dist/bundle.js dist/bundle-original.js
mv -f dist/bundle.min.js dist/bundle.js
# # Uncomment to Use Roadroller
# # Caution: this can make the final output larger! It depends on the size of the input.
# cp dist/bundle.js dist/bundle.original.js
# NODE_SCRIPT=$(cat <<-END
# const RR = require("roadroller");
# var txt = fs.readFileSync("/dev/stdin","utf8");
# const p = new RR.Packer([{ data: txt, type: "js", action: "eval" }], {});
# p.optimize(2).then(() => {
# const { firstLine, secondLine } = p.makeDecoder();
# fs.writeFileSync("dist/bundle.js", firstLine + "\n" + secondLine);
# });
# END
# )
# <dist/bundle.js node -e "$NODE_SCRIPT"
# Inline the script bundle
NODE_SCRIPT=$(cat <<-END
var html = fs.readFileSync("/dev/stdin","utf8");
var js = fs.readFileSync("./dist/bundle.js", "utf8");
const replaced = html.replace(/<!-- REPLACER START -->[\s\S]+<!-- REPLACER END -->/gm, "<script>" + js + "</script>");
fs.writeFileSync("dist/index.html", replaced);
END
)
<dist/index.html node -e "$NODE_SCRIPT"
# minify HTML
html-minifier \
--collapse-whitespace \
--remove-comments \
--remove-optional-tags \
--remove-redundant-attributes \
--remove-script-type-attributes \
--remove-tag-whitespace \
--use-short-doctype \
--minify-css true \
--minify-js true \
dist/index.html -o dist/index.min.html
mv dist/index.html dist/index.original.html
mv dist/index.min.html dist/index.html
# Optimize assets copied by rollup
# find dist/ -iname '*.png' -exec ./tools/tinypng.sh {} {} \;
# optimize texture-packed json, if applicable
# node tools/shrink-texture-packer-json.js ./assets/sprites.json ./assets/sprites.json
# Create/optimize the zip
pushd dist/
# ECT has much better compression than advzip, often times beating even the
# roadrolled+advzip version just by itself! NOTE: it also optimizes PNGs and
# JPEGs.
$(node -p "require('ect-bin')") -strip -zip -10009 index.html *.png *.svg *.bmp
# zip -X -r game.zip index.html *.png *.svg *.bmp *.js
popd
# # Optimize the zip
# if ! [ -x "$(command -v advzip)" ]; then
# echo 'Error: advancecomp is not installed. Try Homebrew.';
# exit 1;
# fi;
# advzip -z -4 -i 60 dist/index.html.zip
# Measure the zip!
echo $(echo "13312 - $(wc -c < dist/index.html.zip)" | bc) bytes remain