Skip to content

JIT Mode

BaroCSS implements a revolutionary Just-In-Time (JIT) CSS generation system that creates styles only when they're needed, providing optimal performance and minimal bundle sizes. Built from the ground up to support the complete utility-first syntax in real-time.

What is JIT Mode?

Just-In-Time (JIT) compilation is a technique where code is compiled at runtime, just before it's needed. In BaroCSS, this means:

  • Generate CSS on-demand - Only create styles for classes you actually use
  • Zero unused CSS - No bloated stylesheets with unused utilities
  • Real-time compilation - Generate styles instantly as you develop
  • Optimal performance - Minimal memory usage and fast loading times

Traditional CSS vs JIT Mode

Traditional CSS Approach

css
/* Traditional: Generate all possible utilities */
.bg-red-500 { background-color: rgb(239 68 68); }
.bg-red-600 { background-color: rgb(220 38 38); }
.bg-red-700 { background-color: rgb(185 28 28); }
.bg-blue-500 { background-color: rgb(59 130 246); }
.bg-blue-600 { background-color: rgb(37 99 235); }
.bg-blue-700 { background-color: rgb(29 78 216); }
.bg-green-500 { background-color: rgb(34 197 94); }
.bg-green-600 { background-color: rgb(22 163 74); }
.bg-green-700 { background-color: rgb(21 128 61); }
/* ... thousands more utilities */

Problems:

  • Large bundle sizes - Includes all possible utilities
  • Unused CSS - Most utilities are never used
  • Slow loading - Large CSS files take time to download
  • Memory waste - Unused styles consume memory

BaroCSS JIT Mode

typescript
import { BrowserRuntime } from 'barocss/runtime/browser';

const runtime = new BrowserRuntime();

// Only generates CSS for classes you actually use
document.body.innerHTML = `
  <div class="bg-blue-500 text-white p-4">
    <h1 class="text-2xl font-bold">Hello JIT!</h1>
  </div>
`;

// Generated CSS (only what's needed):
// .bg-blue-500 { background-color: rgb(59 130 246); }
// .text-white { color: rgb(255 255 255); }
// .p-4 { padding: 1rem; }
// .text-2xl { font-size: 1.5rem; }
// .font-bold { font-weight: 700; }

Benefits:

  • Minimal bundle size - Only includes used utilities
  • Zero unused CSS - No waste
  • Fast loading - Small CSS files
  • Memory efficient - Only loads what's needed

How JIT Mode Works

1. Class Detection

BaroCSS automatically detects which classes are being used:

typescript
// BaroCSS monitors DOM changes
const element = document.createElement('div');
element.className = 'bg-red-500 text-white p-4 rounded-lg';

// JIT system detects: ['bg-red-500', 'text-white', 'p-4', 'rounded-lg']
// Only these 4 classes will generate CSS

2. On-Demand Generation

CSS is generated only when a class is first encountered:

typescript
// First time using 'bg-blue-500'
element1.className = 'bg-blue-500'; // Generates CSS

// Second time using 'bg-blue-500'
element2.className = 'bg-blue-500'; // Uses cached CSS

// First time using 'bg-green-500'
element3.className = 'bg-green-500'; // Generates new CSS

3. Smart Caching

Generated CSS is cached to avoid regeneration:

typescript
class JITCache {
  private cssCache = new Map<string, string>();
  
  generateCSS(className: string) {
    // Check cache first
    if (this.cssCache.has(className)) {
      return this.cssCache.get(className); // Cache hit!
    }
    
    // Generate new CSS
    const css = this.compileClass(className);
    this.cssCache.set(className, css);
    return css;
  }
}

JIT Mode Benefits

1. Minimal Bundle Size

typescript
// Traditional CSS: ~3MB (all utilities)
// BaroCSS JIT: ~50KB (only used utilities)

const stats = runtime.getStats();
console.log(stats);
// {
//   totalClasses: 25,
//   generatedCSS: '2.1KB',
//   bundleSize: '2.1KB',  // vs 3MB traditional
//   unusedCSS: '0KB'      // vs 2.95MB traditional
// }

2. Faster Loading

typescript
// Performance comparison
const traditionalCSS = '3MB'; // Download time: ~2.5s
const jitCSS = '50KB';        // Download time: ~0.1s

// 25x faster loading!

3. Memory Efficiency

typescript
// Memory usage comparison
const traditionalMemory = '15MB'; // All utilities in memory
const jitMemory = '500KB';        // Only used utilities

// 30x less memory usage!

4. Development Speed

typescript
// Traditional: Rebuild entire CSS
npm run build; // 30+ seconds

// JIT: Instant CSS generation
element.className = 'new-class'; // 0ms

Advanced JIT Features

1. Dynamic Class Generation

JIT mode handles dynamic class names:

typescript
// Generate classes dynamically
function createButton(variant: string, size: string) {
  const className = `btn-${variant} btn-${size}`;
  const element = document.createElement('button');
  element.className = className;
  
  // JIT generates CSS for 'btn-primary btn-large' on first use
  return element;
}

const button = createButton('primary', 'large');

2. Arbitrary Value Support

JIT mode supports arbitrary values:

typescript
// Arbitrary values are generated on-demand
element.className = 'bg-[#ff6b6b] text-[18px] p-[2rem]';

