๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Next.js

[๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - Built-In CSS Support

๋‚ด๋ณด๋‚ด๋ฒˆ(๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ๋‚ด๊ฐ€ ๋ฒˆ์—ญํ•œ...) Next.js docs

2021๋…„ 3์›” 30์ผ ๊ธฐ์ค€ Next.js ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ฒˆ์—ญํ–ˆ๋‹ค.

โ€ป ์˜์–ด ์ „๊ณต์ž๋„ ํ•ด์™ธ ์œ ํ•™ํŒŒ๋„ ์•„๋‹ˆ๊ธฐ์— ๋ฒˆ์—ญ์—๋Š” ์˜์—ญ, ์˜ค์—ญ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ˜ผ์ž ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•ด๊ฐ€๋ฉฐ ๋ฒˆ์—ญํ•˜๋‹ค ๋ณด๋‹ˆ ์˜คํƒ€๋„ ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. ์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ง์ ‘ ์‚ดํŽด๋ณด๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ ๋” ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

(ํ•˜์ง€๋งŒ ๋Œ“๊ธ€ ํ”ผ๋“œ๋ฐฑ๋„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ƒ )

Next.js ๊ณต์‹๋ฌธ์„œ ํ™•์ธํ•˜๊ธฐ>>


 

Next.js allows you to import CSS files from a JavaScript file. This is possible because Next.js extends the concept of import beyond JavaScript.

Next.js๋Š” CSSํŒŒ์ผ๋“ค์„ JavaScriptํŒŒ์ผ์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค. Next.js๊ฐ€ JavaScript ๋„˜์–ด์„œ ๊ฐ€์ ธ์˜ค๊ธฐ(?) ๊ฐœ๋…์„ ํ™•์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

Adding a Global Stylesheet

To add a stylesheet to your application, import the CSS file within pages/_app.js.

For example, consider the following stylesheet named styles.css:

์Šคํƒ€์ผ์‹œํŠธ๋ฅผ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด pages/_app.js ์•ˆ์— CSS ํŒŒ์ผ์„ import ํ•ด์•ผํ•œ๋‹ค.

์•„๋ž˜์˜ styles.css ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๋ผ:

Create a pages/_app.js file if not already present. Then, import the styles.css file.

pages/_app.js๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ƒ์„ฑํ•˜๋ผ. ๊ทธ๋ฆฌ๊ณ  styles.cssํŒŒ์ผ์„ importํ•˜๋ผ.

These styles (styles.css) will apply to all pages and components in your application. Due to the global nature of stylesheets, and to avoid conflicts, you may only import them inside [pages/_app.js](<https://nextjs.org/docs/advanced-features/custom-app>).

์ด ์Šคํƒ€์ผ๋“ค์€(styles.css) ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์— ์žˆ๋Š” ๋ชจ๋“  ํŽ˜์ด์ง€๋“ค๊ณผ ์ปดํฌ๋„ŒํŠธ๋“ค์— ๋ฐ˜์˜๋  ๊ฒƒ์ด๋‹ค. ์Šคํƒ€์ผ์‹œํŠธ์˜ ์ „์—ญ ์„ฑ์งˆ ๋•Œ๋ฌธ์— ๊ทธ๋ฆฌ๊ณ  ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ pages/_app.js ๋‚ด์—๋งŒ import ํ•  ์ˆ˜ ์žˆ๋‹ค.

In development, expressing stylesheets this way allows your styles to be hot reloaded as you edit them—meaning you can keep application state.

๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์Šคํƒ€์ผ์‹œํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด ์Šคํƒ€์ผ๋“ค์„ ์ˆ˜์ •ํ–ˆ์„ ๋•Œ ํ•ซ ๋ฆฌ๋กœ๋“œ ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ฆ‰ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ๊ณ„์† ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

In production, all CSS files will be automatically concatenated into a single minified .css file.

์šด์˜ ํ™˜๊ฒฝ์—์„œ ๋ชจ๋“  CSS ํŒŒ์ผ๋“ค์€ ์ž๋™์œผ๋กœ ํ•˜๋‚˜์˜ ์ตœ์†Œํ™”๋œ .css ํŒŒ์ผ๋กœ ์—ฐ๊ฒฐ๋  ๊ฒƒ์ด๋‹ค.

Import styles from node_modules

Since Next.js 9.5.4, importing a CSS file from node_modules is permitted anywhere in your application.

Next.js 9.5.4๋ฒ„์ „๋ถ€ํ„ฐ node_mouldes๋กœ๋ถ€ํ„ฐ CSS ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์–ด๋””์„œ๋“  ํ—ˆ์šฉ๋œ๋‹ค.

For global stylesheets, like bootstrap or nprogress, you should import the file inside pages/_app.js. For example:

bootstrap์ด๋‚˜ nprogress ๊ฐ™์€ ์ „์—ญ ์Šคํƒ€์ผ์‹œํŠธ๋ฅผ ์œ„ํ•ด์„œ ํŒŒ์ผ์„ pages/_app.js ์•ˆ์— import ํ•ด์•ผํ•œ๋‹ค.

For importing CSS required by a third party component, you can do so in your component. For example:

์„œ๋“œ ํŒŒํ‹ฐ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•„์š”ํ•œ CSS๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ ์ปดํฌ๋„ŒํŠธ์— ์ด์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค:

Adding Component-Level CSS

Next.js supports CSS Modules using the [name].module.css file naming convention.

CSS Modules locally scope CSS by automatically creating a unique class name. This allows you to use the same CSS class name in different files without worrying about collisions.

This behavior makes CSS Modules the ideal way to include component-level CSS. CSS Module files can be imported anywhere in your application.

For example, consider a reusable Button component in the components/ folder:

First, create components/Button.module.css with the following content:

Next.js๋Š” [name].module.css๋ผ๋Š” ํŒŒ์ผ ๋ช…๋ช… ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜๋Š” CSS Modules๋ฅผ ์ง€์›ํ•œ๋‹ค.

CSS ๋ชจ๋“ˆ์€ ๊ณ ์œ ํ•œ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์ง€์—ญ CSS ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์ด๊ฒƒ์€ ๋™์ผํ•œ CSS ํด๋ž˜์Šค ๋ช…์„ ๋‹ค๋ฅธ ํŒŒ์ผ๋“ค์—์„œ ์‚ฌ์šฉํ•ด๋„ ์ถฉ๋Œ์— ๋Œ€ํ•œ ๊ฑฑ์ •์„ ํ•  ํ•„์š”๊ฐ€ ์—†๊ฒŒ ํ•ด์ค€๋‹ค.

์ด๋Ÿฌํ•œ ๋™์ž‘์€ CSS ๋ชจ๋“ˆ์„ ์ปดํฌ๋„ŒํŠธ ๋ ˆ๋ฒจ CSS๋ฅผ ํฌํ•จํ•˜๋Š” ์ด์ƒ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ๋“ ๋‹ค.(?) CSS ๋ชจ๋“ˆ ํŒŒ์ผ๋“ค์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์–ด๋””์—์„œ๋‚˜ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด components/ ํด๋”์— ์žˆ๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Button ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ณ ๋ คํ•ด๋ณด์ž:

๋จผ์ €, ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ components/Button.module.css ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

Then, create components/Button.js, importing and using the above CSS file:

๊ทธ๋ฆฌ๊ณ  components/Button.js๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์œ„์˜ CSS ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•œ๋‹ค:

CSS Modules are an optional feature and are only enabled for files with the .module.css extension. Regular <link> stylesheets and global CSS files are still supported.

In production, all CSS Module files will be automatically concatenated into many minified and code-split .css files. These .css files represent hot execution paths in your application, ensuring the minimal amount of CSS is loaded for your application to paint.

CSS ๋ชจ๋“ˆ์€ ์„ ํƒ์ ์ธ ๊ธฐ๋Šฅ์œผ๋กœ .module.css ํ™•์žฅ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ์ผ๋“ค์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. ์ผ๋ฐ˜์ ์ธ <link>์Šคํƒ€์ผ์‹œํŠธ์™€ ์ „์—ญ CSS ํŒŒ์ผ๋„ ์ง€์›ํ•œ๋‹ค.

์šด์˜ ํ™˜๊ฒฝ์—์„œ ๋ชจ๋“  CSS ๋ชจ๋“ˆ ํŒŒ์ผ๋“ค์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ตœ์†Œํ™”๋˜๊ณ  ์ฝ”๋“œ๊ฐ€ ๋ถ„ํ• ๋œ .css ํŒŒ์ผ๋กœ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐ๋  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ .css ํŒŒ์ผ๋“ค์€ ํ•ซ ์‹คํ–‰ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํŽ˜์ธํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ตœ์†Œํ•œ์˜ ์–‘์˜ CSS๊ฐ€ ๋กœ๋“œ๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค.(?)

Sass Support

Next.js allows you to import Sass using both the .scss and .sass extensions. You can use component-level Sass via CSS Modules and the .module.scss or .module.sass extension.

Before you can use Next.js' built-in Sass support, be sure to install sass:

Next.js๋Š” .scss์™€ .sass ๋‘ ํ™•์žฅ์ž๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ Sass๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค. ์ปดํฌ๋„ŒํŠธ ๋ ˆ๋ฒจ Sass๋ฅผ CSS ๋ชจ๋“ˆ๊ณผ .module.css, .module.sass ํ™•์žฅ์ž๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Next.js์˜ ๋‚ด์žฅ Sass ์ง€์›์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— sass๋ฅผ ์„ค์น˜ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์ž:

Sass support has the same benefits and restrictions as the built-in CSS support detailed above.

Sass ์ง€์›์€ ์œ„์—์„œ ์„ค๋ช…๋œ ๋‚ด์žฅ CSS ์ง€์›๊ณผ ๊ฐ™์€ ์ด์ ๊ณผ ์ œํ•œ์ด ์žˆ๋‹ค.

Note: Sass supports two different syntaxes, each with their own extension. The .scss extension requires you use the SCSS syntax, while the .sass extension requires you use the Indented Syntax ("Sass").

If you're not sure which to choose, start with the .scss extension which is a superset of CSS, and doesn't require you learn the Indented Syntax ("Sass").

๋…ธํŠธ: Sass๋Š” ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๊ตฌ๋ฌธ์„ ๊ฐ์ž์˜ ํ™•์žฅ์ž๋กœ ์ง€์›ํ•œ๋‹ค. .scss ํ™•์žฅ์ž๋Š” SCSS ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์š”๊ตฌ๋˜๊ณ  .sass ํ™•์žฅ์ž์˜ ๊ฒฝ์šฐ Indented Syntax(Sass)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์š”๊ตฌ๋œ๋‹ค.

์–ด๋–ค ๊ฒƒ์„ ์„ ํƒํ•ด์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด, CSS์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ .scss ํ™•์žฅ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ  Indented Syntax(Sass)๋Š” ๋ฐฐ์šธ ํ•„์š”๊ฐ€ ์—†๋‹ค.

Customizing Sass Options

If you want to configure the Sass compiler you can do so by using sassOptions in next.config.js.

For example to add includePaths:

Sass ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์„ค์ •ํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด next.config.jsํŒŒ์ผ์— sassOptions๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

includePaths๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์‹œ์ด๋‹ค:

Less and Stylus Support

To support importing .less or .styl files you can use the following plugins:

.less ๋‚˜ .styl ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์˜ ํ”Œ๋Ÿฌ๊ทธ์ธ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค:

If using the less plugin, don't forget to add a dependency on less as well, otherwise you'll see an error like:

less ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด less์—๋„ dependency๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€๋ง๋ผ. ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋ณด์ผ ๊ฒƒ์ด๋‹ค.

CSS-in-JS

It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:

๊ธฐ์กด์˜ CSS-in-JS ์†”๋ฃจ์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์ธ๋ผ์ธ ์Šคํƒ€์ผ์ด๋‹ค.

We bundle styled-jsx to provide support for isolated scoped CSS. The aim is to support "shadow CSS" similar to Web Components, which unfortunately do not support server-rendering and are JS-only.

See the above examples for other popular CSS-in-JS solutions (like Styled Components).

A component using styled-jsx looks like this:

๋…๋ฆฝ๋œ ๋ฒ”์œ„์˜ CSS์— ์ง€์›์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ styled-jsx๋ฅผ ๋ฒˆ๋“ค๋กœ ์ œ๊ณตํ•œ๋‹ค.(?) ๋ชฉํ‘œ๋Š” ์•ˆํƒ€๊น๊ฒŒ๋„ ์„œ๋ฒ„ ๋ Œ๋”๋ง์„ ์ง€์›ํ•˜์ง€ ์•Š๊ณ  JS ์ „์šฉ์ธ ์›น ์ปดํฌ๋„ŒํŠธ์™€ ์œ ์‚ฌํ•œ shadow CSS๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.(?)

Styled Components์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์ธ๊ธฐ์žˆ๋Š” CSS-in-JS ์†”๋ฃจ์…˜๋“ค์„ ์œ„ํ•œ ์œ„์˜ ์˜ˆ์‹œ๋“ค์„ ๋ณด๋ผ (์˜ˆ์‹œ๋Š” ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ)

styled-jsx๋ฅผ ์‚ฌ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค:

Please see the styled-jsx documentation for more examples.

๋” ๋งŽ์€ ์˜ˆ์ œ๋Š” styled-jsx ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.