Skip to content

Instantly share code, notes, and snippets.

@wolfadex
Created April 20, 2020 04:55
Show Gist options
  • Save wolfadex/dca0395215c1a4d5fa52a02e226ef2c0 to your computer and use it in GitHub Desktop.
Save wolfadex/dca0395215c1a4d5fa52a02e226ef2c0 to your computer and use it in GitHub Desktop.
Compile Elm to a module
#!/bin/bash
set -e
js="src/elm.js"
min="src/elm.min.js"
elm make --optimize --output=$js $@
# change the cmpiled elm file to not immediately call the compiled function
perl -i -pe 's/\(function\(scope/function init\(scope/g' $js
perl -i -pe 's/}\(this\)\);/}/g' $js
# export the compiled function as the default export
echo "const scope = {};init(scope);const def = scope.Elm" >> $js
echo "export default def;" >> $js
while read -r line ; do
# extract the name of the module
if [[ $line =~ \$author\$project\$(.+)\$main ]]
then
name="${BASH_REMATCH[1]}"
# add the module as a named export
echo "export const ${name} = def.${name};" >> $js
else
echo "$line doesn't match" >&2
fi
# find modules being exported with a 'main' function
done < <(egrep -o 'var \$author\$project\$(.+)\$main' $js)
terser $js --compress 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters,keep_fargs=false,unsafe_comps,unsafe' | terser --mangle --output=$min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment