Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 5x 5x 5x 5x 5x 5x 28x 29x 28x 28x 29x 28x 28x 28x 28x 28x 2x 3x 2x 2x 2x 28x 28x 28x 28x 28x 28x 28x 28x 2x | /*!
* 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<typeof CreateServer> | 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<Plugin> {
// 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<void>((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`;
}
|