vite.config.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import dayjs from "dayjs";
  2. import { resolve } from "path";
  3. import pkg from "./package.json";
  4. import { wrapperEnv } from "./build";
  5. import { getPluginsList } from "./build/plugins";
  6. import { ConfigEnv, loadEnv, UserConfigExport } from "vite";
  7. import { exclude, include } from "./build/optimize";
  8. import { getProxyConf } from "./build/proxy";
  9. import { getCssOption } from "./build/css";
  10. // 当前执行node命令时文件夹的地址(工作目录)
  11. const root: string = process.cwd();
  12. // 路径查找
  13. const pathResolve = (dir: string): string => {
  14. return resolve(__dirname, ".", dir);
  15. };
  16. // 设置别名
  17. const alias: Record<string, string> = {
  18. "@": pathResolve("src"),
  19. "@build": pathResolve("build")
  20. };
  21. const { dependencies, devDependencies, name, version } = pkg;
  22. const __APP_INFO__ = {
  23. pkg: { dependencies, devDependencies, name, version },
  24. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  25. };
  26. export default ({ mode }: ConfigEnv): UserConfigExport => {
  27. const env = wrapperEnv(loadEnv(mode, root));
  28. return {
  29. base: env.VITE_PUBLIC_PATH,
  30. root,
  31. resolve: {
  32. alias
  33. },
  34. css: getCssOption(),
  35. // 服务端渲染
  36. server: {
  37. open: true,
  38. // 是否开启 https
  39. https: mode === "online",
  40. // 端口号
  41. port: env.VITE_PORT,
  42. host: "0.0.0.0",
  43. // 本地跨域代理
  44. proxy: getProxyConf(env)
  45. },
  46. plugins: getPluginsList(),
  47. optimizeDeps: {
  48. include,
  49. exclude
  50. },
  51. build: {
  52. sourcemap: false,
  53. // 消除打包大小超过500kb警告
  54. chunkSizeWarningLimit: 4000,
  55. rollupOptions: {
  56. input: {
  57. index: pathResolve("index.html")
  58. }
  59. }
  60. },
  61. define: {
  62. __INTLIFY_PROD_DEVTOOLS__: false,
  63. __APP_INFO__: JSON.stringify(__APP_INFO__)
  64. }
  65. };
  66. };