vite.config.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. import { ip } from 'address'
  5. // https://vitejs.dev/config/
  6. export default defineConfig(({ mode, command }) => {
  7. const env = loadEnv(mode, process.cwd())
  8. const { VITE_APP_ENV,VITE_WS_URL } = env
  9. // 获取本机局域网 IP 地址
  10. const localIP = ip()
  11. return {
  12. // 部署生产环境和开发环境下的URL。
  13. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  14. // 例如 https://www.pm.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.pm.vip/admin/,则设置 baseUrl 为 /admin/。
  15. base: VITE_APP_ENV === 'production' ? '/' : '/',
  16. plugins: createVitePlugins(env, command === 'build'),
  17. define: {
  18. __LOCAL_IP__: JSON.stringify(VITE_APP_ENV === 'production' ? `ws://${VITE_WS_URL}:8010/ws` : 'ws://localhost:8010/ws')
  19. },
  20. resolve: {
  21. // https://cn.vitejs.dev/config/#resolve-alias
  22. alias: {
  23. // 设置路径
  24. '~': path.resolve(__dirname, './'),
  25. // 设置别名
  26. '@': path.resolve(__dirname, './src')
  27. },
  28. // https://cn.vitejs.dev/config/#resolve-extensions
  29. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  30. },
  31. // vite 相关配置
  32. server: {
  33. port: 80,
  34. host: true,
  35. open: true,
  36. proxy: {
  37. // https://cn.vitejs.dev/config/#server-proxy
  38. '/dev-api': {
  39. target: 'http://localhost:8010',
  40. // target: 'http://192.168.11.46:8080',
  41. changeOrigin: true,
  42. rewrite: (p) => p.replace(/^\/dev-api/, '')
  43. }
  44. }
  45. },
  46. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  47. css: {
  48. postcss: {
  49. plugins: [
  50. {
  51. postcssPlugin: 'internal:charset-removal',
  52. AtRule: {
  53. charset: (atRule) => {
  54. if (atRule.name === 'charset') {
  55. atRule.remove();
  56. }
  57. }
  58. }
  59. }
  60. ]
  61. }
  62. }
  63. }
  64. })