@swup/scripts-plugin
Version:
A swup plugin for re-evaluating scripts
3 lines (2 loc) • 999 B
JavaScript
function t(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var e=/*#__PURE__*/t(require("@swup/plugin"));module.exports=class extends e.default{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:o}=this.options,r=this.getScope({head:t,body:e});if(!r)return;const s=Array.from(r.querySelectorAll(o?"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:o,value:r}of t.attributes)e.setAttribute(o,r);return e.textContent=t.textContent,t.replaceWith(e),e}getScope(t){let{head:e,body:o}=t;return e&&o?document:e?document.head:o?document.body:null}};
//# sourceMappingURL=index.cjs.map