gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Permit FFI modules in subdirectories

Open lpil opened this issue 2 years ago • 14 comments

i.e. src/one/two/three.js and src/one/two/three.erl.

We will need to track Erlang module names to ensure there are no clashes. For example src/one/themodule.erl and src/two/themodule.erl in one project is an error.

  • [ ] Erlang
  • [ ] Elixir
  • [ ] JavaScript

lpil avatar Mar 27 '22 18:03 lpil

Adding some extra context at @erikareads (on discord)'s request:

You can only write ffi code directly inside src/, but we'd find it a lot more useful if i could co-locate ffi files along with the modules that need it:

src/
 wibble/
  wobble.gleam
  wobble_ffi.mjs

It'd mean inside wobble.gleam we could write

@external(javascript, "./wobble_ffi.mjs", "thing")

instead of

@external(javascript, "../wobble_ffi.mjs", "thing")

and so on.


For a concrete use-case lustre has a lustre/try.gleam module that spins up a http server for either erlang or js and there is some ffi code to do that in http_ffi.erl and http.ffi.mjs accordingly. That ffi code is not relevant to the rest of the codebase so keeping it in the project root places undue importance on the code and also makes it minorly annoying to import from.

As lustre gains more CLI commands more of these one-off FFI modules are likely to be necessary so it'd be really great if they could be organised away in a way that makes more sense than how it is currently.

hayleigh-dot-dev avatar Dec 27 '23 18:12 hayleigh-dot-dev

For Javascript this seems to be the path creation function:

https://github.com/gleam-lang/gleam/blob/4396f9a38a2439d5c7961f952adeec93bd9db624/compiler-core/src/javascript.rs#L324-L341

It currently assumes that it will be in the top of the project.

erikareads avatar Dec 27 '23 18:12 erikareads

There seems to be a similar function in the typescript.rs file:

https://github.com/gleam-lang/gleam/blob/4396f9a38a2439d5c7961f952adeec93bd9db624/compiler-core/src/javascript/typescript.rs#L279-L297

erikareads avatar Dec 27 '23 18:12 erikareads

Has anyone tried to take a stab at implementing this yet? If not, I'd like to give it a shot, I just don't want to step on anyone's toes.

winterqt avatar Mar 09 '24 15:03 winterqt

It's all yours @winterqt !!

lpil avatar Mar 13 '24 11:03 lpil