Building your App

Unless your monorepo is only used for publishing packages to npm, it will likely contain at least one application. Coordinating your app's builds with Turborepo can lead to some extraordinary gains in speed.

Setting up the build

Turborepo works by keeping your workspace tasks where they belong - in each workspace's package.json. Let's imagine you have a monorepo that looks like this:

├── apps
│   └── web
│       └── package.json
├── package.json
└── turbo.json

Your apps/web/package.json should have a build script inside:

apps/web/package.json
{
  "scripts": {
    "build": "next build"
  }
}
apps/web/package.json
{
  "scripts": {
    "build": "vite build"
  }
}

Inside turbo.json, you can add build to the pipeline.

turbo.json
{
  "pipeline": {
    "build": {
      "outputs": [".next/**"]
    }
  }
}
turbo.json
{
  "pipeline": {
    "build": {
      "outputs": ["dist/**"]
    }
  }
}

We configure the outputs so that we can enable caching - an extremely powerful feature of Turborepo that can skip tasks that have been done before.

Now, add a script to your root package.json:

package.json
{
  "scripts": {
    "build": "turbo run build"
  }
}

This means that running build from root using your package manager will build all of the apps in the repository. Thanks to Turborepo's task cache, you can end up with extremely fast build times.

Last updated on September 20, 2022