vue3 vite.config.ts 基础配置

关于项目vite打包之后直接进入dist文件夹访问index.html时出现空白的问题,网上查找了很多也没有查找到相关资料解决,但是如果你用FTP上传到服务器上是可以正常访问的,所以也就没有管了。如果是用vue-cli创建的vue3项目,打包之后是可以正常访问的。

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
 
// 8月更新-自动导入ElementPlus组件,除了图标需要单独引用外,其他的都可以直接在页面上使用组件,会自动导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
 
export default defineConfig({
  plugins: [vue()],
  base: "./", // 类似publicPath,'./'避免打包访问后空白页面,要加上,不然线上也访问不了
  // 8月更新
  productionSourceMap: !isProduction, //关闭生产环境下的SourceMap映射文件
 
  // 8月更新-自动导入ElementPlus
  // 需安装 npm install -D unplugin-vue-components unplugin-auto-import
  plugins: [
      vue(),
      AutoImport({
          resolvers: [ElementPlusResolver()],
      }),
      Components({
          resolvers: [ElementPlusResolver()],
      }),
  ],
  resolve: {
    alias: {
      // 如果报错__dirname找不到,需要安装node,执行npm install @types/node --save-dev
      "@": path.resolve(__dirname, "src"),
      "@assets": path.resolve(__dirname, "src/assets"),
      "@components": path.resolve(__dirname, "src/components"),
      "@images": path.resolve(__dirname, "src/assets/images"),
      "@views": path.resolve(__dirname, "src/views"),
      "@store": path.resolve(__dirname, "src/store"),
    },
  },
  // 8月更新,全局引入less
  css: {
        sourceMap: !isProduction, // css sourceMap 配置
        preprocessorOptions: {
            less: {
                modifyVars: {
                    hack: `true; @import (reference) "${path.resolve("src/assets/css/base.less")}";`,
                },
                javascriptEnabled: true,
            },
        },
    },
  build: {
    outDir: "dist",
    // 9月更新
    assetsDir: "assets", //指定静态资源存放路径
    sourcemap: false, //是否构建source map 文件
    terserOptions: {
      // 生产环境移除console
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
  },
  server: {
    https: false, // 是否开启 https
    open: false, // 是否自动在浏览器打开
    cors: true, // 允许跨域  8月更新
    port: 3000, // 端口号
    host: "0.0.0.0",
    proxy: {
      "/api": {
        target: "", // 后台接口
        changeOrigin: true,
        secure: false, // 如果是https接口,需要配置这个参数
        // ws: true, //websocket支持
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
  // 引入第三方的配置
  optimizeDeps: {
    include: [],
  },
});

Vue3 中,如果你想要通过 Vite 配置文件 `vite.config.ts` 来代理 WebSocket 连接,Vite 提供了一个 `serverMiddleware` 功能,允许你在服务器中间件中设置自定义的请求处理。对于 WebSocket 的代理,你可以创建一个处理 WebSocket 连接的函数,并将其添加到 `viteServer.ws` 数组中。 以下是一个简单的示例,展示如何在 `vite.config.ts` 中配置 WebSocket 代理: ```typescript import { createServer } from 'http' import { createVueApp } from 'vue' import App from './App.vue' const app = createVueApp(App) // 创建 WebSocket 代理函数 async function wsProxy(ws: WebSocket, req: any) { // 如果需要的话,可以在这里对请求做额外的处理 // 比如检查是否已经登录、设置WebSocket路径等 ws.url = '/your-backend-websocket-endpoint'; // 设置代理的目标URL await ws.handshake(req) ws.on('message', async (message) => { // 处理接收到的消息 console.log(`Received message: ${message}`) try { const response = await yourBackendAPI(message); ws.send(response); // 发送回应给客户端 } catch (error) { ws.terminate(); } }); ws.on('close', () => { console.log('WebSocket connection closed') }); } // Vite server 配置 export default defineConfig({ build: {}, server: { middleware: [ { path: /^\/ws/, // 匹配所有以/ws开头的URL handler: createServer((req, res) => { const ws = new WebSocket(req.url.replace(/^ws:/i, 'wss:' if process.env.NODE_ENV === 'production' else 'ws:')); // 使用ws或wss取决于生产环境 wsProxy(ws, req); ws.pipe(res); ws.on('upgrade', (ws) => wsProxy(ws, req)); }), }, ], }, plugins: [], }) ``` 在这个例子中,`wsProxy` 函数会在客户端尝试连接WebSocket时被调用,它会将连接升级并转发到指定的后台WebSocket端点。记得替换 `/your-backend-websocket-endpoint` 为实际的WebSocket服务地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值