rollup-plugin-rld
Version:
Rollup plugin to inject a live reload script into your bundle
100 lines (92 loc) • 2.82 kB
JavaScript
;
var http = require('node:http');
const createEmitter = () => {
const subscribers = [];
const emit = () => subscribers.forEach(({ action }) => action());
const reset = () => {
subscribers.length = 0;
};
const subscribe = (action) => {
subscribers.push({ action });
};
return {
emit,
reset,
subscribe
};
};
const logSuccess = (text) => `\x1B[32m\u{1F680} @Rollup-Plugin-Rld: ${text}\x1B[0m`;
const createServer = (serverInit) => {
return http.createServer(async (req, res) => {
let { emitter, url } = serverInit;
if (req.url !== url)
return;
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*"
});
emitter.reset();
emitter.subscribe(() => {
console.log(logSuccess(`Reloading...`));
res.write('data: { "rld": "true" }\n\n');
});
});
};
const injectable = (port, host, url, attributes, log) => {
if (!window || !document)
return;
if (document.head.querySelector(`[url='${url}']`))
return;
const script = document.createElement("script");
Object.entries(JSON.parse(attributes)).forEach(
([key, value]) => script.setAttribute(key, value)
);
script.setAttribute("url", url);
script.textContent = `new EventSource('http://${host}:${port}${url}').addEventListener('message', (ev) =>
JSON.parse(ev.data).rld && window.navigation.reload())
${log ? `console.log('[Rollup-Plugin-Rld]: Reloaded...');` : ""}`;
document.head.appendChild(document.createComment(" Script injected by Rollup-Plugin-Rld "));
document.head.appendChild(script);
};
const rld = (init = {}) => {
const config = { port: 31415, host: "localhost", url: "/rld", log: false, ...init };
config.url = config.url[0] === "/" ? config.url : "/" + config.url;
const emitter = createEmitter();
let watch = false;
let server = null;
return {
name: "Rollup-Plugin-Rld",
async options(options) {
watch = !!options.watch;
},
async buildStart() {
if (server || !watch)
return;
server = createServer({ emitter, url: config.url });
server.listen(config.port, config.host);
server.on("error", (err) => {
console.log({ err });
});
},
async buildEnd() {
if (!watch)
return;
emitter.emit();
},
async closeWatcher() {
if (server)
server.close();
},
async transform(code, module) {
if (!watch || !this.getModuleInfo(module)?.isEntry)
return code;
return `(${injectable.toString()})(${config.port}, '${config.host}', '${config.url}', '${JSON.stringify(
config.attributes || {}
)}', ${config.log});
` + code;
}
};
};
exports.rld = rld;
//# sourceMappingURL=index.js.map