Documentation comes from this release commit, without inheriting newer content. Commit: 63ea8c2b3d.
Monorepo
Magic Piano is a Bun + Turborepo monorepo. The Expo app stays at the repo root; reusable piano logic lives in packages/* as @piano/* workspace packages plus the instrument-agnostic @rhythm/engine rhythm-game core, all built with tsup and orchestrated by turbo.
Why a monorepo
- The piano engines are reusable. Audio, MIDI, gameplay, progression, catalog, ads, and analytics are domain logic that has nothing to do with React Native. Putting them under
packages/*lets the same code ship to a future web build, a CLI, or a unit-test runner without dragging Expo with them. - The app stays thin.
app/andsrc/only contain UI, navigation, and React Query glue. All gameplay state machines live in@piano/gameplay-{learn,magic,zen}. - One install, one lockfile. Bun's hoisted workspaces share a single
bun.lockandnode_modules, so React, RN, and Reanimated cannot be duplicated across packages — the #1 cause of "Invalid hook call" errors in RN monorepos.
Repo layout
modules/ holds local Expo modules — host-native bridges autolinked on expo prebuild (Swift/Kotlin + expo-module.config.json). They are not Bun workspace packages: no package.json, no tsup build, not imported as @piano/*. The app imports them via relative paths from src/ (e.g. system-gesture-defer.ts → modules/screen-gesture-defer, perf-hud.ts → modules/perf-hud). See architecture.md § Local Expo modules.
Workspace dependency graph
@piano/types is the leaf; @piano/sdk is the facade. Every other package sits between them.
Topological build order (what turbo run build walks):
Deprecation track:
@piano/midi,@piano/gameplay-magic(thejudgementmodule), and parts of@piano/core(event-bus,state-machine,tick-clock) are now thin re-exports from@rhythm/engine. They stay for backward compatibility with@piano/gameplay-learnand@piano/gameplay-zenuntil those packages migrate; track full removal in the rhythm-engine package README.
Package responsibilities
The Expo app only imports @piano/sdk (and re-exported types from it). Reaching past the SDK into a sub-package directly works but defeats the facade — keep that for tests or scripts.
Per-package layout
Every package is identical:
package.json is uniform too:
Workspace deps use "@piano/<dep>": "workspace:*". Runtime/peer deps go under dependencies. Build tooling (tsup, typescript, @types/bun) goes under devDependencies.
Shared configs
tsconfig.base.json
Strict, ES2022, declaration emit + sourcemaps + incremental builds. Extended by every package's tsconfig.json:
The Expo app's tsconfig.json does not extend this file — it extends expo/tsconfig.base because Expo needs different jsx, lib, and types settings. The two tsconfigs intentionally diverge: packages target plain ES2022, the app targets RN.
tsup.config.base.ts
Dual ESM + CJS output, declaration files, sourcemaps, no code splitting (each package ships a single index.{js,cjs,d.ts}). Extended by every package's tsup.config.ts:
If a package needs entry-point overrides (multiple subpath exports, side-effects flag, externals), it can spread baseConfig and add its own keys.
bunfig.toml
The hoisted linker is what lets Metro resolve a single copy of react / react-native / react-native-reanimated across the app and packages. Don't switch to isolated — RN cannot tolerate duplicate copies of those modules.
turbo.json
^build means "build dependencies first" — turbo walks the workspace graph in topological order. So bunx turbo run typecheck rebuilds dependent dist/ outputs before typechecking each package, which guarantees @piano/sdk typechecks against fresh declaration files of @piano/types etc.
How the Expo app consumes packages
Three pieces make this work:
- Bun workspaces (
package.jsonworkspaces: ["packages/*"]) symlink each@piano/*intonode_modules/@piano/. - Metro
watchFolders(metro.config.js) point atpackages/, so editing a file inpackages/midi/src/parser.tstriggers Fast Refresh in the running app. disableHierarchicalLookup: true(metro.config.js) prevents Metro from walking up into nestednode_modules, which is what would otherwise pull in a duplicatereact.DEDUPE_SINGLETONSresolver forcesreact-native-reanimatedandreact-native-workletsto always resolve from the rootnode_modules— a nested copy inpackages/*/node_modulescauses frame-callback id collisions and release crashes. Agent rule:.cursor/rules/monorepo-metro.mdc. After editingmetro.config.js, runexpo start --clear.
The Reanimated and Unistyles babel plugins both live in the root babel.config.js, and autoProcessPaths is ['app', 'packages'] — so worklets and styled components inside packages/* get the same codegen the app sources do.
Common commands
Adding / removing a package
See adding-a-package.md for the full recipe. TL;DR:
- Copy an existing leaf package (
packages/types/is a good template). - Rename it in
package.json(@piano/<new-name>). - Add
"@piano/<dep>": "workspace:*"for any workspace deps. - Run
bun installfrom the repo root — the symlink appears undernode_modules/@piano/. - Add it to
@piano/sdk'sdependenciesandsrc/index.tsre-export if it should be part of the public surface.
Things to watch for
- Don't ship
react/react-nativefrom a package. RN packages only — the engines under@piano/*are pure TS and must stay that way. If you need RN, write a thin adapter insrc/features/<x>/instead. - Peer deps for the host app. When a package eventually does want a peer (e.g.
expo-assetfor sample loading), declare it aspeerDependenciesin the package and ensure the same exact version is in the rootpackage.json. workspace:*pins are protocol-only.bun installrewrites them to actual versions in the published artifact, but inside this repo they always resolve to the live source.turbo run buildcaches per package. If you bypass turbo and runtsupdirectly in one package, turbo will still see clean inputs next time and skip — that's intentional. To force a rebuild,bun run cleanin that package orbunx turbo run build --force.