/*! * Forked from rollup-plugin-livereload * (https://github.com/thgh/rollup-plugin-livereload/blob/main/src/index.ts) * to fix downstream vulnerability issues: it pins livereload 0.9, which pulls * in chokidar 3 and braces. * * The MIT License (MIT) * * Copyright (c) 2016 Thomas Ghysels * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import path from "path"; import getPort from "get-port"; import type { Plugin } from "rollup"; import type { createServer as CreateServer, CreateServerConfig, } from "livereload"; import type { IsomorphicRollupFs } from "../fs-iface"; import nodeFsAdapter from "../node-fs-adapter"; const DEFAULT_PORT = 35729; // Shared across config reloads so rollup watch mode doesn't leak servers. const state: { server: ReturnType | null } = (( global as any ).PLUGIN_LIVERELOAD ??= { server: null }); /** * @param createServer `createServer` from the `livereload` package * @param options.watch Directories to watch, relative to the current directory * @param options.fs File system used to create missing watch dirs before watching */ export async function livereloadPlugin( createServer: typeof CreateServer, options: { watch: string[]; fs?: IsomorphicRollupFs; }, ): Promise { // release previous server instance if rollup is reloading configuration // in watch mode state.server?.close(); const watchPaths = options.watch.map((w) => path.resolve(process.cwd(), w)); const fs = options.fs || globalThis.__rollupFs || nodeFsAdapter; await Promise.all( watchPaths.map((dir) => fs.mkdir(dir, { recursive: true })), ); const { server, port } = await listen(createServer); state.server = server; server.watch(watchPaths); let announced = false; return { name: "livereload", banner() { return `(function(l, r) { if (!l || l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (self.location.host || 'localhost').split(':')[0] + ':${port}/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.getElementsByTagName('head')[0].appendChild(r) })(self.document);`; }, generateBundle() { if (announced) return; announced = true; const customPort = port !== DEFAULT_PORT ? ` on port ${port}` : ""; console.log(green(`LiveReload enabled${customPort}`)); }, }; } // getPort() only probes the port, so it can be taken by the time we bind. // Retry with a fresh port instead of letting EADDRINUSE crash the build. async function listen(createServer: typeof CreateServer, attempts = 5) { for (let i = 1; ; i++) { const port = await getPort({ port: DEFAULT_PORT }); const server = createServer({ port, host: "" } as CreateServerConfig); try { await new Promise((resolve, reject) => { server.server.once("listening", resolve); server.once("error", reject); }); return { server, port }; } catch (err: any) { server.close(); if (err?.code !== "EADDRINUSE" || i >= attempts) throw err; } } } function green(text: string) { return `\u001b[1m\u001b[32m${text}\u001b[39m\u001b[22m`; }