import { readFileSync } from 'node:fs' import { fileURLToPath, URL } from 'node:url' import react from '@vitejs/plugin-react' import { defineConfig } from 'vite' /** * Library build: `npm run build:lib` → dist-lib/. * * One ES module per source file (preserveModules) so apps only bundle the * components they import. Dependencies stay external. Types come from * `tsc -p tsconfig.lib.json`, and tokens.css is copied alongside. */ const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')) as { version: string dependencies: Record peerDependencies?: Record } const external = [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies ?? {})] export default defineConfig({ define: { __TANSY_VERSION__: JSON.stringify(pkg.version), }, plugins: [react()], build: { outDir: 'dist-lib', emptyOutDir: true, sourcemap: true, copyPublicDir: false, lib: { entry: fileURLToPath(new URL('./src/index.ts', import.meta.url)), formats: ['es'], }, rollupOptions: { external: (id) => external.some((dep) => id === dep || id.startsWith(`${dep}/`)) || id.startsWith('react/'), output: { preserveModules: true, preserveModulesRoot: 'src', entryFileNames: '[name].js', }, }, }, })