-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
82 lines (78 loc) · 2.05 KB
/
index.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
import { build, InlineConfig, mergeConfig, transformWithEsbuild, UserConfig } from 'vite'
import { RollupOutput } from 'rollup'
import { existsSync, promises as fsp } from 'fs'
import unocss from 'unocss/vite'
import mini from 'unocss/preset-mini'
import vue from '@vitejs/plugin-vue'
import yaml from '@maikolib/vite-plugin-yaml'
export async function buildExtension(root: string, config: UserConfig = {}) {
if (!existsSync(root + '/client')) return
const outDir = root + '/dist'
if (existsSync(outDir)) {
await fsp.rm(outDir, { recursive: true })
}
await fsp.mkdir(root + '/dist', { recursive: true })
const results = await build(mergeConfig({
root,
build: {
write: false,
outDir: 'dist',
assetsDir: '',
minify: true,
emptyOutDir: true,
commonjsOptions: {
strictRequires: true,
},
lib: {
entry: root + '/client/index.ts',
fileName: 'index',
formats: ['es'],
},
rollupOptions: {
makeAbsoluteExternalsRelative: true,
external: [
'vue',
'vue-router',
'@vueuse/core',
'@koishijs/client',
],
output: {
format: 'iife',
},
},
},
plugins: [
vue(),
yaml(),
unocss({
presets: [
mini({
preflight: false,
}),
],
}),
],
resolve: {
alias: {
'vue-i18n': '@koishijs/client',
'@koishijs/components': '@koishijs/client',
},
},
define: {
'process.env.NODE_ENV': '"production"',
},
} as InlineConfig, config)) as RollupOutput[]
for (const item of results[0].output) {
if (item.fileName === 'index.mjs') item.fileName = 'index.js'
const dest = root + '/dist/' + item.fileName
if (item.type === 'asset') {
await fsp.writeFile(dest, item.source)
} else {
const result = await transformWithEsbuild(item.code, dest, {
minifyWhitespace: true,
charset: 'utf8',
})
await fsp.writeFile(dest, result.code)
}
}
}