🌞Moon Will Know
🏗️

前端项目规范化——环境变量和 git commit 控制

引入环境变量

  1. npm install dotenv --save
  1. 在根目录新建.env 文件
S3_BUCKET="YOURS3BUCKET" SECRET_KEY="YOURSECRETKEYGOESHERE"
  1. 在文件中引入
import 'dotenv/config' import express from 'express' console.log(process.env.S3_BUCKET)

git 提交钩子

  1. npm install husky --save-dev
  1. package.json 中设置
// package.json { //... "script": { // ... "prepare": "husky install", } }
  1. 执行命令,添加hook
npx husky add .husky/pre-commit "yarn lint-staged" git add .husky/pre-commit
  1. 安装stylelinteslintprettier 并配置相应格式化文件
  1. 配置package.json实现代码格式化
// package.json { ... "lint-staged": { "**/*.{json,html,css,less,scss,md,yaml,yml}": [ "prettier --write" ], "**/*.{css,less,scss}": [ "stylelint --fix" ], "**/*.{js,jsx,ts,tsx}": [ "eslint --fix", "prettier --write" ] } }
  1. 安装commitlint cliconventional config
npm install --save-dev @commitlint/config-conventional @commitlint/cli
  1. 添加hook 实现 commit规范
npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1" echo "module.exports = {extends: ['@commitlint/config-conventional']}" > .commitlintrc.j