Home  >  All Categories  >  Dev  >  Nodejs Monorepo

Nodejs Monorepo

Use changesets as monorepo mananger.

Tools🔗

GitHub - atlassian/changesets: 🦋 A way to manage your versioning and changelogs with a focus on monorepos

Use Changesets as monorepo manager🔗

Init a monorepo🔗

mkdir monorepo && cd monorepo
git init
# Add .gitignore file for nodejs <https://github.com/github/gitignore/blob/master/Node.gitignore>
npm init --yes

Add "private":"true" to the root package.json

npm init -w packages/a
npm init -w packages/b
npm init -w packages/c

Let c depends a and b,

Add

"dependencies": {
  "a":"^1.0.0",
  "b":"^1.0.0"
}

to packages/c/package.json

Install changesets🔗

Also see here

npm install -D @changesets/cli && npx changeset init

This action will add a .changeset folder, and a couple of files to help you out:

You should change the .changeset/config.json -> baseBranch to yourself main branch, also change access to public

Commit current files.

git add .
git commit -m "feat: init"

That’s done.

Usage🔗

First publish🔗

First publish you should just use the follow command to publish your module

npx changeset publish

Future changes🔗

You should see changesets philosophy

You should first create an intent to change, that said, before what ever changes you want to make, you should create a intent change

npx changeset

…Make some changes

Now, generate new version, changeset will take care your dependences,

Note, by default, if a upgrade from 1.0.1 to 1.0.2, c depends on a@^1.0.1, then c package.json will not change, cause npm will auto update a@^1.0.1 to the last version 1.0.2 if you want change to the exact version every time, you can set the config to

 "___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
    "updateInternalDependents": "always"
 }
npx changeset version

Then, you can publish it.

npx changeset publish
git add .
git commit -m "chore: version"
git push --follow-tags

With CI🔗

I’ll use Github Actions as example.

The CI workflow assume that you use 开发技巧收藏 as your git workflow.

Note, you can use @changesets/changelog-github as your changelog format log. with this, you can generate a log like this , without this, then the log will only include commit link > npm i -D @changesets/changelog-github

{
  "changelog": [
    "@changesets/changelog-github",
    { "repo": "theowenyoung/monorepo-example" }
  ]
}
  1. Create a script in root package.json
{
  "scripts": {
    "version-packages": "changeset version",
    "release": "changeset publish"
  }
}
  1. Create github actions workflows
mkdir -p .github/workflows
touch .github/workflows/release.yml

With the following content:

name: Release

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: Setup Node.js 12.x
        uses: actions/setup-node@master
        with:
          node-version: 12.x
      - name: Setup NPM Latest
        run: npm i -g npm
      - name: Install Dependencies
        run: npm i

      - name: Create Release Pull Request or Publish to npm
        uses: changesets/action@master
        with:
          # this expects you to have a script called release which does a build for your packages and calls changeset publish
          publish: npm run release
          version: npm run version-packages
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
  1. Add NPM_TOKEN to your github repo secret settings

By 开发技巧收藏

npm token create

Done.

Every time you use npx changeset to generate a new changeset intent, and the change is pulled to the main branch, then CI will create a pull request to generate a new version, and after the pull request was merged, CI will publish npm packages, and create a new release.

Category: Dev 
TagsJavascriptNodejs
Published:   📝 Edit this page

该博客没有评论系统,我正在实验有意义的讨论是否更易在社群中发生,不知道为什么,人在社群中,似乎会有更好的行为规范。然后如果有高质量的讨论,我会手动再精选到原文。为此,我建立了一个 Telegram 讨论群组,你可以在里面讨论这篇文章,点此复制文章标题和链接