Importing emscripten generated JS file in webpack

42 views
Skip to first unread message

Aitha Tarun

unread,
Oct 25, 2022, 12:13:01 PM10/25/22
to emscripten-discuss
Normally I use script tag in html file to attach emscripten generated JS.
But how to import this emscripten (emcc) generated JS file in webpack.

I am using below command to build the javascript file:
emcc ./src/rfb.c -o ./src/js/rfb.js -sWASM=0 --pre-js ./src/js/pre.js -sEXPORTED_FUNCTIONS=_main,_malloc -sEXPORTED_RUNTIME_METHODS=FS,callMain,cwrap,wasmMemory -sASYNCIFY -O3 -sENVIRONMENT=web

And I have using directly import './js/rfb.js';
But Module object is not getting created when html file is opened in browser.

Aitha Tarun

unread,
Oct 25, 2022, 2:27:59 PM10/25/22
to emscripten-discuss
I have also used -sMODULARIZE and exported the Module to webpack, but when using optimization of -O3 .mem file is being created, webpack is unable to load this .mem (memory initializer) file, any solution for this

Lorenzo Portillo

unread,
Oct 25, 2022, 2:48:27 PM10/25/22
to emscripten-discuss
Hello,

I wrote a post about this some weeks ago. 
I recommend using .mjs modules, otherwise you will need to polyfill a lot of things in webpack.

Here the posts, I hope they are useful (second one is full focused on webpack but the guide is about programming Wasm module in Vusial Studio):

Sam Clegg

unread,
Oct 25, 2022, 5:40:21 PM10/25/22
to emscripte...@googlegroups.com
If you drop `-sWASM=0` when the `.mem` file will not be generated/used.  Do you need `-sWASM=0`?  

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/cdad42f4-3b46-49c1-b422-d1b348e7e5e3n%40googlegroups.com.

Aitha Tarun

unread,
Oct 25, 2022, 5:41:58 PM10/25/22
to emscripten-discuss
Yes, I don't want to create .wasm file, is there any alternative?

Brion Vibber

unread,
Oct 25, 2022, 5:49:22 PM10/25/22
to emscripte...@googlegroups.com
On Tue, Oct 25, 2022 at 2:42 PM Aitha Tarun <aitha...@gmail.com> wrote:
Yes, I don't want to create .wasm file, is there any alternative?

Setting `-sWASM=0` will create JavaScript for the compiled code instead of WebAssembly, and will generally perform worse. You may be interested instead in `-sSINGLE_FILE=1` which will embed the WebAssembly code and any other resources into the .js file as a string. This still uses WebAssembly, but avoids creating a separate file which needs to be copied and referenced appropriately.

-- brion
 

Aitha Tarun

unread,
Oct 25, 2022, 6:17:07 PM10/25/22
to emscripten-discuss
Thanks,
It worked with the -sSINGLE_FILE=1 option I am able to achieve O3 optimization without creating any wasm and mem files.
And with -sMODULARIZE option I am able to export the Module object and use that in webpack.

Thanks, guys.

Reply all
Reply to author
Forward
0 new messages