提交 | 用户 | 时间
|
314507
|
1 |
import { resolve } from 'path' |
H |
2 |
import type { UserConfig, ConfigEnv } from 'vite' |
|
3 |
import { createVitePlugins } from './build/vite' |
|
4 |
import { include, exclude } from "./build/vite/optimize" |
|
5 |
import {loadEnv} from "vite"; |
|
6 |
// 当前执行node命令时文件夹的地址(工作目录) |
|
7 |
const root = process.cwd() |
|
8 |
|
|
9 |
// 路径查找 |
|
10 |
function pathResolve(dir: string) { |
|
11 |
return resolve(root, '.', dir) |
|
12 |
} |
|
13 |
|
|
14 |
// https://vitejs.dev/config/ |
|
15 |
export default ({ command, mode }: ConfigEnv): UserConfig => { |
|
16 |
let env = {} as any |
|
17 |
const isBuild = command === 'build' |
|
18 |
if (!isBuild) { |
|
19 |
env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root) |
|
20 |
} else { |
|
21 |
env = loadEnv(mode, root) |
|
22 |
} |
|
23 |
return { |
|
24 |
base: env.VITE_BASE_PATH, |
|
25 |
root: root, |
|
26 |
// 服务端渲染 |
|
27 |
server: { |
|
28 |
port: env.VITE_PORT, // 端口号 |
|
29 |
host: "0.0.0.0", |
|
30 |
open: env.VITE_OPEN === 'true', |
|
31 |
// 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域 |
|
32 |
proxy: { |
|
33 |
'/admin-api': { |
|
34 |
target: env.VITE_BASE_URL, |
|
35 |
ws: false, |
|
36 |
changeOrigin: true, |
|
37 |
rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''), |
|
38 |
}, |
|
39 |
}, |
|
40 |
}, |
|
41 |
// 项目使用的vite插件。 单独提取到build/vite/plugin中管理 |
|
42 |
plugins: createVitePlugins(), |
|
43 |
css: { |
|
44 |
preprocessorOptions: { |
|
45 |
scss: { |
|
46 |
additionalData: '@import "./src/styles/variables.scss";', |
|
47 |
javascriptEnabled: true |
|
48 |
} |
|
49 |
} |
|
50 |
}, |
|
51 |
resolve: { |
|
52 |
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'], |
|
53 |
alias: [ |
|
54 |
{ |
|
55 |
find: 'vue-i18n', |
|
56 |
replacement: 'vue-i18n/dist/vue-i18n.cjs.js' |
|
57 |
}, |
|
58 |
{ |
|
59 |
find: /\@\//, |
|
60 |
replacement: `${pathResolve('src')}/` |
|
61 |
} |
|
62 |
] |
|
63 |
}, |
|
64 |
build: { |
|
65 |
minify: 'terser', |
|
66 |
outDir: env.VITE_OUT_DIR || 'dist', |
|
67 |
sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false, |
|
68 |
// brotliSize: false, |
|
69 |
terserOptions: { |
|
70 |
compress: { |
|
71 |
drop_debugger: env.VITE_DROP_DEBUGGER === 'true', |
|
72 |
drop_console: env.VITE_DROP_CONSOLE === 'true' |
|
73 |
} |
|
74 |
} |
|
75 |
}, |
|
76 |
optimizeDeps: { include, exclude } |
|
77 |
} |
|
78 |
} |