-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvite.config.js
160 lines (159 loc) · 4.95 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import path from 'node:path'
import process from 'node:process'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { VitePWA } from 'vite-plugin-pwa'
// 引入element-plus自动按需导入插件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { visualizer } from 'rollup-plugin-visualizer'
import UnoCSS from 'unocss/vite'
// import cdn from 'vite-plugin-cdn-import'
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: env.VITE_BASE_URL,
plugins: [
vue(),
setupPrintBuildInfo(),
UnoCSS(),
// visualizer(),//打包分析
lifecycle === 'report'
? visualizer({ open: true, brotliSize: true, filename: 'report.html' })
: null, // 打包分析
AutoImport({
// dirs: ['src/hooks'],
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'],
}),
// cdn({
// modules: [
// {
// name: '@antv/g6', //不是用 cdn 2.4M 93分,使用后 2.8M,89 分
// var: 'G6',
// path: `https://unpkg.com/@antv/[email protected]/dist/g6.min.js`,
// },
// ],
// }),
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')
},
esbuild: {
pure: ['console', 'debugger'],
},
server: {
host: true,
open: true,
},
// 打包配置
build: {
outDir: env.VITE_OUTDIR,
// 手动分包,把第三方库单独打包
rollupOptions: {
// external: ['@antv/g6'],
output: {
entryFileNames: 'entries/[name]-[hash].js',
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
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')) {
return 'codemirror'
}
// 默认所有node_modules库都放到vendor包中
return 'vendor'
}
},
},
},
},
}
})