CommonJS Modules

Cheerp supports the CommonJS standard for JavaScript modules via the -cheerp-make-module=commonjs option.
With this option an exports object will be automatically populated with all the exported functions.
This allows JavaScript code from other files to use require() and import them.

The actual object returned by require() is a Promise, since the code produced by Cheerp may contain WebAssembly (which is only loaded and compiled asynchronously).

For example, suppose that you have a project in which you need to sort a big array, and you want to replace the slow Array.sort() with a faster version. Instead of writing your own sorting function, you can use std::sort() from the C++ STL:

You can compile the above code with /opt/cheerp/bin/clang++ -target cheerp sort.cpp -o sort.js -cheerp-make-module=commonjs and put the resulting sort.js in your project source folder.
Than, using it is as easy as: