guides

Guide to Monorepos: Turborepo

Everything you need to know about monorepos and turborepo

Youssef Hajjari

--

Image courtesy of voiceflow

Some development teams use a monorepo. Some use multiple repositories. Here we cover what a monorepo is, and the benefits of using it with tools like turborepo.

Let’s start with a common understanding of what a Monorepo is: 🚀

A monorepo is a single git repository that holds the source code for multiple applications and libraries, along with the tooling for them.

for example, you may keep your frontend code and backend code in two different folders in the same repository Besides, a monorepo can contain both related and independent projects (Web app & Server & Mobile app …).

This concept isn’t new, it first surfaced around a decade ago. Google was one of the first companies to use this method to manage their codebases, followed by Facebook, Microsoft, Uber, and Twitter.

Monorepo style 😎

  • You develop multiple projects in the same repository.
  • The projects can depend on each other, so they can share code. and configuration.
  • When you make a change, you do not rebuild or retest every project in the monorepo. Instead, you only rebuild and retest the projects that can be affected by your change. that's why tools like Turborepo come.

Why a monorepo 🤔

  • Easy to share and reuse code between projects: Keeps your code DRY across your entire repository, Reuse validation code, UI components, and types across the codebase. Reuse code between the backend, the frontend …
  • A single set of dependencies: Use a single version of all third-party dependencies, reducing inconsistencies between applications.
  • Reuse configuration: instead of having a lot of repositories with their own configs, we can have a single configuration to manage all the projects, making them easier to manage.

Monorepo Tools: Turborepo💪

The Monorepo tools perform a variety of tasks, including bootstrapping and linking local packages, compiling and validating code, and so on.

There are many monorepo tools available, such as Turborepo, Lerna, Bazel, and so on. We’ll utilize turborepo in this article.

Turborepo High-performance Build System for JavaScript and TypeScript codebases was created by Jared Palmer as a closed-source enterprise software offering. In late 2021, Vercel acquired Turborepo and open-sourced the codebase.

What TurboRepo does

  • Faster, incremental builds: Building once is painful enough, Turborepo will remember what you’ve built and skip the stuff that’s already been computed.
  • Cloud caching: Share a cloud build cache with your teammates and CI/CD for even faster builds.
  • Parallel execution: Execute builds using every core at maximum parallelism without wasting idle CPUs.
  • Task pipelines: Define the relationships between your tasks and then let Turborepo optimize what to build and when.

Getting Started with Turborepo

you can get started with this command:

npx create-turbo@latest

If you take a look in the current folder you will see the following structure: 📂

  • apps/ folder: a folder that contains all your applications (docs, server, mobile app, web app …).
  • turbo.json file: Turborepo file configuration.
  • packages/ folder: your shared UI, configuration(eslint, tsconfig) throughout the monorepo.

To build with your freshly installed monorepo run:

yarn build

Our first build took 17 seconds

Now run it again. you notice in the second build completes in 100ms because everything is cached 🚀🚀

Turborepo will remember what you’ve built and skip the stuff that’s already been computed.

The other important thing to know is that you can run all these tasks together and Turbo will parallelize as much as possible:

yarn build test lint

you can find more examples in the Turborepo repository:

https://github.com/vercel/turborepo/tree/main/examples

Conclusion:

As you can see, Monorepos can help us a lot during the development process and during the release process. With the right setup and structure, we can focus on developing new features and/or fixing bugs instead of upgrading a number of repositories. With Monorepos and tools like Turborepo, everything is automated.

That’s it

I hope you enjoyed it!

Leave any questions, concerns, recommendations, or criticisms in the comments section. This motivates me to keep improving and writing on Medium

See you later! ❤️

--

--