Project๐Ÿ› ๏ธโš™๏ธ

Next js Vanilla Extract CSS ์„ค์น˜ํ•˜๊ธฐ (next.js 14)

Gaeun Lee 2024. 9. 18. 16:22

zero-runtime css-in-ts์ธ vanilla-extract๋Š” ๋Ÿฐํƒ€์ž„์— ์‹คํ–‰๋˜๋Š” css-in-js ๋ฐฉ์‹( styled-components, emotion )๊ณผ ๋‹ฌ๋ฆฌ ๋นŒ๋“œํƒ€์ž„์— ์‹คํ–‰๋˜์–ด Next.js์˜ SSR์—์„œ Hydration์„ ๊ฑฐ์น˜๊ธฐ ์ „์—๋„ ์Šคํƒ€์ผ์‹œํŠธ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

TailwindCSS ์—ญ์‹œ ๋นŒ๋“œ ํƒ€์ž„์— ์Šคํƒ€์ผ์‹œํŠธ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š์•„ ์ƒˆ๋กญ๊ฒŒ ์‹œ์ž‘ํ•˜๊ฒŒ ๋œ ํ”„๋กœ์ ํŠธ์— vanilla-extract๋ฅผ ๋„์ž…ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

 

1. vanilla-extract ์„ค์น˜

// npm 
npm install @vanilla-extract/css
npm install --save-dev @vanilla-extract/next-plugin

// pnpm
pnpm add @vanilla-extract/css
pnpm add --save-dev @vanilla-extract/next-plugin

// yarn
yarn add @vanilla-extract/css
yarn add -D @vanilla-extract/next-plugin

 

 

2. next.config.mjs ์ˆ˜์ •

๊ณต์‹ ๋ฌธ์„œ์—๋Š” next.config.js ์ธ ๊ฒฝ์šฐ๋กœ๋งŒ ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค.

.mjs ํ™•์žฅ์ž์˜ ํŒŒ์ผ์—์„œ๋Š” require ๋ฌธ๋ฒ•์„ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค.

/** @type {import('next').NextConfig} */
import { createVanillaExtractPlugin } from "@vanilla-extract/next-plugin";

const withVanillaExtract = createVanillaExtractPlugin();
const nextConfig = {};

export default withVanillaExtract(nextConfig);

 

 

3. styles.css.ts ์ž‘์„ฑ

import { style } from "@vanilla-extract/css";

export const container = style({
  padding: 10,
  color: "red",
});
import { container } from "./styles.css";

export default function Home() {
  return <div className={container}>hello</div>;
}

์ž˜ ๋œฌ๋‹ค ใ…Žใ…Ž

vanilla-extract ์ •๋ณต ๊ฐ€๋ณด์ž๊ณ  ... ๐Ÿ‘Š

 

 

 

์ฐธ๊ณ 

https://ui.toast.com/weekly-pick/ko_20190805

https://velog.io/@shinhw371/CSS-why-Nextjs-recommand-Tailwind