Setup ESLint and stylelint in your project to avoid common problems and to keep CSS consistent.
- ESLint is used to warn for unused CSS.
- stylelint is used to make sure that the CSS is compatible with both Web and React Native, and to warn for duplicate properties, vendor prefixes, incompatible units, etc.
yarn add eslint eslint-plugin-css-modules stylelint stylelint-react-native stylelint-config-react-native-css-modules --dev
Add these configs to your project's package.json
(or use .stylelintrc
and .eslintrc
files).
ESlint:
"eslintConfig": {
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017,
"ecmaFeatures": {
"jsx": true
}
},
"plugins": [
"css-modules"
],
"extends": [
"plugin:css-modules/recommended"
]
},
stylelint:
"stylelint": {
"extends": "stylelint-config-react-native-css-modules",
"rules": {
"declaration-block-no-duplicate-properties": true,
"no-duplicate-selectors": true,
"no-extra-semicolons": true,
"no-eol-whitespace": true,
"no-missing-end-of-source-newline": true
}
}
In your project's package.json
, add a new command called lint
to scripts
, so that it looks like this:
"scripts": {
"lint": "eslint . && stylelint '**/*.@(css|scss|sass|less|styl)'"
}
You can remove file extensions that you don't use from the stylelint command.
Run yarn lint
in a terminal window to see if there are any linting errors or warnings.