Designing Turborepo UI kit with core logic and tree-shakeable React components
The design uses a Turborepo monorepo with two packages: a core logic package exporting breakpoints and utilities, and a UI package exporting React components styled via per-component CSS modules. The UI package is configured with "sideEffects": false in package.json to enable tree-shaking, while exporting each component's JS and corresponding CSS file via explicit "exports" mappings. Rollup is used in the UI package to build separate JS and CSS module files for each component with the @rollup/plugin-postcss plugin (configured for CSS modules and extraction per entry). The core package is built with tsup and provides responsive breakpoints and hooks; UI components consume core tokens but style with independent CSS modules containing media queries. The root defines workspaces and a turbo.json pipeline for builds. This setup enables importing components and their CSS individually and supports tree-shaking of unused components and their styles.
Below is a minimal Turborepo layout and build setup that splits the UI kit into two packages:
- Package A (
@acme/core): design tokens, responsive helpers, shared types/utilities.
- Package B (
@acme/ui): React components that consume @acme/core, each with its own CSS module. The package is tree‑shakeable (sideEffects: false) and exports individual CSS assets per component.
Repository structure
.
├── package.json
├── turbo.json
└── packages
├── core
│ ├── package.json
│ ├── tsconfig.json
│ └── src
│ ├── breakpoints.ts
│ ├── tokens.ts
│ └── index.ts
└── ui
├── package.json
├── tsconfig.json
├── rollup.config.js
└── src
├── index.ts
├── button
│ ├── index.tsx
│ └── Button.module.css
└── card
├── index.tsx
└── Card.module.css
Root configuration
package.json
{
"name": "acme-ui-kit",
"private": true,
"workspaces": ["packages/*"],
"scripts": {
"build": "turbo run build"
},
"devDependencies": {
"turbo": "^1.10.0"
}
}
turbo.json
{
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"lint": {},
"test": {}
}
}
Package A: @acme/core
packages/core/package.json
{
"name": "@acme/core",
"version": "0.0.1",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --dts --format esm --clean"
},
"devDependencies": {
"tsup": "^7.2.0",
"typescript": "^5.3.0"
}
}
packages/core/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"]
}
packages/core/src/breakpoints.ts
export const breakpoints = {
sm: 640,
md: 768,
lg: 1024,
xl: 1280
} as const;
export type BreakpointKey = keyof typeof breakpoints;
export const mediaQuery = (key: BreakpointKey) => `@media (min-width: ${breakpoints[key]}px)`;
packages/core/src/tokens.ts
export const spacing = {
xs: 4,
sm: 8,
md: 16,
lg: 24
} as const;
export const radii = {
sm: 4,
md: 8
} as const;
export const palette = {
primary: "#3b82f6",
primaryText: "#ffffff",
surface: "#ffffff",
surfaceBorder: "#e5e7eb",
surfaceText: "#111827"
} as const;
packages/core/src/index.ts
export * from "./breakpoints";
export * from "./tokens";
Package B: @acme/ui
Uses Rollup to emit ESM JS and per‑component CSS modules. Marked as tree‑shakeable with sideEffects: false. Exports point to individual JS and CSS files.
packages/ui/package.json
{
"name": "@acme/ui",
"version": "0.0.1",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./button": {
"import": "./dist/button.js",
"types": "./dist/button.d.ts"
},
"./button.css": "./dist/button.css",
"./card": {
"import": "./dist/card.js",
"types": "./dist/card.d.ts"
},
"./card.css": "./dist/card.css"
},
"sideEffects": false,
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"dependencies": {
"@acme/core": "0.0.1"
},
"scripts": {
"build": "rollup -c"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-typescript": "^11.1.0",
"rollup": "^4.2.0",
"rollup-plugin-postcss": "^4.0.2",
"typescript": "^5.3.0"
}
}
packages/ui/rollup.config.js
import path from "path";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import postcss from "rollup-plugin-postcss";
const components = ["button", "card"];
export default {
input: {
index: "src/index.ts",
...Object.fromEntries(components.map((c) => [c, `src/${c}/index.tsx`]))
},
output: {
dir: "dist",
format: "esm",
sourcemap: true,
entryFileNames: "[name].js",
chunkFileNames: "[name]-[hash].js",
assetFileNames: "[name][extname]"
},
external: ["react", "react/jsx-runtime", "@acme/core"],
plugins: [
resolve({ extensions: [".js", ".ts", ".tsx"] }),
commonjs(),
typescript({ tsconfig: "./tsconfig.json" }),
postcss({
modules: true,
extract: (asset) => {
// Emit one CSS file per entry chunk (button.css, card.css)
const name = path.basename(asset.name, path.extname(asset.name));
return `${name}.css`;
},
minimize: true,
sourceMap: true
})
]
};
packages/ui/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"jsx": "react-jsx",
"outDir": "dist"
},
"include": ["src"]
}
packages/ui/src/index.ts
export * from "./button";
export * from "./card";
packages/ui/src/button/index.tsx
import React from "react";
import { palette, radii, spacing } from "@acme/core";
import styles from "./Button.module.css";
export type ButtonVariant = "primary" | "ghost";
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: ButtonVariant;
fullWidth?: boolean;
}
export const Button: React.FC<ButtonProps> = ({
variant = "primary",
fullWidth = false,
className,
children,
...rest
}) => {
const classes = [
styles.button,
variant === "primary" ? styles.primary : styles.ghost,
fullWidth ? styles.fullWidth : "",
className
]
.filter(Boolean)
.join(" ");
return (
<button className={classes} {...rest}>
{children}
</button>
);
};
packages/ui/src/button/Button.module.css
.button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 0.5rem 1rem;
border-radius: 8px;
border: 1px solid transparent;
font-weight: 600;
cursor: pointer;
transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
/* Responsive adjustments */
@media (min-width: 768px) {
.button {
padding: 0.6rem 1.2rem;
}
}
@media (min-width: 1024px) {
.button {
padding: 0.7rem 1.4rem;
}
}
.primary {
background: #3b82f6;
color: #ffffff;
border-color: #3b82f6;
}
.primary:hover {
background: #2563eb;
border-color: #2563eb;
}
.ghost {
background: transparent;
color: #111827;
border-color: #e5e7eb;
}
.ghost:hover {
border-color: #9ca3af;
color: #0f172a;
}
.fullWidth {
width: 100%;
}
packages/ui/src/card/index.tsx
import React from "react";
import styles from "./Card.module.css";
export interface CardProps extends React.HTMLAttributes<HTMLDivElement> {
padding?: "sm" | "md" | "lg";
}
export const Card: React.FC<CardProps> = ({
padding = "md",
className,
children,
...rest
}) => {
const classes = [styles.card, styles[padding], className]
.filter(Boolean)
.join(" ");
return (
<div className={classes} {...rest}>
{children}
</div>
);
};
packages/ui/src/card/Card.module.css
.card {
border: 1px solid #e5e7eb;
border-radius: 8px;
background: #ffffff;
color: #111827;
box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.sm { padding: 12px; }
.md { padding: 16px; }
.lg { padding: 24px; }
/* Responsive adjustments */
@media (min-width: 640px) {
.sm { padding: 16px; }
.md { padding: 20px; }
.lg { padding: 28px; }
}
@media (min-width: 1024px) {
.sm { padding: 20px; }
.md { padding: 24px; }
.lg { padding: 32px; }
}
Notes on tree‑shaking and CSS exports
@acme/ui/package.json sets "sideEffects": false so bundlers can drop unused components. Because CSS is imported from each component module, unused components and their CSS are removed together.
- Each component is an explicit Rollup entry. Rollup generates
button.js / button.css, card.js / card.css, etc. The exports map exposes both JS and CSS per component to consumers who prefer manual style inclusion (e.g., import '@acme/ui/button.css').
- Keep peer dependencies (
react, react-dom) external in Rollup to avoid bundling them.
Root tsconfig (optional)
At the root, you can define a base tsconfig.json:
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"isolatedModules": true
}
}
Usage
After running npm run build at the root:
import React from "react";
import { Button } from "@acme/ui";
// Optionally import component-specific CSS if your setup doesn't support CSS modules auto-injection:
import "@acme/ui/button.css";
function App() {
return <Button variant="primary" fullWidth>Click me</Button>;
}
This setup yields a responsive, tree‑shakeable UI kit with per‑component CSS assets in a Turborepo monorepo.