-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.baseConfig.ts
More file actions
101 lines (96 loc) · 3.08 KB
/
vite.baseConfig.ts
File metadata and controls
101 lines (96 loc) · 3.08 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
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
import { execSync } from 'node:child_process';
import { copyFile } from 'node:fs/promises';
import chalk from 'chalk';
import { flat } from 'remeda';
import { defineConfig, type LibraryOptions, type PluginOption, type UserConfig } from 'vite';
export function viteBaseConfig({ dependencies = {}, peerDependencies = {}, devDependencies = {}, entries, plugins = [], name, bundle = false }: {
dependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
name: string;
entries: LibraryOptions['entry'];
plugins?: UserConfig['plugins'];
bundle?: boolean;
}): UserConfig {
return defineConfig({
/* eslint-disable no-console */
plugins: [
((): PluginOption => {
const buildEntries: Array<string> = [];
return ({
name: 'dts',
apply: 'build',
config(config): void {
if (config.build?.lib) {
const { entry } = config.build.lib;
if (Array.isArray(entry))
buildEntries.push(...entry);
else
buildEntries.push(entry as string);
}
},
closeBundle(): void {
console.log();
const external = [dependencies, peerDependencies, devDependencies].flatMap(Object.keys);
for (const entry of buildEntries)
execSync(`tsup ${entry} --dts-only --format esm --out-dir dist ${external.map(pack => ` --external ${pack}`).join(' ')}`, { stdio: 'inherit' });
},
});
})(),
((): PluginOption => ({
apply: 'build',
async closeBundle(): Promise<void> {
try {
console.log();
console.log(chalk.blue('Copying legacy.d.ts file'));
await copyFile('./src/legacy.d.ts', './dist/legacy.d.ts');
console.log(chalk.green('Successfully copied legacy.d.ts file'));
}
catch {
console.log(chalk.yellow('No legacy.d.ts file found, skipping copy'));
}
},
name: 'copy-legacy-dts',
}))(),
...plugins,
],
/* eslint-enable no-console */
build: {
emptyOutDir: true,
minify: false,
lib: {
entry: entries,
name,
formats: ['es', 'cjs'],
fileName: format => `${format === 'cjs' ? 'cjs/' : ''}[name].${format === 'cjs' ? 'cjs' : 'js'}`,
},
sourcemap: true,
rollupOptions: {
external: flat([
...Object.keys(dependencies),
...Object.keys(peerDependencies),
].map(dep => [dep, new RegExp(`^${dep}(/.*)?`)])),
output: {
inlineDynamicImports: false,
...(bundle
? {
preserveModulesRoot: 'src',
preserveModules: false,
}
: {
preserveModulesRoot: 'src',
preserveModules: true,
}),
},
},
},
test: {
environment: 'jsdom',
coverage: {
provider: 'v8',
include: ['src/**/*.ts'],
exclude: ['src/types/*.ts'],
},
},
});
}