UNPKG

vite-plugin-ngrok

Version:

A Vite plugin for seamless integration with ngrok, allowing you to easily share your local development server with anyone, anywhere.

28 lines (27 loc) 916 B
// src/index.ts import pc from "picocolors"; import _ngrok from "@ngrok/ngrok"; var ngrok = (options) => ({ name: "ngrok", configureServer({ config, httpServer }) { let listener; httpServer?.on("listening", async () => { const address = httpServer.address(); if (listener || !address || typeof address === "string") return; listener = await _ngrok.forward({ addr: address.port, ...typeof options === "string" ? { authtoken: options } : !options ? { authtoken_from_env: true } : options }); const url = listener.url(); if (!url) return; if (Array.isArray(config.server.allowedHosts)) { config.server.allowedHosts.push(new URL(url).hostname); } config.logger.info(pc.magenta(" \u279C") + pc.magenta(" ngrok: ") + pc.cyan(url)); }); httpServer?.on("close", async () => listener?.close()); } }); export { ngrok };