UNPKG

htmelt

Version:

Bundle your HTML assets with Esbuild and LightningCSS. Custom plugins, HMR platform, and more.

86 lines (84 loc) 2.25 kB
import { toBundleInputs } from "./chunk-4FEH55TE.mjs"; import "./chunk-Q2H5B2YZ.mjs"; import { updateRelatedWatcher } from "./chunk-QYCXBBSD.mjs"; import "./chunk-SE5MUBQP.mjs"; import "./chunk-26E6E5EJ.mjs"; import "./chunk-SGZXFKQT.mjs"; // src/plugins/liveBundles.mts import path from "path"; var liveBundlesPlugin = (config) => { let dirtyBundles = /* @__PURE__ */ new Set(); let pendingBundles = {}; config.watcher.on("change", (file) => { if (!config.bundles) { return; } file = path.relative(process.cwd(), file); for (const bundle of Object.values(config.bundles)) { if (file in bundle.metafile.inputs) { dirtyBundles.add(bundle); } } }); async function rebundle(bundle) { return pendingBundles[bundle.id] ||= bundle.context.rebuild().then( ({ metafile }) => { updateRelatedWatcher( config.relatedWatcher, metafile, bundle.metafile ); bundle.metafile = metafile; bundle.inputs = toBundleInputs(metafile); dirtyBundles.delete(bundle); }, (error) => { console.error( 'Error rebuilding bundle "%s": %s', bundle.id, error.message ); } ).then(() => { delete pendingBundles[bundle.id]; }); } return { document(document) { for (const script of document.scripts) { if (!script.isModule) { continue; } script.srcAttr.value = new URL( script.srcAttr.value, config.server.url ).href; } }, async fullReload() { await Promise.all(Array.from(dirtyBundles, rebundle)); }, async serve(req) { const uri = req.pathname; const file = uri.startsWith("/@fs/") ? uri.slice(4) : path.join(process.cwd(), uri); const fileRelativeToRoot = path.relative(process.cwd(), file); if (!fileRelativeToRoot.startsWith("..")) { await Promise.all( Array.from(dirtyBundles, async (bundle) => { if (!(fileRelativeToRoot in bundle.metafile.outputs)) { return; } await rebundle(bundle); }) ); } } }; }; export { liveBundlesPlugin };