Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
153 lines (152 loc) · 4.84 KB
/
Copy pathvite.config.js
File metadata and controls
153 lines (152 loc) · 4.84 KB
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
import path from 'node:path'
import process from 'node:process'
import vue from '@vitejs/plugin-vue'
import { visualizer } from 'rollup-plugin-visualizer'
import UnoCSS from 'unocss/vite'
// 引入element-plus自动按需导入插件
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig, loadEnv } from 'vite'
import { VitePWA } from 'vite-plugin-pwa'
import vueDevTools from 'vite-plugin-vue-devtools'
import { setupPrintBuildInfo } from './build/print-build-info'
const lifecycle = process.env.npm_lifecycle_event
// 获取npm命令
export default defineConfig(({ _, mode }) => {
const env = loadEnv(mode, process.cwd())
return {
base: 'test',
plugins: [
vue(),
setupPrintBuildInfo(),
vueDevTools(),
UnoCSS(),
// visualizer(),//打包分析
lifecycle === 'report'
? visualizer({ open: true, brotliSize: true, filename: 'report.html' })
: null, // 打包分析
AutoImport({
include: [
// 导入目标文件类型
/\.[tj]s(x|on)?$/, // .ts, .tsx, .js, .jsx .json
/\.vue$/,
/\.vue\?vue/, // .vue
/\.md$/, // .md
],
imports: [
// 预定义
'vue', // 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
'pinia',
'@vueuse/core',
{
'@antv/g6': [
['default', 'G6'], // import G6 from "@antv/g6";
],
},
],
dts: 'src/types/auto-imports.d.ts', // 方案二:生成自动导入的auto-imports.d.ts声明文件, 解决 '找不到名称“Elxxx”' 报错
resolvers: [ElementPlusResolver()], // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
}),
Components({
resolvers: [ElementPlusResolver()],
dts: 'src/types/auto-components.d.ts',
dirs: ['src/components/sync'],
}),
VitePWA({
outDir: 'dist',
manifest: {
lang: 'zh-CN',
name: 'json-viewer',
short_name: 'json-viewer',
scope: '/json-viewer/',
start_url: '/json-viewer/',
theme_color: '#ffffff',
background_color: '#ffffff',
icons: [
{
src: '/json-viewer/logo_512.png',
types: 'img/png',
sizes: '512x512',
purpose: 'any',
},
{
src: '/json-viewer/logo_192.png',
types: 'img/png',
sizes: '192x192',
purpose: 'maskable',
},
{
src: '/json-viewer/logo_144.png',
types: 'img/png',
sizes: '144x144',
purpose: 'any',
},
{
src: '/json-viewer/logo_120.png',
types: 'img/png',
sizes: '120x120',
purpose: 'any',
},
{
src: '/json-viewer/logo_72.png',
types: 'img/png',
sizes: '72x72',
purpose: 'maskable',
},
],
},
}),
],
// 指定@为src目录
resolve: {
// Vite路径别名配置
alias: {
'@': path.resolve(__dirname, 'src'),
},
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
},
eslintrc: {
enabled: true, // Default `false`
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
},
// Vite 8 使用 Oxc 进行 JS 转换
oxc: {
pure: ['console'],
},
server: {
host: '0.0.0.0',
port: 8030,
open: true,
},
// 打包配置 - Vite 8 使用 Rolldown 替代 Rollup
build: {
outDir: env.VITE_OUTDIR,
// 手动分包,把第三方库单独打包
rolldownOptions: {
output: {
entryFileNames: 'entries/[name]-[hash].js',
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
// Vite 8 中 manualChunks 仍然可用(虽标记为弃用但向后兼容)
// 未来可以使用 codeSplitting 替代(API 尚在稳定中)
manualChunks(id) {
if (id.includes('node_modules')) {
if (id.includes('@antv/g6')) {
return 'antv-g6'
}
if (id.includes('element-plus')) {
return 'element-plus'
}
if (id.includes('@codemirror') || id.includes('codemirror')) {
return 'codemirror'
}
return 'vendor'
}
},
},
},
},
}
})