Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shrik450 opened this issue Apr 2, 2022 · 3 comments
Open

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

shrik450 opened this issue Apr 2, 2022 · 3 comments
Labels
question Further information is requested

Comments

@shrik450
Copy link

shrik450 commented Apr 2, 2022

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 thesephist added the question Further information is requested label Aug 10, 2022
@thesephist
Copy link
Owner

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
Copy link
Author

shrik450 commented Aug 11, 2022

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
Copy link
Owner

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:

  • when we emit JS code we can wrap import(...) such that it uses Node's import resolution rules to check in places like node_modules and calls require().
  • Writing JS libraries that export stuff (via module.exports) would be weirder, but I think we can probably just grab all the top-level globals and shove them into the Node exports when compiling to Node as well?

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).

@thesephist thesephist changed the title JS Interop? Interop with Node/CommonJS module system when compiling to JS Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants