콘텐츠 경로와 불필요한 코드 제거
content 배열을 올바르게 구성해 Tailwind가 모든 파일을 검색하고 프로덕션 빌드에서 사용하지 않는 클래스를 제거하도록 합니다.
콘텐츠 경로와 불필요한 코드 제거은(는) CoddyKit의 무료 Tailwind CSS Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Tailwind CSS Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Purging Matters
Tailwind CSS generates utility classes for every combination of your theme values — potentially hundreds of thousands of classes. Without pruning, the raw CSS output would be several megabytes. The purging process (officially called content scanning or tree-shaking) removes every class that does not appear in your source files, shrinking the production CSS to typically just 5–50 KB. Getting your content configuration right is essential for a lean build.
How Content Scanning Works
Tailwind scans the files you list in the content array and searches for string patterns that look like Tailwind class names. It does not execute your code — it does a simple text search. Any string that matches a known utility class is included in the output. This is why you must never construct class names dynamically by concatenating strings; the scanner won't find the full class name.
// BAD: Tailwind cannot detect 'text-red-500'
const color = 'red';
const cls = 'text-' + color + '-500'; // scanner sees 'text-' + 'red' + '-500'
// GOOD: Full class names are always present in source
const cls = isError ? 'text-red-500' : 'text-green-500'; // scanner sees bothWriting Content Glob Patterns
Glob patterns in the content array use standard glob syntax. The double-star ** matches any number of directory levels. Curly braces {} match multiple extensions. Be as specific as possible — avoid patterns that are too broad (like ./**/*) as they will slow down scanning by including files like images and JSON that cannot contain class names.
// tailwind.config.js
module.exports = {
content: [
'./src/**/*.{html,js,jsx,ts,tsx,vue}',
'./components/**/*.{js,jsx,ts,tsx}',
'./pages/**/*.{js,jsx,ts,tsx}',
'./layouts/**/*.html',
// Include a specific file
'./public/index.html',
// Include node_modules (for UI library components)
'./node_modules/@my-ui/components/dist/**/*.js',
],
};Content for Popular Frameworks
Different frameworks organize their files differently. Here are the correct content patterns for the most popular setups. Next.js needs app/ and pages/ directories. Vue projects typically use .vue files. Laravel's Blade templates live in resources/views/. Always check that your entry files (like index.html or layout files) are included explicitly if they sit outside your glob patterns.
// Next.js App Router
content: ['./app/**/*.{js,jsx,ts,tsx}', './components/**/*.{js,jsx,ts,tsx}']
// Vue + Vite
content: ['./index.html', './src/**/*.{vue,js,ts}']
// Laravel Blade
content: ['./resources/**/*.blade.php', './resources/**/*.js']
// Nuxt 3 (usually auto-detected, but explicit)
content: ['./components/**/*.vue', './pages/**/*.vue', './layouts/**/*.vue']The safelist Option
Sometimes classes are constructed at runtime and cannot be detected by static scanning — for example, class names built from CMS content, user input, or API responses. Use the safelist option to force-include specific classes or patterns, ensuring they are always in the output regardless of whether the scanner finds them. You can safelist individual strings or regex patterns.
// tailwind.config.js
module.exports = {
safelist: [
// Individual classes
'text-red-500',
'bg-blue-100',
// Pattern: all bg-{color}-{shade} for red and green
{
pattern: /bg-(red|green)-(100|200|300|400|500)/,
},
// Include hover variants too
{
pattern: /bg-red-(400|500)/,
variants: ['hover', 'focus'],
},
],
};The blocklist Option
The blocklist option is the opposite of safelist — it prevents specific classes from being included in the output even if the scanner finds them in your source. This is useful for enforcing team conventions (preventing use of deprecated utility names) or reducing output size by explicitly excluding utilities you know you will never use, like certain font size or animation classes.
// tailwind.config.js
module.exports = {
blocklist: [
// Prevent these classes from being generated
'container',
'prose',
'animate-bounce',
],
};Content Transformers
For files that need preprocessing before scanning — like .pug templates, .md files, or custom template engines — Tailwind's content.transform option lets you provide a function that transforms the raw file content into a string of class names. This is rarely needed but powerful for edge cases where the file format hides class names in a non-obvious way.
// tailwind.config.js
module.exports = {
content: {
files: ['./src/**/*.{html,js}'],
transform: {
// Transform markdown files before scanning
md: (content) => {
// Render to HTML first so class names are visible
return renderMarkdownToHtml(content);
},
},
},
};Content Extraction for Component Libraries
If your project uses a third-party component library that ships pre-built JavaScript with embedded Tailwind classes, you need to include the library's dist files in your content array. Many popular Tailwind component libraries (like Flowbite, daisyUI, or custom internal libraries) work this way. Add the path to their built JavaScript output so the scanner can detect which classes they use.
// tailwind.config.js
module.exports = {
content: [
'./src/**/*.{html,js,jsx,ts,tsx}',
// Include prebuilt components from a library
'./node_modules/flowbite/**/*.js',
// Or a local package in a monorepo
'../../packages/ui/src/**/*.{js,jsx,ts,tsx}',
],
};Verifying Your Content Config
To confirm that your content configuration is working correctly, run npx tailwindcss build and check the output file size. A well-configured production build should be under 50 KB (often much less). You can also run the Tailwind CLI with the --watch flag during development to see real-time compilation and verify that adding a new class immediately appears in the output.
# Build and check output size
npx tailwindcss -i ./src/input.css -o ./dist/output.css --minify
# Check the size
wc -c < ./dist/output.css # bytes
ls -lh ./dist/output.css # human-readable
# Watch mode for development
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watchJIT and On-Demand Class Generation
Tailwind v3 uses a Just-In-Time (JIT) engine by default. Unlike the old PurgeCSS approach, JIT generates classes on demand as you write them, scanning your content files in real time during development. This means your development CSS is already small and matches production exactly — there is no separate purging step in v3. The content array serves both development scanning and production optimization.
Common Content Configuration Mistakes
The most common mistake is missing files from the content array — classes from those files get purged in production. Another mistake is dynamic class construction where the full class name never appears as a string. A third issue is forgetting template files in non-JS frameworks. Always test your production build locally with NODE_ENV=production to catch purging issues before deploying.
// Common mistake: incomplete pattern
content: ['./src/**/*.js'] // MISSES .jsx, .tsx, .html files!
// Better: cover all template types
content: ['./src/**/*.{html,js,jsx,ts,tsx}']
// Also common: missing the root HTML file
content: [
'./index.html', // don't forget this!
'./src/**/*.{js,jsx,ts,tsx}'
]Quick Check
Test your understanding of Tailwind's content paths and class purging.
Lesson Recap
In this lesson you learned: the content array tells Tailwind which files to scan for class names, full class names must always appear as literal strings for the scanner to detect them, and the safelist option force-includes classes that cannot be statically detected. Next up we explore the difference between extending and overriding the Tailwind theme.
자주 묻는 질문
“콘텐츠 경로와 불필요한 코드 제거” 강의는 무료인가요?
네 — “콘텐츠 경로와 불필요한 코드 제거” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Tailwind CSS Academy 강의 전체를 잠금 해제할 수 있습니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“콘텐츠 경로와 불필요한 코드 제거”에서 뭘 배우나요?
content 배열을 올바르게 구성해 Tailwind가 모든 파일을 검색하고 프로덕션 빌드에서 사용하지 않는 클래스를 제거하도록 합니다. 브라우저에서 직접 실행하는 실습 코드로 Tailwind CSS Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Tailwind CSS Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Tailwind CSS Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“콘텐츠 경로와 불필요한 코드 제거” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Tailwind CSS Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Tailwind CSS Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- tailwind.config.js의 구조
- 콘텐츠 경로와 불필요한 코드 제거
- 테마 확장과 재정의 비교
- 플러그인 추가 및 구성