What a monorepo taught me about boundaries
Putting seven small sites in one repo made the hard part obvious: not sharing code, but deciding which way it is allowed to flow.
This site is a handful of small properties (a blog, a cookbook, a work page and a few more) living in one Turborepo. When I set it up, I assumed the interesting problem would be sharing code. It turned out sharing is easy. The interesting problem is direction.
Apps on top, packages underneath
The rule is one sentence: apps may import packages, and packages may never import apps.
Every arrow points down. That's the whole architecture.
It sounds obvious written out. In practice, the pull to break it shows up weekly. The cookbook has a nice image component; the blog wants it. The lazy fix is to import it straight from the cookbook. The right fix is to move it down a layer so both can depend on it.
Making the rule enforceable
A rule that lives in a README is a suggestion. Turborepo makes the graph explicit through each package's dependencies, so a package that never declares an app as a dependency simply can't resolve it.
{
"name": "@atlas/design-system",
"dependencies": {
"@atlas/types": "workspace:*"
}
}Each property is also its own deploy, and only rebuilds when something in its own graph changes:
npx turbo-ignore @atlas/blogThat one line is the difference between "a typo in a recipe redeploys everything" and "a typo in a recipe redeploys the cookbook".1
What lives where
After a few months the split settled into something I'd defend:
| Package | Owns | Never owns |
|---|---|---|
types | Shapes shared by everyone | Logic of any kind |
content | Reading and validating MDX frontmatter | Anything visual |
design-system | Tokens, type, layout, MDX components | Any one app's copy |
The "never owns" column turned out to matter more than the first one.
The part I got wrong
I started by making everything shared, on the theory that it would save time later. It didn't. Code that only one app uses, sitting in a shared package, is a tax on every other app: more to read, more to keep compatible, more to break.
Now the rule of thumb is simple. Write it in the app. Move it down the second time someone needs it, not the first.
import path from "node:path";
import { getPosts, getPost } from "@atlas/content";
const CONTENT_DIR = path.join(process.cwd(), "content");
export const listPosts = () => getPosts(CONTENT_DIR);
export const findPost = (slug: string) => getPost(CONTENT_DIR, slug);Seven lines, app-specific, and exactly where you would look for them.
Footnotes
-
The other half is declaring env vars in
turbo.json, so that changing one invalidates the build cache instead of quietly replaying an old build. ↩