Skip to content

Commit 2f51c1f

Browse files
committed
feat: 修改vite配置
1 parent 750be75 commit 2f51c1f

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"prepare": "husky install"
1717
},
1818
"dependencies": {
19+
"dayjs": "^1.11.9",
1920
"pinia": "^2.1.3",
2021
"vue": "^3.3.4",
2122
"vue-router": "^4.2.2"

pnpm-lock.yaml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ const app = createApp(App)
1111
app.use(createPinia())
1212
app.use(router)
1313

14+
console.log(import.meta.env)
15+
// @ts-ignore
16+
console.log(process.env)
17+
1418
app.mount('#app')

vite.config.ts

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,92 @@
11
import { fileURLToPath, URL } from 'node:url'
22

3-
import { defineConfig } from 'vite'
3+
import { ConfigEnv, defineConfig, loadEnv } from 'vite'
44
import vue from '@vitejs/plugin-vue'
55
import vueJsx from '@vitejs/plugin-vue-jsx'
6+
import dayjs from 'dayjs'
7+
8+
const { name: title, version: APP_VERSION } = require('./package.json')
69

710
// https://vitejs.dev/config/
8-
export default defineConfig({
9-
plugins: [vue(), vueJsx()],
10-
resolve: {
11-
alias: {
12-
'@': fileURLToPath(new URL('./src', import.meta.url))
11+
export default (configEnv: ConfigEnv) => {
12+
const { mode } = configEnv
13+
const env = loadEnv(mode, process.cwd())
14+
15+
// 增加环境变量
16+
env.APP_VERSION = APP_VERSION
17+
env.APP_NAME = title
18+
env.APP_BUILD_TIME = dayjs().format('YYYY-MM-DD HH:mm:ss')
19+
20+
return defineConfig({
21+
// 设置打包路径
22+
base: env.VITE_BASE_URL,
23+
// 插件
24+
plugins: [vue(), vueJsx()],
25+
// 别名
26+
resolve: {
27+
alias: {
28+
'@': fileURLToPath(new URL('./src', import.meta.url))
29+
}
30+
},
31+
// 打包配置
32+
build: {
33+
sourcemap: false,
34+
rollupOptions: {
35+
// 确保外部化处理那些你不想打包进库的依赖
36+
output: {
37+
// entryFileNames: 'main-app.js',
38+
manualChunks(id, { getModuleInfo }) {
39+
// 打包依赖
40+
if (id.includes('node_modules')) {
41+
return 'vendor'
42+
}
43+
const reg = /(.*)\/src\/components\/(.*)/
44+
if (reg.test(id)) {
45+
// @ts-ignore
46+
const importersLen = getModuleInfo(id).importers.length
47+
// 被多处引用
48+
if (importersLen > 1) {
49+
return 'common'
50+
}
51+
}
52+
},
53+
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
54+
globals: {
55+
vue: 'Vue'
56+
}
57+
}
58+
}
59+
},
60+
// 环境变量常量
61+
define: {
62+
'import.meta.env': JSON.stringify({
63+
...env
64+
}),
65+
'process.env': JSON.stringify({
66+
...env
67+
})
68+
},
69+
// 本地服务配置
70+
server: {
71+
headers: {
72+
'Access-Control-Allow-Origin': '*'
73+
},
74+
cors: true,
75+
open: false,
76+
port: 5000,
77+
host: true,
78+
proxy: {
79+
'/api': {
80+
target: 'https://cloud-app.com.cn/',
81+
changeOrigin: true,
82+
rewrite: path => path.replace(/^\/api/, '')
83+
},
84+
'/amap': {
85+
target: 'https://restapi.amap.com/',
86+
changeOrigin: true,
87+
rewrite: path => path.replace(/^\/amap/, '')
88+
}
89+
}
1390
}
14-
}
15-
})
91+
})
92+
}

0 commit comments

Comments
 (0)