-
Notifications
You must be signed in to change notification settings - Fork 9
/
vite.config.ts
78 lines (68 loc) · 2.09 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
import { readFileSync } from 'node:fs'
import { basename, dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { BuildOptions, UserConfig } from 'vite'
import { defineConfig } from 'vite'
import globalPackageJson from './package.json'
export const libFileName = (format: string) => `index.${format}.js`
function isObject(item: unknown): item is Record<string, unknown> {
return Boolean(item && typeof item === 'object' && !Array.isArray(item))
}
function mergeDeep<T>(target: T, ...sources: T[]): T {
if (!sources.length)
return target
const source = sources.shift()
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key])
Object.assign(target, { [key]: {} })
mergeDeep(target[key] as T, source[key] as T)
}
else {
Object.assign(target, { [key]: source[key] })
}
}
}
return mergeDeep(target, ...sources)
}
const externals = [
'svelte/store',
/^lit/,
]
function viteBuild(path: string, options: BuildOptions = {}): BuildOptions {
const dir = dirname(fileURLToPath(path))
const packageDirName = basename(dir)
const packageJson = JSON.parse(readFileSync(resolve(dir, 'package.json'), { encoding: 'utf-8' }))
const deps = {
...(packageJson.dependencies || {}),
...(packageJson.devDependencies || {}),
...(packageJson.peerDependencies || {}),
...(globalPackageJson.dependencies || {}),
}
return mergeDeep<BuildOptions>(
{
sourcemap: true,
emptyOutDir: false,
lib: {
entry: resolve(dir, 'src', 'index.ts'),
name: `prosemirror-adapter_${packageDirName}`,
fileName: libFileName,
formats: ['es'],
},
rollupOptions: {
external: [...Object.keys(deps), ...externals],
output: {
dir: resolve(dir, 'lib'),
},
},
},
options,
)
}
export function viteConfigFactory(packageDirName: string, options: UserConfig = {}) {
return defineConfig({
...options,
build: viteBuild(packageDirName, options.build),
})
}