pnpm changesets 发布npm包入门

需求

pnpm Monorepo 多包项目,使用 changesets 来发布 npm 包。

解决

  1. 登陆 npm 账户

确认下本地当前的 registry 地址

npm config get registry

显示https://registry.npmjs.org/即可,如果不是,使用以下命令切换下

npm config set registry https://registry.npmjs.org/

再查看下当前登陆的 npm 账户

npm whoami

如果不是你想要的账户,退出

npm logout

再登陆自己的账户

npm login
  1. 安装 Changesets

安装依赖

npm install @changesets/cli
  1. 增加你要发布的包
npx changeset

根据交互提示来选择你想要发布的子包

D:\code\github\openHacking>npx changeset
🦋  Which packages would you like to include? · @openHacking/core
🦋  Which packages should have a major bump? · No items were selected
🦋  Which packages should have a minor bump? · No items were selected
🦋  The following packages will be patch bumped:
🦋  @openHacking/core@0.0.1
🦋  Please enter a summary for this change (this will be in the changelogs).
🦋    (submit empty line to open external editor)
🦋  Summary · init npm package
🦋
🦋  === Summary of changesets ===
🦋  patch:  @openHacking/core
🦋
🦋  Note: All dependents of these packages that will be incompatible with
🦋  the new version will be patch bumped when this changeset is applied.
🦋
🦋  Is this your desired changeset? (Y/n) · true
🦋  Changeset added! - you can now commit it
🦋
🦋  If you want to modify or expand on the changeset summary, you can find it here
🦋  info D:\code\github\openHacking\.changeset\rich-tools-pretend.md
  1. 升级版本
npx changeset version
  1. 发布
npx changeset publish

发布 Prerelease

发布正式版本之前,我们可能需要先发布 alpha、 beta 版本,或着叫 Prerelease 版本,就需要用到changeset pre命令

enter进入 Prerelease 模式 exit退出 Prerelease 模式

发布 Prerelease

yarn changeset pre enter next
yarn changeset version
git add .
git commit -m "Enter prerelease mode and version packages"
yarn changeset publish
git push --follow-tags

退出 Prerelease 模式,开始发布 release

yarn changeset pre exit
yarn changeset version
git add .
git commit -m "Exit prerelease mode and version packages"
yarn changeset publish
git push --follow-tags

参考

评论