使用vite创建vue3+ts项目时,需要对vite.config.ts(同vue2中vue.config.js)进行一些基础配置,下面对一些配置项做一些记录
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue" // 以上两项生成项目时默认就有,不需要配置
import path from 'path' // 配置文件路径相关时,需要用到此项 由于node不支持ts,需要安装依赖以便支持 需执行如下命令 npm install @types/node --save-dev
// https://vitejs.dev/config/
export default defineConfig({
base: process.env.NODE_ENV === 'production' ? './' : '/', //静态资源访问路径
resolve: {
alias: { // 设置别名
'@': path.resolve(__dirname, 'src') // 用 @ 符号替换 src 文件路径
}
},
build: {
outDir: 'dist',
assetsDir: 'assets', // 指定静态资源存放路径
sourcemap: false, // 是否构建source map 文件
terserOptions: {
// 生产环境移除console
compress: {
drop_console: true,
drop_debugger: true
}
}
},
server: {
https: false, // 是否开启https
port: 8080, // 端口号
open: true, // 配置后,运行项目是自动启动浏览器
proxy: { // 本地代理
'/api': {
target: "", // 后端接口
changeOrigin: true,
secure: false, // 如果是https接口,需要配置这个参数
// ws: true, // websocket支持
rewrite: (path) => path.replace(/^\/api/, "")
}
}
},
// 引入第三方的配置
optimizeDeps: {
include: []
},
plugins: [vue()]
})