Elm-Runtime reuse

Hi!

I recently used elm to add a new feature to an existing js app. I am very happy with the result and I want to add another feature at a completely different part of the app. My plan is to create a new elm app and embed it like the first one in the js app. But that would increase the bundle size unnecessarily because the elm runtime would be bundled twice.

My question: Is it possible to have only one elm runtime which can be used by all other elm instances?

Yes. You can have one compiled bundle with many mains. The runtime will be shared.

elm make src/Main1.elm src/Main2.elm
4 Likes

Oh and there’s no way to split the runtime into a separate bundle or something because Elm doesn’t have code splitting yet. What I mentioned above is the only option.
By the way try this if you haven’t already

If those separate mains need to communicate with each other, is ports with javascript glue the only solution?

Good question! I never got that far with it so I’ve never even thought about it!

But yes I think ports would be the only way. The different mains could have different message types, so I don’t think you could send a message from one to the other in a way that type-checks. Even if you could, you’d need some kind of ID for the other main in order to route a message to it, and Elm doesn’t have that.

To do this in a more “first-class” way, Elm would need to support multiple processes communicating with each other, which it doesn’t… yet!

It’s also possible to communicate between two Elm programs with events. E.g. one Elm program is small widget, and another Elm program is bigger widget which includes smaller widget inside. If small widget is simple, then one can just use it differently from Elm. But if it’s more complex, e.g. with ports or have some complex logic with update and subscriptions, then it’s possible to wrap small widget with custom element and communicate with larger widget via events. BTW this way it’s possible to achieve component based structure :wink:

2 Likes

Thanks a lot! That’s exactly what I need for my project!

1 Like

If you intend to use webpack my suggestion may apply to you.

Our project is comprised of many Elm main’s and only a single runtime. We do this by using a project I created specifically for that purpose.

You may confuse this with elm-webpack-loader which is a solid project. But we haven’t been able to use it to effectively produce a single runtime. This seems to be because of requiring different files for each Main which webpack sees as a separate entry and therefore bundles the whole runtime each time.

Let me know if you have any question or if this helps!

1 Like

Thanks for the hint but I’m using meteor.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.