This post is a writeup of a POC I did with Bazel and Svelte. The Bazel rules are still considered experimental, but I have a working Bazel build with Svelte compilation and Rollup bundling.
First of all, what is Bazel?
Bazel is the open source version of Google’s internal build system (Blaze). The core design principle behind Bazel is building applications at scale. The idea is to avoid a linear slowdown of builds as the codebase grows huge.
The key to Bazel’s performance is incremental builds. Incremental means Bazel will only rebuild the parts of the application that actually changed after a code change.
If the application has 1000s of modules, but a code change is isolated to a single module, only that module, plus any dependent modules are rebuilt.
How does Bazel keep track of the changes?
Bazel requires us to define a Build graph where all input and output relationships are declared. The build graph defines the granularity of the compilation units in the application. The smaller the unit, the smaller the penalty of a rebuild.
In my POC I have defined compilation units at the Svelte component level since this is the scope of the Svelte compiler.
The build graph dependencies are defined in special BUILD.bazel files.
It's important to note that this only deals with setting up the build of your application. It doesn't impact how you write your Svelte code. Think of this as the Bazel equivalent of defining webpack configs for a webpack build.
Creating the build graph manually is one extra step, but in a more mature setup, the BUILD files can potentially be auto created based on code analysis.
In any event, the burden of creating the BUILD files is IMO manageable.
I have created a new Bazel rule called svelte for defining the dependencies that make up the component.
Below is an example:
The svelte rule wraps the Svelte compiler under the hood, so you can think of the rule as a way to pass data to the compiler.
The entry_point is the .svelte file for the component. I am also passing in any external .js files in the srcs array.
Svelte components may depend on other Svelte components. Below is an example where a top level component depends on three other svelte rules in the deps array. The other svelte rules are addressed using folder paths. The leading // is Bazel’s way of defining the root of the workspace.
To bundle the output from every component into a single file, I am using Rollup. Like before I am wrapping the underlying binary in a Bazel rule.Below I have defined two bundle rules. One for dev and one for prod
In the deps array I specify the top level svelte rule (App) since it defines the full dep graph of Svelte components. The : prefix in front of “App” just means that it’s defined in the same BUILD file as bundle.
Fast incremental builds is not the only benefit of using Bazel.
Bazel is created as a language agnostic build system. This means it can be used to create a single build for any supported programming language. The Bazel ecosystem already has wide support for common languages like Java, C++, Typescript, ++.
I show an example of a full stack build with React + Java here.
Another benefit of building across language boundaries is the possibility of shared typing across different languages.
An example where this is useful is in client – server communication. Synchronizing the types between the server and the client means we can catch mismatches at compile time rather than runtime.
In the demo I have added watching on file changes using iBazel. This regenerates the bundles for every code change. That said, we still get incrementality. The Svelte compiler will only be called for changed components.
This holds true even if you stop the build. The next time you start the build, it will complete almost instantly since there is nothing to build until the next code change.
I have pushed my POC to Github in case you want to try it out.
Just follow the instructions in the readme to get started.