Bazel build rules for Elm

#1

Hi folks,

I recently wanted to write a web service with a frontend written in Elm and backend written in Go. I wanted to be able to build both of these components in one go and bundle them together in a single Docker image. For both components I also have unit testing coverage; ideally those test can be built and run through a single mechanism. All in all, not an all to uncommon setup I suppose.

To be able to do this, I wrote Elm build rules for the Bazel build system. Bazel is a build system that uses a Python-esque language called Starlark that can be used to declare and instantiate build rules that are built on top of a low-level rules API. Bazel itself ships with rules for C/C++, Python and Java. Other people have taken the effort to write rules for Go, Rust, Haskell, Docker, etc. etc. etc.

An interesting aspect of the Elm rules that I wrote is that it also provides first-class support for tiziano88/elm-protobuf. By using a feature provided by Bazel called aspects, it is also able to automatically instantiate elm-protobuf build rules for a full hierarchy of Bazel proto_library() rules. This makes it easy to use Protobufs in Elm, even if the .proto files use many levels of nested imports.

Compared to plain elm make, it also provides more flexible management of third-party dependencies. It is possible to override dependencies, so that they are downloaded from arbitrary locations. You may also apply a series of patches to dependencies after extraction. This is useful in case you want to test/use patches yourself prior to upstreaming them. It may also be helpful in case upstream doesn’t take the effort to merge your changes. No need for you to remain blocked on that.

To give insight in how these rules can be used, I created a fork of elm-spa-example that uses them. Be sure to check out the diff against master to get an overview of what this all looks like. Though I agree that the BUILD.bazel / WORKSPACE files are not as terse as plain elm.json, I think the increase in flexibility and ability to integrate multiple languages into a single build makes up for it.

Be sure to give these rules a try and let me know what you think. Thanks!

8 Likes
#2

This sounds very useful, thanks for creating it.

1 Like
closed #3

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