36 lines
1.1 KiB
Bash
36 lines
1.1 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
mkdir -p static-tmp
|
|
cp -a static/* static-tmp/
|
|
|
|
declare -a replacements
|
|
|
|
while IFS= read -r file; do
|
|
[ -f "$file" ] || continue
|
|
hash=$(sha256sum "$file" | head -c 8)
|
|
sri_hash=sha256-$(openssl dgst -sha256 -binary "$file" | openssl base64 -A)
|
|
dest="$(dirname "$file")/$hash.$(basename "$file")"
|
|
rel_path="${file#*/}"
|
|
dest_path="${dest#*/}"
|
|
|
|
if [[ "$file" == *.css ]]; then
|
|
replacements+=("s@{{css|/$rel_path}}@<link rel=\"stylesheet\" href=\"/$dest_path\" integrity=\"$sri_hash\"/>@g")
|
|
elif [[ "$file" == *.js ]]; then
|
|
replacements+=("s@{{js|/$rel_path}}@<script type=\"module\" src=\"/$dest_path\" integrity=\"$sri_hash\"></script>@g")
|
|
fi
|
|
|
|
mv "$file" "$dest"
|
|
replacements+=("s@{{integrity|/$rel_path}}@$sri_hash@g")
|
|
replacements+=("s@{{path|/$rel_path}}@/$dest_path@g")
|
|
done < <(find static-tmp -type f \( -name "*.css" -o -name "*.js" \))
|
|
|
|
while IFS= read -r htmlfile; do
|
|
for rep in "${replacements[@]}"; do
|
|
sed -i "$rep" "$htmlfile"
|
|
done
|
|
done < <(find static-tmp -type f -name "*.html")
|
|
|
|
rm -rf static
|
|
mv static-tmp static
|