Closed7

Next.jsのApp Routerを使ってみる

おとのおとの

現時点でNode.jsの安定版の18.16.0をインストール。

自分はnodenvを利用しているので、以下コマンドを実行。

nodenv install 18.16.0

その後、リポジトリに.node-versionを配置。

.node-version
18.16.0
おとのおとの

さっそくNext.jsを導入するために、以下コマンドを実行。

npx create-next-app@latest

諸々の質問にYesで答えていく。使うかどうかは未定だがTailwind CSSも導入。

おとのおとの

そういえばyarnを使いたいため、とりあえずインストール。

npm i -g yarn

その後、各ディレクトリをリポジトリのルートに配置し、yarnを実行。

yarn
おとのおとの

prettierとimport順自動並び替えを有効にするための諸々のプラグインを導入

yarn add -D eslint-config-prettier eslint-plugin-import eslint-plugin-simple-import-sort eslint-plugin-unused-imports prettier @typescript-eslint/eslint-plugin @typescript-eslint/parser

eslintrc.jsonの内容を以下に変更

eslintrc.json
{
  "extends": [
    "next/core-web-vitals", // デフォルト設定
    "eslint:recommended", // eslint推奨ルール設定
    "plugin:@typescript-eslint/recommended", // typescript-eslintの推奨ルール設定
    "plugin:storybook/recommended", // storybook関連設定
    "prettier" // Prettierとの競合ルールをOFFにする(他の設定を上書きするため、最終行に記述する必要有り)
  ],
  "plugins": [
    "simple-import-sort", // import文の自動整列を実現
    "import", // 上記プラグインを拡張(自動整列のルールを追加)
    "unused-imports" // 未使用のimport文を削除
  ],
  "rules": {
    "simple-import-sort/imports": "error", // importのsortルールを設定
    "simple-import-sort/exports": "error", // exportのsortルールを設定
    "import/first": "error", // すべてのimportがファイルの先頭にあることを確認
    "import/newline-after-import": "error", // import後に改行があることを確認
    "import/no-duplicates": "error", // 同じファイルのimportをマージ
    "unused-imports/no-unused-imports": "error" // 未使用のimport文を削除
  }
}

prettierの設定はとりあえずデフォルトで設定

prettier.config.js
module.exports = {
  printWidth: 80, // 1行で表示する文字数
  tabWidth: 2, // インデントのサイズ
  useTabs: false, // インデントにスペースの代わりにタブを使うかどうか
  semi: true, // 文の後にセミコロンを付けるかどうか
  singleQuote: false, // 文字列をシングルクォートで囲むかどうか(falseだとダブルクォート)
  quoteProps: "as-needed", // オブジェクトのプロパティ名をクォートで囲むかどうか
  jsxSingleQuote: false, // JSX内のクォートをシングルクォートで囲むかどうか
  trailingComma: "es5", // 複数行のときの末尾のカンマを付けるかどうか
  bracketSpacing: true, // オブジェクトリテラルの{}内の前後にスペースを入れるかどうか
  bracketSameLine: false, // JSX内の要素の閉じタグを最後の行に含んで表示するか
  arrowParens: "always", // アロー関数の引数が1つのときにカッコで囲むかどうか
};
おとのおとの

stylelint関連のライブラリをinstall

yarn add -D stylelint stylelint-config-prettier stylelint-config-recess-order stylelint-config-recommended-scss stylelint-prettier stylelint-scss

stylelint.jsonを追加

stylelint.json
module.exports = {
  plugin: ["stylelint-scss"],
  extends: [
    "stylelint-config-recommended-scss", // scssのための拡張ルール追加
    "stylelint-config-recess-order", // cssプロパティの自動ソートを設定
    "stylelint-config-prettier", // Prettierとの競合ルールをOFFにする
  ],
  rules: {
    "scss/at-rule-no-unknown": [
      true,
      {
        // Tailwind CSS関連は許容する
        ignoreAtRules: [
          "tailwind",
          "apply",
          "variants",
          "responsive",
          "screen",
        ],
      },
    ],
  },
};
おとのおとの

上記で設定したLintがVsCodeでのファイル保存時にも効くように以下を追加。
VsCodeのプラグインは各自でinstallしてもらう必要あり。

.vscode/extensions.jsonc
{
  // 導入推奨プラグイン一覧
  "recommendations": [
    // esLintの保存時自動フォーマットを有効
    "dbaeumer.vscode-eslint",
    // styleLintの保存時自動フォーマットを有効
    "stylelint.vscode-stylelint",
    // prettierの保存時自動フォーマットを有効
    "esbenp.prettier-vscode",
    // cssModulesのクラス名補完を有効
    "clinyong.vscode-css-modules",
    // css変数の補完を有効
    "vunguyentuan.vscode-css-variables",
    // cssクラス名の補完を有効
    "gencer.html-slim-scss-css-class-completion"
  ]
}
.vscode/settings.jsonc
{
  // ファイル保存時のフォーマット処理を有効化する
  "editor.formatOnSave": true,

  // 各ファイルのフォーマッターを指定する
  "[css]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[scss]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },

  // Stylelintの対象を設定
  "stylelint.validate": ["css", "scss"],

  // ESLintの対象を設定
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact"
  ],

  // 保存時に実行される各コードアクションを有効化する
  "editor.codeActionsOnSave": {
    "source.fixAll.stylelint": true,
    "source.fixAll.eslint": true
  },

  // VSCodeデフォルトのLintを無効にする(各Lintのルールで統一する際に設定)
  "css.validate": false,
  "less.validate": false,
  "scss.validate": false,
  "javascript.format.enable": false,
  "typescript.format.enable": false
}
おとのおとの

Storybookを導入

npx storybook@latest init

諸々の質問にyesで回答後、sassでも扱えるようにしたいので以下ライブラリのインストール。

yarn add -D sass @storybook/addon-styling

scssに対応させるために.storybook/main.tsを以下のように修正。
パス指定をNext.jsでの設定に揃えるための設定も追加。

.storybook/main.ts
import path from "path";
import type { StorybookConfig } from "@storybook/nextjs";

const rootPath = path.resolve(__dirname, "../src/");

const config: StorybookConfig = {
  stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/addon-interactions",
+    {
+      name: "@storybook/addon-styling",
+      options: {
+        sass: {
+          // Require your Sass preprocessor here
+          implementation: require("sass"),
+        },
+      },
+    },
  ],
  framework: {
    name: "@storybook/nextjs",
    options: {},
  },
  docs: {
    autodocs: "tag",
  },
+  staticDirs: ["../public"],
+  webpackFinal: async (config: any) => {
+    config.resolve.alias["@"] = rootPath; // srcを@と省略してパスを記載できるように設定
+    return config;
+  },
};
export default config;
このスクラップは2024/11/29にクローズされました