This is my directory structure for a project which uses React and WebAssembly.
build.sh
src/
main.c
pre.js
react-wasm/
public/react-wasm.wasm
src/
wasm/
react-wasm.js
My build process is a bit convoluted, but it's something I use in a larger project successfully, so I think it's not the problem.
build.sh
export EMMAKEN_CFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0"
WASM_FILENAME=react-wasm.wasm
JS_FILENAME=react-wasm/src/wasm/react-wasm.js
sudo docker run --rm -v $(pwd):/src trzeci/emscripten emcc \
src/main.c \
--pre-js src/pre.js \
-s MODULARIZE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 \
-s EXPORT_NAME='WasmReact'\
-s ASYNCIFY=1 \
-o ${JS_FILENAME}
# So eslint/typescript doesn't complain
gsed -i.old '1s;^;\/* eslint-disable */\n&;' ${JS_FILENAME}
gsed -i.old '2s;^;\// @ts-ignore;' ${JS_FILENAME}
# Make directory absolute
gsed -i.old "s|$WASM_FILENAME|/$WASM_FILENAME|" ${JS_FILENAME}
# Prevent it from trying to resolve relative path
gsed -i.old "s|wasmBinaryFile = locateFile|// wasmBinaryFile = locateFile|" ${JS_FILENAME}
# Move to public directory
mv react-wasm/src/wasm/${WASM_FILENAME} react-wasm/public/${WASM_FILENAME}
pre.js
Module["preRun"] = function () {
# This is the part that errors out
FS.mkdir("/work");
FS.chdir("/work");
};
When I run it with React, it complains FS is not defined
I needed to use -s FORCE_FILESYSTEM=1 in my build script. My src/main.c did not use the filesystem so emscripten did not include it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With