thesephist / oak

An expressive, simple, dynamic programming language.
https://oaklang.org
MIT License
231 stars 11 forks source link

Interop with Node/CommonJS module system when compiling to JS #4

Open shrik450 opened 2 years ago

shrik450 commented 2 years ago

Given that oak can be transpiled to js, is it:

  1. Possible,
  2. Desirable given the goals of the project

to allow interop with js libraries?

(Sorry if this is the wrong place to put this - if this is better asked in another place or a discussion thread, please let me know 🙇‍♂️)

thesephist commented 2 years ago

I think it's desirable to have interop with JS libraries as much as we can without changing the language semantics! Are you interested in any specific libraries, or more importantly any specific ways of interacting with those libraries?

In the simplest case, if you have JavaScript code loaded into a browser window you can access them as normal global variables in the Oak code. Calling into/out of JS functions from Oak should mostly "just work", though you may occasionally have to call a .toString(), and Oak atoms are JS Symbol values.

If you're asking about something more like interop with a JS toolchain like webpack so that you can import from/export to JS.... that feels like a big can of worms 😅 Down to discuss to see if there's a way to make it work, but I think the tradeoffs become more questionable there.

shrik450 commented 2 years ago

Yes, I was mostly thinking of libraries like puppeteer (and, to a certain extent, chalk) which I use(d) quite a bit when I scripting with node. I don't particularly care about exporting to node - oak feels perfect for small tools, and I was more interested in using the few npm libraries I used frequently working in it. That's also why I was wondering if it would be desirable to do this, since the npm ecosystem is great, but maybe that's not what "server-side" oak is supposed to be.

thesephist commented 2 years ago

Ah, I see. I believe what you're asking is whether there's a good way/workflow to have Oak code that (by compiling to target Node.js as the runtime) can interop with NPM libraries on the server-side?

I don't think there's any fundamental reason that this can't be a goal of the project. I think:

The other consideration here is that right now "compile to JS and then run with Node" is not a single command you can run, you have to do something messy like compiling to a temporary file and then giving that to Node to run. It might be nice to have oak whatever.oak --runtime=node or something like that automatically compile-then-immediately-run an Oak program as JS.

TLDR Since Oak-to-JS currently emits good JS code, I think it's just a matter of making import/export work within Node's CommonJS module system. Doable, though I don't have bandwidth at the moment. I'll keep this open for when I get some time to hack on Oak (or in case someone else wants to try it).