在本指南中,我們將使用 PNPM 和變更集 cli 發布一個名為「npm-package-template-changesets」的簡單 NPM 打字稿包。當我們對庫進行任何更改時,自動化部分就會出現,機器人將打開一個需要批准的拉取請求,並將包含要包含在新版本以及更改日誌中的所有更改。
該軟體包將支援舊版的 CJS 和 ESM。
npm install -g pnpm
pnpm init
這將產生一個 package.json 文件,將 name 屬性更改為尚未存在的套件名稱:
同時在 GitHub 上建立一個新的儲存庫並將 url 新增至repository.url 屬性中,這對於出處很重要:
{ "name": "npm-package-template-changesets", "repository": { "url": "https://github.com/sebastianwd/npm-package-template-changesets" }, "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "license": "ISC" }
pnpm add tsup typescript @changesets/cli -D
對於這種情況,我們將使用 2 個 tsconfig 檔案:tsconfig.build.json 和 tsconfig.json。它們之間的區別在於tsconfig.build.json 將使用屬性compoto: true 和rootDir: "./src" 因此構建僅查看src 目錄中的文件,而在開發中tsconfig.json 將覆蓋這些設定並使用rootDir" : "." 在根層級啟用設定檔的打字稿。
tsconfig.build.json
{ "compilerOptions": { /* Base Options: */ "rootDir": "./src", "esModuleInterop": true, "skipLibCheck": true, "target": "es2022", "allowJs": true, "resolveJsonModule": true, "moduleDetection": "force", "isolatedModules": true, "verbatimModuleSyntax": true, /* Strictness */ "strict": true, "noUncheckedIndexedAccess": true, "noImplicitOverride": true, "module": "preserve", "outDir": "dist", "sourceMap": true, "declaration": true, "composite": true, "noEmit": true, /* If your code doesn't run in the DOM: */ "lib": [ "es2022" ], }, "include": [ "src" ], }
{ "extends": "./tsconfig.build.json", "compilerOptions": { "composite": false, "rootDir": "." } }
索引.ts
export const hello = () => "hello world";
"scripts": { "build": "tsup src", "release": "changeset", "ci:publish": "pnpm build && changeset publish --access public" }
"publishConfig": { "provenance": true, "access": "public" }
"type": "module", "exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.mjs", "require": "./dist/index.cjs" } }, "main": "dist/index.cjs", "module": "dist/index.mjs", "types": "dist/index.d.ts",
要建立 .cjs 和 .mjs 副檔名的文件,我們可以使用 tsup:
tsup.config.ts
import { defineConfig } from "tsup"; export default defineConfig({ entry: ["src/index.ts"], splitting: false, clean: true, dts: true, format: ["cjs", "esm"], outExtension({ format }) { return { js: format === "cjs" ? ".cjs" : ".mjs", }; }, });
name: Publish on: push: branches: - master concurrency: ${{ github.workflow }}-${{ github.ref }} permissions: contents: write pull-requests: write packages: write id-token: write jobs: publish: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 with: version: 9 - uses: actions/setup-node@v4 with: node-version: 20.x cache: "pnpm" registry-url: "https://registry.npmjs.org" - run: pnpm install --frozen-lockfile - name: Create Release Pull Request or Publish id: changesets uses: changesets/action@v1 with: publish: pnpm run ci:publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
然後,在 Github 上建立一個新的儲存庫,前往「設定」並將其新增至秘密:
另請前往「操作」>一般
並啟用此選項,否則變更集將無法開啟 PR:
npm install -g pnpm
pnpm init
{ "name": "npm-package-template-changesets", "repository": { "url": "https://github.com/sebastianwd/npm-package-template-changesets" }, "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "license": "ISC" }
如果您收到有關「無法找到 HEAD 從 main 分支的位置」的錯誤,請在 .changeset/config.json 中設定您的基礎分支
系統會提示您一些選項,在本例中我們將選擇補丁:
pnpm add tsup typescript @changesets/cli -D
{ "compilerOptions": { /* Base Options: */ "rootDir": "./src", "esModuleInterop": true, "skipLibCheck": true, "target": "es2022", "allowJs": true, "resolveJsonModule": true, "moduleDetection": "force", "isolatedModules": true, "verbatimModuleSyntax": true, /* Strictness */ "strict": true, "noUncheckedIndexedAccess": true, "noImplicitOverride": true, "module": "preserve", "outDir": "dist", "sourceMap": true, "declaration": true, "composite": true, "noEmit": true, /* If your code doesn't run in the DOM: */ "lib": [ "es2022" ], }, "include": [ "src" ], }
CI 完成後,檢查儲存庫上的 Pull Requests 選項卡,應該有一個開啟的
檢視並合併它。
以上是使用變更集和 GitHub Actions 自動發布 NPM 包的詳細內容。更多資訊請關注PHP中文網其他相關文章!