nvm:node管理工具
nrm:npm管理工具
通过nvm安装node

package.json 依赖版本控制

为了避免部分库升级带来的不兼容等问题,做如下规定:

  • 不允许直接使用 git 地址引用的三方库版本
  • 使用遵循 semver 语义化版本控制规范的库,如若不遵循可考虑更换替代品或者锁死版本
  • 不允许使用 ^ 进行控制版本
  • 非特殊原因,统一使用最小版本控制,如:”react”: “~16.5.2”
  • 旧项目如出现依赖版本问题,请自行优化调整

    为了避免运行环境不一致造成的问题,做如下规定:

  • 明确在 package.json 添加 engines 配置,声明开发环境版本要求

  • 确保将生成的包锁 package-lock.json 提交到源代码控制,以确保团队中其他人或 CI 安装得到相同的依赖关系树

如对 npm-package-locks 有疑问,请阅读 官方文档

项目根目录下统一配置 .npmrc 文件管理依赖:

save-prefix=”~” registry=https://pkgs.d.xiaomi.net/artifactory/api/npm/mi-npm/ @mi:registry=https://pkgs.d.xiaomi.net/artifactory/api/npm/mi-npm/


使用规定的脚手架生成项目

  • 我们会统一维护 package.json 里的版本,统一升级 CLI 及模板。比如中后台应用统一使用 @mi/hi-create-app 等工具

VScode的环境配置项

.editorconfig

  1. # MIFE dotfiles
  2. root = true
  3. [*]
  4. charset = utf-8
  5. end_of_line = lf
  6. indent_size = 2
  7. indent_style = space
  8. trim_trailing_whitespace = true
  9. insert_final_newline = true
  10. [*.md]
  11. trim_trailing_whitespace = false

Workspace Settings

  1. // MIFE dotfiles
  2. {
  3. "[typescript]": {
  4. "editor.defaultFormatter": "esbenp.prettier-vscode"
  5. },
  6. "[typescriptreact]": {
  7. "editor.defaultFormatter": "esbenp.prettier-vscode"
  8. },
  9. "[javascript]": {
  10. "editor.defaultFormatter": "esbenp.prettier-vscode"
  11. },
  12. "[javascriptreact]": {
  13. "editor.defaultFormatter": "esbenp.prettier-vscode"
  14. },
  15. "eslint.validate": [
  16. "typescript",
  17. "typescriptreact",
  18. "javascript",
  19. "javascriptreact"
  20. ],
  21. "stylelint.useLocal": true,
  22. "stylelint.autoFixOnSave": true,
  23. "editor.defaultFormatter": "esbenp.prettier-vscode",
  24. "editor.codeActionsOnSave": {
  25. "source.fixAll.eslint": true,
  26. "source.fixAll.tslint": true,
  27. "source.fixAll.stylelint": true,
  28. "source.organizeImports": false
  29. }
  30. }

如果不使用脚手架配置文件:

