vite.config.ts 2.4 KB

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