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?

Web Developer Bootcampでtailwind cssを使いたい!

Last updated at Posted at 2025-01-08

はじめに

Web Developer Bootcampを受講しているときに、私は思いました。
「Boot StrapよりもモダンなCSSフレームワーク使いたい!」
動画でBootstrapの説明は見れるので、ここでは自分でドキュメントを読みながら、自分で書いていった方が楽しいと思いませんか?

express + ejs環境でtailind cssを導入する方法

package.jsonの概要

{
  "name": "yelp_camp",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "css": "postcss public/styles/tailwind.css -o public/styles/style.css"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "autoprefixer": "^10.4.20",
    "ejs": "^3.1.10",
    "ejs-mate": "^4.0.0",
    "express": "^4.21.2",
    "method-override": "^3.0.0",
    "mongoose": "^8.9.3",
    "postcss": "^8.4.49",
    "postcss-cli": "^11.0.0"
  },
  "devDependencies": {
    "tailwindcss": "^3.4.17"
  }
}

やること

1. 各moduleのインストール

npm install tailwindcss postcss autoprefixer postcss-cli

2. tailwind.config.jsを作る

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./views/*.ejs', './views/**/*.ejs'],
  theme: {
    extend: {},
  },
  plugins: [
    {
      tailwindcss: {},
      autoprefixer: {},
    },
  ],
};

3. postcss.config.jsを作る

module.exports = {
    plugins: [require('tailwindcss'), require('autoprefixer')],
};

4. ./public/stylesheets下にtailwind.cssを作る

@tailwind base;
@tailwind components;
@tailwind utilities;

5.package.jsonにスクリプトを追加

{
  "name": "yelp_camp",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "css": "postcss public/styles/tailwind.css -o public/styles/style.css"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "autoprefixer": "^10.4.20",
    "ejs": "^3.1.10",
    "ejs-mate": "^4.0.0",
    "express": "^4.21.2",
    "method-override": "^3.0.0",
    "mongoose": "^8.9.3",
    "postcss": "^8.4.49",
    "postcss-cli": "^11.0.0"
  },
  "devDependencies": {
    "tailwindcss": "^3.4.17"
  }
}

7.CSSファイルをビルド

npm run css

8.index.js(app.jsなどメインのjsファイル)で下記を忘れずに

app.use('/public', express.static(__dirname + '/public'));

参考

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?