ios icon indicating copy to clipboard operation
ios copied to clipboard

Support ES modules

Open rigor789 opened this issue 4 years ago • 1 comments

The runtime currently handles commonjs modules - and if an import statement is encountered the application will crash.

The implementation is currently in ModuleInternal.mm

It would be awesome if es modules were supported, since v8 already supports them - we are just missing the implementation for loading them (same-ish logic as for commonjs)

A reduced example for supporting es modules: https://stackoverflow.com/a/52031275/2270725

The gist of it is - we have to call ScriptCompiler::CompileModule on the source

Local<Module> module;
if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
  // if you have a v8::TryCatch, you should check it here.
  return;
}

Unresolved questions:

  • Can the runtime support both types and interop between them?

rigor789 avatar Nov 21 '20 16:11 rigor789

In my opinion, due to the use of bundlers, adding support for ES modules in the runtimes won’t bring additional value. Developers can already use ES modules in their applications.

As far as the open question is concerned: yes the runtime can support both types of modules but they cannot be mixed. What this means is that if the entry point of your application uses ES modules - it can only load other ES modules and cannot require commonjs modules. And vice versa. But here again, because of webpack, this doesn’t make much difference.

darind avatar Nov 21 '20 17:11 darind