1. Django Tutorial(Airbnb) - Tailwind with Gulp

ID์งฑ์žฌยท2021๋…„ 8์›” 16์ผ
0

Django

๋ชฉ๋ก ๋ณด๊ธฐ
23/43
post-thumbnail

๐ŸŒˆ Tailwind with Gulp

๐Ÿ”ฅ Setting Up

๐Ÿ”ฅ Tailwind ์ ์šฉ


1. Setting Up

  • Tailwind๋Š” css๊ฐ€ ์•„๋‹Œ postcss๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”,, postcss๋Š” ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์ดํ•ดํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ "Node.js"๋‚˜ "Gulp"๋ฅผ ์ด์šฉํ•ด์„œ CSS๋กœ ๋ณ€ํ™˜์‹œ์ผœ ์ค˜์•ผํ•ด์š”. ์›น๋ธŒ๋ผ์šฐ์ €๊ฐ€ Tailwind๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •์„ ํ•ด๋ณด๋„๋ก ํ• ๊ป˜์š”:)
  • ์ฐธ๊ณ  : https://tailwindcss.com/docs/installation

1) Node.js ์„ค์น˜ ๋ฐ ์ดˆ๊ธฐํ™”

  • Node.js ์ตœ์‹  ์„ค์น˜ : ๐Ÿ”Ž brew install node
  • Node.js ํŠน์ • ์„ค์น˜ : ๐Ÿ”Ž brew install node@12
  • Node.js ์„ค์น˜ ํ™•์ธ : ๐Ÿ”Ž node -v
  • Node.js ์ดˆ๊ธฐํ™” : ๐Ÿ”Ž npm init ๐Ÿ‘ˆ package.json ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์š”!
# package.json
{
  "name": "airbnb-clone",
  "version": "1.0.0",
  "description": "Cloning Airbnb with Python, Django, Tailwind and more...",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Jang-Jaewon/airbnb-clone.git"
  },
  "scripts": {
    "css": "gulp"
  },
  "homepage": "https://github.com/Jang-Jaewon/airbnb-clone#readme",
  "devDependencies": {
    "autoprefixer": "^9.7.0",
    "gulp": "^4.0.2",
    "gulp-csso": "^3.0.1",
    "gulp-postcss": "^8.0.0",
    "gulp-sass": "^4.0.2",
    "node-sass": "^4.13.0",
    "tailwindcss": "^1.1.3"
  }
}

2) gulp & tailwind & autoprefixer ์„ค์น˜

  • gulp ์„ค์น˜ : ๐Ÿ”Ž npm i gulp gulp-postcss gulp-sass gulp-csso node-sass -D
  • tailwind ์„ค์น˜ : ๐Ÿ”Ž npm install tailwindcss -D
  • autoprefixer ์„ค์น˜ : ๐Ÿ”Ž npm i autoprefixer -D
  • "-D"๋Š” Developer ๋ชจ๋“œ๋กœ ์„ค์น˜๋ฅผ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์ด์—์š”!
  • ๋ฐฉ๊ธˆ ์„ค์น˜ํ•œ ๋งŽ์€ ํŒŒ์ผ์„ git์— ์˜ฌ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด .gitignore์— ์ถ”๊ฐ€ํ•ด์ค˜์š”:)
# .gitignore ์ถ”๊ฐ€
node_modules/

3) tailwind ์ดˆ๊ธฐํ™”

  • tailwind ์ดˆ๊ธฐํ™” : ๐Ÿ”Ž npx tailwind init ๐Ÿ‘ˆ tailwind.config.js๊ฐ€ ์ƒ๊ฒจ์š”!
module.exports = {
  theme: {
    extend: {}
  },
  variants: {},
  plugins: []
}

4) "gulpfile.js" ๋ฐ "styles.scss" ์ƒ์„ฑ

  • ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— "gulpfile.js" ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€ ๋’ค, ์•„๋ž˜ ๋‚ด์šฉ์„ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ฉ๋‹ˆ๋‹ค.
