Files
vue-pure-admin/node_modules/vite/dist/node/server/serverPluginProxy.js
2021-03-01 15:06:11 +08:00

33 lines
1.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.proxyPlugin = void 0;
const url_1 = require("url");
exports.proxyPlugin = ({ app, config, server }) => {
if (!config.proxy) {
return;
}
const debug = require('debug')('vite:proxy');
const proxy = require('koa-proxies');
const options = config.proxy;
Object.keys(options).forEach((path) => {
let opts = options[path];
if (typeof opts === 'string') {
opts = { target: opts };
}
opts.logs = (ctx, target) => {
debug(`${ctx.req.method} ${ctx.req.oldPath} proxy to -> ${new url_1.URL(ctx.req.url, target)}`);
};
app.use(proxy(path, opts));
});
server.on('upgrade', (req, socket, head) => {
if (req.headers['sec-websocket-protocol'] !== 'vite-hmr') {
for (const path in options) {
let opts = options[path];
if (typeof opts === 'object' && opts.ws) {
proxy.proxy.ws(req, socket, head, opts);
}
}
}
});
};
//# sourceMappingURL=serverPluginProxy.js.map