vite.config.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. // https://vitejs.dev/config/
  5. export default defineConfig(({ mode, command }) => {
  6. const env = loadEnv(mode, process.cwd())
  7. const alias = {
  8. // 设置路径
  9. '~': path.resolve(__dirname, './'),
  10. // 设置别名
  11. '@': path.resolve(__dirname, './src')
  12. }
  13. if (command === 'serve') {
  14. // 解决警告You are running the esm-bundler build of vue-i18n.
  15. alias['vue-i18n'] = 'vue-i18n/dist/vue-i18n.cjs.js'
  16. }
  17. return {
  18. plugins: createVitePlugins(env, command === 'build'),
  19. resolve: {
  20. // https://cn.vitejs.dev/config/#resolve-alias
  21. alias: alias,
  22. // 导入时想要省略的扩展名列表
  23. // https://cn.vitejs.dev/config/#resolve-extensions
  24. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  25. },
  26. css: {
  27. devSourcemap: true //开发模式时启用
  28. },
  29. base: env.VITE_APP_ROUTER_PREFIX,
  30. // 打包配置
  31. build: {
  32. sourcemap: command === 'build' ? false : 'inline',
  33. outDir: 'dist', //指定输出目录
  34. assetsDir: 'assets', //指定静态资源存储目录(相对于outDir)
  35. // 将js、css文件分离到单独文件夹
  36. rollupOptions: {
  37. output: {
  38. chunkFileNames: 'static/js/[name]-[hash].js',
  39. entryFileNames: 'static/js/[name]-[hash].js',
  40. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  41. }
  42. }
  43. },
  44. // vite 相关配置
  45. server: {
  46. port: 8887,
  47. host: true,
  48. open: true,
  49. proxy: {
  50. // https://cn.vitejs.dev/config/#server-proxy
  51. '/dev-api': {
  52. target: env.VITE_APP_API_HOST,
  53. changeOrigin: true,
  54. rewrite: (path) => path.replace(/^\/dev-api/, '')
  55. },
  56. '/msghub': {
  57. target: env.VITE_APP_API_HOST,
  58. ws: true,
  59. rewrite: (path) => path.replace(/^\/msgHub/, '')
  60. }
  61. }
  62. }
  63. }
  64. })