目錄
React Native 項目初始化工作
一、目錄結構
二伟阔、插件配置
eslint
prettier
husky
TypeScript
目錄結構
demo
├── .buckconfig
├── .eslintrc.js
├── .gitattributes
├── .prettierrc.js
├── .watchmanconfig
├── android
├── babel.config.js
├── commitlint.config.js
├── index.js
├── ios
├── metro.config.js
├── package.json
├── src
| ├── app.json
| ├── App.tsx
| ├── components
| ├── reduxState
| ├── routers
| ├── screens
| | └── Navigation
| ├── types
| └── utils
├── tsconfig.json
└── yarn.lock
依賴配置
eslint
-
配置.eslintrc.js 文件
module.exports = { root: true, extends: '@react-native-community', parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], rules: { semi: ['error', 'never'], 'linebreak-style': 'error', 'comma-dangle': ['warn', 'never'], 'no-unused-vars': 'warn', 'react-native/no-inline-styles': 'off', 'eslint-disable-next-line': 'off' } }
prettier
-
配置.prettierrc.js 文件修改
module.exports = { bracketSpacing: false, jsxBracketSameLine: true, singleQuote: true, endOfLine: 'lf', semi: false, trailingComma: 'none' }
stylelint
-
安裝
yarn add --dev stylelint stylelint-config-standard stylelint-config-css-modules stylelint-config-prettier stylelint-config-rational-order stylelint-declaration-block-no-ignored-properties stylelint-order
-
配置 .stylelintrc文件
// package.json { "extends": [ "stylelint-config-standard", "stylelint-config-css-modules", "stylelint-config-rational-order", "stylelint-config-prettier" ], "plugins": ["stylelint-order", "stylelint-declaration-block-no-ignored-properties"], "rules": { "no-descending-specificity": null, "plugin/declaration-block-no-ignored-properties": true, "at-rule-no-unknown": null } }
husky
-
安裝
yarn add --dev husky npm global add lint-staged @commitlint/cli @commitlint/config-conventional
-
配置
// package.json { ... "lint-staged": { "*.{ts,tsx,js,jsx}": [ "prettier -w", "eslint" ] }, "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", "pre-commit": "lint-staged" } }, ... }
-
根目錄下創(chuàng)建文件
commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional'] }
TypeScript
通過TypeScript使用自定義路徑別名
-
編輯您的文件
tsconfig.json
以具有自定義的路徑映射。將的根目錄中的任何內容設置src
為可用忌愚,而無需前面的路徑引用,并允許使用test/File.tsx
以下命令訪問任何測試文件:"target": "esnext", + "baseUrl": ".", + "paths": { + "*": ["src/*"], + "tests": ["tests/*"], + "@components/*": ["src/components/*"], + }, }
-
通過添加新的依賴項來配置Babel端
babel-plugin-module-resolver
:yarn add --dev babel-plugin-module-resolver
-
最后转培,配置您的
babel.config.js
(請注意耿芹,您的語法與的語法babel.config.js
不同tsconfig.json
):{ plugins: [ + [ + 'module-resolver', + { + root: ['./src'], + extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'], + alias: { + "tests": ["./tests/"], + "@components": "./src/components", + } + } + ] ] }