-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.lib.js
More file actions
65 lines (61 loc) · 1.93 KB
/
vite.config.lib.js
File metadata and controls
65 lines (61 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import tsconfigPaths from 'vite-tsconfig-paths';
const dirname = path.dirname(fileURLToPath(import.meta.url));
const componentsDir = path.resolve(dirname, 'src/components');
// Auto-discover components with primitives.ts files
const primitiveComponents = fs
.readdirSync(componentsDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.filter((dirent) => fs.existsSync(path.join(componentsDir, dirent.name, 'primitives.ts')))
.map((dirent) => dirent.name);
// Build entry points object
const entry = {
index: path.resolve(dirname, 'src/index.ts'),
...Object.fromEntries(
primitiveComponents.map((component) => [
component,
path.resolve(dirname, `src/components/${component}/primitives.ts`),
]),
),
};
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths()],
css: {
postcss: './postcss.config.js',
},
build: {
lib: {
entry,
formats: ['es', 'cjs'],
fileName: (format, entryName) => {
const ext = format === 'es' ? 'js' : 'cjs';
return `${entryName}.${ext}`;
},
},
cssCodeSplit: false,
rollupOptions: {
// Make sure to externalize deps that shouldn't be bundled
external: ['react', 'react-dom', 'react/jsx-runtime'],
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'react/jsx-runtime': 'react/jsx-runtime',
},
// Preserve module structure for better tree-shaking
preserveModules: false,
},
},
// Generate sourcemaps for easier debugging
sourcemap: true,
// Clear the output directory before building
emptyOutDir: true,
},
});