vite.config.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { resolve } from "path"
  2. import { defineConfig, loadEnv } from "vite"
  3. import createVitePlugins from "./vite/plugins"
  4. export default defineConfig(({ mode, command }) => {
  5. const env = loadEnv(mode, process.cwd())
  6. const { VITE_APP_ENV } = env
  7. return {
  8. // 部署生产环境和开发环境下的URL。
  9. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  10. // 例如 https://www.vber.net/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.vber.net/admin/,则设置 baseUrl 为 /admin/。
  11. base: VITE_APP_ENV === "production" ? "/" : "/",
  12. plugins: createVitePlugins(env, command === "build"),
  13. resolve: {
  14. // https://cn.vitejs.dev/config/#resolve-alias
  15. alias: {
  16. // 设置路径
  17. "~": resolve(__dirname, "./"),
  18. // 设置别名
  19. "@": resolve(__dirname, "./src"),
  20. },
  21. // https://cn.vitejs.dev/config/#resolve-extensions
  22. extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  23. },
  24. // vite 相关配置
  25. server: {
  26. port: 80,
  27. //host: true,
  28. host: "0.0.0.0",
  29. // 热更新
  30. hmr: true,
  31. //设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
  32. strictPort: true,
  33. open: true,
  34. proxy: {
  35. // https://cn.vitejs.dev/config/#server-proxy
  36. "/dev-api": {
  37. target: "http://localhost:6060",
  38. changeOrigin: true,
  39. rewrite: (p) => p.replace(/^\/dev-api/, ""),
  40. },
  41. },
  42. },
  43. preview: {
  44. port: 8080,
  45. //host: true,
  46. host: "0.0.0.0",
  47. // 热更新
  48. hmr: true,
  49. //设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
  50. strictPort: true,
  51. open: true,
  52. proxy: {
  53. // https://cn.vitejs.dev/config/#server-proxy
  54. "/prod-api": {
  55. target: "http://localhost:6060",
  56. //target: "http://shvber.com:6066",
  57. changeOrigin: true,
  58. rewrite: (p) => p.replace(/^\/prod-api/, ""),
  59. //rewrite: (p) => p.replace(/^\/prod-api/, "/prod-api"),
  60. },
  61. },
  62. },
  63. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  64. css: {
  65. postcss: {
  66. plugins: [
  67. {
  68. postcssPlugin: "internal:charset-removal",
  69. AtRule: {
  70. charset: (atRule) => {
  71. if (atRule.name === "charset") {
  72. atRule.remove()
  73. }
  74. },
  75. },
  76. },
  77. ],
  78. },
  79. },
  80. }
  81. })