@swup/scripts-plugin
Version:
A swup plugin for re-evaluating scripts
3 lines (2 loc) • 1.12 kB
JavaScript
import t from"@swup/plugin";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},e.apply(this,arguments)}class r extends t{constructor(t={}){super(),this.name="SwupScriptsPlugin",this.requires={swup:">=4"},this.defaults={head:!0,body:!0,optin:!1},this.options=void 0,this.options=e({},this.defaults,t)}mount(){this.on("content:replace",this.runScripts)}runScripts(){const{head:t,body:e,optin:r}=this.options,n=this.getScope({head:t,body:e});if(!n)return;const s=Array.from(n.querySelectorAll(r?"script[data-swup-reload-script]":"script:not([data-swup-ignore-script])"));s.forEach(t=>this.runScript(t)),this.swup.log(`Executed ${s.length} scripts.`)}runScript(t){const e=document.createElement("script");for(const{name:r,value:n}of t.attributes)e.setAttribute(r,n);return e.textContent=t.textContent,t.replaceWith(e),e}getScope({head:t,body:e}){return t&&e?document:t?document.head:e?document.body:null}}export{r as default};
//# sourceMappingURL=index.modern.js.map