forked from Milkdown/milkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
113 lines (100 loc) · 3.27 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
/* 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 path from 'path';
import type { Plugin } from 'rollup';
import autoExternal from 'rollup-plugin-auto-external';
import type { BuildOptions, UserConfig as ViteUserConfig } from 'vite';
import { defineConfig } from 'vite';
import { UserConfig } from 'vitest';
export const libFileName = (format: string) => `index.${format}.js`;
export const rollupPlugins: Plugin[] = [autoExternal()];
const resolvePath = (str: string) => path.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 = [
'tslib',
'@emotion/css',
'@emotion/cache',
'@emotion/sheet',
'remark',
'vue',
'react',
'react-dom',
'@milkdown/core',
'@milkdown/ctx',
'@milkdown/design-system',
'@milkdown/exception',
'@milkdown/prose',
'@milkdown/transformer',
'@milkdown/utils',
'@milkdown/preset-gfm',
'@milkdown/preset-commonmark',
'@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',
];
export const viteBuild = (packageDirName: string, options: BuildOptions = {}): BuildOptions =>
mergeDeep<BuildOptions>(
{
sourcemap: true,
lib: {
entry: resolvePath(`packages/${packageDirName}/src/index.ts`),
name: `milkdown_${packageDirName}`,
fileName: libFileName,
formats: ['es'],
},
rollupOptions: {
external,
output: {
dir: resolvePath(`packages/${packageDirName}/lib`),
},
plugins: rollupPlugins,
},
},
options,
);
export const pluginViteConfig = (packageDirName: string, options: ViteUserConfig = {}) => {
const vitePlugins = options.plugins ?? [];
return defineConfig({
...options,
build: viteBuild(packageDirName, options.build),
plugins: vitePlugins,
});
};
export default defineConfig({
test: {
include: ['packages/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: 'jsdom',
},
} as UserConfig);