64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
|
|
import vue from "@vitejs/plugin-vue";
|
|
import { resolve } from "path";
|
|
import { wrapperEnv } from "./build";
|
|
import VueRouter from 'unplugin-vue-router/vite';
|
|
|
|
// 路径查找
|
|
const pathResolve = (dir: string): string => {
|
|
return resolve(__dirname, ".", dir);
|
|
};
|
|
|
|
// 设置别名,还可以添加其他路径
|
|
const alias: Record<string, string> = {
|
|
"@": pathResolve("src"),
|
|
"@views": pathResolve("src/views"),
|
|
"@store": pathResolve("src/store"),
|
|
"@language": pathResolve("src/language"),
|
|
"@css": pathResolve("src/assets/css"),
|
|
};
|
|
|
|
// https://vitejs.dev/config/
|
|
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
|
|
const root = process.cwd();
|
|
const env = loadEnv(mode, root);
|
|
const viteEnv = wrapperEnv(env);
|
|
|
|
return {
|
|
base: viteEnv.VITE_PUBLIC_PATH,
|
|
plugins: [
|
|
VueRouter({
|
|
routesFolder: 'src/views', // 指定路由文件所在的目录
|
|
exclude: ['**/components/*.vue'],
|
|
extensions: ['.vue'], // 指定路由文件的后缀名
|
|
}),
|
|
vue(),
|
|
],
|
|
resolve: {
|
|
alias, // 设置别名
|
|
},
|
|
css: {
|
|
preprocessorOptions: {
|
|
scss: {
|
|
additionalData: `@import "@css/variables.scss";`, // 引入全局变量
|
|
},
|
|
},
|
|
},
|
|
server: {
|
|
host: "0.0.0.0", // 设置服务器主机名
|
|
port: viteEnv.VITE_PORT, // 设置服务启动端口号
|
|
https: undefined, // 是否开启 https
|
|
open: true, // 是否自动打开浏览器
|
|
cors: true, // 允许跨域
|
|
// 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
|
|
proxy: {
|
|
"^/api": {
|
|
target: "http://127.0.0.1:8686", // 代理的目标地址
|
|
changeOrigin: true, // 开发模式,默认的 origin 是真实的 origin:localhost:3000
|
|
rewrite: (path) => path.replace(/^\/api/, ""), // 把 /api 替换成 target 中的地址
|
|
},
|
|
},
|
|
},
|
|
};
|
|
});
|