├── README.md ├── index.ts └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # bun-livereload 2 | 3 | Wrap a function with `bun-livereload` to automatically reload any imports _inside_ the function the next time it is called. 4 | 5 | ```ts 6 | import liveReload from "bun-livereload"; 7 | 8 | export default { 9 | fetch: liveReload(async function (req: Request) { 10 | const { render } = await import("./my-page.js"); 11 | return new Response(render(req)); 12 | }), 13 | }; 14 | ``` 15 | 16 | ## Install 17 | 18 | ```bash 19 | bun add bun-livereload 20 | ``` 21 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Wrap a function with `bun-livereload` to automatically reload any imports 3 | * _inside_ the function the next time it is called. 4 | * @param callback The function to wrap. 5 | * @param log Whether to log the number of modules unloaded. 6 | * @returns The wrapped function. 7 | */ 8 | export function liveReload(callback: CallableFunction, log = false) { 9 | var registry = new Map([...Loader.registry.entries()]); 10 | function runWithLog() { 11 | if (Loader.registry.size !== registry.size) { 12 | var count = 0; 13 | for (let key of Loader.registry.keys()) { 14 | if (!registry.has(key)) { 15 | count++; 16 | Loader.registry.delete(key); 17 | } 18 | } 19 | if (count > 0) { 20 | console.info(`[bun-livereload] ${count} modules unloaded`); 21 | } 22 | } 23 | } 24 | 25 | function runWithoutLog() { 26 | if (Loader.registry.size !== registry.size) { 27 | for (let key of Loader.registry.keys()) { 28 | if (!registry.has(key)) { 29 | Loader.registry.delete(key); 30 | } 31 | } 32 | } 33 | } 34 | 35 | var reload = log ? runWithLog : runWithoutLog; 36 | 37 | const isAsync = callback?.constructor?.name === "AsyncFunction"; 38 | 39 | if (isAsync) { 40 | return async (...args) => { 41 | try { 42 | return await callback(...args); 43 | } catch (e) { 44 | throw e; 45 | } finally { 46 | reload(); 47 | } 48 | }; 49 | } else { 50 | return (...args) => { 51 | try { 52 | return callback(...args); 53 | } catch (e) { 54 | throw e; 55 | } finally { 56 | reload(); 57 | } 58 | }; 59 | } 60 | } 61 | export default liveReload; 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bun-livereload", 3 | "version": "1.0.2", 4 | "module": "index.ts", 5 | "main": "index.ts", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Jarred Sumner", 9 | "email": "jarred@jarredsumner.com" 10 | }, 11 | "description": "Livereload for Bun.js", 12 | "keywords": [ 13 | "bun", 14 | "bun.js", 15 | "livereload", 16 | "hmr", 17 | "reload", 18 | "live" 19 | ], 20 | "exports": { 21 | "bun": "./index.ts" 22 | } 23 | } 24 | --------------------------------------------------------------------------------