Files
wasm/scripts/build-package.sh
T
Jaco Greeff 9438234750 Add wasm-crypto package (just start, incomplete) (#3)
* Add wasm-crypto package (just start, incomplete)

* Update

* Merge master

* Headers

* Add sha512

* Add blake2b

* Add twox

* xxhash with multiple rounds

* Cleanup tests (non-async)

* Adjust signatures

* Doc comments

* schnorrkel 0.1.0 crate

* Simplify export names

* Bip39

* Updates, ext_* function exposure

* bip39 tested and working

* Re-add wasm optimisations

* Cleanup build script

* Auto-detect internal packages

* bip39ToMiniSecret
2019-03-20 10:09:40 +01:00

92 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# cleanup old
echo "*** Cleaning old builds"
rm -rf ./build ./pkg
# build new via nightly & wasm-pack
echo "*** Building WASM output"
rustup run nightly wasm-pack build --release --scope polkadot --target nodejs
mv pkg build
# shortcuts for files
echo "*** Adjusting output"
BGJ=build/wasm_bg.js
SRC=build/wasm.js
DEF=build/wasm.d.ts
WSM=build/wasm_bg.wasm
OPT=build/wasm_opt.wasm
ASM=build/wasm_asm.js
# optimise
echo "*** Optimising WASM output"
../../binaryen/bin/wasm-opt $WSM -Os -o $OPT
# cp -f build/wasm_bg.wasm build/wasm_opt.wasm
# build asmjs version from the input (optimised) WASM
# echo "*** Building asm.js version"
# ../../binaryen/bin/wasm2js --no-validation --output $ASM $OPT
# convert wasm to base64 structure
echo "*** Packing WASM into base64"
node ../../scripts/pack-wasm-base64.js
# copy our package interfaces
echo "*** Copying package sources"
cp src/js/* build/
# cleanup asm
# sed -i -e 's/import {/\/\/ import {/g' $ASM
# sed -i -e 's/function asmFunc/var schnorrkel = require('\''\.\/wasm'\''); function asmFunc/g' $ASM
# sed -i -e 's/export const /module\.exports\./g' $ASM
# sed -i -e 's/{abort.*},memasmFunc/schnorrkel, memasmFunc/g' $ASM
# we are swapping to a async interface for webpack support (wasm limits)
sed -i -e 's/wasm = require/\/\/ wasm = require/g' $SRC
# construct our promise and add ready helpers
echo "
module.exports.abort = function () { throw new Error('abort'); };
const createPromise = require('./wasm_bg');
const wasmPromise = createPromise().catch(() => null);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC
# add extra methods to type definitions
echo "
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
" >> $DEF
# create the init promise handler
echo "
const asm = null; // require('./wasm_asm');
const bytes = require('./wasm_wasm');
const js = require('./wasm');
module.exports = async function createExportPromise () {
const imports = {
'./wasm': js
};
if (!WebAssembly) {
return asm;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
return asm;
}
}
" > $BGJ