Tailwind CSS 4 và Oxide Engine: Khi CSS Framework được viết lại bằng Rust

Posted on: 4/18/2026 7:11:09 AM

1. Tailwind CSS 4 — Cuộc cách mạng từ Oxide Engine

Tailwind CSS đã trở thành framework CSS phổ biến nhất thế giới với hơn 30 triệu lượt tải mỗi tháng trên npm. Nhưng phiên bản 4.0 không chỉ là một bản cập nhật thông thường — đây là một bản viết lại hoàn toàn từ đầu, với Oxide Engine được xây dựng bằng Rust thay thế hoàn toàn pipeline JavaScript cũ.

Nếu bạn đang dùng Vue.js, Nuxt, hoặc bất kỳ framework frontend nào, Tailwind CSS 4 mang đến những thay đổi ảnh hưởng trực tiếp đến trải nghiệm phát triển hàng ngày: build nhanh hơn 100 lần ở incremental, cấu hình bằng CSS thuần thay vì JavaScript, và tích hợp native với các tính năng CSS hiện đại.

5x Nhanh hơn — Full build
100x Nhanh hơn — Incremental build
5ms Thời gian incremental rebuild
0 File config JS cần thiết

2. Oxide Engine — Tại sao viết lại bằng Rust?

Ở các phiên bản trước (v1–v3), Tailwind CSS sử dụng pipeline hoàn toàn bằng JavaScript: PostCSS plugin quét source code, regex matching class names, rồi generate CSS tương ứng. Cách tiếp cận này có vấn đề nghiêm trọng ở dự án lớn — khi project có hàng nghìn file component, thời gian build tăng tuyến tính và trở thành bottleneck của CI/CD pipeline.

Oxide Engine giải quyết triệt để bằng cách viết lại toàn bộ core bằng Rust:

