0

I've created a library in C++ using Embind and Emscripten.

Some hand written JS code is also added to the library using --pre-js

The library works. But I would like to rearrange the code, to this:

var MYLIB = (function(){
  // ... Original Code ...
  return Module;
})();

So the code would not pollute the global namespace, and the code minifier could do better optimizations.

Are there build in functions for this in emcc ?
The library will only run in webbrowsers, not in nodejs.

Iter Ator
  • 8,226
  • 20
  • 73
  • 164
  • Related: http://stackoverflow.com/questions/33623682/how-to-use-fs-when-modularize-and-export-name-are-used-in-emscripten – user1906 May 31 '16 at 01:43

1 Answers1

1

What you're looking for are the MODULARIZE and EXPORT_NAME options. Check the documentation in settings.js.

Quoting from that file:

// By default we emit all code in a straightforward way into the output
// .js file. That means that if you load that in a script tag in a web
// page, it will use the global scope. With MODULARIZE set, we will instead emit
//
// var EXPORT_NAME = function(Module) {
// Module = Module || {};
// // .. all the emitted code from emscripten ..
// return Module;
// };
//
// where EXPORT_NAME is from the option of the same name

user1906
  • 2,310
  • 2
  • 20
  • 37