Monorepo with Lerna

Monolith?

still bad

monorepo β‰  monolith

Modularity

Unix Philosophies

  • Write programs that do one thing and do it well
  • Write programs that work together

https://en.wikipedia.org/wiki/Unix_philosophy

a key to JavaScript's growth and success

npm 🧑 modularity

>600,000 modules on npm

Orchestrating
high modularity

Repository proliferation

πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡πŸ‡

  • where is it
  • what's in it
  • who owns it

inter-dependency management

  • tracking compatibility between versions
  • updating downstream version strings

Testing

  • Find and clone related modules
  • run npm install & npm link
  • run all the tests

β˜‘οΈ
β˜‘οΈ
β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

β˜‘οΈ

Publishing

  • Build
  • Publish
  • Tag & push
  • ...all the right modules

Solutions

More developersΒ  πŸ‘¨β€πŸ’»
more discipline πŸ‹οΈβ€β™‚οΈ

more process 🚦
and
more managers πŸ’πŸ˜±

Custom automation

monorepo

A single repository holding the code of many modules

Monorepos in the Wild - Markus Oberlehner

πŸ“πŸŠ

πŸ₯“πŸ₯¦

βœ”οΈ

βœ”οΈ

🍏🍎

βœ”οΈ

./my-component-library
    /packages
        /Component1
        /Component2
        /Component3
        /Component4
        /Component5
  • Twitter
  • Facebook
  • Google
  • Babel
  • React
  • Jest
  • Vue
  • Angular...

monorepo

Each component/module can stand on it's own!

monorepo

Solves

Doesn't Solve

  • inter-dependency management
  • publishing complexity
  • testing complexity

Pros

  • One source of truth
  • Sweeping changes in 1 commit
  • Shared metadata & config

Cons

  • Slow git
  • Slow builds
  • Permission management
  • git repository proliferation

Yarn Workspaces

https://yarnpkg.com/lang/en/docs/workspaces/

Β a new way to setup your package architecture

Yarn workspaces

./my-component-library
    /Component1
    /Component2
    /Component3
    /Component4
    /Component5

πŸ“πŸŠ

πŸ₯“πŸ₯¦

βœ”οΈ

βœ”οΈ

🍏🍎

βœ”οΈ

Yarn Workspaces

Solves

  • git repository proliferation

Doesn't Solve

  • inter-dependency management
  • publishing complexity
  • testing complexity

Pros

  • One source of truth
  • Sweeping changes in 1 commit
  • Shared metadata & config
  • Dependency optimization

Cons

  • Slow git
  • Slow builds
  • Permission management

Bolt

https://github.com/boltpkg/bolt

Super-powered JavaScript project management

once we get around to implementing it

Bolt

  • command-line tools for multi-module orchestration
    • running tests
    • compiling code
    • publishing modules
    • lots more
  • for your yarn worksapces

Solves

Doesn't Solve

  • inter-dependency management
  • publishing complexity
  • testing complexity

Bolt with Yarn Workspaces

Pros

  • One source of truth
  • Sweeping changes in 1 commit
  • Shared metadata & config
  • Dependency optimization
  • Automated testing
  • Easier dev env setup & contribution

Cons

  • Slow git
  • Slow builds
  • Permission management

{

  • git repository proliferation

Lerna

https://lernajs.io

A tool for managing JavaScript projects with multiple packages

Lerna

  • command-line tools for multi-module orchestration
    • running tests
    • compiling code
    • publishing modules
    • more
  • for your monorepo

Lerna with monorepo

Solves

  • git repository proliferation
  • inter-dependency management
  • publishing complexity
  • testing complexity

Doesn't Solve

Pros

  • One source of truth
  • Sweeping changes in 1 commit
  • Shared metadata & config
  • Dependency optimization
  • Automated testing
  • Easier dev env setup & contribution

Cons

  • Slow git
  • Slow builds
  • Permission management

Monorepo with Lerna

monorepo with Lerna

  • keep all your modules in one git repository
  • shared metadata and config resides at the root
  • each module remains independent
  • Lerna simplifies the orchestration

Lerna commands

lerna init

  • creates a lerna.json config file βš™οΈ
  • creates a packages folder πŸ“‚
  • adds lerna as a dev dependency in package.json
  • -i for independent versioning

Create a new Lerna repo or upgrade an existing repo to the current version of Lerna

lerna bootstrap

  • installs external dependencies
  • symlinks internal dependencies
  • ...

Links dependencies in the repo together

lerna updated

list which packages have changed since the last release

lerna run/exec

  • run npm scripts in some or all modules
  • execute arbitrary commands in some or all modules

lerna publish

  • Update the version updated packages
  • Update all dependencies of the updated packages with the new versions
  • Commit and tag the new version
  • Publish updated packages to npm

publish packages in the current Lerna project

other lerna commands

  • clean
    delete all node_modules folders
  • diff
    diff packages since the latest release
  • ls
    list all the public packages in the repo
  • import
    import an existing module including git history
  • link
    symlink together all packages that are dependencies of each other in the current repo
  • ...

Demo

Lerna & monorepo uses

  • Eden component library
  • Portfolio specific component libraries

Almost The End

https://www.youtube.com/watch?v=lV8-1S28ycM

https://www.youtube.com/watch?v=kwhOI4mmqnM
https://medium.com/@maoberlehner/monorepos-in-the-wild-33c6eb246cb9

http://slides.com/olegskl/lerna#/2

http://slides.com/clickclickonsal/deck-8#/2/3

http://danluu.com/monorepo/

https://github.com/babel/babel/blob/master/doc/design/monorepo.md

https://graphcommons.com/graphs/a7ec343d-2a0c-47bb-9658-bb8315e8a096

https://macwright.org/2016/07/08/lerna-npm-organizations-new-wave-modularity.html

https://github.com/lerna/lerna/blob/master/doc/hoist.md

...

The End

https://en.wikipedia.org/wiki/Unix_philosophy

https://yarnpkg.com/blog/2017/08/02/introducing-workspaces/

https://www.drmaciver.com/2016/10/why-you-should-use-a-single-repository-for-all-your-companys-projects/

https://en.wikipedia.org/wiki/Monolithic_application

https://github.com/boltpkg/bolt

https://slides.com/chrisweight/monorepo#/2/3

http://danluu.com/monorepo/

https://yarnpkg.com/lang/en/docs/workspaces/

https://github.com/boennemann/alle

https://github.com/lerna/lerna/blob/master/doc/hoist.md

Monorepos with Lerna

By Bruce Campbell

Monorepos with Lerna

  • 1,756