Cleanup build, no sed mangling (#129)

* Polyfill require adjust

* Assert in promise for getRandomBytes

* Adjust jest

* Revert explicit overrides (from base)

* use typeof

* Re-add crypto-polyfill (backwards, direct imports)

* crypto log

* Fix dev

* Adjust

* Cleanups, no sed mangling

* Cleanup build folder

* Remove exports

* Cleanups

* Adjust env vs util

* Remove Buffer alloc

* Cleant ext vs int

* Rework naming

* Mapping rename

* Combine index & exports

* withWasm wrapper

* pass{String, U8a} -> alloc{String, U8a}

* Internal reuse

* Explict package-only test
This commit is contained in:
Jaco Greeff
2020-11-02 16:39:54 +01:00
committed by GitHub
parent 2ac68bdb18
commit da946b18ce
18 changed files with 422 additions and 241 deletions
+7 -63
View File
@@ -5,11 +5,8 @@
set -e
BGJ=build/wasm_bg.js
SRC_WASM=build/wasm.js
DEF=build/wasm.d.ts
WSM=build/wasm_bg.wasm
OPT=build/wasm_opt.wasm
WSM=pkg/wasm_bg.wasm
OPT=pkg/wasm_opt.wasm
ASM=build/wasm_asm.js
echo "*** Building package"
@@ -17,11 +14,11 @@ echo "*** Building package"
# cleanup old
echo "*** Cleaning old builds"
rm -rf ./build ./pkg
mkdir -p build
# build new via wasm-pack
echo "*** Building WASM output"
wasm-pack build --release --scope polkadot --target nodejs
mv pkg build
wasm-pack build --release --scope polkadot --target web
# optimise
echo "*** Optimising WASM output"
@@ -37,65 +34,12 @@ echo "*** Building asm.js version"
# cleanup the generated asm, converting to cjs
sed -i -e '/import {/d' $ASM
echo "const imported = require('./wasm');
echo "const imports = require('./imports');
$(cat $ASM)" > $ASM
sed -i -e 's/{abort.*},memasmFunc/imported, memasmFunc/g' $ASM
sed -i -e 's/{abort.*},memasmFunc/imports, memasmFunc/g' $ASM
sed -i -e 's/export var /module\.exports\./g' $ASM
# copy our package interfaces
echo "*** Copying package sources"
cp package.json build/
cp src/js/* build/
echo "const crypto = require('crypto');
const { stringToU8a, u8aToString } = require('@polkadot/util');
const requires = { crypto };
$(cat $SRC_WASM)
" > $SRC_WASM
# whack comments
sed -i -e '/^\/\*\*/d' $SRC_WASM
sed -i -e '/^\*/d' $SRC_WASM
sed -i -e '/^\*\//d' $SRC_WASM
# we are swapping to a async interface for webpack support (wasm limits)
sed -i -e '/^wasm = require/d' $SRC_WASM
# We don't want inline requires
sed -i -e 's/ret = require(getStringFromWasm0(arg0, arg1));/ret = requires[getStringFromWasm0(arg0, arg1)];/g' $SRC_WASM
# this creates issues in both the browser and RN (@polkadot/util has a polyfill)
sed -i -e '/^const { TextEncoder } = require/d' $SRC_WASM
sed -i -e '/^let cachedTextEncoder = new /d' $SRC_WASM
sed -i -e 's/cachedTextEncoder\.encode/stringToU8a/g' $SRC_WASM
# this creates issues in both the browser and RN (@polkadot/util has a polyfill)
sed -i -e '/^const { TextDecoder } = require/d' $SRC_WASM
sed -i -e '/^let cachedTextDecoder = new/d' $SRC_WASM
sed -i -e 's/cachedTextDecoder\.decode/u8aToString/g' $SRC_WASM
# this is where we get the actual bg file
sed -i -e '/^const path = require/d' $SRC_WASM
sed -i -e '/^const bytes = require/d' $SRC_WASM
sed -i -e '/^const wasmModule =/d' $SRC_WASM
sed -i -e '/^const wasmInstance =/d' $SRC_WASM
sed -i -e '/^wasm = wasmInstance/d' $SRC_WASM
# construct our promise and add ready helpers (WASM)
echo "module.exports.abort = function () { throw new Error('abort'); };
const createPromise = require('./wasm_promise');
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_WASM
# add extra methods to type definitions
echo "
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
" >> $DEF
-9
View File
@@ -1,9 +0,0 @@
#!/usr/bin/env bash
# Copyright 2019-2020 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
set -e
rustup toolchain install stable
yarn polkadot-ci-ghact-build
+6 -20
View File
@@ -5,27 +5,13 @@
set -e
rustup toolchain install stable
./scripts/install-build-deps.sh
echo "*** Building packages"
cd packages
cd packages/wasm-crypto
PACKAGES=( $(ls -1d *) )
../../scripts/build-package.sh
../../scripts/test-package.sh
for PKG in "${PACKAGES[@]}"; do
if [ -f "$PKG/package.json" ]; then
echo "*** Building $PKG"
cd $PKG
rm -rf build/*-e build/package.json build/README.md
ls -al build
../../scripts/build-package.sh
../../scripts/test-package.sh
rm -rf build/*-e build/package.json build/README.md
ls -al build
cd ..
fi
done
cd ..
cd ../..
+4 -17
View File
@@ -5,23 +5,10 @@
set -e
./scripts/install-build-deps.sh
echo "*** Cleaning packages"
cd packages
cd packages/wasm-crypto
PACKAGES=( $(ls -1d *) )
rm -rf build
cargo clean
for PKG in "${PACKAGES[@]}"; do
if [ -f "$PKG/package.json" ]; then
echo "*** Cleaning $PKG"
cd $PKG
rm -rf build
cargo clean
cd ..
fi
done
cd ..
cd ../..
+2
View File
@@ -10,6 +10,8 @@ BINARYEN=( "wasm-opt" "wasm2js" )
unamestr=`uname`
rustup toolchain install stable
# install wasm-pack as required
if ! [ -x "$(command -v wasm-pack)" ]; then
echo "*** Installing wasm-pack"
+1 -2
View File
@@ -1,10 +1,9 @@
// Copyright 2019-2020 @polkadot/wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const buffer = fs.readFileSync('./build/wasm_opt.wasm');
const buffer = fs.readFileSync('./pkg/wasm_opt.wasm');
fs.writeFileSync('./build/wasm_wasm.js', `// Generated as part of the build, do not edit
+3 -15
View File
@@ -5,20 +5,8 @@
set -e
rustup toolchain install stable
cd packages
cd packages/wasm-crypto
PACKAGES=( $(ls -1d *) )
RUST_BACKTRACE=full cargo test --release -- --nocapture
for PKG in "${PACKAGES[@]}"; do
if [ -f "$PKG/package.json" ]; then
cd $PKG
echo "*** Testing Rust $PKG"
RUST_BACKTRACE=full cargo test --release -- --nocapture
cd ..
fi
done
cd ..
cd ../..