serverPluginProxy.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.proxyPlugin = void 0;
  4. const url_1 = require("url");
  5. exports.proxyPlugin = ({ app, config, server }) => {
  6. if (!config.proxy) {
  7. return;
  8. }
  9. const debug = require('debug')('vite:proxy');
  10. const proxy = require('koa-proxies');
  11. const options = config.proxy;
  12. Object.keys(options).forEach((path) => {
  13. let opts = options[path];
  14. if (typeof opts === 'string') {
  15. opts = { target: opts };
  16. }
  17. opts.logs = (ctx, target) => {
  18. debug(`${ctx.req.method} ${ctx.req.oldPath} proxy to -> ${new url_1.URL(ctx.req.url, target)}`);
  19. };
  20. app.use(proxy(path, opts));
  21. });
  22. server.on('upgrade', (req, socket, head) => {
  23. if (req.headers['sec-websocket-protocol'] !== 'vite-hmr') {
  24. for (const path in options) {
  25. let opts = options[path];
  26. if (typeof opts === 'object' && opts.ws) {
  27. proxy.proxy.ws(req, socket, head, opts);
  28. }
  29. }
  30. }
  31. });
  32. };
  33. //# sourceMappingURL=serverPluginProxy.js.map