UNPKG

@swup/scripts-plugin

Version:

A swup plugin for re-evaluating scripts

3 lines (2 loc) 914 B
import t from"@swup/plugin";class e extends t{constructor(t){void 0===t&&(t={}),super(),this.name="SwupScriptsPlugin",this.requires={swup:">=4"},this.defaults={head:!0,body:!0,optin:!1},this.options=void 0,this.options={...this.defaults,...t}}mount(){this.on("content:replace",this.runScripts)}runScripts(){const{head:t,body:e,optin:s}=this.options,o=this.getScope({head:t,body:e});if(!o)return;const r=Array.from(o.querySelectorAll(s?"script[data-swup-reload-script]":"script:not([data-swup-ignore-script])"));r.forEach(t=>this.runScript(t)),this.swup.log(`Executed ${r.length} scripts.`)}runScript(t){const e=document.createElement("script");for(const{name:s,value:o}of t.attributes)e.setAttribute(s,o);return e.textContent=t.textContent,t.replaceWith(e),e}getScope(t){let{head:e,body:s}=t;return e&&s?document:e?document.head:s?document.body:null}}export{e as default}; //# sourceMappingURL=index.module.js.map