Skip to content

Demo project to try out Yarn workspaces and Lerna features for monorepos.

License

Notifications You must be signed in to change notification settings

buffle-project/semaphore-demo-monorepo-javascript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript Monorepo Demo

Build Status

This demo repository teaches you how to use Yarn Workspaces and Lerna to manage JavaScript monorepos. You’ll build a monorepo from two separate applications.

Check the final branch to see the end result.

Contents

This repository contains two projects. Their folders are:

  • api: An API that returns geographical information.
  • web: A static website generated from the information returned by the API.

You can run each project independently or in combination. Check each folder to learn how to run them piecemeal. The next section shows how to run them together.

Setup monorepo with Yarn workspaces

  1. Fork this repo.
  2. Clone it to your machine.
  3. Download latest yarn version.
    $ yarn set version berry
  4. Initialize top-level package.json.
    $ yarn init -w
  5. Move projects to their workspaces.
    $ git mv api web packages
    $ yarn workspaces list
  6. Install all modules.
    $ yarn install
  7. Delete old yarn.lock
    $ git rm packages/api/yarn.lock packages/web/yarn.lock
  8. Add the new files to the repository. This will also commit all dependencies.
    $ git add .yarnrc.yml .yarn yarn.lock .gitattributes .gitignore package.json .pnp.js
    $ git commit -m "finalize monorepo setup"
    $ git push

You can peek at the final state of the pipeline in the final branch.

Final pipeline

Run development version

$ yarn workspace api start &
$ yarn workspace web dev

Run tests

# Linting
$ yarn workspace api lint
$ yarn workspace web test

# Unit tests - api
$ yarn workspace api start &
$ yarn workspace api test

# Unit tests - web
$ yarn workspace web test

# Integration tests
$ yarn workspace api start &
$ yarn workspace web build
$ yarn workspace web start &
$ yarn workspace web test-integration

Add modules in one workspace

$ yarn workspace <workspace-name> add <package-name>

Run scripts

$ yarn worksapce <workspace-name> run <script-name>

Build static site

$ yarn workspace api start &
$ yarn workspace web build
$ yarn workspace web export

Tips for Yarn

A note about Yarn Plug’n’play: scripts running from Scripts in package.json or with the yarn run command will work. But if you run a program directly as node app.js it will fail. Instead, run it like yarn node app.js this will initialize PnP.

If you’re using zero-installs and accepting PRs from untrusted sources, it’s a good idea to add a cache check job somewhere in your CI pipeline. This command will re-download all the modules and check them against the cache to ensure none of the dependencies were modified maliciously.

yarn install --check-cache

Setup a monorepo with Lerna

To setup a monorepo with Lerna + NPM, first initialize the repository.

$ lerna init
$ lerna import api
$ lerna import web

Now Lerna should detect the monorepo.

$ lerna ls
api
web
found 2 packages

With bootstrap, Lerna downloads dependencies with NPM and links the packages.

$ lerna bootstrap

Run the tests for each package in the monorepo.

$ lerna exec npm run lint
$ lerna exec npm dev &
$ lerna exec npm test
$ lerna exec npm run test integration

Push the updates to the repository.

$ git add lerna.json package.json packages
$ git commit -m "install lerna, ready to publish"

Publish the new version to NPM. You need to be already-authenticated with npm login.

$ lerna version
$ lerna publish from-git

Tips for coupling Lerna and Workspaces

Add the following lines to lerna.json.

 "npmClient": "yarn",
 "useWorkspaces": true

License

MIT License

Copyright (c) 2021 Rendered Text

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Demo project to try out Yarn workspaces and Lerna features for monorepos.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.6%
  • CSS 2.4%