【備忘録】Next 14 のセットアップ

Next の初期設定をざっくり備忘録的にまとめておく。
2023/12/02 時点のものです。

Nextプロジェクトの作成。
色々聞かれるけど全部デフォルトでいいと思う。
TailwindCSS使う場合は Yes にする。
npx create-next-app@latest

この時 Github のリポジトリも作って push までしておく

node version の指定をする。
私は nodenv を使っているので .node-version ファイルを作成すればOK。
以下サイトの stable になっているバージョンで問題ないと思う。
nodenv local 20.10.0

パッケージマネージャーの設定をする。
好きなのを使えばいいと思うが今回は pnpm を使う。
// npm でインストールした時出来るファイルを削除
rm -rf node_modules
rm package-lock.json
pnpm install
pnpm-lock.yaml ができたら成功。

ついでにバージョン指定もしておく。
これで pnpm バージョン8 以外でインストールしようとするとエラーが出るはず。
engine-strict=true
// 中略
"engines": {
"pnpm": ">=8",
"npm": "please_use_pnpm_instead",
"yarn": "please_use_pnpm_instead"
},

create-next-app でインストールされたライブラリが package.json でメジャーバージョンしか指定されていない物があった。前までそうだったっけな。。
pnpm 使っているので以下コマンドですべてアップデートしておく。
pnpm update

lefthook の設定をする。
これは git フックを管理するライブラリである。
今回はプロジェクトにインストールする。
pnpm add -D lefthook
pnpm lefthook install

commitlint を入れる。
これはコミットの message のルールを設定できる。
2行目で commitlint.config.js が生成される。ここにルールを書いていく。
pnpm add -D @commitlint/{cli,config-conventional}
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

個人的にはプレフィックスがちゃんとついて入れば後はそんなにこだわり無いので commitlint.config.js は以下のようにルールを設定しておく。
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'subject-case': [0, 'always'], // subject のルールを無効化
'header-max-length': [2, 'always', 999] // メッセージヘッダーの文字制限を 999 文字に
}
};

lefthook.yml で commitlint が動くように設定する。
commit-msg:
commands:
commitlint:
run: pnpm commitlint -e
静的解析も入れるが一旦設定しないでおく。

eslint と prettier の設定。
後から eslint のルールを追加するのはつらいのでできるだけこのタイミングで入れたいルールは入れておく。

ちなみに自分の設定したもの
pnpm add -D eslint-config-prettier eslint-plugin-import eslint-plugin-unused-imports eslint-plugin-jest eslint-plugin-jest-dom eslint-plugin-testing-library eslint-plugin-storybook @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier

{
"extends": [
"next/core-web-vitals",
"plugin:import/recommended",
"plugin:import/warnings",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:storybook/recommended"
],
"plugins": ["import", "unused-imports", "jest", "jest-dom", "storybook", "testing-library"],
"rules": {
"no-console": [
"warn",
{
"allow": ["warn", "info", "error"]
}
],
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
["parent", "sibling", "index"],
"object",
"type"
],
"pathGroups": [
{
"pattern": "~/**",
"group": "internal"
}
],
"pathGroupsExcludedImportTypes": ["builtin"],
"alphabetize": {
"order": "asc"
},
"newlines-between": "always"
}
],
"import/first": "error",
"import/newline-after-import": "error",
"unused-imports/no-unused-imports": "error",
"@typescript-eslint/no-unused-vars": "error",
"no-unused-vars": "off",
"jest/consistent-test-it": ["warn", { "fn": "test" }],
"jest/require-top-level-describe": ["warn"]
}
}

{
"trailingComma": "none",
"singleQuote": true,
"jsxSingleQuote": true,
"printWidth": 100
}

next.config.js と package.json の scripts の設定をする。
typedRoutes を使いたいので追加。
// 一部略
const nextConfig = {
experimental: {
typedRoutes: true
}
};
scripts は以下のように設定した。
pnpm add -D npm-run-all
// scripts のみ抜粋
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"check": "run-p check:*",
"check:lint": "next lint",
"check:tsc": "tsc --noEmit",
"format": "pnpm check:lint --fix"
},

ここで lefthook の更新をしておく。
pre-commit 時に 静的解析 するように設定する。
pre-push は以前設定していたが最近は CI で確認するし、なくてもいいかも?という気持ちなので設定しない。
+ pre-commit:
+ parallel: true
+ commands:
+ check:
+ run: pnpm check && pnpm format && git add {staged_files}
commit-msg:
commands:
commitlint:
run: pnpm commitlint -e

CI を設定する。
これは環境によりまちまちと思うが参考までに設定したものを貼っておく。
name: Setup Node.js and pnpm
description: Setup Node.js and pnpm Action
runs:
using: composite
steps:
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version-file: ./.node-version
- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 8
run_install: false
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
shell: bash
run: pnpm install
name: CI
on: [workflow_dispatch, pull_request]
jobs:
check-static:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-and-pnpm
- name: Run Check Static
run: pnpm check
# テストが追加されたら以下のコメントアウトを外す
# unit-test:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Setup Node.js and pnpm
# uses: ./.github/actions/setup-node-and-pnpm
# - name: Run Unit Test
# run: pnpm test
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-and-pnpm
- name: Run Build
run: pnpm build
参考↓

最後、デフォルトで設定されているページは消しておく。
これで大体設定はできた。