提交 | 用户 | 时间
|
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 |
}, |
8e4ab7
|
40 |
hmr: { |
H |
41 |
overlay: false |
|
42 |
} |
314507
|
43 |
}, |
H |
44 |
// 项目使用的vite插件。 单独提取到build/vite/plugin中管理 |
|
45 |
plugins: createVitePlugins(), |
|
46 |
css: { |
|
47 |
preprocessorOptions: { |
|
48 |
scss: { |
|
49 |
additionalData: '@import "./src/styles/variables.scss";', |
|
50 |
javascriptEnabled: true |
|
51 |
} |
|
52 |
} |
|
53 |
}, |
|
54 |
resolve: { |
|
55 |
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'], |
|
56 |
alias: [ |
|
57 |
{ |
|
58 |
find: 'vue-i18n', |
|
59 |
replacement: 'vue-i18n/dist/vue-i18n.cjs.js' |
|
60 |
}, |
|
61 |
{ |
|
62 |
find: /\@\//, |
|
63 |
replacement: `${pathResolve('src')}/` |
|
64 |
} |
|
65 |
] |
|
66 |
}, |
|
67 |
build: { |
|
68 |
minify: 'terser', |
|
69 |
outDir: env.VITE_OUT_DIR || 'dist', |
|
70 |
sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false, |
|
71 |
// brotliSize: false, |
|
72 |
terserOptions: { |
|
73 |
compress: { |
|
74 |
drop_debugger: env.VITE_DROP_DEBUGGER === 'true', |
|
75 |
drop_console: env.VITE_DROP_CONSOLE === 'true' |
|
76 |
} |
|
77 |
} |
|
78 |
}, |
|
79 |
optimizeDeps: { include, exclude } |
|
80 |
} |
|
81 |
} |