-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
vite.config.ts
170 lines (152 loc) · 4.2 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// This config is for building library, do not use to create serve.
import { resolve } from 'node:path'
import { readFileSync } from 'node:fs'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import glob from 'fast-glob'
import MagicString from 'magic-string'
import { visualizer } from 'rollup-plugin-visualizer'
import { components } from './scripts/constant'
import type { LogLevel, Plugin, UserConfig } from 'vite'
interface Manifest {
dependencies?: Record<string, string>,
peerDependencies?: Record<string, string>,
version?: string
}
const pkg = JSON.parse(readFileSync(resolve(__dirname, 'package.json'), 'utf-8')) as Manifest
const logLevel = process.env.LOG_LEVEL
const externalPkgs = ['@vue'].concat(
Object.keys(pkg.dependencies || {}),
Object.keys(pkg.peerDependencies || {})
)
const external = (id: string) => externalPkgs.some(p => p === id || id.startsWith(`${p}/`))
export default defineConfig(async (): Promise<UserConfig> => {
const input = await glob('components/**/*.{ts,vue}', {
cwd: __dirname,
absolute: true,
onlyFiles: true
})
input.push(resolve(__dirname, 'index.ts'))
return {
logLevel: (logLevel || 'info') as LogLevel,
publicDir: false,
define: {
__VERSION__: JSON.stringify(pkg.version)
},
resolve: {
alias: [
{ find: /^@\/components/, replacement: resolve(__dirname, 'components') },
{ find: /^@\/directives/, replacement: resolve(__dirname, 'directives') }
// { find: '@vexip-ui/config', replacement: resolve(__dirname, 'common/config/src') }
]
},
esbuild: {
drop: ['debugger'],
pure: ['console.log']
},
build: {
outDir: 'es',
sourcemap: true,
lib: {
entry: resolve(__dirname, 'index.ts'),
name: 'VexipUI'
},
rollupOptions: {
input,
external,
output: [
{
format: 'cjs',
preserveModules: true,
preserveModulesRoot: __dirname,
dir: 'lib',
entryFileNames: '[name].cjs'
},
{
format: 'es',
preserveModules: true,
preserveModulesRoot: __dirname,
dir: 'es',
entryFileNames: '[name].mjs'
}
],
treeshake: false
},
commonjsOptions: {
sourceMap: false
},
chunkSizeWarningLimit: 10000
},
plugins: [
createResolvePlugin(),
vue(),
vueJsx(),
visualizer({
filename: 'temp/stats-[format].html',
gzipSize: true,
brotliSize: true
}) as any
]
}
})
function createResolvePlugin(): Plugin {
const files = new Set([...components, 'preset', 'dark'])
const rootRE = /vexip-ui\//g
return {
name: 'vexip-ui:resolve',
enforce: 'pre',
resolveId(id) {
if (id.startsWith('@/style')) {
return {
id: id.replace(/@\/style\/(.+).scss$/, 'vexip-ui/style/$1.scss'),
external: 'absolute'
}
}
if (id.startsWith('@/css')) {
return {
id: id.replace(/@\/css\/(.+).css$/, 'vexip-ui/css/$1.css'),
external: 'absolute'
}
}
},
buildStart() {
for (const name of files) {
this.emitFile({
type: 'chunk',
id: resolve('components', name, 'style.ts'),
name: `style/${name}`
})
this.emitFile({
type: 'chunk',
id: resolve('components', name, 'css.ts'),
name: `css/${name}`
})
}
this.emitFile({
type: 'chunk',
id: resolve('components', 'style.ts'),
name: 'style/index'
})
this.emitFile({
type: 'chunk',
id: resolve('components', 'css.ts'),
name: 'css/index'
})
},
renderChunk(code, chunk) {
if (
files.has(chunk.name.substring('style/'.length)) ||
files.has(chunk.name.substring('css/'.length)) ||
chunk.name === 'style/index' ||
chunk.name === 'css/index'
) {
code = code.replace(rootRE, '../../')
return {
code,
map: new MagicString(code).generateMap()
}
}
}
}
}