So I'm finally getting around to trying EXPORT_ES6 mode, and it just doesn't work in node (when loaded as an actual ES6 Module), because it still refers to __dirname. I assume other people have only been using it Webpack/rollup/whatever?
It doesn't look like there are any tests for EXPORT_ES6 either.
I think we don't have tests for it because at the time there wasn't a version of node that supported it. If there is now, we should add some.
I think we have 12.10 on CI, and can maybe add another version. Another option is to use v8, which we already install a new version of, if it has ES6 module support.
It would be good if someone could write up a test for this and then we can check the options.
Node 12.10 will definitely be enough.
Just FYI, it looks like out current version of node that we use in emsdk and in all our testing is 12.18.1.
Could this issue be extended to cover also "require" vs "import"? I wonder if replacing all lines in the output such as
var nodeFS = require('fs');`
with
#if EXPORT_ES6
const { default: nodeFS } = await import('fs');
#else
var nodeFS = require('fs');
would be sufficient.
Unfortunately my insight in the build system is very limited. But if someone could start then I could help at least with testing.
Realised this is going to be difficult to handle for multi-environment builds. ES import statements can't be put in conditionals, and while the import function can, it needs to be handled async.
ES import statements can't be put in conditionals, and while the import function can, it needs to be handled async.
They don't need to, Emscripten could emit static import declarations instead.
I said it would be difficult for multi-environment builds. A static import declaration for node will fail in the browser.
Ah, right, I see. In that case, the async import sounds fine. The fact that it's async shouldn't be an issue - Emscripten already has a set of async runtime dependencies and a way to wait on them.
Take a look at usages of addRunDependency from https://github.com/emscripten-core/emscripten/blob/0cc4a667828be0793db01c8c6b85434b6ccb38dc/src/preamble.js#L488.