forked from Milkdown/milkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.mts
149 lines (137 loc) · 3.8 KB
/
vite.config.mts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This file only:
* 1. provide common vite config for submodules in `packages` dir,
* 2. as config file for vitest.
* Please don't use this file for other purpose.
*/
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'
const external = [
// common
'tslib',
'remark',
'unified',
'remark-parse',
'remark-stringify',
'vue',
'react',
'react-dom',
'refractor',
// core
'@milkdown/core',
'@milkdown/ctx',
'@milkdown/design-system',
'@milkdown/exception',
'@milkdown/transformer',
'@milkdown/utils',
// prose
'@milkdown/prose',
'@milkdown/prose/commands',
'@milkdown/prose/dropcursor',
'@milkdown/prose/gapcursor',
'@milkdown/prose/history',
'@milkdown/prose/inputrules',
'@milkdown/prose/keymap',
'@milkdown/prose/model',
'@milkdown/prose/schema-list',
'@milkdown/prose/state',
'@milkdown/prose/tables',
'@milkdown/prose/transform',
'@milkdown/prose/view',
// preset
'@milkdown/preset-gfm',
'@milkdown/preset-commonmark',
// plugin
'@milkdown/plugin-automd',
'@milkdown/plugin-block',
'@milkdown/plugin-clipboard',
'@milkdown/plugin-collab',
'@milkdown/plugin-cursor',
'@milkdown/plugin-diagram',
'@milkdown/plugin-emoji',
'@milkdown/plugin-history',
'@milkdown/plugin-indent',
'@milkdown/plugin-listener',
'@milkdown/plugin-math',
'@milkdown/plugin-menu',
'@milkdown/plugin-prism',
'@milkdown/plugin-slash',
'@milkdown/plugin-tooltip',
'@milkdown/plugin-upload',
'@milkdown/plugin-trailing',
]
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)
}
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.devDependencies || {}),
// ...(globalPackageJson.dependencies || {}),
}
return mergeDeep<BuildOptions>(
{
sourcemap: true,
emptyOutDir: false,
lib: {
entry: resolve(dir, 'src', 'index.ts'),
name: `milkdown_${packageDirName}`,
fileName: libFileName,
formats: ['es'],
},
rollupOptions: {
external: Array.from(new Set([...Object.keys(deps), ...external])),
output: {
dir: resolve(dir, 'lib'),
},
},
},
options,
)
}
/**
* Config for plugins
*
* @param packageDirName - package directory name
* @param options - custom options
* @returns user config
*/
export function pluginViteConfig(packageDirName: string, options: UserConfig = {}) {
return defineConfig({
...options,
build: viteBuild(packageDirName, options.build),
})
}
export default defineConfig({
test: {
include: ['packages/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: 'jsdom',
},
})