# gulpfile.js
const gulp = require("gulp");
const css = () => {
  const postCSS = require("gulp-postcss");
  const sass = require("gulp-sass");
  const minify = require("gulp-csso");
  sass.compiler = require("node-sass");
  return gulp
    .src("assets/scss/styles.scss",{allowEmpty: true})
    .pipe(sass().on("error", sass.logError))
    .pipe(postCSS([require("tailwindcss"), require("autoprefixer")]))
    .pipe(minify())
    .pipe(gulp.dest("static/css"));
};
exports.default = css;
  • ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— "assets/scss/styles.scss" ๋””๋ ‰ํ† ๋ฆฌ ๋ฐ ํŒŒ์ผ์„ ์ƒ์„ฑ ํ›„ ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
# styles.scss
@tailwind base;
@tailwind components;
@tailwind utilities;
  • "npm run i", "npm run css"๋ฅผ console์— ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ์•„๋ž˜์ฒ˜๋Ÿผ ์˜ค๋ฅ˜๊ฐ€ ์—†์œผ๋ฉด ์ •์ƒ์ž…๋‹ˆ๋‹ค.
  • ์ •์ƒ์ ์œผ๋กœ setting ๋˜์—ˆ๋‹ค๋ฉด, static ๋””๋ ‰ํ† ๋ฆฌ์— cssํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜:)
  • ๋ชจ๋“  ์ž‘์—…์€ "assets/scss/styles.scss"์—์„œ ์ˆ˜ํ–‰ํ•˜๊ณ , "staic/css/styles.css"์€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. "gulpfile.js"์—์„œ css๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด์—์š”!
  • ๋˜ํ•œ "assets/scss/styles.scss"์˜ ๋ณ€๊ฒฝ์ด ์žˆ์„ ๋•Œ ๋งˆ๋‹ค "npm run css" ๋ช…๋ น์„ ํ•ด์ฃผ์–ด์•ผ ํ•ด์š”. "npm run css"๋Š” "styles.scss"์˜ ๊ฐ’๋ฅผ ์›น๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ CSS๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ๋ช…๋ น์ž…๋‹ˆ๋‹ค.

5) STATICFILES_DIRS ์„ค์ •

  • "http://127.0.0.1:8000/static/css/styles.css" ๋ฅผ ์ ‘๊ทผํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Page not found ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • ์–ด๋–ค ๋””๋ ‰ํ† ๋ฆฌ์—์„œ static ํŒŒ์ผ์„ ์ฝ์–ด์˜ฌ์ง€ settings.py์— ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.
    • ๐Ÿ”Ž STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

6) {% load static %}

  • Tailwind๋Š” Django์—์„œ css ํŒŒ์ผ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์•„์š”. ๋งจ ์œ„์— static ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ฒ ๋‹ค๊ณ  ์„ ์–ธํ•œ ๋’ค, <head> ์˜์—ญ์— <link:css>๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹จ, <link:css> ์ค„ ๋•Œ, ์•„๋ž˜์ฒ˜๋Ÿผ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์žก์•„์ค๋‹ˆ๋‹ค.
    • static ํŒŒ์ผ load ์„ ์–ธ : ๐Ÿ”Ž {% load static %}
    • CSS ๊ฒฝ๋กœ ์„ค์ • : ๐Ÿ”Ž <link rel="stylesheet" href="{% static 'cssํŒŒ์ผ ๊ฒฝ๋กœ' %}">
  • Tailwind์€ reset css์™€ ๊ฐ™์€ Default๊ฐ’์ด ์ ์šฉ๋˜์–ด ์žˆ์–ด, ๊น”๋”ํ•˜๊ฒŒ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ณ ๋กœ "Tailwind CSS IntelliSense" extension์„ ์„ค์น˜ํ•ด์ฃผ๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€