Turborepo with Svelte

A small monorepo with two SvelteKit sites, a shared component package, and remote build caching.

2 min di lettura

  • Svelte
  • Tooling

ArchiviatoQuesto articolo è archiviato. Resta qui perché i link continuino a funzionare, ma potrebbe non essere aggiornato.

ENQuesto articolo è disponibile solo in inglese.

Updated April 22, 2022.

Turborepo organizes JavaScript applications and shared packages in one repository. It can run tasks across the repository, reuse build output, and cache work locally or through Vercel.

I built a small example with two SvelteKit sites and a shared Svelte component library. The code is available in the sites example repository.

Start with the Turborepo template.

npx create-turbo@latest

The template separates applications into apps and shared code into packages. I added two SvelteKit sites named nkostic.dev and tehnika8x8.com under apps. I also added a small component package named uikit under packages.

Every shared package needs its own package.json so applications can import it.

{
  "name": "uikit",
  "version": "0.0.0",
  "main": "./index.svelte",
  "types": "./index.svelte",
  "devDependencies": {
    "svelte": "^3.44.0"
  }
}

The root turbo.json tells Turborepo which output folders belong to each task. My original SvelteKit setup used this configuration.

{
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**", "svelte-kit/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    }
  }
}

An application can import the shared Svelte component like any other package.

<script>
  import Button from "uikit";
</script>

<Button>Test Button</Button>

I assigned each application a different development port so both could run at once.

{
  "scripts": {
    "dev": "svelte-kit dev --port 3003"
  }
}

Run commands from the root of the repository. Turborepo finds the relevant applications and packages.

npm run dev
npm run build

The most useful feature is caching. Sign in and link the repository to enable Vercel’s remote cache.

npx turbo login
npx turbo link
Turborepo login prompt

Vercel asks you to authorize the connection with a GitHub or Google account.

Turborepo account authorization

After linking the repository, repeated tasks can reuse cached output instead of running again.

npm run build
Turborepo cached build output

You can also run a task without adding a root script.

npx turbo run dev
npx turbo run build

The Turborepo documentation covers configuration and remote caching. Monorepo Tools is a useful comparison of the available approaches.