By default, Emscripten creates a module which can be used from both Node.JS and the browser, but it has the following issues:
- The module pollutes the global namespace
- The module is created with the name
Module(in my case, I require
- The module cannot be loaded by some module loaders such as require.js
While the above issues can (mostly) be corrected by using
var x = new Module.Klass() would become
var x = new Module().Klass(). I found this semantic change unacceptable, so I decided to abandon Emscripten’s
-s MODULARIZE=1 option in favor of hand-crafting a UMD module.
I determined that the most appropriate pattern for my use case was the no dependencies pattern from UMD’s
templates/returnExports.js. Applied to an Emscripten module, and using the default module name
streamingPercentiles, the stanzas look like the following:
While I might be able to use Emscripten’s
As my build system is CMake based, I needed to change change the compilation process to generate an intermediate file
With the above code, all of the original three issues are fixed without any semantic changes for users.