// Generated CSS:
// .bg-\[\#ff6b6b\] { background-color: #ff6b6b; }
// .text-\[18px\] { font-size: 18px; }
// .p-\[2rem\] { padding: 2rem; }

3. Variant Generation

Complex variants are generated only when needed:

typescript
// Complex variants
element.className = 'sm:dark:hover:bg-red-500/50';

// Generated CSS:
// @media (min-width: 640px) {
//   @media (prefers-color-scheme: dark) {
//     .sm\:dark\:hover\:bg-red-500\/50:hover {
//       background-color: rgb(239 68 68 / 0.5);
//     }
//   }
// }

4. Plugin Integration

JIT mode works seamlessly with plugins:

typescript
const customPlugin = (ctx) => {
  ctx.extendTheme('colors', {
    'brand-primary': '#3b82f6',
    'brand-secondary': '#64748b'
  });
};

const runtime = new BrowserRuntime({
  config: {
    plugins: [customPlugin]
  }
});

// JIT generates CSS for custom colors
element.className = 'bg-brand-primary text-brand-secondary';

Performance Optimization

1. Lazy Loading

CSS is generated only when elements are visible:

typescript
// Intersection Observer integration
const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      // Generate CSS when element becomes visible
      runtime.processElement(entry.target);
    }
  });
});

observer.observe(element);

2. Batch Processing

Multiple classes are processed together:

typescript
// Process multiple classes in one batch
const classes = ['bg-red-500', 'text-white', 'p-4', 'rounded-lg'];
const css = runtime.generateCss(classes.join(' '));

// More efficient than processing each class individually

3. Memory Management

Unused CSS is automatically cleaned up:

typescript
class JITManager {
  private usedClasses = new Set<string>();
  private cssCache = new Map<string, string>();
  
  cleanup() {
    // Remove CSS for classes no longer in use
    for (const [className, css] of this.cssCache) {
      if (!this.usedClasses.has(className)) {
        this.cssCache.delete(className);
        this.removeCSS(css);
      }
    }
  }
}

Use Cases

1. Component Libraries

Perfect for dynamic component systems:

typescript
class ComponentLibrary {
  createCard(variant: string, size: string) {
    const className = `card-${variant} card-${size}`;
    const element = document.createElement('div');
    element.className = className;
    
    // JIT generates CSS for new combinations
    return element;
  }
}

const library = new ComponentLibrary();
const card = library.createCard('primary', 'large'); // CSS generated on-demand

2. Dynamic Theming

Handle theme changes efficiently:

typescript
function switchTheme(theme: string) {
  document.documentElement.className = theme;
  
  // JIT generates CSS for theme-specific classes
  const elements = document.querySelectorAll('[class*="theme-"]');
  elements.forEach(element => {
    const newClasses = element.className.replace(/theme-\w+/g, `theme-${theme}`);
    element.className = newClasses;
  });
}

3. User-Generated Content

Handle user-created styles:

typescript
// User creates custom styles
function createUserStyle(backgroundColor: string, textColor: string) {
  const className = `user-bg-${backgroundColor} user-text-${textColor}`;
  const element = document.createElement('div');
  element.className = className;
  
  // JIT generates CSS for user's custom classes
  return element;
}

Best Practices

1. Use Semantic Class Names

typescript
// Good: Semantic and reusable
const buttonClasses = 'btn btn-primary btn-large';

// Avoid: Too specific
const specificClasses = 'bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600';

2. Batch Class Operations

typescript
// Good: Batch multiple classes
const classes = ['bg-red-500', 'text-white', 'p-4'];
runtime.generateCss(classes.join(' '));

// Avoid: Process classes individually
classes.forEach(cls => runtime.generateCss(cls));

3. Monitor Performance

typescript
// Track JIT performance
const stats = runtime.getStats();
if (stats.generatedCSS > '100KB') {
  console.warn('Large CSS generated:', stats);
}

4. Optimize for Your Use Case

typescript
// For applications with many dynamic classes
const runtime = new BrowserRuntime({
  config: {
    cache: {
      maxSize: 1000,        // Larger cache
      ttl: 300000          // 5 minute TTL
    }
  }
});

// For applications with mostly static classes
const runtime = new BrowserRuntime({
  config: {
    cache: {
      maxSize: 100,         // Smaller cache
      ttl: 60000           // 1 minute TTL
    }
  }
});

Performance Comparison

MetricTraditional CSSBaroCSS JITImprovement
Bundle Size3MB50KB60x smaller
Load Time2.5s0.1s25x faster
Memory Usage15MB500KB30x less
Unused CSS2.95MB0KB100% elimination
Build Time30s+0msInstant
Development SpeedSlowInstantImmediate feedback

Conclusion

JIT mode is the cornerstone of BaroCSS's performance and efficiency. By generating CSS only when needed, it provides:

  • Minimal bundle sizes - Only includes used utilities
  • Fast loading times - Small CSS files
  • Memory efficiency - No unused styles
  • Development speed - Instant feedback
  • Scalability - Handles any number of classes

JIT mode makes BaroCSS perfect for modern web applications that need optimal performance and developer experience.

Ready to experience JIT mode? Check out the Quick Start Guide to get started with BaroCSS.

Released under the MIT License.