vue-pure-admin2/vite.config.ts

80 lines
2.4 KiB
TypeScript
Raw Normal View History

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";
import { warpperEnv } from "./build";
2022-02-05 17:34:01 +08:00
import { getPluginsList } from "./build/plugins";
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
/** 当前执行node命令时文件夹的地址工作目录 */
2021-12-04 01:16:50 +08:00
const root: string = process.cwd();
/** 路径查找 */
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
/** 设置别名 */
2021-03-01 15:26:05 +08:00
const alias: Record<string, string> = {
"@": 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 => {
const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
warpperEnv(loadEnv(mode, root));
return {
2021-12-04 01:16:50 +08:00
base: VITE_PUBLIC_PATH,
root,
resolve: {
2021-07-07 13:33:47 +08:00
alias
},
// 服务端渲染
server: {
2021-12-04 01:16:50 +08:00
// 端口号
port: VITE_PORT,
2021-07-22 11:33:45 +08:00
host: "0.0.0.0",
// 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
proxy: {},
// 预热文件以提前转换和缓存结果,降低启动期间的初始页面加载时长并防止转换瀑布
warmup: {
clientFiles: ["./index.html", "./src/{views,components}/*"]
}
},
2023-11-16 22:01:20 +08:00
plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
// 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,
exclude
2021-04-21 23:20:14 +08:00
},
build: {
// https://cn.vitejs.dev/guide/build.html#browser-compatibility
target: "es2015",
2021-08-27 01:33:57 +08:00
sourcemap: false,
// 消除打包大小超过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
};
};