-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild-wasm-app.nix
67 lines (62 loc) · 2.48 KB
/
build-wasm-app.nix
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
{ lib, runCommand, lndir, binaryen, wabt, webabi, gzip
}:
let
indexHtml = { ename, scripts, styles }: builtins.toFile "index.html" ''
<!doctype html>
<html>
<head>
<meta charset="utf-8"></meta>
${lib.concatMapStrings (s: "<link href=\"${s}\" type=\"text/css\" rel=\"stylesheet\">") styles}
</head>
<body>
${lib.concatMapStrings (s: "<script src=\"${s}\"></script>") scripts}
<script type="text/javascript">var WASM_URL_FOR_MAINTHREAD_RUNNER_JS = '${ename}-opt';</script>
<script defer="defer" src="jsaddle_core.js" type="text/javascript"></script>
<script defer="defer" src="jsaddle_mainthread_interface.js" type="text/javascript"></script>
<script defer="defer" src="mainthread_runner.js" type="text/javascript"></script>
</body>
</html>
'';
indexHtmlWorker = { ename, scripts, styles }: builtins.toFile "index.html" ''
<!doctype html>
<html>
<head>
<meta charset="utf-8"></meta>
${lib.concatMapStrings (s: "<link href=\"${s}\" type=\"text/css\" rel=\"stylesheet\">") styles}
</head>
<body>
<script src="jsaddle.js"></script>
${lib.concatMapStrings (s: "<script src=\"${s}\"></script>") scripts}
<script>
var jsaddleVals = jsaddleJsInit();
const worker = new Worker("worker_runner.js");
worker.postMessage({ url: "${ename}-opt"
, jsaddleVals: jsaddleVals }
, [jsaddleVals.jsaddleListener]);
</script>
</body>
</html>
'';
# On certain apps like reflex-todomvc, doing wasm-strip first and then wasm-opt gives
# smaller size files.
in { ename, pkg, assets ? [], scripts ? [], styles ? [] }: runCommand "wasm-app-${ename}" {
nativeBuildInputs = [ lndir binaryen wabt ];
passthru = { inherit pkg; };
} ''
if [ ! -f "${pkg}/bin/${ename}" ]; then
echo "The pkg: ${pkg} does not have the executable named ${ename}"
exit 1
fi
mkdir -p $out
lndir ${webabi}/lib/node_modules/webabi/build $out
lndir ${webabi}/lib/node_modules/webabi/jsaddleJS $out
ln -s ${pkg}/bin/${ename} $out/
${lib.concatMapStringsSep "\n" (a: "ln -s ${a} $out/") assets}
ln -s ${indexHtml { inherit ename scripts styles; }} $out/index.html
cp ${pkg}/bin/${ename} $out/${ename}-tmp
chmod u+w $out/${ename}-tmp
wasm-strip $out/${ename}-tmp
wasm-opt -Oz $out/${ename}-tmp -o $out/${ename}-opt
rm $out/${ename}-tmp
gzip -c $out/${ename}-opt > $out/${ename}-opt.gz
''