@unifycss/core
v1.1.0
Published
Core compiler engine, scanner, token generator & plugin system for UnifyCSS
Maintainers
Readme
@unifycss/core
Core compiler engine, content scanner, design token processor, and plugin system for UnifyCSS.
This is the foundation that powers all UnifyCSS integrations — Vite, Next.js, PostCSS, Webpack, and CLI all build on top of @unifycss/core.
Table of Contents
- When to Use @unifycss/core
- Installation
- Configuration File
- Programmatic API
- Plugin System
- Design Token System
- Semantic Component API
- License
When to Use @unifycss/core
Use @unifycss/core directly when:
- Building a custom integration with a bundler or framework not officially supported.
- Writing scripts or CI tools that need to compile, scan, or validate UnifyCSS output.
- Creating a custom UnifyCSS plugin.
- Running UnifyCSS in a Node.js environment (e.g., email template generators, static site tools).
For standard Next.js, Vite, Webpack, or PostCSS setups, use the dedicated integration packages instead.
Installation
npm install @unifycss/core
# or
pnpm add @unifycss/core
# or
yarn add @unifycss/coreConfiguration File
Create unifycss.config.js in your project root:
// unifycss.config.js
/** @type {import('@unifycss/core').Config} */
export default {
// Files to scan for class names
content: [
'./src/**/*.{html,js,ts,jsx,tsx,vue,svelte}',
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
// Theme token overrides
theme: {
extend: {
colors: {
brand: {
primary: '#7c3aed',
dark: '#5b21b6',
light: '#a78bfa',
},
gold: '#e8c9a0',
},
fontFamily: {
sans: '"Outfit", system-ui, sans-serif',
mono: '"JetBrains Mono", monospace',
},
borderRadius: {
DEFAULT: '0.5rem',
lg: '0.75rem',
},
spacing: {
'128': '32rem',
'144': '36rem',
},
},
},
// Custom plugins
plugins: [],
// Enable CSS purging in production
purge: process.env.NODE_ENV === 'production',
};Programmatic API
loadConfig
Loads and resolves a UnifyCSS configuration file:
import { loadConfig } from '@unifycss/core';
const config = await loadConfig('./unifycss.config.js');
console.log(config.content);
// → ['./src/**/*.{html,js,ts,jsx,tsx}', ...]Signature:
function loadConfig(configPath?: string): Promise<ResolvedConfig>compile
Compiles UnifyCSS output CSS from a configuration and detected class names:
import { compile, loadConfig } from '@unifycss/core';
const config = await loadConfig('./unifycss.config.js');
const css = await compile({
content: ['./src/**/*.tsx'],
config,
minify: process.env.NODE_ENV === 'production',
});
// Write to file
import { writeFileSync } from 'fs';
writeFileSync('./dist/unifycss.css', css);Signature:
function compile(options: CompileOptions): Promise<string>
interface CompileOptions {
content: string[]; // Glob patterns of files to scan
config?: ResolvedConfig; // Loaded config (or path string)
minify?: boolean; // Minify output CSS
verbose?: boolean; // Log class detection info
}Scanner
Scans source files and extracts unique CSS class names used:
import { Scanner } from '@unifycss/core';
const scanner = new Scanner({
content: ['./src/**/*.{html,tsx}'],
});
// Scan files via glob
const classes = await scanner.scanFiles();
console.log([...classes]);
// → Set { 'btn-primary', 'card', 'flex', 'gap-4', 'text-gold', ... }
// Scan a raw HTML/JSX string directly
const inlineClasses = scanner.scanString(
'<button class="btn-gold btn-gold-sm">Go</button>'
);
console.log([...inlineClasses]);
// → Set { 'btn-gold', 'btn-gold-sm' }Generator
Generates CSS output for a specific set of detected class names:
import { Generator } from '@unifycss/core';
const generator = new Generator({
classes: new Set(['btn-primary', 'card', 'text-gold', 'flex', 'gap-4']),
minify: false,
});
const css = await generator.generate();
console.log(css);
// → .btn-primary { ... } .card { ... } ...Tokens
Access and transform the design token system:
import { tokens, resolveToken } from '@unifycss/core/tokens';
// Get all default design tokens
console.log(tokens.colors.gold);
// → '#e8c9a0'
console.log(tokens.spacing[4]);
// → '1rem'
// Resolve a token with theme overrides applied
const resolved = resolveToken('colors.brand.primary', {
colors: { brand: { primary: '#7c3aed' } },
});
console.log(resolved);
// → '#7c3aed'Plugin System
Create custom UnifyCSS plugins to extend the class system or add new token utilities:
import type { Plugin } from '@unifycss/core';
// Plugin that adds a custom .gradient-brand utility
const gradientPlugin: Plugin = {
name: 'gradient-brand',
handler({ addUtilities }) {
addUtilities({
'.gradient-brand': {
background: 'linear-gradient(135deg, var(--brand-primary), var(--gold))',
},
'.gradient-brand-vertical': {
background: 'linear-gradient(180deg, var(--brand-primary), var(--gold))',
},
});
},
};
// Use in unifycss.config.js
export default {
content: ['./src/**/*.tsx'],
plugins: [gradientPlugin],
};Design Token System
@unifycss/core exports the complete UnifyCSS design token catalog:
import { tokens } from '@unifycss/core/tokens';
// Color tokens
tokens.colors.brand.primary // '#3b82f6'
tokens.colors.gold // '#e8c9a0'
tokens.colors.success // '#10b981'
tokens.colors.danger // '#ef4444'
tokens.colors.dark.base // '#07070a'
// Spacing tokens (matches 4px grid system)
tokens.spacing[0] // '0px'
tokens.spacing[1] // '0.25rem'
tokens.spacing[4] // '1rem'
tokens.spacing[8] // '2rem'
tokens.spacing[16] // '4rem'
// Border radius tokens
tokens.borderRadius.sm // '0.25rem'
tokens.borderRadius.md // '0.5rem'
tokens.borderRadius.lg // '0.75rem'
tokens.borderRadius.full // '9999px'
// Typography tokens
tokens.fontSize.sm // '0.875rem'
tokens.fontSize.base // '1rem'
tokens.fontSize['2xl'] // '1.5rem'
tokens.fontSize['6xl'] // '3.75rem'
// Shadow tokens
tokens.boxShadow.sm // '0 1px 3px rgba(0,0,0,0.12)'
tokens.boxShadow.lg // '0 8px 32px rgba(0,0,0,0.35)'
tokens.boxShadow.gold // '0 0 24px rgba(232,201,160,0.3)'Semantic Component API
import { components } from '@unifycss/core/semantic';
// List all registered semantic component names
console.log(Object.keys(components));
// → ['btn', 'btn-primary', 'btn-gold', 'card', 'badge', 'alert', 'form-input', ...]
// Get CSS for a specific component
console.log(components['btn-primary']);
// → { display: 'inline-flex', padding: '14px 32px', background: '...', ... }License
MIT © UnifyCSS Team
