
作为一名开发人员,特别是如果您是团队的新手,增加价值的最快方法之一就是引入改进日常工作流程的工具。这些工具有助于维护代码质量、确保一致性并简化开发流程。以下是我认为任何 javascript 项目都必须具备的要素的列表:
基本设置:
npm install --save-dev prettier
为您的规则添加 .prettierrc 配置文件:
{
  "semi": true,
  "singlequote": false
}
在 package.json 中添加格式化脚本:
基本设置:
npm install --save-dev eslint
初始化 eslint:
npx eslint --init
安装特定于框架的插件(例如 next.js):
npm install --save-dev eslint-config-next
创建 .eslintrc 文件进行配置或使用向导设置。
设置:
立即学习“Java免费学习笔记(深入)”;
安装 husky 和 lint-staged:
npm install --save-dev husky lint-staged
启用 husky 挂钩:
npx husky install
添加预提交和预推送挂钩:
npx husky add .husky/pre-commit "npx lint-staged" npx husky add .husky/pre-push "npm run build"
在 package.json 中配置 lint-staged:
"lint-staged": {
  "*.js": ["eslint --fix", "prettier --write", "jest --findrelatedtests"]
}
设置:
立即学习“Java免费学习笔记(深入)”;
使用他们的文档将 sonarcloud 集成到您的 ci 管道中。
添加 sonar-project.properties 文件来配置扫描仪。
使用 github actions 的设置示例:
创建 .github/workflows/ci.yml 文件:
name: ci
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
        timeout-minutes: 5
        strategy:
      matrix:
        node-version: [20.x]
    steps:
      - name: checkout repository
        uses: actions/checkout@v4
      - name: install node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - name: run build
        run: npm run build
      - name: run unit tests
        run: npm run test
暂存和生产部署的设置示例:
将作业添加到 ci 管道以在测试通过后进行部署:
deployment:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      matrix:
        node-version: [20.x]
    environment:
      name: ${{ github.ref == 'refs/heads/main' && 'production' || 'staging' }}
    needs:
      - integration
    if: always() && needs.integration.result == 'success'
    steps:
      - name: checkout repository
        uses: actions/checkout@v4
      - name: install node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - name: install dependencies
        run: npm ci
      - name: install serverless globally
        run: npm install serverless@3.38.0 --global
      - name: serverless aws authentication
        run: sls config credentials --provider aws --key ${{ secrets.aws_access_key_id }} --secret ${{ secrets.aws_secret_access_key }}
      - name: deploy api
        run: serverless deploy --stage $stage
使用 cypress 的设置示例:
安装赛普拉斯:
在package.json中添加测试脚本:
"scripts": {
  "test:e2e": "cypress open"
}
设置:
立即学习“Java免费学习笔记(深入)”;
安装 typescript:
npm install --save-dev typescript
初始化 tsconfig.json 文件:
npx tsc --init
更新 package.json 中的脚本:
"scripts": {
  "build": "tsc"
}
将 .js 文件重构为 .ts 并开始享受类型安全!
添加这些工具将显着提高项目的可维护性,并帮助您的团队专注于最重要的事情:构建出色的功能。
以上就是每个 JavaScript 项目的必备要素的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号