Using Changesets with pnpm

Using Changesets with pnpm - 图1note

At the time of writing this documentation, the latest pnpm version was v6.14. The latest Changesets version was v2.16.0.

Setup

To setup changesets on a pnpm workspace, install changesets as a dev dependency in the root of the workspace:

  1. pnpm add -Dw @changesets/cli

Then changesets’ init command:

  1. pnpm changeset init

Adding new changesets

To generate a new changeset, run pnpm changeset in the root of the repository. The generated markdown files in the .changeset directory should be committed to the repository.

Releasing changes

  1. Run pnpm changeset version. This will bump the versions of the packages previously specified with pnpm changeset (and any dependents of those) and update the changelog files.
  2. Run pnpm install. This will update the lockfile and rebuild packages.
  3. Commit the changes.
  4. Run pnpm publish -r. This command will publish all packages that have bumped versions not yet present in the registry.

Using GitHub Actions

To automate the process, you can use changeset version with GitHub actions.

Bump up package versions

The action will detect when changeset files arrive in the main branch, and then open a new PR listing all the packages with bumped versions. Once merged, the packages will be updated and you can decide whether to publish or not by adding the publish property.

Publishing

Add a new script ci:publish which executes pnpm publish -r. It will publish to the registry once the PR is opened by changeset version.

package.json

  1. {
  2. "scripts": {
  3. "ci:publish": "pnpm publish -r"
  4. },
  5. ...
  6. }
  1. name: Changesets
  2. on:
  3. push:
  4. branches:
  5. - main
  6. env:
  7. CI: true
  8. PNPM_CACHE_FOLDER: .pnpm-store
  9. jobs:
  10. version:
  11. timeout-minutes: 15
  12. runs-on: ubuntu-latest
  13. steps:
  14. - name: checkout code repository
  15. uses: actions/checkout@v3
  16. with:
  17. fetch-depth: 0
  18. - name: setup node.js
  19. uses: actions/setup-node@v3
  20. with:
  21. node-version: 14
  22. - name: install pnpm
  23. run: npm i pnpm@latest -g
  24. - name: Setup npmrc
  25. run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
  26. - name: setup pnpm config
  27. run: pnpm config set store-dir $PNPM_CACHE_FOLDER
  28. - name: install dependencies
  29. run: pnpm install
  30. - name: create and publish versions
  31. uses: changesets/action@v1
  32. with:
  33. version: pnpm ci:version
  34. commit: "chore: update versions"
  35. title: "chore: update versions"
  36. publish: pnpm ci:publish
  37. env:
  38. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

More info and documentation regarding this action can be found here.