简体   繁体   English

在 package.json 上发布语义发布

[英]Semantic Release publishing on package.json

I am currently try to auto publish my npm package to GitHub packages using semantic release with GitHub Actions.我目前正在尝试使用语义发布和 GitHub 操作将我的 npm package 自动发布到 GitHub 包。 The package publishes successfully but when Installed, only the package.json file is found in the published package. package 发布成功,但安装后,在发布的 package 中仅找到package.json文件。

I have a build command that builds my package into a dist folder and would like to publish the contents of the dist folder to GitHub packages.我有一个构建命令,可以将我的 package 构建到dist文件夹中,并希望将 dist 文件夹的内容发布到 GitHub 包中。

Below are the configuration files for the package release and GitHub Actions workflow以下是 package 版本和 GitHub 操作工作流的配置文件

The workflow below runs the test and creates a new release to GitHub Packages下面的工作流运行测试并创建一个新版本到 GitHub 包

test-publish.yml测试发布.yml

name: Test and Publish
on:
  push:
    branches: 'main'
    paths-ignore:
      - .gitignore
      - README.md

jobs:
  test-publish:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Check for forgotten it.only in unit tests
        run: "! git grep -F 'it.only' -- src/__tests__"
      - name: Install dependencies
        run: yarn
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Run Tests
        run: yarn test
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Prepare Publish to NPM
        run: yarn run preparePub
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Release
        run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GITHUB_TOKEN_NPM: ${{ secrets.NPM_TOKEN }}

Below is the package.json package.json下面是 package.json package.json

{
  "name": "@organisation/component-library",
  "version": "0.0.0-semantic-release",
  "author": "Author Name",
  "license": "MIT",
  "description": "Component library",
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/organisation/component-library.git"
  },
  "main": "dist/cjs/index.js",
  "module": "dist/esm/index.js",
  "files": [
    "dist"
  ],
  "scripts": {
    "build": "rm -rf dist/ && yarn run prettier --write src/ && yarn run lint && yarn build:esm && yarn build:cjs",
    "build:cjs": "tsc --module CommonJS --OutDir dist/cjs",
    "build:esm": "tsc",
    "lint": "eslint src/**/*.ts src/**/*.tsx",
    "test": "jest",
    "preparePub": "yarn run build &&  cp package.json dist"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.19.1",
    "@babel/preset-react": "^7.18.6",
    "@babel/preset-typescript": "^7.18.6",
    "@semantic-release/commit-analyzer": "^9.0.2",
    "@semantic-release/github": "^8.0.6",
    "@semantic-release/npm": "^9.0.1",
    "@semantic-release/release-notes-generator": "^10.0.3",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@types/jest": "^29.0.3",
    "@types/node": "^18.7.15",
    "@types/react": "^18.0.18",
    "@types/react-dom": "^18.0.6",
    "@types/styled-components": "^5.1.26",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-standard-with-typescript": "^23.0.0",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-jsx-a11y": "^6.6.1",
    "eslint-plugin-n": "^15.0.0",
    "eslint-plugin-promise": "^6.0.0",
    "eslint-plugin-react": "^7.31.8",
    "husky": "^8.0.1",
    "jest": "^29.0.3",
    "jest-environment-jsdom": "^29.0.3",
    "prettier": "^2.7.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "typescript": "*"
  },
  "peerDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },
  "husky": {
    "hooks": {
      "pre-commit": "yarn run lint && git add -A ."
    }
  },
  "dependencies": {
    "styled-components": "^5.3.6"
  }
}

The preparePub script is used to build the library and copy the package.json file to the list folder preparePub脚本用于建库,复制package.json文件到list文件夹

Below is the semantic release config file where I set the package root to dist .releaserc下面是语义发布配置文件,我将 package root 设置为 dist .releaserc

{
  "branches": [
    "main",
    {
      "name": "beta",
      "prerelease": true
    }
  ],
  "repositoryUrl": "https://github.com/organisation/component-library",
  "debug": "true",
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    [
      "@semantic-release/npm",
      {
        "pkgRoot": "dist"
      }
    ],
    "@semantic-release/github"
  ]
}

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "lib": ["ES5", "ES2015", "ES2016", "DOM", "ESNext"],
    "jsx": "react",
    "module": "ES2015",
    "moduleResolution": "node",
    "types": ["node", "jest", "@testing-library/jest-dom"],
    "declaration": true,
    "sourceMap": true,
    "outDir": "dist/esm",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
  },
  "include": ["src/**/*.ts", "src/**/*.tsx", "src/jest.setup.ts"]
}

The issue here is that the GitHub Actions pipeline seem to work fine but after the package is released to GitHub packages and installed in whatever app I want to use the library, only the package.json file is found within the installed library and none of the contents of the list folder.这里的问题是 GitHub Actions 管道似乎工作正常但是在 package 被发布到 GitHub 包并安装在我想使用该库的任何应用程序中之后,只有package.json文件在已安装的库中找到并且没有任何内容列表文件夹。

I assume when you build locally, you have a build folder with package.json and a cjs folder, but only the file itself is coming through (since that's what I did in the past).我假设当您在本地构建时,您有一个包含 package.json 的build文件夹和一个 cjs 文件夹,但只有文件本身通过(因为这是我过去所做的)。

Try having your package.json files property look like this instead:尝试让您的 package.json files属性看起来像这样:

"files": [
  "dist/*"
],

That's what I needed to get it to publish folders as well.这也是我发布文件夹所需要的。

The issue was with pkgRoot in .releaserc .问题出在pkgRoot中的.releaserc Instead of setting it to dist , I set to dist/ and it somehow worked.我没有将它设置为dist ,而是设置为dist/并且它以某种方式工作。 Funny thing is, I have configured with just dist before and it worked.有趣的是,我之前只配置了dist并且它有效。 Thank you all for the effort.谢谢大家的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM