dillonkearns / elm-typescript-interop

Generate TypeScript declaration files for your elm ports!
BSD 3-Clause "New" or "Revised" License
165 stars 13 forks source link

Improve support for projects with multiple main Elm modules #28

Open mulias opened 4 years ago

mulias commented 4 years ago

Hi @dillonkearns, thanks so much for creating this awesome tool.

I'm interested in using elm-typescript-interop for situations where multiple small elm applications are embedded into an existing typescript project. I created a test project to try out the viability of the idea, and found that there are some inconsistencies with how type declarations are generated in this context.

I made a branch that demos the issues I found, and tries to clearly outline my motivations, setup, and some potential solutions. You can find that here: https://github.com/mulias/elm-typescript-components-demo/tree/elm-typescript-interop-issues

The short version is that when using elm-typescript-interop to generate type declarations for a React app that mounts multiple Elm modules as separate apps, a I found that:

  1. Declaration files are generated for each Elm module individually, even when multiple modules have been compiled together into one .js file.

  2. Port types are shared between all Elm modules, even when modules are compiled separately into different .js files.

I'm interested in helping solve one or both of these issues, if you're open to that. It's possible that issue 1 is outside the scope of the project for now, but issue 2 is something I'd like to see fixed.

mulias commented 4 years ago

Ok, I'm starting to dig into the code and am working on getting a bigger picture on the state of things. I think I'm going to ask a bunch of questions here, and hopefully at some point you'll have time to answer, or someone else with context will chime in :)

The big issue right now seems to be using elm-ast for parsing elm code. I see that you're using a local version of tunguski/elm-ast. Does this library support all Elm 19 programs, or are there programs that would break? Have you looked into alternative parsing libraries? https://github.com/stil4m/elm-syntax has come up, have you had a chance to evaluate if that project meets your needs here?

It seems like there's a decent amount of work needed to switch over to a new parsing library, before it's worthwhile to address bugs in generated TS declarations. Are you interested in that work happening here, or would you prefer that parsing updates happen in a forked repo? The more I think about it, I think that some exploratory work needs to happen up front, since switching parsing libraries might mean different code structure.

Looking forward, I definitely see why the ports declarations are getting generated the way that they are right now. It seems like it would be possible to walk the ast and isolate the ports for a particular program. Lots to think about.

mthadley commented 4 years ago

I'm also running into issue #2 that @mulias described. I also agree that it seems like elm-typescript-interop needs to walk the import tree of each "main" to determine which ports will actually be available in the final JS file.

Is there anything we can be doing to help out with a fix?