vite.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { fileURLToPath, URL } from "node:url";
  2. import { loadEnv, defineConfig } from "vite";
  3. import pkg from "./package.json";
  4. import dayjs from "dayjs";
  5. import AutoImport from "unplugin-auto-import/vite";
  6. import AutoComponents from "unplugin-vue-components/vite";
  7. import vue from "@vitejs/plugin-vue";
  8. import vueJsx from "@vitejs/plugin-vue-jsx";
  9. import legacy from "@vitejs/plugin-legacy";
  10. import { createHtmlPlugin } from "vite-plugin-html";
  11. // 当前执行node命令时文件夹的地址(工作目录)
  12. const root = process.cwd();
  13. // 打包后静态资源的存放路径
  14. const assetsDir = "assets";
  15. // __APP_INFO__
  16. const { dependencies, devDependencies, name, version } = pkg;
  17. const appVersion = dayjs().format("YYYYMMDDHHmm");
  18. const lastBuildTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
  19. const __APP_INFO__ = {
  20. pkg: { dependencies, devDependencies, name, version },
  21. version: appVersion,
  22. lastBuildTime,
  23. };
  24. /**
  25. * Configuring Vite
  26. *
  27. * @see https://vitejs.dev/config/
  28. */
  29. export default defineConfig(({ mode }) => {
  30. const env = loadEnv(mode, root);
  31. return {
  32. base: "./",
  33. plugins: [
  34. vue(),
  35. vueJsx(),
  36. AutoImport({
  37. imports: ["vue", "vue-router"],
  38. // dirs: ['./src/hooks/**'],
  39. dts: "./src/auto-imports.d.ts",
  40. eslintrc: {
  41. enabled: true,
  42. },
  43. }),
  44. AutoComponents({
  45. dirs: ["./src/components"],
  46. extensions: ["vue", "jsx", "tsx"],
  47. dts: "./src/auto-components.d.ts",
  48. }),
  49. createHtmlPlugin({
  50. inject: {
  51. data: {
  52. title: env.VITE_APP_TITLE,
  53. },
  54. },
  55. minify: true,
  56. }),
  57. env.VITE_LEGACY === "true" ? legacy() : null,
  58. ],
  59. server: {
  60. port: Number(env.VITE_PORT),
  61. proxy: {
  62. "/super_cloud": {
  63. target: `https://wx.palmnest.com`, // https://wx.palmnest.com/super_cloud
  64. changeOrigin: true,
  65. secure: false,
  66. // followRedirects: true,
  67. // cookieDomainRewrite: '', // 使用 cookieDomainRewrite 或者是 设置cookie 那行都可以
  68. configure: (proxy, options) => {
  69. proxy.on("proxyReq", (proxyReq, req, res) => {
  70. proxyReq.removeHeader("referer");
  71. proxyReq.removeHeader("origin");
  72. });
  73. proxy.on("proxyRes", (proxyRes, req, res) => {
  74. // 设置cookie
  75. if (proxyRes.headers["set-cookie"]) {
  76. proxyRes.headers["set-cookie"] = replaceDomain(
  77. Array.isArray(proxyRes.headers["set-cookie"])
  78. ? proxyRes.headers["set-cookie"][0]
  79. : proxyRes.headers["set-cookie"]
  80. );
  81. }
  82. function replaceDomain(value) {
  83. return value
  84. .replace(/Domain=[a-z.]*;/, "")
  85. .replace(/secure=.*;/, "");
  86. }
  87. });
  88. },
  89. },
  90. },
  91. },
  92. build: {
  93. assetsDir: assetsDir,
  94. sourcemap: false,
  95. chunkSizeWarningLimit: 1500,
  96. rollupOptions: {
  97. output: {
  98. entryFileNames: `${assetsDir}/[name].${appVersion}.js`,
  99. chunkFileNames: `${assetsDir}/[name].${appVersion}.js`,
  100. assetFileNames: `${assetsDir}/[ext]/[name].${appVersion}.[ext]`,
  101. },
  102. },
  103. },
  104. resolve: {
  105. alias: {
  106. "@": fileURLToPath(new URL("./src", import.meta.url)),
  107. },
  108. },
  109. define: {
  110. __APP_INFO__: JSON.stringify(__APP_INFO__),
  111. },
  112. css: {
  113. modules: {
  114. localsConvention: "camelCaseOnly",
  115. },
  116. preprocessorOptions: {
  117. less: {
  118. modifyVars: {
  119. hack: `true; @import './src/styles/variable.less';`,
  120. },
  121. javascriptEnabled: true,
  122. },
  123. },
  124. },
  125. };
  126. });