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 typescript
Change 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 eslint
Note 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 --init
And 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
.prettierrc
file in the root directory and fill the rules. See example here -
Create
.prettierignore
file in the root directory and fill the rules. See example here -
Fill the
.eslintrc.json
file as in example here. ❗ Note that the order in which the "extends" object keys are placed is important. -
Create
.eslintignore
file in the root directory and fill the rules. See example here -
Fill the
tsconfig.json
file as in example here -
Create a
.env
file in the root directory. You can add a row likePORT=3002
to the.env
file, and run the project on the desired port. ❗ Note that we need to put.env
file inside.gitignore
in order to exclude data sharing from it. -
To install
Prettier
plugin to WebStorm open the Webstorm > Preferences > Plugins, find Prettier and install it. -
To run
Prettier
automatically 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 start
It'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:codestyle
The 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.