Replies: 1 comment 4 replies
-
Your journey into building a component library is completely understandable—these challenges are common when dealing with bundlers, tree-shaking, and monorepos. Let’s break things down systematically. 1. Is Vite (library mode) the right choice? If your goal is: Then Vite is fine. But if you need: Then Turborepo with ESBuild or SWC might be a better alternative. 2. What Went Wrong with Your Setup? Improper ESM/CJS configuration – Tree-shaking may not work if package.json isn’t correctly configured. 📂 Project Structure Example (Monorepo-based)
Key Fixes: {
"name": "@newbie-ui/button",
"version": "0.1.0",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"sideEffects": false
} Then, update tsconfig.json to ensure correct TypeScript resolution: {
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
"declaration": true,
"outDir": "dist"
}
} And finally, in vite.config.ts, ensure library mode is properly set up: import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
export default defineConfig({
build: {
lib: {
entry: "src/index.ts",
formats: ["es", "cjs"],
fileName: (format) => `index.${format}.js`,
},
rollupOptions: {
external: ["react", "react-dom"],
},
},
plugins: [dts()],
}); 5. Would a Monorepo Like ShadCN’s Help? A monorepo with Turborepo + pnpm workspaces is the way to go. Alternative Option: If monorepos feel too complex, consider barrel files instead: // src/components/index.ts
export { Button } from "./button";
export { Card } from "./card"; Then, import like: import { Button } from "newbie-ui/components"; 6. Next Steps Hope it is usefull! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I’ve never built a component library before (though I’ve worked with some). I’m struggling with how to approach the beginning of the project. So, if you don’t mind, I’ll give a brief explanation of:
(Feel free to jump between topics! Or go to the questions at the end)
What I Need
I need to build a React component library that includes:
What I Want
I want to create a package similar to ShadCN/UI mixed with HeroUI (NextUI), meaning:
What I Did
I had bad experiences with bundlers (especially Rollup), so after researching, Vite in library mode seemed like the perfect solution.
My Initial Setup:
Created a project and configured:
vite-plugin-dts
,vite-plugin-configure-pathnames
, etc.)Importing Components Initially:
This imports the entire package, even if I only need a single button. That doesn’t seem efficient (probably a code-splitting issue).
Reorganizing the Library (Accidentally Mimicked a Monorepo Pattern):
packages
folderpackages
, added acomponents
folderButton
to its own folder:Then Everything Broke:
tsconfig
configurationsindex.ts
files everywhere, but it didn’t workdist
folder wouldn't break down into smaller piecesWhat I Got
Nothing but stress.
From my research, I realized a few things:
The Big Question: I Need Guidance!
I need help from package/bundler experts. My main concerns:
I think I’ve fallen into a rabbit hole—very deep indeed.
Any guidance is highly appreciated! 🚀
Beta Was this translation helpful? Give feedback.
All reactions