-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
115 lines (108 loc) · 3.99 KB
/
vite.config.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import path from 'node:path';
import { readdirSync, existsSync } from 'node:fs';
import { defineConfig } from 'vite';
import { InlineConfig } from 'vitest/node';
import dts from 'vite-plugin-dts';
import generateFile from 'vite-plugin-generate-file';
import { getConfig } from '@powerhousedao/codegen';
const { documentModelsDir, editorsDir } = getConfig();
const entry: Record<string, string> = {
index: 'index.ts',
documentModels: path.resolve(documentModelsDir, 'index.ts'),
editors: path.resolve(editorsDir, 'index.ts'),
};
// add subpackage for each editor
readdirSync(documentModelsDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.forEach((name) => {
entry[name] = path.resolve(documentModelsDir, name, 'index.ts');
});
readdirSync(editorsDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.forEach((name) => {
const editorPath = path.resolve(editorsDir, name, 'index.ts');
if (existsSync(editorPath)) {
entry[`editors/${name}`] = editorPath;
}
});
export default defineConfig(() => {
const external = [
'react',
'react/jsx-runtime',
'react-dom',
/^document-model\//,
'document-model-libs',
];
const test: InlineConfig = {
globals: true,
};
return {
test,
build: {
outDir: `dist`,
emptyOutDir: true,
lib: {
entry,
formats: ['es', 'cjs'],
},
rollupOptions: {
external,
output: {
manualChunks: (id) => {
if (
id.startsWith(path.join(__dirname, 'editors')) &&
/editors\/[^/]+\/editor.tsx/.exec(id)
) {
const editorName = path.basename(path.dirname(id));
return `editors/${editorName}/editor`;
} else if (
id.startsWith(
path.join(__dirname, 'document-models'),
) &&
/document-models\/[^/]+\/index.ts/.exec(id)
) {
const modelName = path.basename(path.dirname(id));
return `document-models/${modelName}`;
} else if (id.includes('lazy-with-preload')) {
return 'utils/lazy-with-preload';
}
},
entryFileNames: '[format]/[name].js',
chunkFileNames: (info) => {
// creates named chunk for editor components, document-models and utils
if (info.name.startsWith('editors/')) {
return `[format]/${info.name}.js`;
} else if (info.name.startsWith('document-models/')) {
return `[format]/${info.name}.js`;
} else if (info.name.startsWith('utils')) {
return `[format]/${info.name}.js`;
} else {
return '[format]/internal/[name]-[hash].js';
}
},
},
},
},
plugins: [
dts({ insertTypesEntry: true, exclude: ['**/*.stories.tsx'] }),
generateFile([
{
type: 'json',
output: './es/package.json',
data: {
type: 'module',
},
},
{
type: 'json',
output: `./cjs/package.json`,
data: {
type: 'commonjs',
},
},
]),
],
};
});