javascriptrubyopalrb

Compile an opalized Ruby gem to Node package


It's not clear to me from the docs and guides whether the following is feasible with Opal:

I've written a builder gem which implements an API to build an object tree which is then written to XML (or AIXM to be exact, an aeronautical format). There are currently runtime dependencies, but I could get rid of those.

There's a guide on configuring gems for Opal, however, is it possible to use an opalized gem in a JavaScript project? Or convert it to a Node package?


Solution

  • We are currently in a process of designing a good NPM integration. But as of now, yes. You can build an Opal project to a .js (or .mjs) file and require it in the Node environment.

    One issue though: Opal does not behave well in the JS environment, as it defines globals, modifies prototypes, etc. - it may cause a mess, though most incompatibilities have been sorted out over the years (in any case, you may consider running Opal code in a WebWorker). The main issue here is that such a package will inadvertently include opal corelib - and you should take care to include it just once. So, your Opal part of the application should be compiled together, not split together into multiple packages. We will work on correcting that issue in the following months.

    As for interfacing Opal from JS, you should take a look at this: https://github.com/opal/opal/blob/master/docs/compiled_ruby.md#ruby-from-javascript