-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
75 lines (68 loc) · 2.22 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
// vite.config.js
import fs from 'fs';
import { execSync } from 'child_process';
import { resolve } from 'path';
import { defineConfig } from 'vitest/config';
import dts from 'vite-plugin-dts';
import tsconfigPaths from 'vite-tsconfig-paths';
// relative to ./src/
const exports = ['main.ts'];
const main = exports[0];
// noinspection JSUnusedGlobalSymbols
export default defineConfig({
build: {
lib: {
entry: toEntry({ exports, main }),
formats: ['es', 'cjs']
},
sourcemap: true,
minify: false
},
plugins: [
dts({
exclude: ['./vite.config.ts', './src/tests/**/*']
}),
tsconfigPaths()
],
test: {
include: ['./src/tests/**/*.test.ts'],
benchmark: {
outputFile: './test-results/vitest-benchmark.json'
},
outputFile: './test-results/vitest-report/index.html',
reporters: ['html', 'default'],
exclude: [
'./src/**/*.js'
],
coverage: {
provider: 'istanbul',
exclude: [
'src/kvin2/index.js',
'src/kvin2',
'src/kvin2/',
'src/kvin2/*',
],
reportsDirectory: './test-results/vitest-coverage'
}
}
});
// helper to generate entry
function toEntry({ exports, main }: { exports: string[]; main: string }) {
// update package.json
const pkg = JSON.parse(fs.readFileSync(resolve(__dirname, './package.json'), 'utf-8'));
const oldExports = pkg.exports;
pkg.exports = {};
for (const file of exports) {
const fileWithoutExt = file.replace(/\.[^/.]+$/, '');
pkg.exports[file === main ? '.' : './' + fileWithoutExt] = {
import: './dist/' + fileWithoutExt + '.mjs',
require: './dist/' + fileWithoutExt + '.js'
};
}
if (JSON.stringify(pkg.exports) !== JSON.stringify(oldExports)) {
console.warn('package.json exports changed, updating & re-formatting...');
fs.writeFileSync(resolve(__dirname, './package.json'), JSON.stringify(pkg, null, 4));
execSync('npx prettier --write package.json');
}
return exports.map(file => resolve(__dirname, './src/', file));
}