vite.config.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, VITE_API_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. __LOCAL_API__: JSON.stringify(VITE_API_URL)
  20. },
  21. resolve: {
  22. // https://cn.vitejs.dev/config/#resolve-alias
  23. alias: {
  24. // 设置路径
  25. '~': path.resolve(__dirname, './'),
  26. // 设置别名
  27. '@': path.resolve(__dirname, './src')
  28. },
  29. // https://cn.vitejs.dev/config/#resolve-extensions
  30. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  31. },
  32. // vite 相关配置
  33. server: {
  34. port: 80,
  35. host: true,
  36. open: true,
  37. proxy: {
  38. // https://cn.vitejs.dev/config/#server-proxy
  39. '/dev-api': {
  40. target: 'http://localhost:8010',
  41. // target: 'http://192.168.11.46:8080',
  42. changeOrigin: true,
  43. rewrite: (p) => p.replace(/^\/dev-api/, '')
  44. }
  45. }
  46. },
  47. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  48. css: {
  49. postcss: {
  50. plugins: [
  51. {
  52. postcssPlugin: 'internal:charset-removal',
  53. AtRule: {
  54. charset: (atRule) => {
  55. if (atRule.name === 'charset') {
  56. atRule.remove();
  57. }
  58. }
  59. }
  60. }
  61. ]
  62. }
  63. }
  64. }
  65. })