summaryrefslogtreecommitdiff
path: root/process-static
blob: e198e22051d74a11959e12ddb3376fefc590c530 (plain)
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
#!/bin/bash

set -o errexit -o nounset -o pipefail
shopt -s dotglob extglob globstar

if [[ $# -eq 1 ]]; then
    fd=$1
else
    touch lock
    exec {fd}< lock
fi

if ! flock -n $fd; then
    echo already processing/deploying static files >&2
    exit 1
fi

export PATH="$PWD/node_modules/.bin:$PATH"

rm -rf static-tmp
cp -a static static-tmp

python3 process-templates.py static-tmp

for file in static-tmp/**/*.@(json|webmanifest); do
    json_verify < "$file" >/dev/null
    json_reformat -m < "$file" | sponge "$file"
done

stylelint static-tmp/**/*.css
find static-tmp -name '*.css' -exec csso {} -o {} \;

eslint static-tmp/**/!(fastboot.min.m|z-worker-pako.|pako_inflate.min.|)js
find static-tmp -name '*.js' -exec terser --ecma 2022 --module -cmo {} {} \;

replace=
for file in static-tmp/**/*.css static-tmp/js/*.js static-tmp/**/!(bimi|favicon).svg; do
    hash=$(sha256sum "$file" | head -c 8)
    sri_hash=sha256-$(openssl dgst -sha256 -binary "$file" | openssl base64 -A)
    dest="$(dirname $file)/$hash.$(basename $file)"

    if [[ $file == *.css ]]; then
        replace+=";s@\[\[css|/${file#*/}\]\]@<link rel=\"stylesheet\" href=\"/${dest#*/}\" integrity=\"$sri_hash\"/>@g"
    elif [[ $file == *.js ]]; then
        replace+=";s@\[\[js|/${file#*/}\]\]@<script type=\"module\" src=\"/${dest#*/}\" integrity=\"$sri_hash\"></script>@g"
    fi

    mv "$file" "$dest"
    replace+=";s@\[\[integrity|/${file#*/}\]\]@${sri_hash}@g"
    replace+=";s@\[\[path|/${file#*/}\]\]@/${dest#*/}@g"
done
sed -i "$replace" static-tmp/**/*.html

xmllint --noout static-tmp/**/*.@(html|svg|xml)
java -jar node_modules/vnu-jar/build/dist/vnu.jar --errors-only static-tmp/**/*.html
java -jar node_modules/vnu-jar/build/dist/vnu.jar --Werror --css static-tmp/**/*.css
java -jar node_modules/vnu-jar/build/dist/vnu.jar --Werror --svg static-tmp/**/!(bimi).svg
find static-tmp -name '*.html' -exec html-minifier-terser --collapse-whitespace \
    --process-scripts "application/ld+json" --collapse-boolean-attributes \
    --remove-attribute-quotes --remove-comments --remove-empty-attributes \
    --remove-redundant-attributes --remove-script-type-attributes \
    --remove-style-link-type-attributes --sort-attributes --sort-class-name {} -o {} \;

for file in static-tmp/**/*.@(atom|xml); do
    xmllint --noblanks "$file" --output "$file"
done

[[ -f indexnow-key.txt ]] && index_now_key="$(cat indexnow-key.txt)" && echo "$index_now_key" > "static-tmp/$index_now_key.txt"

find static-tmp -regex '.+\.\(atom\|css\|html\|ico\|js\|json\|mjs\|pdf\|svg\|txt\|webmanifest\|xml\)' |
    parallel -q ::: brotli zopfli :::: -