zs/contrib/plugins/importmap

26 lines
556 B
Plaintext
Raw Normal View History

#!/bin/bash
if [ $# -ne 1 ]; then
printf "Usage: %s <path>\n" "$(basename "$0")"
exit 1
fi
modpath="$1"
modpath="${modpath%"/"}"
ext=".js"
if [ -n "$ZS_PRODUCTION" ]; then
ext=".min.js"
fi
echo "<script type=\"importmap\">"
{
find "$modpath" -type f -name '*.js' | while read -r file; do
name="${file#"$modpath"}"
name="${name#"/"}"
name="${name%.*}"
echo "${name}=./${modpath}/${name}${ext}"
done
} | jq -Rs '{ imports: split("\n") | [.[] | capture("^(?<name>[^=]+)=(?<src>.*)$") | {(.name): .src}] | add }'
echo "</script>"