Giter Site home page Giter Site logo

zcf0508 / unplugin-https-reverse-proxy Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 515 KB

๐Ÿ‘ป Add a reverse proxy for your dev service, support `https`.

License: MIT License

TypeScript 71.26% JavaScript 28.74%
nuxt rebuild repack unplugin vite webpack react vue

unplugin-https-reverse-proxy's Introduction

unplugin-https-reverse-proxy

NPM version

A plugin for https reverse proxy, support viteใ€webpack and rspack.

Install

Support set caddy file path by environment variable UHRP_CADDY_PATH, default is system cache folder.

#.env.local
UHRP_CADDY_PATH=~/tmp/.uhrp
# not auto install caddy when postinstall
npm i unplugin-https-reverse-proxy --save-dev

# auto install
UHRP_AUTO_INSTALL_CADDY=true npm i unplugin-https-reverse-proxy --save-dev
interface Options {
  /** default: true */
  enable?: boolean
  /** target hostname */
  target: string
  /** default: false */
  showCaddyLog?: boolean
  /** default: false */
  https?: boolean
}
Vite
// vite.config.ts
import HttpsReverseProxy from 'unplugin-https-reverse-proxy/vite'

export default defineConfig({
  plugins: [
    HttpsReverseProxy({ /* options */ }),
  ],
})

Example: playground/


Webpack 5
// webpack.config.js

/** @type {Parameters<import('unplugin-https-reverse-proxy/webpack')['default']>[0]} */
const reverseProxyOptions = {
  enable: false,
  target: 'xxx',
  https: false,
}

module.exports = {
  /* ... */
  devServer: {
    client: {
      // โ†“ for HMR
      webSocketURL: {
        ...(reverseProxyOptions.enable
          ? {
              hostname: reverseProxyOptions.target,
            }
          : {}),
        ...(reverseProxyOptions.enable && reverseProxyOptions.https
          ? {
              protocol: 'wss',
              port: 443,
            }
          : {})
      }
    },
    setupExitSignals: true,
    allowedHosts: 'all',
  },
  plugins: [
    require('unplugin-https-reverse-proxy/webpack')(reverseProxyOptions)
  ]
}


Webpack 4
// webpack.config.js

/** @type {Parameters<import('unplugin-https-reverse-proxy/webpack')['default']>[0]} */
const reverseProxyOptions = {
  enable: false,
  target: 'xxx',
  // https: false, // not support yet
}

module.exports = {
  /* ... */
  devServer: {
    host: '0.0.0.0',
    disableHostCheck: true,
  },
  plugins: [
    require('unplugin-https-reverse-proxy/webpack')(reverseProxyOptions)
  ]
}


Rspack
// rspack.config.js

/** @type {Parameters<import('unplugin-https-reverse-proxy/webpack')['default']>[0]} */
const reverseProxyOptions = {
  enable: false,
  target: 'xxx',
  https: false,
}

module.exports = {
  /* ... */
  devServer: {
    client: {
      // โ†“ for HMR
      webSocketURL: {
        ...(reverseProxyOptions.enable
          ? {
              hostname: reverseProxyOptions.target,
            }
          : {}),
        ...(reverseProxyOptions.enable && reverseProxyOptions.https
          ? {
              protocol: 'wss',
              port: 443,
            }
          : {})
      }
    },
  },
  plugins: [
    require('unplugin-https-reverse-proxy/rspack')(reverseProxyOptions)
  ]
}


Rsbuild
// rsbuild.config.js

/** @type {Parameters<import('unplugin-https-reverse-proxy/webpack')['default']>[0]} */
const reverseProxyOptions = {
  enable: false,
  target: 'xxx',
  https: false,
}

module.exports = {
  /* ... */
  dev: {
    client: {
      // โ†“ for HMR
      ...(reverseProxyOptions.enable
        ? {
            host: reverseProxyOptions.target,
          }
        : {}),
      ...(reverseProxyOptions.enable && reverseProxyOptions.https
        ? {
            protocol: 'wss',
            port: '443',
          }
        : {})
    },
  },
  tools: {
    rspack: {
      plugins: [
        require('unplugin-https-reverse-proxy/rspack')(reverseProxyOptions)
      ]
    },
  },
}


Nuxt
// nuxt.config.js
export default defineNuxtConfig({
  modules: [
    ['unplugin-https-reverse-proxy/nuxt', { /* options */ }],
  ],
})

This module works for both Nuxt 2 and Nuxt Vite


Vue CLI 5
// vue.config.js

/** @type {Parameters<import('unplugin-https-reverse-proxy/webpack')['default']>[0]} */
const reverseProxyOptions = {
  enable: false,
  target: 'xxx',
  https: false,
}

module.exports = {
  devServer: {
    client: {
      // โ†“ for HMR
      webSocketURL: {
        ...(reverseProxyOptions.enable
          ? {
              hostname: reverseProxyOptions.target,
            }
          : {}),
        ...(reverseProxyOptions.enable && reverseProxyOptions.https
          ? {
              protocol: 'wss',
              port: 443,
            }
          : {})
      }
    },
    setupExitSignals: true,
    allowedHosts: 'all',
  },
  configureWebpack: {
    plugins: [
      require('unplugin-https-reverse-proxy/webpack')(reverseProxyOptions),
    ],
  },
}


Vue CLI 4
// vue.config.js

/** @type {Parameters<import('unplugin-https-reverse-proxy/webpack')['default']>[0]} */
const reverseProxyOptions = {
  enable: false,
  target: 'xxx',
  // https: false, // not support yet
}

module.exports = {
  devServer: {
    host: '0.0.0.0',
    disableHostCheck: true,
  },
  configureWebpack: {
    plugins: [
      require('unplugin-https-reverse-proxy/webpack')(reverseProxyOptions),
    ],
  },
}


Usage

# macOS
sudo -E npm run dev # your dev script
#    ^ `-E` is important

# windows
# use powershell as administrator
npm run dev # your dev script

If your broswer shows a not trusted certificate warning, please check the keychain.

keychain

More info, you can check caddy about CA Root.

unplugin-https-reverse-proxy's People

Contributors

chenweiyi avatar zcf0508 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

chenweiyi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.