CodeBlanks Logo Icon CodeBlanks

Summary - Monorepo

Combine Yarn workspaces, Turborepo and shared packages to set up a monorepo.

Summary - Monorepo

A monorepo keeps many packages in one repo. Yarn workspaces and npm workspaces are built-in solutions; both use the workspaces field and set up symlinks.

Turborepo cuts build time with parallel execution and a cache. Shared packages (@my/ui, @my/utils) prevent code duplication; apps use them with a normal import.

Practice Shared theme package

Define a colors object. The primary key should be '#0066ff', the background key '#ffffff'. Export it with export.

Starter code

App.js
// Define the colors object here

export default colors;

Sample solution

App.js
// Define the colors object here
const colors = {
  primary: '#0066ff',
  background: '#ffffff'
};

export default colors;

Practice Turborepo build script

Add scripts to a root package.json. It should contain "build": "turbo run build".

Starter code

App.js
{
  "name": "my-monorepo",
  "private": true,
  "workspaces": ["apps/*", "packages/*"]
  // Add the scripts field here
}

Sample solution

App.js
{
  "name": "my-monorepo",
  "private": true,
  "workspaces": ["apps/*", "packages/*"],
  "scripts": {
    "build": "turbo run build"
  }
}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the biggest gain of a monorepo?

  • A smaller APK
  • Writing fewer tests
  • Fewer UI components
  • Code sharing and single repo Correct answer

Explanation: Code sharing is the core gain of a monorepo.

2 Match all the tools

Term Meaning
Yarn workspaces Yarn built-in
npm workspaces npm built-in
Turborepo Build accelerator
@my/ui Shared package

All lessons in Expert