UNPKG

3.24 kBJavaScriptView Raw
1function g(s){let t;return s?(t=s.toIterable)?t.call(s):s:[]}var j=Symbol.for("#__initor__"),S=Symbol.for("#__inited__"),O=Symbol.for("#type"),w=Symbol.for("#__listeners__");function m(s,t=null){let n={},o=/\$\$\d+\$\$/,l=function(r){return n[r]||(n[r]=t?t(r):{})},c=function(r,i){if(typeof i=="string"){if(i[0]=="$"&&o.test(i))return l(i)}else if(typeof r=="string"&&r[0]=="$"&&o.test(r)){let a=l(r);return Object.assign(a,i),a}return i};return JSON.parse(s,c)}function y(s,t){var n,o,l,c;let f={},r={added:[],changed:[],removed:[],all:[],urls:{}};if(s.assets)for(let i=0,a=g(s.assets),h=a.length;i<h;i++){let e=a[i],u=e.originalPath||e.path;f[u]=e,e.url&&((n=t.urls)[o=e.url]||(n[o]=e))}for(let i=0,a=g(t.assets||[]),h=a.length;i<h;i++){let e=a[i],u=e.originalPath||e.path,p=f[u];e.url&&s.urls&&(s.urls[e.url]=e),p?(p.hash!=e.hash&&(p.invalidated=Date.now(),p.replacedBy=e,e.replaces=p,r.changed.push(e),r.all.push(e),p==s.main&&(r.main=e)),l=f[u],delete f[u]):(r.added.push(e),r.all.push(e))}for(let i=0,a=Object.keys(f),h=a.length,e,u;i<h;i++)e=a[i],u=f[e],u.removed=Date.now(),r.all.push(u);for(let i=0,a=g(r.all),h=a.length;i<h;i++){let e=a[i];(r[c=e.type]||(r[c]=[])).push(e)}return r.removed=Object.values(f),t.changes=r,t}function _(s){let t;return s?(t=s.toIterable)?t.call(s):s:[]}var N=Symbol.for("#__initor__"),D=Symbol.for("#__inited__"),d=globalThis.document,x=class{constructor(){this.data={}}get changes(){return this.data.changes||{}}get inputs(){return this.data.inputs}get outputs(){return this.data.outputs}get urls(){return this.data.urls}get main(){return this.data.main}init(t){return this.update(t)}update(t){return typeof t=="string"&&(t=m(t)),this.data=y(this.data,t),this.data.changes}},b=class{constructor(){this.start(),this.manifest=new x({}),this.debug=!1}log(...t){if(!!this.debug)return console.log(...t)}refresh(t){let n={css:[],js:[]};for(let o of _(d.styleSheets)){let l,c=o.ownerNode.getAttribute("href");(l=this.manifest.urls[c])&&l.replacedBy&&(o.ownerNode.href=l.replacedBy.url)}for(let o of _(d.querySelectorAll("script[src]"))){let l;(l=this.manifest.urls[o.getAttribute("src")])&&l.replacedBy&&n.js.push(l)}return n.js.length&&(this.log("js changed - reload?",n.js),d.location.reload()),this}start(){var t=this;if(!this.socket)return this.socket=new EventSource("/__hmr__"),t.socket.onmessage=function(n){return t.log("sse.onmessage",n)},t.socket.addEventListener("paused",function(n){return t.log("server paused"),!0}),t.socket.addEventListener("state",function(n){let o=JSON.parse(n.data);return t.log("server state",o)}),t.socket.addEventListener("init",function(n){let o=JSON.parse(n.data);return t.manifest.init(o),t.log("hmr init",t.manifest.data)}),t.socket.addEventListener("errors",function(n){let o=JSON.parse(n.data);for(let l=0,c=_(o),f=c.length;l<f;l++){let r=c[l];console.error("error in "+r.location.file+": "+r.location.lineText+" ("+r.text+")")}}),t.socket.addEventListener("manifest",function(n){let o=JSON.parse(n.data),l=t.manifest.update(o);return t.refresh(l)}),t.socket.addEventListener("reload",function(n){return t.log("asked to reload by server"),d.location.reload()}),t.socket.onerror=function(n){return t.log("hmr disconnected",n)}}};globalThis.imba_devtools=new b;