vite.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "@api": pathResolve("src/api"),
  19. "@utils": pathResolve("src/utils"),
  20. "@hooks": pathResolve("src/hooks"),
  21. "@views": pathResolve("src/views"),
  22. "@store": pathResolve("src/store"),
  23. "@components": pathResolve("src/components")
  24. };
  25. const { dependencies, devDependencies, name, version } = pkg;
  26. const __APP_INFO__ = {
  27. pkg: { dependencies, devDependencies, name, version },
  28. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  29. };
  30. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  31. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  32. warpperEnv(loadEnv(mode, root));
  33. return {
  34. base: VITE_PUBLIC_PATH,
  35. root,
  36. resolve: {
  37. alias
  38. },
  39. // 服务端渲染
  40. server: {
  41. // 是否开启 https
  42. https: false,
  43. // 端口号
  44. port: VITE_PORT,
  45. host: "0.0.0.0",
  46. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  47. proxy: {}
  48. },
  49. plugins: getPluginsList(command, VITE_CDN, VITE_COMPRESSION),
  50. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  51. optimizeDeps: {
  52. include,
  53. exclude
  54. },
  55. build: {
  56. sourcemap: false,
  57. // 消除打包大小超过500kb警告
  58. chunkSizeWarningLimit: 4000,
  59. rollupOptions: {
  60. input: {
  61. index: pathResolve("index.html")
  62. },
  63. // 静态资源分类打包
  64. output: {
  65. chunkFileNames: "static/js/[name]-[hash].js",
  66. entryFileNames: "static/js/[name]-[hash].js",
  67. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  68. }
  69. }
  70. },
  71. define: {
  72. __INTLIFY_PROD_DEVTOOLS__: false,
  73. __APP_INFO__: JSON.stringify(__APP_INFO__)
  74. }
  75. };
  76. };