Skip to content

Commit

Permalink
Remove Nix store path from source maps
Browse files Browse the repository at this point in the history
Obelisk successfully serves minified JS, the corresponding unminified JS,
and the source map that relates the two. But the source map's `sources`
field contains the Nix store path of the unminified JS, not a URL. Browsers
try to fetch the source code from a bogus URL:
`https://example.com/nix/store/HASH-compressedJs/frontend.jsexe/all.unminified.js`.

This change improves the situation by tweaking the arguments to
`closure-compiler`; using relative paths to the JS files instead of absolute
paths.

Works on Chromium, but Firefox still has trouble.
  • Loading branch information
isaac-uptrust committed Jun 5, 2024
1 parent b50ac6a commit 3f898da
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,36 @@ in rec {
set -euo pipefail
cd '${haskellLib.justStaticExecutables frontend}'
shopt -s globstar
for f in **/all.js; do
dir="$out/$(basename "$(dirname "$f")")"
base=$(pwd)
mkdir -p "$out"
cd "$out"
for f in $base/**/all.js; do
dir="$(basename "$(dirname "$f")")"
mkdir -p "$dir"
ln -s "$(realpath "$f")" "$dir/all.unminified.js"
pushd "$dir"
ln -s "$(realpath "$f")" "$out/$dir/all.unminified.js"
${if optimizationLevel == null then ''
ln -s "$dir/all.unminified.js" "$dir/all.js"
ln -s "$out/$dir/all.unminified.js" "$out/$dir/all.js"
'' else ''
# NOTE: "--error_format JSON" avoids closurecompiler crashes when trying to report errors.
'${pkgs.closurecompiler}/bin/closure-compiler' --error_format JSON ${if externs == null then "" else "--externs '${externs}'"} --externs '${reflex-platform.ghcjsExternsJs}' -O '${optimizationLevel}' --jscomp_warning=checkVars --warning_level=QUIET --create_source_map="$dir/all.js.map" --source_map_format=V3 --js_output_file="$dir/all.js" "$dir/all.unminified.js"
echo '//# sourceMappingURL=all.js.map' >> "$dir/all.js"
'${pkgs.closurecompiler}/bin/closure-compiler' \
--error_format JSON \
${if externs == null then "" else "--externs '${externs}'"} \
--externs '${reflex-platform.ghcjsExternsJs}' \
-O '${optimizationLevel}' \
--jscomp_warning=checkVars \
--warning_level=QUIET \
--create_source_map="all.js.map" \
--source_map_format=V3 \
--js_output_file="all.js" \
"all.unminified.js"
echo '//# sourceMappingURL=all.js.map' >> "all.js"
popd
''}
done
'';
Expand Down

0 comments on commit 3f898da

Please sign in to comment.