添加代理 __PROXY_LIST__

This commit is contained in:
icssoa 2021-10-27 19:15:27 +08:00
parent 98b7a9228f
commit ab623bf1f0
3 changed files with 30 additions and 15 deletions

View File

@ -1,5 +1,7 @@
import { isObject } from "../utils";
console.log(__PROXY_LIST__);
export function Permission(value: string) {
return function (target: any, key: any, descriptor: any) {
if (!target.permission) {
@ -26,13 +28,18 @@ export function Service(value: any) {
// 复杂项
if (isObject(value)) {
const { proxy, namespace, url, mock } = value;
const item = __PROXY_LIST__[proxy];
if (proxy && !item) {
console.error(`${proxy} 指向的地址不存在!`);
}
target.prototype.namespace = namespace;
target.prototype.mock = mock;
if (proxy) {
target.prototype.proxy = proxy;
target.prototype.url = url;
target.prototype.url = url || item ? item.target : null;
}
}
};

2
src/shims-vue.d.ts vendored
View File

@ -62,3 +62,5 @@ declare module "mockjs" {
const Mock: any;
export default Mock;
}
declare const __PROXY_LIST__: any[];

View File

@ -1,5 +1,5 @@
import path from "path";
import type { UserConfig } from "vite";
import { UserConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import viteCompression from "vite-plugin-compression";
@ -13,6 +13,21 @@ function resolve(dir: string) {
// https://vitejs.dev/config/
export default (): UserConfig => {
// 请求代理地址
const proxy = {
"/dev": {
target: "http://127.0.0.1:8001",
changeOrigin: true,
rewrite: (path: string) => path.replace(/^\/dev/, "")
},
"/pro": {
target: "https://show.cool-admin.com",
changeOrigin: true,
rewrite: (path: string) => path.replace(/^\/pro/, "/api")
}
};
return {
base: "/",
plugins: [vue(), viteCompression(), Components(), vueJsx(), svgBuilder("./src/icons/svg/")],
@ -32,23 +47,14 @@ export default (): UserConfig => {
},
server: {
port: 9000,
proxy,
hmr: {
overlay: true
},
proxy: {
"/dev": {
target: "http://127.0.0.1:8001",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/dev/, "")
},
"/pro": {
target: "https://show.cool-admin.com",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/pro/, "/api")
}
}
},
define: {
__PROXY_LIST__: JSON.stringify(proxy)
},
build: {
sourcemap: false,
polyfillDynamicImport: false // 必须为false