I am generally wrapping the generated JS code in several steps but essentially,
it's:
(function(){
// generated nim code
})();
This way, most of the assignments in the generated code create variables local
to this anonymous function (since there declarations are starting with var).
There are a few exceptions, such as
if (typeof Int8Array === 'undefined') Int8Array = Array;
But I find that typed arrays are available on the environments I run JS anyway.
On Chrome today, the expression
Object.keys(window).length
evaluates to 195 (for this forum website) before and after running a minimal
echo "Hello, World!" that I cross-compiled to JS and wrapped with the pattern
above.
In my actual project, I am using [webpack](https://webpack.js.org) and a suffix
in the form of
exports.funcA ==funcA;
...
to declare proper CommonJS modules. With that, I get not only proper guards
against pollution of the global scope but also benefits such as tree shaking
and magnification "for free".
This is my command in the run section of my package.json
"nim": "nim js -d:release --noMain --lineDir:on --embedsrc
--nimCache:src/nim_generated src/nim/game.nim 2>&1 && echo \"exports.init =
game_init; exports.update = game_update; exports.render = game_render;\"
>>src/nim_generated/game.js",
The resulting JS code is pretty compact