在上篇文章中,我们对 Changesets 的基本使用进行了简单介绍。本文将在此基础上,进一步深入讲解如何实现 Changesets 的自动化流程,帮助你更高效地管理版本发布。

前置准备

初始化 Monorepo 项目

coreplugin-dashboard 两个库中,分别提供最基础的 package.json 信息:

-| packages/
-|-| core
-|-| plugin-dashboard
-|-| ...
-| package.json
-| pnpm-workspace.yaml

然后在 coreplugin-dashboard 两个库中提供最基础的 package.json 信息:

package.json

{
  "name": "@easy-editor/plugin-dashboard",
  "version": "0.0.0",
  "description": "xxx",
  "type": "module",
  "main": "src/index.ts",
  "files": [
    "dist",
    "README.md"
  ],
  "publishConfig": {
    "access": "public",
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    "typings": "dist/index.d.ts",
    "module": "dist/index.js",
    "exports": {
      ".": {
        "import": "./dist/index.js",
        "require": "./dist/cjs/index.js",
        "default": "./dist/index.js"
      }
    }
  },
  // ...
}

初始化 Changesets

接下来,安装并配置 Changesets:

pnpm add @changesets/cli
pnpm changeset init

此时,项目结构变为:

-| ++ .changesets/
-|-| ++ config.json
-| packages/
-|-| core
-|-| plugin-dashboard
-|-| ...
-| package.json
-| pnpm-workspace.yaml

同时,为 Changesets 添加一些便捷的 scripts:

packages.json

{
	// ...
  "scripts": {
    // ...
    "pub:changeset": "pnpm changeset",
    // 换成自己的打包工具,这里使用的是 turbo
    "pub:build": "turbo run build --filter=\\"@easy-editor/*\\" && turbo run types --filter=\\"@easy-editor/*\\"",
    "pub:alpha": "pnpm changeset pre enter alpha",
    "pub:beta": "pnpm changeset pre enter beta",
    "pub:rc": "pnpm changeset pre enter rc",
    "pub:exit-pre": "pnpm changeset pre exit",
    "pub:version": "pnpm changeset version",
    "pub:release": "pnpm changeset publish"
  }
}