React project config set up for: Create React App + TypeScript + ESLint + Prettier + WebStorm from scratch.
Recommend to use Yarn as a package manager.
- To install Create React App select a start directory for a new project (folder with all projects) and enter the following command:
yarn create react-app your-app-name --template typescriptChange the your-app-name to desired project name and note that we are using TypeScript as our template language.
- To install ESLint open the root project directory and enter the following command:
yarn add -D eslintNote that we are using -D flag to install the package in devDependencies.
- After installing ESLint we need to initialize the configuration file. To do this, enter the following command:
npx eslint --initAnd answer the next questions:
How would you like to use ESLint? …
To check syntax only
▸ To check syntax and find problems
To check syntax, find problems, and enforce code style What type of modules does your project use? …
▸ JavaScript modules (import/export)
CommonJS (require/exports)
None of these Which framework does your project use? …
▸ React
Vue.js
None of these Does your project use TypeScript?
‣ Yes / No Where does your code run? … (Press <space> to select, <a> to toggle all, <i> to invert selection)
✔ Browser
✔ Node What format do you want your config file to be in? …
JavaScript
YAML
▸ JSON The config that you've selected requires the following dependencies:
eslint-plugin-react@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
? Would you like to install them now with npm? ‣ No / Yes- Since we chose No for the last questions (we use Yarn), we need to install the suggested dependencies:
yarn add -D eslint-plugin-react@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest- To install TypeScript plugins related to ESLint enter the following command:
yarn add -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript- To install Prettier enter the following command:
yarn add -D prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react-hooks- To install Prettier plugin sort imports enter the following command:
yarn add @trivago/prettier-plugin-sort-imports-
Create
.prettierrcfile in the root directory and fill the rules. See example here -
Create
.prettierignorefile in the root directory and fill the rules. See example here -
Fill the
.eslintrc.jsonfile as in example here. ❗ Note that the order in which the "extends" object keys are placed is important. -
Create
.eslintignorefile in the root directory and fill the rules. See example here -
Fill the
tsconfig.jsonfile as in example here -
Create a
.envfile in the root directory. You can add a row likePORT=3002to the.envfile, and run the project on the desired port. ❗ Note that we need to put.envfile inside.gitignorein order to exclude data sharing from it. -
To install
Prettierplugin to WebStorm open the Webstorm > Preferences > Plugins, find Prettier and install it. -
To run
Prettierautomatically against specific files, open the Webstorm > Preferences > Languages & Frameworks > JavaScript > Prettier, select the right path to Prettier package, and use the "On reformatting code" and "On save" checkboxes to specify the actions that will trigger Prettier.
The project structure should look like this:
.
├── public
├── src
├── .env
├── .eslintignore
├── .eslintrc.json
├── .gitignore
├── .gitignore
├── .prettierignore
├── .prettierrc
├── package.json
├── README.md
└── tsconfig.json
To run project, open the root directory and enter the following command:
yarn startIt's expected that there will be errors after run the project. Add the next row to your package.json file to the section scripts:
"fix:codestyle": "find src/ -type f \\( -name \"*.ts\" -o -name \"*.tsx\" -o -name \"*.js\" -o -name \"*.jsx\" \\) -not -name \"*.d.ts\" | xargs prettier --write --plugin=./node_modules/@trivago/prettier-plugin-sort-imports/",Run it by command:
yarn fix:codestyleThe command will fix some issues automatically. Also, you have to fix some issues by manual (for example, delete redundant import react from 'react')
The project is open source software provided under the Apache License 2.0.
