Tailwind CSS HUGO 시작하기 (Getting started with HUGO)
HUGO는 파일과 에셋을 쉽게 정리할 수 있는 인기 있는 오픈소스 정적 사이트 생성기 프레임워크예요. 택소노미 시스템, 다국어 지원, 빠른 에셋 파이프라인 등을 활용할 수 있으며, 수백만 개발자와 Bootstrap, Litecoin, Smashing Magazine, 심지어 Flowbite 같은 웹사이트에서도 사용됩니다. HUGO 프로젝트에 Tailwind CSS와 Flowbite의 UI 컴포넌트를 함께 설치·구성해 프론트엔드 개발 워크플로우를 향상시키는 스택으로 웹사이트를 구축하는 법을 알아보아요.
출처: 문서
본문
사전 요구사항 (Requirements)
컴퓨터에 HUGO와 Node.js가 모두 로컬로 설치되어 있는지 확인하세요.
HUGO 프로젝트 만들기 (Create a HUGO project)
의존성을 설치한 뒤 다음 단계를 따라 새 HUGO 프로젝트를 만드는 법을 알아봐요.
- 새 HUGO 애플리케이션을 만들려면 다음 CLI 명령을 실행해요:
hugo new site flowbite-app
cd flowbite-app
- 다음 단계로 로컬 커스텀 테마를 만들어요:
hugo new theme flowbite-theme
이 명령은 HUGO 앱에서 확장할 수 있는 새 scaffold 테마 디렉터리를 만들어요.
- 다음으로
config.toml파일에 테마를 추가해요:
theme = ["flowbite-theme"]
- 다음 명령으로 로컬 서버를 실행해요:
hugo server -D
이제 생성된 localhost 서버에서 기본 HUGO 웹사이트가 실행되는 것을 볼 수 있어요. HUGO를 성공적으로 설치·구성했습니다!
Tailwind CSS 설치 (Install Tailwind CSS)
Tailwind CSS는 웹사이트 스타일링을 더 잘 제어할 수 있게 하고 조직 내 다른 팀원들과 협업하기 쉽게 만드는 인기 있는 utility-first CSS 프레임워크예요.
flowbite-theme/디렉터리로 이동해 다음 명령을 실행해요:
npm install tailwindcss @tailwindcss/cli --save-dev
flowbite-theme/디렉터리의main.css파일 안에 다음을 추가해요:
@import "tailwindcss";
- 테마 디렉터리 안에서 이 명령을 실행해 Tailwind CSS 코드를 컴파일해요:
npx @tailwindcss/cli -i ./assets/css/main.css -o ./assets/css/output.css --watch
flowbite-theme/디렉터리의css.html파일을 다음으로 업데이트해요:
{{- with resources.Get "css/output.css" }}
{{- if eq hugo.Environment "development" }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{- else }}
{{- with . | minify | fingerprint }}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
{{- end }}
{{- end }}
{{- end }}
- Tailwind CSS를 테스트하기 위해
single.html파일에 유틸리티 클래스를 추가해요:
{{ define "main" }}
<h1 class="text-fg-brand">{{ .Title }}</h1>
{{ $dateMachine := .Date | time.Format "2006-01-02T15:04:05-07:00" }}
{{ $dateHuman := .Date | time.Format ":date_long" }}
<time datetime="{{ $dateMachine }}">{{ $dateHuman }}</time>
{{ .Content }}
{{ partial "terms.html" (dict "taxonomy" "tags" "page" .) }}
{{ end }}
게시물 페이지 중 하나로 이동하면 텍스트가 파란색으로 바뀐 것을 볼 수 있어요.
Flowbite 설치 (Install Flowbite)
Tailwind CSS로 구성된 동작하는 HUGO 프로젝트가 있으니 이제 Flowbite를 설치할 수 있어요. Tailwind CSS와 마찬가지로 flowbite-theme 디렉터리 안에 의존성을 설치해야 합니다.
- NPM을 사용해 Flowbite를 의존성으로 설치해요:
npm install flowbite --save
- 메인
main.cssCSS 파일 안에 Flowbite의 기본 테마 변수를 가져와요:
@import "flowbite/src/themes/default";
- CSS에 Flowbite 플러그인 파일을 가져와요:
@plugin "flowbite/plugin";
- CSS에서 Flowbite의 소스 파일을 구성해요:
@source "../../node_modules/flowbite";
js.html파일에 Flowbite JavaScript를 추가해요:
<script src="https://cdn.jsdelivr.net/npm/flowbite@4.0.1/dist/flowbite.min.js"></script>
{{- with resources.Get "js/main.js" }}
{{- if eq hugo.Environment "development" }}
{{- with . | js.Build }}
<script src="{{ .RelPermalink }}"></script>
{{- end }}
{{- else }}
{{- $opts := dict "minify" true }}
{{- with . | js.Build $opts | fingerprint }}
<script src="{{ .RelPermalink }}" integrity="{{- .Data.Integrity }}" crossorigin="anonymous"></script>
{{- end }}
{{- end }}
{{- end }}
- 이제 테마 디렉터리의
menu.html파일을 업데이트해 Flowbite를 테스트해 보아요:
{{- /*
Renders a menu for the given menu ID.
@context {page} page The current page.
@context {string} menuID The menu ID.
@example: {{ partial "menu.html" (dict "menuID" "main" "page" .) }}
*/}}
{{- $page := .page }}
{{- $menuID := .menuID }}
{{- with index site.Menus $menuID }}
<nav class="bg-white border-gray-200 dark:bg-gray-900">
<div class="max-w-screen-xl flex flex-wrap items-center justify-between mx-auto p-4">
<a href="https://flowbite.com/" class="flex items-center space-x-3 rtl:space-x-reverse">
<img src="https://flowbite.com/docs/images/logo.svg" class="h-8" alt="Flowbite Logo" />
<span class="self-center text-2xl font-semibold whitespace-nowrap dark:text-white">Flowbite</span>
</a>
<button data-collapse-toggle="navbar-default" type="button" class="inline-flex items-center p-2 w-10 h-10 justify-center text-sm text-gray-500 rounded-lg md:hidden hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600" aria-controls="navbar-default" aria-expanded="false">
<span class="sr-only">Open main menu</span>
<svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 17 14">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M1 1h15M1 7h15M1 13h15"/>
</svg>
</button>
<div class="hidden w-full md:block md:w-auto" id="navbar-default">
<ul class="font-medium flex flex-col p-4 md:p-0 mt-4 border border-gray-100 rounded-lg bg-gray-50 md:flex-row md:space-x-8 rtl:space-x-reverse md:mt-0 md:border-0 md:bg-white dark:bg-gray-800 md:dark:bg-gray-900 dark:border-gray-700">
{{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
</ul>
</div>
</div>
</nav>
{{- end }}
{{- define "partials/inline/menu/walk.html" }}
{{- $page := .page }}
{{- range .menuEntries }}
{{- $attrs := dict "href" .URL }}
{{- if $page.IsMenuCurrent .Menu . }}
{{- $attrs = merge $attrs (dict "class" "active" "aria-current" "page") }}
{{- else if $page.HasMenuCurrent .Menu .}}
{{- $attrs = merge $attrs (dict "class" "ancestor" "aria-current" "true") }}
{{- end }}
{{- $name := .Name }}
{{- with .Identifier }}
{{- with T . }}
{{- $name = . }}
{{- end }}
{{- end }}
<li>
<a
class="block py-2 px-3 text-heading rounded-sm hover:bg-neutral-secondary-soft md:hover:bg-transparent md:border-0 md:hover:text-fg-brand md:p-0"
{{- range $k, $v := $attrs }}
{{- with $v }}
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end }}
{{- end -}}
>{{ $name }}</a>
{{- with .Children }}
<ul>
{{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
</ul>
{{- end }}
</li>
{{- end }}
{{- end }}
서버를 다시 로드하면 메뉴의 페이지들이 내비바 컴포넌트 안에서 렌더링되는 것을 볼 수 있어요.
UI 컴포넌트 (UI components)
이제 모든 것을 설치·설정했으니 Flowbite의 UI 컴포넌트를 사용해 hero 섹션, 내비게이션 바, 어드민 CRUD 레이아웃 등으로 HUGO와 함께 훨씬 빠르고 쉽게 웹사이트를 구축할 수 있어요.
HUGO 스타터 테마 (HUGO starter theme)
HUGO, Tailwind CSS, Flowbite가 이미 구성된 개인 블로그 웹사이트로 바로 사용하거나 시작점으로 쓸 수 있는 무료 오픈소스 HUGO 스타터 테마를 개발했어요.