forked from Milkdown/milkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
143 lines (129 loc) · 4.02 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
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
/* Copyright 2021, Milkdown by Mirone. */
/* This file only:
* 1. provide common vite config for sub modules in `packages` dir,
* 2. as config file for vitest.
* Please don't use this file for other purpose.
*/
import { resolve } from 'pathe';
import type { Plugin } from 'rollup';
import autoExternal from 'rollup-plugin-auto-external';
import type { BuildOptions, PluginOption, UserConfig as ViteUserConfig, UserConfigExport } from 'vite';
import { defineConfig } from 'vite';
export const libFileName = (format: string) => `index.${format}.js`;
export const rollupPlugins: Plugin[] = [autoExternal()];
const resolvePath = (str: string) => resolve(__dirname, str);
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);
}
export const external = [
// common
'tslib',
'@emotion/css',
'@emotion/cache',
'@emotion/sheet',
'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-block',
'@milkdown/plugin-clipboard',
'@milkdown/plugin-collaborative',
'@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 viteBuild = (packageDirName: string, options: BuildOptions = {}): BuildOptions =>
mergeDeep<BuildOptions>(
{
sourcemap: true,
emptyOutDir: false,
lib: {
entry: resolvePath(`packages/${packageDirName}/src/index.ts`),
name: `milkdown_${packageDirName}`,
fileName: libFileName,
formats: ['es'],
},
rollupOptions: {
external,
output: {
dir: resolvePath(`packages/${packageDirName}/lib`),
},
},
},
options,
);
/**
* Config for plugins
*
* @param packageDirName - package directory name
* @param options - custom options
* @returns user config
*/
export const pluginViteConfig = (packageDirName: string, options: ViteUserConfig = {}) => {
const vitePlugins = options.plugins ?? [];
return defineConfig({
...options,
build: viteBuild(packageDirName, options.build),
plugins: [...vitePlugins, ...(rollupPlugins as PluginOption[])],
});
};
export default defineConfig({
test: {
include: ['packages/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: 'jsdom',
},
} as UserConfigExport);