vite.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  7. /** 当前执行node命令时文件夹的地址(工作目录) */
  8. const root: string = process.cwd();
  9. /** 路径查找 */
  10. const pathResolve = (dir: string): string => {
  11. return resolve(__dirname, ".", dir);
  12. };
  13. /** 设置别名 */
  14. const alias: Record<string, string> = {
  15. "@": pathResolve("src"),
  16. "@build": pathResolve("build")
  17. };
  18. const { dependencies, devDependencies, name, version } = pkg;
  19. const __APP_INFO__ = {
  20. pkg: { dependencies, devDependencies, name, version },
  21. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  22. };
  23. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  24. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  25. warpperEnv(loadEnv(mode, root));
  26. return {
  27. base: VITE_PUBLIC_PATH,
  28. root,
  29. resolve: {
  30. alias
  31. },
  32. // 服务端渲染
  33. server: {
  34. // 是否开启 https
  35. https: false,
  36. // 端口号
  37. port: VITE_PORT,
  38. host: "0.0.0.0",
  39. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  40. proxy: {}
  41. },
  42. plugins: getPluginsList(command, VITE_CDN, VITE_COMPRESSION),
  43. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  44. optimizeDeps: {
  45. /**
  46. * 依赖预构建,vite启动时会将下面 include 里的模块,编译成 esm 格式并缓存到 node_modules/.vite 文件夹,页面加载到对应模块时如果浏览器有缓存就读取浏览器缓存,如果没有会读取本地缓存并按需加载
  47. * 尤其当您禁用浏览器缓存时(这种情况只应该发生在调试阶段)必须将对应模块加入到 include里,否则会遇到开发环境切换页面卡顿的问题(vite 会认为它是一个新的依赖包会重新加载并强制刷新页面),因为它既无法使用浏览器缓存,又没有在本地 node_modules/.vite 里缓存
  48. * 温馨提示:如果您使用的第三方库是全局引入,也就是引入到 src/main.ts 文件里,就不需要再添加到 include 里了,因为 vite 会自动将它们缓存到 node_modules/.vite
  49. */
  50. include: [
  51. "xlsx",
  52. "dayjs",
  53. "pinia",
  54. "swiper",
  55. "intro.js",
  56. "vue-i18n",
  57. "lodash",
  58. "lodash-es",
  59. "cropperjs",
  60. "jsbarcode",
  61. "sortablejs",
  62. "swiper/vue",
  63. "@vueuse/core",
  64. "vue3-danmaku",
  65. "v-contextmenu",
  66. "vue-pdf-embed",
  67. "lodash-unified",
  68. "china-area-data",
  69. "@faker-js/faker",
  70. "vue-json-pretty",
  71. "@logicflow/core",
  72. "@pureadmin/utils",
  73. "@howdyjs/mouse-menu",
  74. "@logicflow/extension",
  75. "@amap/amap-jsapi-loader",
  76. "el-table-infinite-scroll",
  77. "@wangeditor/editor-for-vue",
  78. "xgplayer/dist/simple_player",
  79. "xgplayer/es/controls/volume",
  80. "vuedraggable/src/vuedraggable",
  81. "xgplayer/es/controls/screenShot",
  82. "xgplayer/es/controls/playbackRate"
  83. ],
  84. exclude: ["@pureadmin/theme/dist/browser-utils"]
  85. },
  86. build: {
  87. sourcemap: false,
  88. // 消除打包大小超过500kb警告
  89. chunkSizeWarningLimit: 4000,
  90. rollupOptions: {
  91. input: {
  92. index: pathResolve("index.html")
  93. },
  94. // 静态资源分类打包
  95. output: {
  96. chunkFileNames: "static/js/[name]-[hash].js",
  97. entryFileNames: "static/js/[name]-[hash].js",
  98. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  99. }
  100. }
  101. },
  102. define: {
  103. __INTLIFY_PROD_DEVTOOLS__: false,
  104. __APP_INFO__: JSON.stringify(__APP_INFO__)
  105. }
  106. };
  107. };