A modern React component library built with TypeScript, esbuild, and Material-UI (MUI). A Dev mode is also available where you can see the changes and test components in the playground, is built with vite. This library provides reusable UI components, hooks, exports MUI components and theming solutions that can be consumed by multiple applications.
- Using esbuild as the bundler for fast, efficient builds
- Supporting subpath exports for granular imports
- Providing independent component bundling
- Components: Reusable React components built on top of MUI
- Hooks: Custom React hooks for common functionality
- Theme: Centralized theming system with MUI integration and a custom theme provider
- MUI Re-exports: Direct access to MUI components without duplicate dependencies
- Dev mode: A playground where you can see the changes and test components
esbuild over other bundlers because:
- 10-100x faster build times
- Simpler configuration
- Better tree-shaking
- Native ES modules support
- More predictable output structure
- Better support for library builds
- No virtual modules issues (Vite creates
_virtualsfolders)
- Significantly faster builds
- Built-in TypeScript support
- Less configuration complexity
src/
βββ index.ts # Main entry point
βββ lib/ # Reusable components
β βββ AppBar/
β βββ Checkbox/
β βββ IconButton/
β βββ Logo/
βββ hooks/ # Custom React hooks
β βββ useGetBoundingClientRect.tsx
β βββ useSetTabTitle.tsx
βββ theme/ # Theming system
β βββ ThemeProvider.tsx
β βββ theme.ts
β βββ types.ts
βββ mui/ # MUI re-exports
βββ index.ts
npm install @your-org/ui-library
# or
pnpm add @your-org/ui-library
# or
yarn add @your-org/ui-library// Import everything from main entry
import { AppBar, Checkbox, ThemeProvider, MUI } from '@your-org/ui-library';
// Or use subpath imports for better tree-shaking
import AppBar from '@your-org/ui-library/lib/AppBar';
import { AppBar } from '@your-org/ui-library/lib';
import { useSetTabTitle } from '@your-org/ui-library/hooks';
import { ThemeProvider } from '@your-org/ui-library/theme';
import { Button } from '@your-org/ui-library'; // Re-exported MUI componentsThe library supports granular imports through subpath exports:
// Components
import { AppBar } from '@your-org/ui-library/lib/AppBar';
import { Checkbox } from '@your-org/ui-library/lib/Checkbox';
// Hooks
import { useSetTabTitle } from '@your-org/ui-library/hooks';
// Theme
import { ThemeProvider } from '@your-org/ui-library/theme';
// MUI components (re-exported to avoid duplication)
import { MUIBox } from '@your-org/ui-library/mui';
// From multiple entry points
import { MUIBox, Checkbox, useSetTabTitle } from '@your-org/ui-library';@your-org/ui-library- Main entry point@your-org/ui-library/lib- All components@your-org/ui-library/lib/*- Individual components@your-org/ui-library/hooks- All hooks@your-org/ui-library/hooks/*- Individual hooks@your-org/ui-library/theme- Theme system@your-org/ui-library/theme/*- Individual theme files@your-org/ui-library/mui- MUI components
- Run
pnpm run devto start the local server athttp://localhost:5173. - The Playground is under the
devfolder.
The recommended way to test the library locally with consuming applications:
- In the library directory:
# Build the library
pnpm run build
# Link the package globally
pnpm link --global- In your consuming application (optional):
# Link to the local library
pnpm link --global @your-org/ui-library- Important: Handle React Hook Issues
When testing locally, you might encounter React hooks issues. Solve this by linking React from the consuming app to the library:
# In the consuming app, link React
cd node_modules/react
pnpm link --global
# In the library directory, use the linked React
cd /path/to/ui-library
pnpm link --global react- In module project, execute
npm pack - This will build a
<package-name>-<version>.tar.gzfile. - Move the file to the consumer project
- In consuming app's package.json, add the file protocol:
# In consuming app's package.json
{
"dependencies": {
"@your-org/ui-library": "file:../path/to/ui-library"
}
}- Make changes to the library
- Run
pnpm run buildto rebuild - Changes will be immediately available in linked applications
- For automatic rebuilding:
pnpm run build:watch
The library re-exports Material-UI components to:
- Avoid peer dependency conflicts in consuming applications
- Ensure version consistency across all apps using the library
- Reduce bundle size by preventing duplicate MUI imports
- Simplify dependency management for consuming applications
// Instead of importing MUI directly in your app:
// import { Button, TextField } from '@mui/material'; // β Don't do this
// Use the re-exported MUI from the library:
import { MUIButton, MUITextField } from '@your-org/ui-library'; // β
Preferred approach
function MyComponent() {
return (
<div>
<MUIButton variant="contained">Click me</MUIButton>
<MUITextField label="Enter text" />
</div>
);
}# Clean and build everything
pnpm run build
# Build with watch mode for development
pnpm run build:watch
# Build only TypeScript declarations
pnpm run build:types
# Clean dist folder
pnpm run cleandist/
βββ index.js # Main entry
βββ index.d.ts # Main types
βββ lib/
β βββ index.js # All components
β βββ index.d.ts
β βββ AppBar/
β β βββ index.js # Individual component
β β βββ index.d.ts
β β βββ AppBar.d.ts
β β βββ AppBar.js
β βββ ...
βββ hooks/
β βββ index.js # All hooks
β βββ *.js # Individual hooks
βββ theme/
βββ index.js # Theme system
βββ *.js # Individual theme files
The library uses modern TypeScript configuration:
{
"compilerOptions": {
"moduleResolution": "bundler",
"module": "ES2022",
"target": "ES2020",
"jsx": "react-jsx"
}
}Using "moduleResolution": "bundler" enables:
- Better tree-shaking
- Proper subpath exports support
- Modern import/export handling
- Add Jest for testing
- Find a way to create documentation
- Time to Say Goodbye to Webpack
- JS Toolbox 2024 Part 3: Bundlers and Test Frameworks
- Why Webpack Slowly Losing Leadership to Vite and There is No Way to Compete in 2024
- esbuild Getting Started
- Vite
- Webpack
- Turborepo
- Can Rsbuild be used to build libraries or UI components?
- Buildable and Publishable Libraries - Nx
- Subpath Exports
- How to support subpath imports using React, Rollup, and TypeScript
- Importing from subfolders for a JavaScript package
- How to create a single source npm module
- Setting up subpath import aliases in a TypeScript project
- The native way to configure path aliases in frontend projects
- Browse MUI Material
- TypeScript module resolution bundler value
- TypeScript and subpath imports