vite-plugin-ngrok
Version:
A Vite plugin for seamless integration with ngrok, allowing you to easily share your local development server with anyone, anywhere.
38 lines (37 loc) • 1.21 kB
JavaScript
// src/index.ts
import pc from "picocolors";
import _ngrok from "@ngrok/ngrok";
var ngrok = (options) => {
let listener;
let url = "";
return {
name: "ngrok",
apply: (_config, { command, mode }) => command === "serve" && mode !== "test",
async configResolved(config) {
const port = config.server.port || 5173;
listener = await _ngrok.forward({
addr: port,
...typeof options === "string" ? { authtoken: options } : !options ? { authtoken_from_env: true } : options
});
url = listener.url() ?? "";
if (!url) return;
const hostname = new URL(url).hostname;
if (config.server.allowedHosts === true) return;
const hosts = Array.isArray(config.server.allowedHosts) ? config.server.allowedHosts : [];
config.server.allowedHosts = [...hosts, hostname];
},
configureServer(server) {
const { config, printUrls } = server;
if (url) {
server.printUrls = () => {
printUrls();
config.logger.info(` ${pc.magenta("\u279C")} ${pc.magenta("ngrok")}: ${pc.cyan(url)}`);
};
}
server.httpServer?.on("close", () => listener?.close());
}
};
};
export {
ngrok
};