Node.js プロジェクトの設定 - TypeScript & ESLint & Prettier
December 14, 2021
最近使いまわしている設定ファイルの内容を自分用に記録。
前提:Node.js 14 を想定しています。
パッケージインストール #
今回の構成に必要なパッケージをインストールするには以下を実行。
npm i -D \
typescript \
prettier \
eslint \
@typescript-eslint/parser \
@typescript-eslint/eslint-plugin \
eslint-config-airbnb-base \
eslint-config-airbnb-typescript \
eslint-config-prettier \
eslint-plugin-import
TypeScript 関連 #
tsconfig.json
{
"compilerOptions": {
"outDir": "lib",
"target": "es2020", // Node.js 14
"module": "commonjs",
"lib": ["es2020"], // Node.js 14
"sourceMap": true,
"removeComments": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noImplicitOverride": true
},
"exclude": ["node_modules", "lib"],
"include": ["src"]
}
参考:設定値の一覧
tsconfig.eslint.json
{
"extends": "./tsconfig.json",
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
ESLint 関連 #
.eslintrc.json
{
"root": true,
"env": {
"node": true,
"es2020": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.eslint.json"],
"sourceType": "module",
"ecmaVersion": 2020
},
"plugins": ["@typescript-eslint", "import"],
"extends": [
"airbnb-base",
"airbnb-typescript/base",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier"
],
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/consistent-type-assertions": [
"error",
{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }
],
"@typescript-eslint/consistent-type-definitions": ["error", "type"],
"@typescript-eslint/consistent-type-imports": "error",
"import/prefer-default-export": "off",
"import/exports-last": "error",
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object",
"type"
],
"alphabetize": { "order": "asc" },
"newlines-between": "never"
}
],
"sort-imports": ["error", { "ignoreDeclarationSort": true, "ignoreMemberSort": false }],
"arrow-body-style": "off"
}
}
参考:プリセットの内容
- airbnb-base
- eslint:recommended
- @typescript-eslint/recommended
- import/recommended
.eslintignore
node_modules/
lib/
参考:書き方の例
- https://github.com/eslint/eslint/blob/main/.eslintignore
- https://github.com/expressjs/express/blob/master/.eslintignore
Prettier 関連 #
.prettierrc.json
{
"printWidth": 100,
"singleQuote": true,
"trailingComma": "all"
}
参考:設定値の一覧
.prettierignore
node_modules/
lib/
参考:書き方の例
- https://github.com/prettier/prettier/blob/main/.prettierignore
- https://github.com/nestjs/nest/blob/master/.prettierignore
package.json 関連 #
package.json
engines の明示と scripts のフィールドにリント実行用のコマンドを追加
{
"engines": {
"node": "14"
},
"scripts": {
"lint:tsc": "tsc --noEmit",
"lint:eslint": "eslint src --ext .js,.jsx,.ts,.tsx",
"fix:eslint": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
"lint:prettier": "prettier --check src",
"fix:prettier": "prettier --write src",
"lint:all": "npm run lint:tsc && npm run lint:eslint && npm run lint:prettier"
}
}