-
Notifications
You must be signed in to change notification settings - Fork 15
/
vite.config.js
136 lines (136 loc) · 3.48 KB
/
vite.config.js
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
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import eslintPlugin from 'vite-plugin-eslint'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
const stylePlugin = (options = {}) => {
const virtualModuleId = 'virtual:style-module'
const resolvedVirtualModuleId = '\0' + virtualModuleId
return {
name: 'style-plugin', // 必须的,将会在 warning 和 error 中显示
resolveId (id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId
}
},
load (id) {
let result = ''
if (id === resolvedVirtualModuleId) {
const filterStyle = 'import \'@ER/theme/filter/index.scss\''
const messageStyle = 'import \'element-plus/es/components/message/style/css\''
if (options.mode === 'development') {
result = messageStyle + '\n' + filterStyle
} else {
if (options.FORMATS === 'with-element-plus') {
result = messageStyle + '\n' + filterStyle
}
if (options.FORMATS === 'without-element-plus') {
result = filterStyle
}
}
return result
}
}
}
}
export default defineConfig(({ command, mode }) => {
const FORMATS = process.env.FORMATS
const config = {
test: {
environment: 'jsdom',
clearMocks: true,
setupFiles: ['./vitest.setup.js'],
transformMode: {
web: [/\.[jt]sx$/]
},
deps: {
inline: ['element-plus']
}
},
base: './',
define: {
'process.env.NODE_ENV': `"${process.env.NODE_ENV}"`,
'import.meta.env.FORMATS': FORMATS
},
resolve: {
alias: [
{
find: '@ER',
replacement: resolve(__dirname, 'packages')
},
{
find: '@ER-examples',
replacement: resolve(__dirname, 'examples')
},
{
find: '@ER-server',
replacement: resolve(__dirname, 'server')
}
]
},
plugins: [
vue(),
eslintPlugin(),
vueJsx({
}),
stylePlugin({
mode,
FORMATS
})
],
css: {
preprocessorOptions: {
scss: {
additionalData: `
@use 'sass:math';
@use 'sass:map';
@use '@ER/theme/base.scss' as *;
`
}
}
}
}
if (command === 'serve') {
config.plugins.push(
Components({
resolvers: [ElementPlusResolver()]
})
)
}
if (command === 'build') {
const buildConfig = {
emptyOutDir: false,
lib: {
entry: resolve(__dirname, 'packages/filter/index.js'),
name: 'EverrightFilter',
formats: ['es', 'umd', 'iife'],
fileName: `EverrightFilter-${FORMATS}`
},
rollupOptions: {
external: ['vue', 'lodash-es'],
output: {
globals: {
vue: 'Vue',
'lodash-es': '_'
},
assetFileNames: `EverrightFilter-${FORMATS}.css`
}
}
}
if (FORMATS === 'without-element-plus') {
buildConfig.rollupOptions.external.push('element-plus')
buildConfig.rollupOptions.output.globals['element-plus'] = 'ElementPlus'
}
if (FORMATS === 'with-element-plus') {
config.plugins.push(
Components({
resolvers: [ElementPlusResolver()]
})
)
}
config.build = buildConfig
}
return config
})