I have a C++ project that uses EMSDK to compile to WebAssembly. I have an asset that my project loads, a bundle.zip that I currently use the ‘–embed-file’ flag from the SDK at compile time, and this works.
However, I would like to know if there is a way, in HTML or JavaScript, to pre-load this asset. So, instead of having to rebuild my project every time I change something in ‘bundle.zip’, I would just upload it and keep the same ‘.wasm’.
Is this option available? When searching online, I only found questions and answers related to C#’s Blazor, which is not related to what I want.
A crucial detail is that my application needs to read this file from the file system, as if it were on the native platform, not on the web (it does a fopen).
2
Answers
After the response of @VonC, this is my final HTML
You might consider using JavaScript to fetch the ‘
bundle.zip
‘ file at runtime, and then use the Emscripten Filesystem API to make it accessible to your WebAssembly module.That way, you can change ‘
bundle.zip
‘ without recompiling your WebAssembly module.First, remove the
--embed-file
flag from your Emscripten build command. That will stop embedding ‘bundle.zip’ into your WebAssembly module.Check if, as commented, the
--preload-file
option mentioned in Emscripten » Porting » Files and File Systems » Packaging Files would workWhen compiling your project with Emscripten, use the
--preload-file
flag followed by the path to ‘bundle.zip
‘. Emscripten then generates a ‘.data
‘ file alongside your ‘.wasm
‘ and ‘.js
‘ files.The generated JavaScript loader code will automatically fetch the ‘
.data
‘ file when loading your WebAssembly module. That means ‘bundle.zip
‘ is downloaded separately from your ‘.wasm
‘ file, but in a way that is transparent to your application.The contents of ‘
bundle.zip
‘ are automatically unpacked into the Emscripten virtual filesystem at runtime. Your application should then usefopen
to access the files as if they were on a regular filesystem.To update ‘
bundle.zip
‘, you simply replace the ‘.data
‘ file on your server. There is no need to recompile your WebAssembly module unless the C++ code itself changes.Or use JavaScript to fetch ‘
bundle.zip
‘ when your web page loads. You can use thefetch
API for that.Before you call any functions from your WebAssembly module that need ‘
bundle.zip
‘, make sure to mount a virtual filesystem and write your fetched file into it: that will make ‘bundle.zip
‘ accessible as if it were part of a normal filesystem.Your C++ code should now be able to use
fopen
to open ‘bundle.zip
‘, as it would on a native filesystem.FS.writeFile
(part of the Emscripten Filesystem API) is used to write the fetched file to the Emscripten virtual filesystem.