Shared dev command in monorepo
πŸ‰

Shared dev command in monorepo

Tags
Technology
Description
Typescript monorepo done differently
Published
Published February 7, 2022

Context

Β 
Given a Yarn Workspace with codebase all written in TypeScript
  • lib (e.g. a component library)
  • lib2, lib3 … (e.g. lodash utility library)
  • app (e.g. a CRA app)
Β 
In lib, we have
import lib2 from β€œlib2” import lib3 from β€œlib3”
Β 
In app, we have
import lib from β€œlib”
Β 
A common dev workflow could be
  1. work on app
  1. notice lib needs update
  1. notice lib2 and lib3 need update
  1. lib2 and lib3 updated
  1. lib updated
  1. back to continue working on app
Β 

The Problem

Β 
  • There are cross dependencies among those workspaces
  • The workspaces need to be build in the correct order (i.e. topological order) for app to work properly
Β 
Yarn workspace addressed the first problem pretty well but left the second to developers.That's you're likely required to a. run yarn watch/yarn dev in each workspace b. in a right order.
But can we just use a shared build cycle for all workspaces above?? Just like all those libs are part of the app workspace for development.
Β 

The Trick

Points the main/exports field in package.json to src/index.ts for all libs.
Β 

Caveat

There is clear trade-offs in this approach, this essentially turns monorepo into a single package during local development. But you will still need to figure out how to release/publish those libraries https://yarnpkg.com/features/workspaces#publishing-workspaces, which is a separate problem to solve.