1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【React】ViteでReact & TypeScript & ESLint, Prettier 基盤環境の構築(簡潔版)

Posted at

$ yarn create vite my-app --template react-ts

$ cd my-app
$ yarn
$ yarn dev

image.png

Eslint 初期設定

$ npx eslint --init

How would you like to use ESLint?
image.png
What type of modules does your project use?
image.png
Which framework does your project use?
image.png

Does your project use TypeScript?
image.png

Where does your code run?
image.png
How would you like to define a style for your project?
image.png
Which style guide do you want to follow?
image.png

What format do you want your config file to be in?
image.png
必要なパッケージをインストールする。
image.png
Which package manager do you want to use?
image.png

すべてが完了したら.eslintrc.jsonが作られた。
image.png
image.png
.eslintrc.jsonが作られたので、デフォルトで作られた.eslintrc.cjsを削除する。

.eslintrc.jsonを編集する

「/.eslintrc.json」を下記の様に修正します。

.eslintrc.json
{
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
+       "plugin:@typescript-eslint/recommended",
+       "prettier"
    ],
+   "overrides": [],
    "parserOptions": {
        "ecmaVersion": "latest",
        "sourceType": "module"
    },
    "plugins": [
        "react",
+       "@typescript-eslint"
    ],
    "rules": {
+       "react/react-in-jsx-scope": "off"
    },
+   "settings": {
+       "react": {
+           "version": "detect"
+       }
+   }
}

prettierの設定

$ yarn add --dev prettier
.prettierrcファイルを手動で作成する
image.png

.prettierrcを編集する

.eslintrc
{
  "endOfLine": "lf",
  "printWidth": 80,
  "tabWidth": 2,
  "trailingComma": "es5",
  "singleQuote": true,
  "jsxSingleQuote": true,
  "semi": true
}

eslint-config-prettier 設定

$ yarn add --dev eslint-config-prettier

package.jsonに下記を追加する。
①typeエラーがないか確認
②eslintとprettierの設定に重複(衝突)するものはないかを確認
③eslintでコードを確認し、修正可能なものは修正する
④prettierでコードをフォーマットする

"fix": "tsc && yarn conflict && yarn lint && yarn format"

image.png

これで環境構築は完了しました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?