Compiling Elm locally within a webapp


#1

Hi all,

I am going to start working on a webapp, and am currently deciding which language/tech/framework/etc to use. Elm is my first choice but I am anticipating a potential issue:

Chrome OS will be the primary platform where the app will be used. While I don’t anticipate any problems running an Elm app on Chrome OS, the design of the app is such that I want to give people the ability to customize the app and create additional functionality using whatever language the app itself will be written in.

With the above design goal in mind, I wanted to ask if there are any plans for compiling Elm code locally within a webapp? I am familiar with Ellie, but the last time I looked into it, there was still a server component required for the compilation process.

As best I can tell, compiling Elm directly in the browser would currently require some kind of Haskell → JS transpiler. There seem to be a few efforts along these lines like Haste or ghcjs, but I’m not familiar with Haskell so I don’t know if the Elm compiler, as it’s currently built, can work with these tools.

Any thoughts about the near-term (or long-term) viability of compiling Elm within a webapp?


#2

Based off of what I heard @luke talk about at Elm Conf in 2017, the latest versions of Ellie have the Elm compiler running 100% in the browser. What you’re trying to do is rather specific, so my guess is that you will not find find too many other examples of the Elm compiler in the browser. I would take another look Ellie.

As a sidenote, if you’re planning on allowing the user to customize the app from within the app, you could look into allowing the user to write in an Elm-like Domain-Specific Language, and then parsing that DSL in Elm using a tool like elm-tools/parser. This way you don’t need to recompile, just interpret the DSL that the user inputs into Elm. The other benefit of this approach is that you can limit the customizations that the user can make to whatever your DSL includes.

Another great introduction to DSLs: DSL For the Uninitiated


#3

Thanks! Based on Luke’s talk, he seems intent to get the Elm Platform working entirely within the browser, so it looks like it’ll happen eventually. Using a DSL is an interesting idea as well.


#4

I have some more recent learnings that I can share which might help you make decisions:

  • The Elm compiler is pretty compute-intensive, and running JavaScript in a browser is going to be slower than running a Haskell binary especially on a low-powered machine like a Chromebook. I mentioned in that talk that I was considering returning a server compilation option for low-powered machines to make sure that the browser compiler doesn’t exclude the users of such machines. So going down this road is probably not going to be great for your program’s performance as compared to setting up a server to do the compilation.
  • The Elm compiler isn’t meant to be compatible with Haskell-to-JavaScript compilers; it’s meant to be really fast as a native binary. To achieve that goal it may one day include source code that makes FFI calls that make it impossible to compile to JavaScript at all. From what I understand this is already going to happen in the next version of the compiler, which is approaching release.
  • Since Elm is a statically typed, compiled language with no runtime reflection there aren’t any ready-made options for using it to create and host extensions. There will be a lot of exploration you’ll have to do in order to figure out how to get from source code the user writes to compiled code running within the same program. It’s not that it can’t be done, it’s just that Elm doesn’t allow for this by default so you’ll be writing hacks and manipulating the compiled JavaScript a lot.

For these reasons, if I were trying to build a program where I wanted the user to write extensions and I just wanted to get the feature finished then I would either use a different language or make a DSL like @christian suggested. Using Elm will be a lot more work and it includes the risk that upgrading will be blocked in the future.

he seems intent to get the Elm Platform working entirely within the browser

These things I outlined in combination with access to new funding for a server have led me to change course. I’m currently moving Ellie’s compiler integration completely back to the server.


#5

Thank you Luke for these updates and insights! As much as I’d like to use Elm, looks like I might have to go with a different stack for this particular project.


#6

Hey @remmah, have you considered using ports to open up some extension points? I don’t know the details of what kind of extensions you’re trying to enable, but it seems like that would be simpler and would also probably be simpler for users if they had some clear extension points that they could hook into.


#7

An exciting update: It looks like Chrome OS will be able to run linux programs locally in the future, so the standard Elm compiler should be able to be invoked (along with using your linux text editor/IDE of choice).