forked from XiaoDaiGua-Ray/ray-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.plugin.config.ts
228 lines (213 loc) · 5.91 KB
/
vite.plugin.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
*
* @author Ray <https://github.com/XiaoDaiGua-Ray>
*
* @date 2023-09-15
*
* @workspace ray-template
*
* @remark 今天也是元气满满撸代码的一天
*/
import path from 'node:path'
import vue from '@vitejs/plugin-vue'
import viteVueJSX from '@vitejs/plugin-vue-jsx'
import viteVeI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
import viteInspect from 'vite-plugin-inspect'
import viteSvgLoader from 'vite-svg-loader'
import vitePluginImp from 'vite-plugin-imp'
import { analyzer, adapter } from 'vite-bundle-analyzer'
import viteCompression from 'vite-plugin-compression'
import { ViteEjsPlugin as viteEjsPlugin } from 'vite-plugin-ejs'
import viteAutoImport from 'unplugin-auto-import/vite'
import viteEslint from 'vite-plugin-eslint'
import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import unpluginViteComponents from 'unplugin-vue-components/vite'
import { cdn as viteCDNPlugin } from 'vite-plugin-cdn2'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import { cdnResolve, svgIconResolve } from './vite-helper'
import config from './vite.custom.config'
import type { PluginOption } from 'vite'
function pathResolve(dir: string) {
return path.resolve(__dirname, dir)
}
// 仅适用于报告模式(report)
function onlyReportOptions(mode: string): PluginOption[] {
if (mode !== 'report') {
return []
}
return [
adapter(
analyzer({
analyzerMode: 'server', // 以默认服务器代理打开文件
openAnalyzer: true, // 以默认服务器代理打开文件
}),
),
]
}
// 仅适用于构建模式(任何构建模式:preview、build、report...)
function onlyBuildOptions(mode: string): PluginOption[] {
return [
viteCDNPlugin({
// modules 顺序 vue, vue-demi 必须保持当前顺序加载,否则会出现加载错误问题
resolve: cdnResolve(),
modules: [
{
name: 'vue',
global: 'Vue',
relativeModule: 'vue.global.min.js',
},
{
name: 'vue-demi',
global: 'VueDemi',
relativeModule: 'index.iife.min.js',
},
{
name: 'naive-ui',
global: 'naive',
relativeModule: 'index.prod.js',
},
{
name: 'pinia',
global: 'Pinia',
relativeModule: 'pinia.iife.min.js',
},
{
name: 'vue-router',
global: 'VueRouter',
relativeModule: 'vue-router.global.min.js',
},
{
name: 'vue-i18n',
global: 'VueI18n',
relativeModule: 'vue-i18n.global.min.js',
},
{
name: 'echarts',
global: 'echarts',
relativeModule: 'echarts.min.js',
},
{
name: 'axios',
global: 'axios',
relativeModule: 'axios.min.js',
},
{
name: 'jsbarcode',
global: 'JsBarcode',
relativeModule: 'JsBarcode.all.min.js',
},
],
}),
]
}
// 仅适用于开发模式
function onlyDevOptions(mode: string): PluginOption[] {
if (mode !== 'development') {
return []
}
return []
}
// 基础插件配置
function baseOptions(mode: string): PluginOption[] {
const { title, appPrimaryColor, preloadingConfig } = config
return [
vue(),
viteVueJSX(),
title,
viteVeI18nPlugin({
runtimeOnly: true,
compositionOnly: true,
forceStringify: true,
defaultSFCLang: 'json',
include: [pathResolve('../locales/**')],
}),
viteAutoImport({
eslintrc: {
enabled: true,
filepath: './unplugin/.eslintrc-auto-import.json',
},
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
/\.md$/, // .md
],
dts: './unplugin/auto-imports.d.ts',
imports: ['vue', 'vue-router', 'pinia'],
resolvers: [NaiveUiResolver()],
}),
unpluginViteComponents({
dts: './unplugin/components.d.ts',
resolvers: [NaiveUiResolver()],
types: [
{
from: 'vue-router',
names: ['RouterLink', 'RouterView'],
},
],
}),
viteCompression(),
viteSvgLoader({
defaultImport: 'url', // 默认以 url 形式导入 svg
}),
// 基础插件配置,在 report 模式下不需要 eslint 插件
mode !== 'report'
? viteEslint({
lintOnStart: true,
failOnError: true,
failOnWarning: true,
fix: true,
exclude: ['dist/**', '**/node_modules/**', 'vite-env.d.ts', '*.md'],
include: ['src/**/*.{vue,js,jsx,ts,tsx}'],
cache: true,
})
: null,
vitePluginImp({
libList: [
{
libName: 'lodash-es',
libDirectory: '',
camel2DashComponentName: false,
},
{
libName: '@vueuse',
libDirectory: '',
camel2DashComponentName: false,
},
],
}),
viteEjsPlugin({
preloadingConfig,
appPrimaryColor,
}),
viteInspect(), // 仅适用于开发模式(检查 `Vite` 插件的中间状态)
mockDevServerPlugin({
include: ['mock/**/*.mock.ts'],
exclude: [
'**/node_modules/**',
'**/test/**',
'**/cypress/**',
'src/**',
'**/.vscode/**',
'**/.git/**',
'**/dist/**',
'mock/shared/**',
],
reload: true,
build: true,
}),
createSvgIconsPlugin({
iconDirs: svgIconResolve(),
symbolId: 'icon-[dir]-[name]',
inject: 'body-last',
customDomId: '__svg__icons__dom__',
}),
]
}
export default function (mode: string): PluginOption[] {
const plugins =
mode === 'development' ? onlyDevOptions(mode) : onlyBuildOptions(mode)
const reportPlugins = mode === 'report' ? onlyReportOptions(mode) : []
return [...baseOptions(mode), ...plugins, ...reportPlugins]
}