vite.config.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { fileURLToPath, URL } from "node:url";
  2. import { defineConfig, loadEnv } from "vite";
  3. import vue from "@vitejs/plugin-vue";
  4. import vueJsx from "@vitejs/plugin-vue-jsx";
  5. import Components from "unplugin-vue-components/vite";
  6. import { VantResolver } from "unplugin-vue-components/resolvers";
  7. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  8. import path from "path";
  9. // import mockDevServerPlugin from "vite-plugin-mock-dev-server";
  10. // import vueSetupExtend from "vite-plugin-vue-setup-extend";
  11. import viteCompression from "vite-plugin-compression";
  12. import { createHtmlPlugin } from "vite-plugin-html";
  13. const timeStamp = new Date().getTime();
  14. // 当前工作目录路径
  15. const root = process.cwd();
  16. // https://vitejs.dev/config/
  17. export default defineConfig(({ mode }) => {
  18. // 环境变量
  19. const env = loadEnv(mode, root, "");
  20. return {
  21. base: "/",
  22. plugins: [
  23. vue(),
  24. // vueJsx(),
  25. // mockDevServerPlugin(),
  26. // vant 组件自动按需引入
  27. Components({
  28. dts: "src/typings/components.d.ts",
  29. resolvers: [VantResolver()]
  30. }),
  31. // svg icon
  32. createSvgIconsPlugin({
  33. // 指定图标文件夹
  34. iconDirs: [path.resolve(root, "src/icons/svg")],
  35. // 指定 symbolId 格式
  36. symbolId: "icon-[dir]-[name]"
  37. }),
  38. // 允许 setup 语法糖上添加组件名属性
  39. // vueSetupExtend(),
  40. // 生产环境 gzip 压缩资源
  41. viteCompression()
  42. // 注入模板数据
  43. // createHtmlPlugin({
  44. // inject: {
  45. // data: {
  46. // ENABLE_ERUDA: env.VITE_ENABLE_ERUDA || "false"
  47. // }
  48. // }
  49. // })
  50. ],
  51. resolve: {
  52. alias: {
  53. "@": fileURLToPath(new URL("./src", import.meta.url))
  54. }
  55. },
  56. server: {
  57. port: "8081",
  58. proxy: {
  59. "/api": {
  60. target: "http://localhost:2001",
  61. changeOrigin: true,
  62. secure: false
  63. }
  64. }
  65. },
  66. build: {
  67. rollupOptions: {
  68. output: {
  69. // chunkFileNames: "static/js/[name]-[hash].js",
  70. // entryFileNames: "static/js/[name]-[hash].js",
  71. // assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  72. chunkFileNames: `js/[name]-[hash].${timeStamp}.js`,
  73. entryFileNames: `js/[name]-[hash].${timeStamp}.js`,
  74. assetFileNames: `static/[ext]/[name].[hash]${timeStamp}.[ext]`
  75. }
  76. }
  77. }
  78. };
  79. });