LoginSignup
2
0

More than 3 years have passed since last update.

nextjsの最新のチュートリアルの実施環境を構築する(prettier + eslint + typescript)

Posted at

前提

nextjsの以下のチュートリアルまで実施している
Create a Next.js App

概要

プロジェクトにprettier eslint typescript を導入し、快適にチュートリアルを実施するための環境を構築する
参考レポジトリ

環境構築

typescriptをインストールする

typescriptをインストールする

yarn add -D typescript

型定義をインストールする

yarn add -D @types/react @types/react-dom

ファイルの拡張子を変更する

  • page/index.jspage/index.tsx に変更する

プロジェクトを実行して、tsconfig.jsonを追加する

yarn run dev
  • tsconfig.jsonが追加されていることを確認してください
  • next-env.d.tsファイルも追加されています
  • strict がfalseになっていたりするので、変更する

型定義をインストールする

yarn add -D @types/react @types/react-dom

prettierとeslintをインストールする

prettierとpretty-quickをインストールする

yarn add -D prettier eslint-config-prettier eslint-plugin-prettier pretty-quick

prettierの設定ファイルを追加する

touch .prettierrc.json

prettierの設定ファイルに以下を追加する

prettierrc.json
{
  "printWidth": 100,
  "trailingComma": "all",
  "tabWidth": 2,
  "semi": true,
  "singleQuote": true
}

eslintをインストールする

yarn add -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react

eslintの設定ファイルを追加する

touch .eslintrc.json
eslintrc.json
{
  "env": {
    "browser": true,
    "es6": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2018,
    "sourceType": "module",
    "project": "./tsconfig.json",
    "useJSXTextNode": true
  },
  "plugins": ["react", "@typescript-eslint", "prettier"],
  "rules": {
    "prettier/prettier": "error"
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

lint実行用のスクリプトをpackage.jsonに追加する

package.json
"scripts": {
  "dev": "next dev",
  "build": "next build",
  "start": "next start",
  "lint": "eslint --ext .ts,.tsx ./"
},

lintを実行する

yarn run lint
  • 特にコードを変更していなければ、以下のようなエラーが出力されるはずです。
3:16  warning  Missing return type on function          @typescript-eslint/explicit-function-return-type
5:5   error    'React' must be in scope when using JSX  react/react-in-jsx-scope
...省略... 

vscodeの自動整形を有効にする

eslintの拡張機能をインストールする

code --install-extension dbaeumer.vscode-eslint

.vscode/settings.jsonファイルを追加する

settings.json
{
  "eslint.nodePath": "./node_modules/eslint",
  "eslint.packageManager": "npm",
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.alwaysShowStatus": true
}
2
0
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
2
0