0
0

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 3 years have passed since last update.

Next.js プロジェクト環境構築

Posted at

Next.js プロジェクト環境構築 with typescript , tailwind css

環境構築

  • ①プロジェクト作成
npx create-next-app プロジェクト名
  • ②TypeScriptの導入

つづいて、作成したアプリケーションにTypeScriptを導入していきます。

まず必要な型定義を取得しましょう。

yarn add --dev typescript @types/react @types/node

TypeScriptの導入は、簡単でプロジェクトのルートディレクトリにtsconfig.jsonを作成して、yarn dev or npm run devのコマンドを叩くだけ!

// 作成したプロジェクトに移動しましょう。
cd プロジェクト名

// Typescript導入に必要なパッケージを取得
yarn add --dev typescript @types/react @types/node

// tsconfig.jsonを作成して、アプリ起動
touch tsconfig.json

yarn dev

pages/index.jsindex.tsxに変更して、localhost:3000にアクセスして、問題なく画面が表示されていればTypeScriptの導入は完了です。

  • ③Tailwind Cssの導入

はい。とりあえず、インストールしましょう。

※Next.jsのバージョンによってインストールコマンドが異なるので、自分のバージョンに適応したモノをインストールしましょう。

yarn add -D tailwindcss@latest postcss@latest autoprefixer@latest

次に、構成ファイルを作成します。

下記コマンドを実行すると、ルートディレクトリにtailwind.config.jspostcss.config.jsが作成されます。

npx tailwindcss init -p

To make the development experience as productive as possible, Tailwind generates thousands of utility classes for you, most of which you probably won't actually use.

公式ドキュメントに、↑のように記載されていますね。

実際は使わないようなモノも多く含んだ状態で大量のクラスを生成して容量が大きいから、不要なものを除外してしまいましょうってことですね。

// tailwind.config.js
purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],

次にTailwindをプロジェクトに適用する方法が2つあって

  • 1._app.tsに直接インポートする方法
  • 2.globals.cssにTailwindを含める方法

とはいえ、この環境構築では、.style/global.css作れていないが、チュートリアルを進めるにあたって作成する予定なので、2つ目の方法で行きたいと思います。

/* ./styles/globals.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
// pages/_app.js
import '../styles/globals.css'

以上で、環境構築については終了です。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?