graph LR
    subgraph V3["Tailwind v3 — JavaScript Pipeline"]
        A1["Source Files"] --> B1["PostCSS Plugin
(Node.js)"] B1 --> C1["Regex Scanner
(JavaScript)"] C1 --> D1["CSS Generator
(JavaScript)"] D1 --> E1["Output CSS"] end subgraph V4["Tailwind v4 — Oxide Engine"] A2["Source Files"] --> B2["Oxide Scanner
(Rust/WASM)"] B2 --> C2["Lightning CSS
(Rust)"] C2 --> D2["Optimized Output"] end style V3 fill:#f8f9fa,stroke:#e0e0e0,color:#2c3e50 style V4 fill:#f8f9fa,stroke:#e94560,color:#2c3e50 style B2 fill:#e94560,stroke:#fff,color:#fff style C2 fill:#e94560,stroke:#fff,color:#fff

So sánh pipeline xử lý giữa Tailwind v3 (JavaScript) và v4 (Rust/Oxide)

2.1 Kiến trúc Oxide Engine

Oxide Engine gồm 3 thành phần chính:

  • Content Scanner (Rust): Quét tất cả source files trong project để tìm Tailwind class names. Thay vì regex đơn giản, Oxide dùng parser chuyên biệt hiểu được cú pháp template của nhiều framework (Vue SFC, JSX, Svelte, HTML). Scanner chạy parallel trên nhiều thread — điều JavaScript không thể làm hiệu quả do single-threaded event loop.
  • Lightning CSS (Rust): Thay thế PostCSS cho việc parse và transform CSS. Lightning CSS nhanh hơn PostCSS hàng chục lần nhờ zero-copy parsing và native CSS nesting support. Nó cũng handle vendor prefixing, minification trong cùng một pass.
  • Incremental Compilation: Oxide cache kết quả scan trước đó. Khi chỉ 1 file thay đổi, chỉ file đó được re-scan — không cần chạy lại toàn bộ pipeline. Đây là lý do incremental rebuild chỉ mất 5ms thay vì hàng trăm ms.

2.2 Benchmark thực tế

Dưới đây là kết quả benchmark trên một dự án thực tế với ~2.000 component files:

Full Build (Cold Start)

Tailwind v3
378ms
Tailwind v4
100ms

Incremental Rebuild (HMR)

Tailwind v3
~500ms
Tailwind v4
5ms

Ý nghĩa thực tế

Incremental rebuild 5ms có nghĩa là khi bạn sửa một component Vue và save, CSS output được cập nhật gần như tức thì — nhanh hơn cả thời gian Vite cần để gửi HMR update đến browser. Trên CI/CD pipeline cho dự án lớn, tổng thời gian build CSS giảm từ phút xuống giây.

3. CSS-First Configuration — Khai tử tailwind.config.js

Đây là thay đổi lớn nhất về developer experience. Tailwind v4 loại bỏ hoàn toàn file tailwind.config.js. Mọi cấu hình giờ nằm trong CSS, sử dụng directive @theme.

3.1 Trước và sau

Tailwind v3 (JavaScript) Tailwind v4 (CSS-first)
tailwind.config.js với module.exports Directive @theme trong file CSS
content: ['./src/**/*.vue'] — khai báo thủ công Tự động detect — Oxide scanner tìm class names không cần config
theme.extend.colors trong JS object @theme { --color-primary: #e94560; }
Plugin system qua addUtilities() Native CSS @utility directive
Cần restart dev server khi đổi config Hot reload — thay đổi @theme được apply ngay

3.2 Cấu hình bằng @theme

Thay vì viết JavaScript config phức tạp, bạn định nghĩa design tokens trực tiếp bằng CSS variables:

/* app.css — thay thế hoàn toàn tailwind.config.js */
@import "tailwindcss";

@theme {
  --color-primary: #e94560;
  --color-secondary: #2c3e50;
  --color-accent: #4CAF50;

  --font-family-display: "Inter", sans-serif;
  --font-family-mono: "Fira Code", monospace;

  --breakpoint-xs: 475px;

  --animate-fade-in: fade-in 0.3s ease-out;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

Mỗi biến trong @theme tự động tạo ra utility class tương ứng. Ví dụ --color-primary: #e94560 sinh ra bg-primary, text-primary, border-primary, v.v. — không cần khai báo thêm gì.

Tại sao CSS-first tốt hơn?

Type safety tự nhiên: IDE hiểu CSS variables, autocompletion hoạt động ngay không cần extension riêng. Composability: @theme có thể import từ nhiều file CSS, dễ tổ chức design system theo module. Runtime access: Vì tokens là CSS custom properties, JavaScript có thể đọc qua getComputedStyle() — hữu ích cho dynamic theming hoặc chart libraries.

4. Tận dụng CSS hiện đại — Không còn polyfill

Tailwind v3 phải polyfill nhiều tính năng CSS vì cần hỗ trợ browser cũ. V4 tận dụng triệt để CSS native features:

4.1 Cascade Layers (@layer)

Tailwind v4 sử dụng @layer chuẩn CSS (không phải directive nội bộ như v3) để tổ chức specificity:

@layer theme, base, components, utilities;

/* Utility classes luôn thắng component styles,
   không cần !important hay phức tạp hóa selector */
@layer utilities {
  .text-primary { color: var(--color-primary); }
}

@layer components {
  .btn { color: blue; } /* Bị override bởi text-primary */
}

Điều này giải quyết triệt để bài toán specificity wars — utility classes luôn có độ ưu tiên cao hơn component styles mà không cần !important.

4.2 Container Queries

Thay vì responsive dựa trên viewport width, container queries responsive theo kích thước parent element — phù hợp hơn cho component-based architecture:

<!-- Vue component sử dụng container queries -->
<div class="@container">
  <div class="grid grid-cols-1 @md:grid-cols-2 @lg:grid-cols-3">
    <ProductCard v-for="item in products" :key="item.id" />
  </div>
</div>

Component này tự điều chỉnh layout dựa trên container width, không phải viewport — nên dù đặt trong sidebar hay full-width page đều responsive đúng.

4.3 color-mix() và Advanced Color

V4 hỗ trợ color-mix() cho phép blend màu trực tiếp trong utility:

/* Tạo variant sáng/tối từ 1 màu gốc */
<button class="bg-primary bg-primary/80 hover:bg-primary/60">
  Transparency tự nhiên qua opacity modifier
</button>

/* color-mix cho blend 2 màu */
@theme {
  --color-primary-light: color-mix(in oklch, var(--color-primary) 30%, white);
  --color-primary-dark: color-mix(in oklch, var(--color-primary) 70%, black);
}

5. Tích hợp với Vue.js và Vite

Tailwind v4 được thiết kế để tích hợp liền mạch với Vite — build tool mặc định của Vue 3. Không cần PostCSS config phức tạp, chỉ cần một dòng import.

graph TD
    A["vite.config.ts"] --> B["@tailwindcss/vite plugin"]
    B --> C["Oxide Engine"]
    C --> D["Scan .vue SFC"]
    C --> E["Scan .ts/.tsx"]
    C --> F["Scan .html"]
    D --> G["Generate CSS"]
    E --> G
    F --> G
    G --> H["Lightning CSS
Minify + Prefix"] H --> I["Production Bundle"] style A fill:#f8f9fa,stroke:#e0e0e0,color:#2c3e50 style B fill:#e94560,stroke:#fff,color:#fff style C fill:#e94560,stroke:#fff,color:#fff style H fill:#16213e,stroke:#fff,color:#fff style I fill:#4CAF50,stroke:#fff,color:#fff

Pipeline tích hợp Tailwind v4 với Vite cho dự án Vue

5.1 Setup cho dự án Vue + Vite

# Cài đặt
npm install tailwindcss @tailwindcss/vite
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'

export default defineConfig({
  plugins: [
    vue(),
    tailwindcss(),
  ],
})
/* src/app.css — chỉ cần 1 dòng */
@import "tailwindcss";

Không cần postcss.config.js, không cần tailwind.config.js, không cần khai báo content paths. Oxide Engine tự động quét project và tìm tất cả Tailwind classes.

5.2 Vue SFC với Tailwind v4

Tailwind v4 hiểu Vue Single File Component (SFC) natively — scanner parse đúng phần <template>, <script> (cho dynamic class bindings), và bỏ qua phần <style>:

<template>
  <div class="@container p-4">
    <h1 class="text-3xl @lg:text-5xl font-display text-primary
               animate-fade-in">
      {{ title }}
    </h1>
    <div :class="[
      'mt-4 rounded-lg p-6',
      isActive ? 'bg-accent/10 border-accent' : 'bg-secondary/5'
    ]">
      <slot />
    </div>
  </div>
</template>

<script setup lang="ts">
defineProps<{
  title: string
  isActive: boolean
}>()
</script>

Dynamic class bindings

Oxide Engine phân tích cả biểu thức JavaScript trong :class binding. Các class như bg-accent/10bg-secondary/5 trong ternary expression đều được detect và include trong output — không cần safelist thủ công.

6. Tạo Custom Utilities bằng @utility

Một trong những tính năng mạnh nhất của v4 là @utility — cho phép tạo utility class mới chỉ bằng CSS, có đầy đủ tính năng responsive, hover, dark mode... giống utility gốc:

@utility scrollbar-hidden {
  -ms-overflow-style: none;
  scrollbar-width: none;
  &::-webkit-scrollbar { display: none; }
}

@utility text-gradient {
  background: linear-gradient(135deg, var(--color-primary), var(--color-accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* Dùng ngay trong template */
/* class="scrollbar-hidden hover:text-gradient" */

Trước đây với v3, tạo custom utility cần viết plugin JavaScript với API phức tạp (addUtilities, matchUtilities). Giờ chỉ cần CSS thuần — dễ đọc, dễ maintain, và được IDE highlight chính xác.

7. Migration từ v3 lên v4

Tailwind cung cấp codemod tự động xử lý phần lớn thay đổi. Tuy nhiên, có một số breaking changes cần lưu ý:

Thay đổi v3 v4 Xử lý
Config format tailwind.config.js @theme trong CSS Codemod tự chuyển
Content scanning content: [...] Tự động detect Xóa config, tự hoạt động
Color opacity bg-red-500/50 bg-red-500/50 (giữ nguyên) Không cần thay đổi
Dark mode darkMode: 'class' Mặc định dùng prefers-color-scheme Thêm @variant dark (&.dark) nếu cần class strategy
Custom plugins plugin(function({ addUtilities })) @utility hoặc JS plugin (vẫn support) Migrate dần sang @utility
PostCSS Bắt buộc postcss.config.js Không cần (dùng Vite plugin) Xóa postcss config

Lưu ý khi migrate

Chạy codemod trước: npx @tailwindcss/upgrade. Tool này tự chuyển config JS sang @theme, rename deprecated utilities, và cập nhật import. Tuy nhiên, custom plugins phức tạp (dùng matchUtilities với regex) cần migrate thủ công. Nên test kỹ dark mode behavior sau khi upgrade — mặc định v4 dùng media query thay vì class.

8. Tối ưu Production Build

Tailwind v4 với Lightning CSS tích hợp sẵn tối ưu mà trước đây cần nhiều tool riêng biệt:

graph LR
    subgraph BEFORE["v3 — Nhiều tool"]
        P1["PostCSS"] --> P2["Autoprefixer"]
        P2 --> P3["cssnano"]
        P3 --> P4["PurgeCSS"]
    end
    subgraph AFTER["v4 — All-in-one"]
        Q1["Oxide + Lightning CSS"]
    end
    P4 --> R["Output"]
    Q1 --> R
    style BEFORE fill:#f8f9fa,stroke:#e0e0e0,color:#2c3e50
    style AFTER fill:#f8f9fa,stroke:#e94560,color:#2c3e50
    style Q1 fill:#e94560,stroke:#fff,color:#fff
    

v4 hợp nhất scanning, prefixing, minification trong một pipeline duy nhất

Kết quả trên production build:

  • Vendor prefixing: Lightning CSS thêm prefix chính xác theo browserslist, không thừa như Autoprefixer.
  • Minification: Thay cssnano. Lightning CSS minify nhanh hơn 10x và output nhỏ hơn ~5% nhờ hiểu sâu CSS syntax.
  • Dead code elimination: Oxide đã chỉ generate CSS cho class thực sự dùng — không cần PurgeCSS riêng.
  • Nesting flattening: CSS nesting được flatten cho browser không hỗ trợ, nếu browserslist yêu cầu.

9. Xây dựng Design System với @theme

Với @theme, việc xây dựng design system có cấu trúc trở nên tự nhiên hơn rất nhiều:

/* tokens/colors.css */
@theme {
  --color-brand-50: oklch(0.97 0.02 250);
  --color-brand-100: oklch(0.93 0.04 250);
  --color-brand-500: oklch(0.55 0.18 250);
  --color-brand-900: oklch(0.25 0.10 250);

  --color-surface: var(--color-brand-50);
  --color-on-surface: var(--color-brand-900);
}

/* tokens/spacing.css */
@theme {
  --spacing-gutter: 1rem;
  --spacing-section: 4rem;
}

/* tokens/typography.css */
@theme {
  --font-size-display: clamp(2.5rem, 5vw, 4rem);
  --line-height-display: 1.1;
}

/* app.css — compose tất cả */
@import "tailwindcss";
@import "./tokens/colors.css";
@import "./tokens/spacing.css";
@import "./tokens/typography.css";

Mỗi team member có thể làm việc trên token file riêng. Design tokens được version control, review trong PR, và tự động sinh utility classes — không cần bước trung gian nào.

10. So sánh với các giải pháp CSS khác

Tiêu chí Tailwind v4 UnoCSS Panda CSS Vanilla Extract
Engine Rust (Oxide) TypeScript TypeScript TypeScript
Config CSS-first (@theme) JS/TS config JS/TS config TypeScript
Build speed Cực nhanh (Rust) Nhanh Trung bình Chậm
Type safety CSS variables Partial Full TypeScript Full TypeScript
Ecosystem Lớn nhất Nhỏ hơn Mới Niche
Vue support Native SFC scanning Tốt Trung bình Hạn chế
Container queries Built-in (@container) Plugin Manual

Kết luận

Tailwind CSS 4 với Oxide Engine không chỉ là bản nâng cấp hiệu năng — nó thay đổi cách chúng ta tư duy về CSS trong dự án hiện đại. CSS-first configuration biến design tokens thành công dân hạng nhất, Oxide Engine xóa bỏ hoàn toàn bottleneck build time, và native CSS features loại bỏ lớp abstraction không cần thiết.

Với hệ sinh thái Vue + Vite, Tailwind v4 là lựa chọn tự nhiên: setup chỉ 2 dòng config, HMR dưới 5ms, và container queries hoàn hảo cho component-based architecture. Nếu bạn đang trên v3 — migration tool hoạt động tốt, và performance gain đủ lớn để justify effort.

Bắt đầu ngay

Với dự án mới: npm create vite@latest my-app -- --template vue-ts rồi npm install tailwindcss @tailwindcss/vite. Với dự án cũ: npx @tailwindcss/upgrade và test. Tailwind v4 backward-compatible ở mức class names — phần lớn template không cần sửa.

Nguồn tham khảo: