grind-assets
Version:
Asset management for Grind
1 lines • 3.4 kB
JavaScript
;function _isnil(val){return null==val}function Socket(referenceScript){if(!window.WebSocket)throw new Error("This browser does not support websockets, live reload will not work.");var listeners={},attempts=0,attemptsReset=null,pending=!1,socket=connect(),firstConnect=!0;function connect(){var protocol="https:"===window.location.protocol?"wss":"ws",socket=new WebSocket("".concat(protocol,"://").concat(window.location.host,"/@assets/socket"));return socket.onopen=function(){attemptsReset&&clearTimeout(attemptsReset),firstConnect&&!_isnil(referenceScript)&&referenceScript.hasAttribute("data-since")&&socket.send(JSON.stringify({type:"init",since:Number(referenceScript.getAttribute("data-since"))})),attemptsReset=setTimeout(function(){attempts=1},1e3),firstConnect=!1},socket.onclose=_reconnect.bind(null,"close"),socket.onerror=_reconnect.bind(null,"error"),socket.onmessage=function(message){message=JSON.parse(message.data),(listeners[message.type]||[]).forEach(function(listener){return listener(message.asset,message)})},socket}function _reconnect(){var delay;socket.readyState!==WebSocket.OPEN&&(pending||(pending=!0,delay=1e3*Math.min(30,Math.pow(2,attempts)-1),attemptsReset&&(clearTimeout(attemptsReset),attemptsReset=null),setTimeout(function(){attempts++,pending=!1,socket=connect()},delay)))}return{on:function(event,callback){Array.isArray(listeners[event])||(listeners[event]=[]),listeners[event].push(callback)}}}function cacheBust(url){return 0<=(url=url.replace(/(\?|&)?__ts=\d+/g,"")).indexOf("?")?url+="&":url+="?","".concat(url,"__ts=").concat(Date.now())}function CssReloader(pathname){for(var reload=[],i=0,length=document.styleSheets.length;i<length;i++){var sheet=document.styleSheets[i];-1!==findFiles$1(sheet).indexOf(pathname)&&reload.push(sheet)}for(var j=0,length2=reload.length;j<length2;j++)!function(j){var link=reload[j].ownerNode,replacement=link.cloneNode();replacement.href=cacheBust(replacement.href),replacement.addEventListener("load",function(){link.remove()},!1),replacement.addEventListener("error",function(){replacement.remove()},!1),link.parentNode.insertBefore(replacement,link)}(j)}function findFiles$1(stylesheet){for(var rules=stylesheet.cssRules||[],i=rules.length-1;0<=i;i--){var rule=rules[i];if("#__liveReloadModule"===rule.selectorText)return JSON.parse(JSON.parse(rule.style.content))}return[]}var attributeName="data-live-reload-module";function JsReloader(pathname){for(var scripts=getScripts(),i=0,length=scripts.length;i<length;i++)if(-1!==findFiles(scripts[i].getAttribute(attributeName)).indexOf(pathname)){window.location.reload();break}}function findFiles(name){return(window.__liveReloadModules||{})[name]||[]}function getScripts(){for(var results=[],scripts=document.getElementsByTagName("script"),i=0,length=scripts.length;i<length;i++){var script=scripts[i];script.hasAttribute(attributeName)&&results.push(script)}return results}function LiveReloader(context){context.socket.on("change",function(pathname){return 0<Object.keys(context.errors).length?window.location.reload():void(/(css|sass|less|stylus|styl)$/i.test(pathname)?CssReloader(pathname):/(js|jsx)$/i.test(pathname)&&JsReloader(pathname))})}var context={errors:{}};context.socket=Socket(document.currentScript),LiveReloader(context),context.socket.on("error",function(_,error){context.errors[error.id]=error}),window.$grindAssets=Object.freeze(context);