插件安装

  • 安装 ESlint 、prettier 、stylelint以及相关辅助插件 ```tsx npm install -D eslint eslint-config-standard eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-standard prettier eslint-config-prettier eslint-plugin-prettier

npm install -D stylelint stylelint-config-standard stylelint-config-recommended-scss stylelint-scss

  1. - 如果是 JavaScript 环境,需要额外安装如下插件:
  2. ```tsx
  3. npm install -D babel-eslint
  • 如果是 TypeScript 环境,需要额外安装如下插件:

    1. npm install -D @typescript-eslint/eslint-plugin @typescript-eslint/parser

    插件配置

  • 在项目根目录下创建 .eslintrc.js 、.stylelintrc.js文件

    1. touch .eslintrc.js
    2. touch .stylelintrc.js
  • 如果是 JavaScript 环境,在 .eslintrc.js 中填充如下内容 ```tsx

    MIFE dotfiles

module.exports = { parser: ‘babel-eslint’, root: true, env: { browser: true, es2021: true }, extends: [‘plugin:react/recommended’, ‘standard’, ‘prettier’], parserOptions: { ecmaFeatures: { jsx: true }, ecmaVersion: 12, sourceType: ‘module’ }, plugins: [‘react’, ‘react-hooks’, ‘prettier’], settings: { react: { version: ‘detect’ } }, rules: { ‘prettier/prettier’: [ ‘error’, { singleQuote: true, semi: false, printWidth: 100, trailingComma: ‘none’ } ], ‘react/prop-types’: 0, ‘react/no-children-prop’: 0, ‘react-hooks/rules-of-hooks’: 2, ‘react-hooks/exhaustive-deps’: 1, ‘no-case-declarations’: 0 }, ignorePatterns: [‘!.*.js’] }

  1. - 如果是 TypeScript 环境,在 .eslintrc.js 中填充如下内容
  2. ```tsx
  3. # MIFE dotfiles
  4. module.exports = {
  5. parser: '@typescript-eslint/parser',
  6. root: true,
  7. env: {
  8. browser: true,
  9. es2021: true
  10. },
  11. extends: ['plugin:react/recommended', 'standard', 'prettier', 'prettier/@typescript-eslint'],
  12. parserOptions: {
  13. ecmaFeatures: {
  14. jsx: true
  15. },
  16. ecmaVersion: 12,
  17. sourceType: 'module'
  18. },
  19. plugins: ['@typescript-eslint', 'react', 'react-hooks', 'prettier'],
  20. settings: {
  21. react: {
  22. version: 'detect'
  23. }
  24. },
  25. rules: {
  26. 'prettier/prettier': [
  27. 'error',
  28. {
  29. singleQuote: true,
  30. semi: false,
  31. printWidth: 100,
  32. trailingComma: 'none'
  33. }
  34. ],
  35. 'no-use-before-define': 0,
  36. '@typescript-eslint/no-use-before-define': ['error', { typedefs: false, functions: false }],
  37. 'react/prop-types': 0,
  38. 'react/no-children-prop': 0,
  39. 'react-hooks/rules-of-hooks': 2,
  40. 'react-hooks/exhaustive-deps': 1,
  41. 'no-case-declarations': 0
  42. },
  43. ignorePatterns: ['!.*.js']
  44. }
  • 在 .stylelintrc.js 中填充如下内容

    1. module.exports = {
    2. // 引入标准配置文件和scss配置扩展
    3. extends: ['stylelint-config-standard', 'stylelint-config-recommended-scss'],
    4. rules: {
    5. // 引号必须为单引号
    6. 'string-quotes': ['single'],
    7. // url值必须使用单引号包裹
    8. 'function-url-quotes': ['always'],
    9. // 冒号后要加空格
    10. 'declaration-colon-space-after': ['always'],
    11. // 冒号前不加空格
    12. 'declaration-colon-space-before': ['never'],
    13. // 变量后必须添加!default,本地局部变量可以不加
    14. 'scss/dollar-variable-default': [true, { ignore: 'local' }],
    15. // 属性单独成行
    16. 'declaration-block-single-line-max-declarations': [1],
    17. // 属性和值前不带厂商标记(通过autofixer自动添加,不要自己手工写)
    18. 'property-no-vendor-prefix': [true],
    19. 'value-no-vendor-prefix': [true],
    20. // 多选择器必须单独成行,逗号结尾
    21. 'selector-list-comma-newline-after': ['always'],
    22. // 不要使用@while
    23. 'at-rule-blacklist': ['while'],
    24. // 不能使用颜色名定义颜色,只能使用HEX、rgab或hsl格式
    25. 'color-named': ['never'],
    26. // 不能有无效的16进制颜色值
    27. 'color-no-invalid-hex': [true]
    28. },
    29. ignoreFiles: ['src/**/*.tsx', 'src/**/*.ts', 'src/**/*.jsx', 'src/**/*.js']
    30. }

    提交前检查

  • 安装 husky 和 lint-staged

    1. npm install -D husky lint-staged
  • 配置 package.json 进行代码提交检查集成

    1. {
    2. "scripts": {
    3. "precommit": "lint-staged"
    4. },
    5. "lint-staged": {
    6. "*.{ts,tsx,js,jsx}": "eslint --fix",
    7. "*.{css,scss}": "stylelint --fix"
    8. },
    9. "husky": {
    10. "hooks": {
    11. "pre-commit": "npm run precommit"
    12. }
    13. }
    14. }