jiangping
2023-08-21 2837bdd57f72e386bbf9a725e7b3a13e5eb9e930
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { fileURLToPath, URL } from 'node:url'
 
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
 
// https://vitejs.dev/config/
export default defineConfig(({mode, command}) => {
  let env = loadEnv(mode, process.cwd(), '')
  console.log(env.NODE_ENV);
  return {
    plugins: [
      vue(),
      vueJsx(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    // 打包配置
    build: {
      target: 'modules',
      outDir: 'dist', //指定输出路径
      assetsDir: 'assets', // 指定生成静态资源的存放路径
      minify: 'terser' // 混淆器,terser构建后文件体积更小
    },
  
    // 本地运行配置,及反向代理配置
    server: {
      cors: true, // 默认启用并允许任何源
      open: true, // 在服务器启动时自动在浏览器中打开应用程序
      //反向代理配置,注意rewrite写法,开始没看文档在这里踩了坑
      proxy: {// 本地开发环境通过代理实现跨域,生产环境使用 nginx 转发
        '/api': {
          target: 'http://localhost/3000', // 通过代理接口访问实际地址。这里是实际访问的地址。vue会通过代理服务器来代理请求
          changeOrigin: true,
          ws: true,  // 允许websocket代理
          rewrite: (path) => path.replace(/^\/api/, '') // 将api替换为空
        }
      }
    }
  }
 
})