forked from rrweb-io/rrweb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
70 lines (63 loc) · 2.01 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
import path from 'path';
import glob from 'fast-glob';
import { Plugin } from 'vite';
import config from '../../vite.config.default';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import sveltePreprocess from 'svelte-preprocess';
import { emitDts, EmitDtsConfig } from 'svelte2tsx';
import { createRequire } from 'node:module';
import { copyFileSync } from 'node:fs';
const declarationDir = path.resolve('./types');
const require = createRequire(import.meta.url);
const svelteShimsPath = require.resolve('svelte2tsx/svelte-shims-v4.d.ts');
// Helper function to emit TypeScript definitions
async function generateDts(inputPath: string) {
const config: EmitDtsConfig = {
declarationDir: declarationDir,
libRoot: path.dirname(inputPath),
svelteShimsPath: svelteShimsPath,
};
try {
await emitDts(config);
} catch (error) {
console.error(`Error generating .d.ts for ${inputPath}:`, error);
}
}
function viteSvelteDts(): Plugin {
return {
name: 'vite-plugin-svelte-dts',
async buildStart(options) {
console.log('Generating .d.ts files for Svelte components...');
const { input } = options;
if (typeof input === 'string') {
await generateDts(input);
} else if (Array.isArray(input)) {
for (const file of input) {
await generateDts(file);
}
} else {
for (const file of Object.values(input)) {
await generateDts(file);
}
}
// copy .d.ts files to src directory
const files = await glob('**/*.svelte.d.ts', {
cwd: declarationDir,
absolute: true,
});
for (const file of files) {
// resolve the path relative to the src directory
const dest = path.resolve('src', path.relative(declarationDir, file));
copyFileSync(file, dest);
}
},
};
}
export default config(path.resolve(__dirname, 'src/main.ts'), 'rrwebPlayer', {
plugins: [
viteSvelteDts(),
svelte({
preprocess: [sveltePreprocess({ typescript: true })],
}),
],
});