2022-03-02 20:47:14 +08:00
|
|
|
|
import dayjs from "dayjs";
|
2021-08-31 11:50:39 +08:00
|
|
|
|
import { resolve } from "path";
|
2022-03-02 20:47:14 +08:00
|
|
|
|
import pkg from "./package.json";
|
2022-11-10 11:47:07 +08:00
|
|
|
|
import { warpperEnv } from "./build";
|
2022-02-05 17:34:01 +08:00
|
|
|
|
import { getPluginsList } from "./build/plugins";
|
2022-11-30 12:39:12 +08:00
|
|
|
|
import { include, exclude } from "./build/optimize";
|
2023-11-16 22:01:20 +08:00
|
|
|
|
import { type UserConfigExport, type ConfigEnv, loadEnv } from "vite";
|
2021-03-01 15:26:05 +08:00
|
|
|
|
|
2022-09-11 16:46:01 +08:00
|
|
|
|
/** 当前执行node命令时文件夹的地址(工作目录) */
|
2021-12-04 01:16:50 +08:00
|
|
|
|
const root: string = process.cwd();
|
|
|
|
|
|
2022-09-11 16:46:01 +08:00
|
|
|
|
/** 路径查找 */
|
2021-10-19 18:58:47 +08:00
|
|
|
|
const pathResolve = (dir: string): string => {
|
2021-08-31 11:50:39 +08:00
|
|
|
|
return resolve(__dirname, ".", dir);
|
|
|
|
|
};
|
2021-03-01 15:26:05 +08:00
|
|
|
|
|
2022-09-11 16:46:01 +08:00
|
|
|
|
/** 设置别名 */
|
2021-03-01 15:26:05 +08:00
|
|
|
|
const alias: Record<string, string> = {
|
2022-10-27 12:43:01 +08:00
|
|
|
|
"@": pathResolve("src"),
|
2022-03-11 21:28:43 +08:00
|
|
|
|
"@build": pathResolve("build")
|
2021-08-31 11:50:39 +08:00
|
|
|
|
};
|
2021-03-01 15:26:05 +08:00
|
|
|
|
|
2022-03-02 20:47:14 +08:00
|
|
|
|
const { dependencies, devDependencies, name, version } = pkg;
|
|
|
|
|
const __APP_INFO__ = {
|
|
|
|
|
pkg: { dependencies, devDependencies, name, version },
|
|
|
|
|
lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-16 22:01:20 +08:00
|
|
|
|
export default ({ mode }: ConfigEnv): UserConfigExport => {
|
2022-11-17 22:11:13 +08:00
|
|
|
|
const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
|
|
|
|
|
warpperEnv(loadEnv(mode, root));
|
2021-03-29 16:38:52 +08:00
|
|
|
|
return {
|
2021-12-04 01:16:50 +08:00
|
|
|
|
base: VITE_PUBLIC_PATH,
|
2021-03-29 16:38:52 +08:00
|
|
|
|
root,
|
|
|
|
|
resolve: {
|
2021-07-07 13:33:47 +08:00
|
|
|
|
alias
|
2021-03-29 16:38:52 +08:00
|
|
|
|
},
|
|
|
|
|
// 服务端渲染
|
|
|
|
|
server: {
|
2021-12-04 01:16:50 +08:00
|
|
|
|
// 端口号
|
2021-03-29 16:38:52 +08:00
|
|
|
|
port: VITE_PORT,
|
2021-07-22 11:33:45 +08:00
|
|
|
|
host: "0.0.0.0",
|
2022-11-10 11:47:07 +08:00
|
|
|
|
// 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
|
2023-11-10 13:16:05 +08:00
|
|
|
|
proxy: {},
|
|
|
|
|
// 预热文件以提前转换和缓存结果,降低启动期间的初始页面加载时长并防止转换瀑布
|
|
|
|
|
warmup: {
|
|
|
|
|
clientFiles: ["./index.html", "./src/{views,components}/*"]
|
|
|
|
|
}
|
2021-03-29 16:38:52 +08:00
|
|
|
|
},
|
2023-11-16 22:01:20 +08:00
|
|
|
|
plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
|
2022-11-25 15:50:21 +08:00
|
|
|
|
// https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
|
2021-04-21 23:20:14 +08:00
|
|
|
|
optimizeDeps: {
|
2022-11-26 14:26:19 +08:00
|
|
|
|
include,
|
2022-11-30 12:39:12 +08:00
|
|
|
|
exclude
|
2021-04-21 23:20:14 +08:00
|
|
|
|
},
|
2021-03-29 16:38:52 +08:00
|
|
|
|
build: {
|
2023-11-05 21:56:39 +08:00
|
|
|
|
// https://cn.vitejs.dev/guide/build.html#browser-compatibility
|
|
|
|
|
target: "es2015",
|
2021-08-27 01:33:57 +08:00
|
|
|
|
sourcemap: false,
|
2021-03-29 16:38:52 +08:00
|
|
|
|
// 消除打包大小超过500kb警告
|
2022-10-27 15:51:59 +08:00
|
|
|
|
chunkSizeWarningLimit: 4000,
|
|
|
|
|
rollupOptions: {
|
|
|
|
|
input: {
|
|
|
|
|
index: pathResolve("index.html")
|
|
|
|
|
},
|
|
|
|
|
// 静态资源分类打包
|
|
|
|
|
output: {
|
|
|
|
|
chunkFileNames: "static/js/[name]-[hash].js",
|
|
|
|
|
entryFileNames: "static/js/[name]-[hash].js",
|
|
|
|
|
assetFileNames: "static/[ext]/[name]-[hash].[ext]"
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 12:54:03 +08:00
|
|
|
|
},
|
|
|
|
|
define: {
|
2022-03-02 20:47:14 +08:00
|
|
|
|
__INTLIFY_PROD_DEVTOOLS__: false,
|
|
|
|
|
__APP_INFO__: JSON.stringify(__APP_INFO__)
|
2021-07-07 13:33:47 +08:00
|
|
|
|
}
|
2021-08-31 11:50:39 +08:00
|
|
|
|
};
|
|
|
|
|
};
|