-
Notifications
You must be signed in to change notification settings - Fork 180
/
vite.config.js
111 lines (110 loc) · 3.51 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
import vue from '@vitejs/plugin-vue'
import {defineConfig, loadEnv} from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {NaiveUiResolver} from 'unplugin-vue-components/resolvers'
import path from "path";
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js" // css 整合 js
import {analyzer} from "vite-bundle-analyzer"; // 分析包大小依赖
import VueDevTools from 'vite-plugin-vue-devtools' // 调试面板
import history from 'connect-history-api-fallback'
function historyPlugin() {
return {
name: 'vite-plugin-history',
configureServer(server) {
server.middlewares.use(history({
rewrites: [
{ from: /\/admin/, to: '/admin.html' } // 解决页面 在history 刷新下会 404 的问题
],
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
}))
}
}
}
export default defineConfig(({mode}) => {
const env = loadEnv(mode, process.cwd())
const config = {
// vite 配置
plugins: [
vue(),
AutoImport({
imports: [
'vue',
{
'naive-ui': [
'useDialog',
'useMessage',
'useNotification',
'useLoadingBar'
]
}
]
}),
Components({
resolvers: [NaiveUiResolver()]
}),
VueDevTools(),
historyPlugin()
],
define: {
__APP_ENV__: JSON.stringify(env.APP_ENV),
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
outDir: 'lib',
rollupOptions: {}
},
server: {
// hmr: false,
host: '0.0.0.0',
port: 5100,
headers: {
'Access-Control-Allow-Origin': '*',
},
proxy: {
'/api': {
target: 'http://localhost:1337/api',
changeOrigin: true,
rewrite: (url) => url.replace(/^\/api/, ''),
},
},
}
}
if (mode === 'lib') {
config.build.lib = {
entry: path.resolve(__dirname, 'src/packages/install.js'),
name: 'vue-bag-admin',
formats: ['es', 'cjs'],
fileName: (format) => `vue-bag-admin.${format}.js` // 打包后的文件名
}
config.build.rollupOptions = {
output: {
manualChunks: () => {
return 'vendor'
}
},
//设置为库模式
external: ['vue', 'naive-ui'], // 这个配置很重要,不然会导致 公用的组件无法使用
}
config.build.minify = 'terser'
config.build.terserOptions = {
compress: {
drop_console: true,
drop_debugger: true,
},
}
config.plugins.push(cssInjectedByJsPlugin())
// config.plugins.push(analyzer())
} else {
config.build.outDir = 'dist'
config.build.rollupOptions.input = {
main: path.resolve(__dirname, 'index.html'),
admin: path.resolve(__dirname, 'admin.html'),
}
}
return config;
})