Build System
Relevant source files- package.json
- scripts/cva-extractor.ts
- src/index.ts
- src/layouts/index.ts
- src/lib/core-classes.json
- tsconfig.json
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 insrc/ into ES2022 JavaScript modules with declaration files in dist/, along with generated artifacts that support Tailwind CSS and build tooling.
Build Commands
The build system provides several npm scripts defined inpackage.json for different aspects of the build and development workflow.
Core Build Commands
| Command | Script | Purpose |
|---|---|---|
npm run build | tsc -p tsconfig.json | Compile TypeScript to JavaScript with declaration files |
npm run type-check | tsc --noEmit | Type-check without emitting files |
npm run lint | eslint src --ext .ts,.tsx | Check code quality and style |
npm run lint:fix | eslint src --ext .ts,.tsx --fix | Auto-fix linting issues |
Tooling Commands
| Command | Script | Purpose |
|---|---|---|
npm run scan | bunx buildy-ui@latest scan | Generate component registry metadata |
npm run build:r | bunx buildy-ui@latest build | Build registry package |
npm run publish | cd packages/registry && npm version patch && npm publish | Publish registry to NPM |
Manual Build Scripts
| Script | Command | Purpose |
|---|---|---|
| CVA Extraction | bun scripts/cva-extractor.ts | Extract CSS classes from variant definitions |
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:| Setting | Value | Purpose |
|---|---|---|
target | ES2022 | Output modern JavaScript with ES2022 features |
module | ES2022 | Use ES modules (import/export syntax) |
moduleResolution | Bundler | Module resolution for modern bundlers |
rootDir | src | Source root directory |
outDir | ./dist | Compiled output directory |
declaration | true | Generate .d.ts type declaration files |
declarationDir | ./dist | Output location for declarations |
jsx | react-jsx | Use React 18+ automatic JSX transform |
strict | true | Enable all strict type-checking options |
Path Aliases
The compiler resolves path aliases during compilation:Output Structure
The compilation process generates a structured output in thedist/ 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:dist/index.js with all imports resolved to relative paths.
File Types Generated
| File Type | Extension | Purpose |
|---|---|---|
| JavaScript modules | .js | ES2022 executable code |
| Type declarations | .d.ts | TypeScript type information |
| Declaration maps | .d.ts.map | Source mapping for declarations |
Distribution Configuration
The package is configured for NPM distribution through settings inpackage.json that define how the compiled code is exposed to consumers.
Module Exports Configuration
The package defines its exports using the modernexports field:
main: Default entry point for CommonJS and older toolstypes: TypeScript declaration file locationexports["."]: Modern conditional exports for ESM and types
Package Distribution Files
Thefiles field in package.json39-43 specifies which files are included in the published package:
Package Metadata
| Field | Value | Purpose |
|---|---|---|
name | @ui8kit/core | NPM package name |
version | 0.1.8 | Semantic version number |
type | module | Declares package as ESM |
sideEffects | false | Enables tree-shaking optimization |
publishConfig.access | public | Allows public NPM access |
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
TheSimpleCVAExtractor class processes variant files:
-
File Discovery: Recursively scans src/core/variants/ for
.tsand.tsxfiles -
AST Parsing: Uses
@babel/parserto parse TypeScript syntax -
CVA Detection: Finds
cva()function calls via AST traversal -
Class Extraction: Extracts all string literals from:
- Base classes (first argument)
- Variant objects (second argument properties)
- Nested variant definitions
-
Deduplication: Uses a
Set<string>to ensure uniqueness - Output Generation: Writes JSON array of 618 classes
Generated Class Whitelist
The extractor produces src/lib/core-classes.json1-624 with this structure:- Tailwind safelist: Prevents CSS purging from removing classes generated by variants
- tw-merge configuration: Enables safe merging of variant-generated classes
Component Registry Scanner
Thebuildy-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
| Artifact | Location | Generated By | Purpose | Committed |
|---|---|---|---|---|
| Compiled modules | dist/**/*.js | tsc | Runtime code for NPM package | Yes |
| Type declarations | dist/**/*.d.ts | tsc | TypeScript type information | Yes |
| CSS class whitelist | src/lib/core-classes.json | cva-extractor.ts | Tailwind safelist configuration | Yes |
| Component registry | src/registry.json | buildy-ui scan | Per-component installation | Yes |
Why Artifacts Are Committed
Build artifacts are committed to the repository (unlike typical Node.js projects) for several reasons:- Immediate NPM readiness: The
dist/directory can be published directly without requiring consumers to run build steps - Submodule compatibility: Git submodule installations can use the compiled output without build tooling
- Whitelist stability: The
core-classes.jsonfile serves as a versioned contract for Tailwind configurations - Registry accessibility: The
registry.jsonenables per-component installation without requiring full package installation
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:Dependencies and Requirements
The build system requires specific tools and dependencies to function correctly.Build-Time Dependencies
| Dependency | Version | Purpose |
|---|---|---|
typescript | ^5.6.3 | TypeScript compiler |
@babel/parser | ^7.28.4 | AST parsing for CVA extraction |
@babel/traverse | ^7.28.4 | AST traversal for CVA extraction |
bun-types | ^1.1.29 | Type definitions for Bun runtime |
Runtime Dependencies
These are bundled with the package:| Dependency | Version | Purpose |
|---|---|---|
class-variance-authority | ^0.7.1 | CVA variant engine |
clsx | ^2.1.1 | Class name utility |
tailwind-merge | ^3.3.1 | Class name merging |
lucide-react | ^0.525.0 | Icon components |
react-resizable-panels | ^3.0.6 | Layout resizing |
Peer Dependencies
Consumer applications must provide:| Dependency | Version Range | Purpose |
|---|---|---|
react | ^18.0.0 || ^19.0.0 | React runtime |
react-dom | ^18.0.0 || ^19.0.0 | React DOM renderer |
Troubleshooting Build Issues
Common Build Problems
| Issue | Cause | Solution |
|---|---|---|
tsc not found | TypeScript not installed | Run npm install to install dev dependencies |
| Type errors during build | Strict type checking | Fix type errors or temporarily disable with npm run type-check |
Missing dist/ output | Build not run | Run npm run build explicitly |
| CVA classes not found | Whitelist not generated | Run bun scripts/cva-extractor.ts |
| Registry out of sync | Components changed | Run npm run scan to regenerate |