Skip to main content

Build System

Relevant source files

Purpose and Scope

This document covers the build system that compiles TypeScript source code into distributable JavaScript modules for NPM publication. It explains the compilation process, build scripts, TypeScript configuration, output structure, and distribution setup. For information about the package structure and module exports, see Package Structure. For details about the component registry system that powers per-component installation, see Component Registry. For TypeScript-specific configuration details including path aliases and type generation, see TypeScript Configuration.

Build Pipeline Overview

The build system transforms TypeScript source code in src/ into ES2022 JavaScript modules with declaration files in dist/, along with generated artifacts that support Tailwind CSS and build tooling.
Sources: package.json21-28 tsconfig.json1-26 scripts/cva-extractor.ts1-339

Build Commands

The build system provides several npm scripts defined in package.json for different aspects of the build and development workflow.

Core Build Commands

Tooling Commands

Manual Build Scripts

Sources: package.json21-28

TypeScript Compilation Process

The TypeScript compiler (tsc) transforms source files from src/ into compiled JavaScript modules and declaration files in dist/ using the configuration defined in tsconfig.json.

Compilation Configuration

The TypeScript compiler is configured in tsconfig.json1-26 with the following key settings:

Path Aliases

The compiler resolves path aliases during compilation:
These aliases are used in source code but resolved to relative paths in the compiled output. Sources: tsconfig.json4-24 Diagram 2 from high-level architecture

Output Structure

The compilation process generates a structured output in the dist/ directory that mirrors the source structure.

Distribution Directory Layout

Entry Point Structure

The main entry point at src/index.ts1-32 defines the public API through a series of exports:
This structure is preserved in the compiled dist/index.js with all imports resolved to relative paths.

File Types Generated

Sources: src/index.ts1-32 tsconfig.json13-19 package.json31-37

Distribution Configuration

The package is configured for NPM distribution through settings in package.json that define how the compiled code is exposed to consumers.

Module Exports Configuration

The package defines its exports using the modern exports field:
This configuration:
  • main: Default entry point for CommonJS and older tools
  • types: TypeScript declaration file location
  • exports["."]: Modern conditional exports for ESM and types

Package Distribution Files

The files field in package.json39-43 specifies which files are included in the published package:

Package Metadata

Sources: package.json1-43 package.json67-69

Build-Time Tooling

The build system includes specialized scripts that run during development to generate artifacts required by the runtime system and consumer applications.

CVA Class Extractor

The scripts/cva-extractor.ts1-339 script analyzes variant definitions to extract CSS classes used by the library.

Extractor Implementation

The SimpleCVAExtractor class processes variant files:
  1. File Discovery: Recursively scans src/core/variants/ for .ts and .tsx files
  2. AST Parsing: Uses @babel/parser to parse TypeScript syntax
  3. CVA Detection: Finds cva() function calls via AST traversal
  4. Class Extraction: Extracts all string literals from:
    • Base classes (first argument)
    • Variant objects (second argument properties)
    • Nested variant definitions
  5. Deduplication: Uses a Set<string> to ensure uniqueness
  6. Output Generation: Writes JSON array of 618 classes

Generated Class Whitelist

The extractor produces src/lib/core-classes.json1-624 with this structure:
This whitelist serves two purposes:
  1. Tailwind safelist: Prevents CSS purging from removing classes generated by variants
  2. tw-merge configuration: Enables safe merging of variant-generated classes

Component Registry Scanner

The buildy-ui scan command generates src/registry.json by analyzing component files for metadata used by the per-component installation system. See Component Registry for details. Sources: scripts/cva-extractor.ts11-279 src/lib/core-classes.json1-624 Diagram 2 and Diagram 5 from high-level architecture

Build Artifacts

The build process generates several committed artifacts that bridge build-time and runtime systems.

Artifact Summary

Artifact Details

Why Artifacts Are Committed

Build artifacts are committed to the repository (unlike typical Node.js projects) for several reasons:
  1. Immediate NPM readiness: The dist/ directory can be published directly without requiring consumers to run build steps
  2. Submodule compatibility: Git submodule installations can use the compiled output without build tooling
  3. Whitelist stability: The core-classes.json file serves as a versioned contract for Tailwind configurations
  4. Registry accessibility: The registry.json enables per-component installation without requiring full package installation
Sources: package.json39-43 src/lib/core-classes.json1-624 Diagram 5 from high-level architecture

Build Workflow Example

A typical development workflow involves running build commands in sequence:

Full Build Sequence

Development Build

For iterative development, run a minimal build:

Continuous Integration

A CI/CD pipeline typically runs:
Sources: package.json21-28

Dependencies and Requirements

The build system requires specific tools and dependencies to function correctly.

Build-Time Dependencies

Runtime Dependencies

These are bundled with the package:

Peer Dependencies

Consumer applications must provide: Sources: package.json48-66

Troubleshooting Build Issues

Common Build Problems

Verification Steps

To verify a successful build:
Sources: package.json21-28 tsconfig.json1-26