Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Build process #3

Closed
jacogr opened this issue Feb 13, 2019 · 2 comments
Closed

Build process #3

jacogr opened this issue Feb 13, 2019 · 2 comments

Comments

@jacogr
Copy link
Contributor

jacogr commented Feb 13, 2019

At least in the https://github.com/polkadot-js projects we need to support Browser/Node/React Native. wasm-pack is great, however the output options either creates something just for the browser or just for node.

Browser assumed webpack so does import * from 'schnorrkel_js_bg' - which is a WASM file, cannot be done on Node without webpack. The node output (--target nodejs) emits something where fs.readySync is used, once again, not quite usable in the Browser or RN.

Basically to get this working, I'm using the following scripts to support both environments.

build.sh -

#!/bin/sh

# install wasm-pack as required
if ! [ -x "$(command -v wasm-pack)" ]; then
  curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
fi

rm -rf ./pkg

rustup default nightly
wasm-pack build --target nodejs
rustup default stable

# make a backup of the original, use it, don't use it (useful for checking)
mv ./pkg/schnorrkel_js_bg.js ./pkg/schnorrkel_js_bg_node.js

./wasm2js.js

wasm2js.js -

#!/usr/bin/env node

const fs = require('fs');
const buffer = fs.readFileSync('./pkg/schnorrkel_js_bg.wasm');

fs.writeFileSync('./pkg/schnorrkel_js_bg.js', `const imports = {};\n
imports['./schnorrkel_js'] = require('./schnorrkel_js');\nconst bytes = Buffer.from('${buffer.toString('hex')}', 'hex');\nconst wasmModule = new WebAssembly.Module(bytes);\nconst wasmInstance = new WebAssembly.Instance(wasmModule, imports);\n\n
module.exports = wasmInstance.exports;`);

By far from the prettiest hack around, but it basically yields something that I can use as-is.

@kianenigma
Copy link
Collaborator

This is most likely fixed in this commit.

@jacogr
Copy link
Contributor Author

jacogr commented Feb 14, 2019

Fixed, working, brilliant! (@parity/schnorrkel-js 0.1.2)

@jacogr jacogr closed this as completed Feb 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants