@ecopages/scripts-injector 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,24 +1,32 @@
1
1
  {
2
2
  "name": "@ecopages/scripts-injector",
3
3
  "main": "scripts-injector.js",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "type": "module",
6
6
  "types": "types.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ecopages/scripts-injector"
10
+ },
7
11
  "exports": {
8
12
  "./package.json": {
9
13
  "import": "./package.json"
10
14
  },
11
15
  ".": {
12
- "import": "./dist/scripts-injector.js",
13
- "require": "./dist/scripts-injector.js"
16
+ "import": "./scripts-injector.js",
17
+ "require": "./scripts-injector.js"
14
18
  },
15
19
  "./types": "./types.d.ts"
16
20
  },
17
21
  "scripts": {
18
- "build:lib": "rm -rf dist && bun build --entry scripts-injector.ts --outdir . --minify"
22
+ "build:lib": "rm -rf dist && bun build --entry scripts-injector.ts --outdir . --minify",
23
+ "test": "vitest",
24
+ "test:run": "vitest run"
19
25
  },
20
26
  "devDependencies": {
21
- "@types/bun": "latest"
27
+ "@types/bun": "latest",
28
+ "@vitest/browser-playwright": "^4.0.16",
29
+ "vitest": "^4.0.16"
22
30
  },
23
31
  "peerDependencies": {
24
32
  "typescript": "^5.0.0"
@@ -1 +1 @@
1
- var i;(function(t){t["DATA_LOADED"]="data-loaded"})(i||(i={}));var o=["on:visible","on:idle","on:interaction"];class n extends HTMLElement{intersectionObserver=null;scriptsToLoad=[];registeredEvents=[];conditionsMap={visible:this.onVisible.bind(this),idle:this.onIdle.bind(this),interaction:this.onInteraction.bind(this)};constructor(){super();this.loadScripts=this.loadScripts.bind(this),this.listenToDataLoaded=this.listenToDataLoaded.bind(this)}connectedCallback(){this.scriptsToLoad=this.getAttribute("scripts")?.split(",")||[],document.addEventListener(i.DATA_LOADED,this.listenToDataLoaded),this.applyConditions()}disconnectedCallback(){if(this.unregisterEvents(),this.intersectionObserver)this.intersectionObserver.disconnect()}notifyInjectors(){document.dispatchEvent(new CustomEvent(i.DATA_LOADED,{detail:{loadedScripts:this.scriptsToLoad}}))}applyConditions(){const e=Object.keys(this.conditionsMap);for(let t of e)if(this.hasAttribute(`on:${t}`))this.conditionsMap[t]()}onVisible(){return this.setupIntersectionObserver()}onIdle(){return this.loadScripts()}onInteraction(){const e=this.getAttribute("on:interaction");if(!e)return;for(let t of e.split(","))this.addEventListener(t,this.loadScripts),this.registeredEvents.push({type:t,listener:this.loadScripts})}listenToDataLoaded(e){if(this.hasAttribute("data-loaded"))return;const{loadedScripts:t}=e.detail;if(this.scriptsToLoad=this.scriptsToLoad.filter((s)=>!t.includes(s)),this.scriptsToLoad.length===0)this.setAttribute("data-loaded",""),this.unregisterEvents()}unregisterEvents(){document.dispatchEvent(new CustomEvent(i.DATA_LOADED,{detail:{loadedScripts:this.scriptsToLoad}})),this.intersectionObserver?.disconnect();for(let{type:e,listener:t}of this.registeredEvents)this.removeEventListener(e,t)}loadScripts(){try{for(let e of this.scriptsToLoad)this.loadScript(e)}catch(e){console.error("Error loading scripts",e)}finally{this.setAttribute("data-loaded",""),this.unregisterEvents(),this.notifyInjectors()}}loadScript(e){const t=document.createElement("script");t.src=e,t.type="module",document.head.appendChild(t)}setupIntersectionObserver(){const e={rootMargin:"50px 0px",threshold:0.1};this.intersectionObserver=new IntersectionObserver((t)=>{for(let s of t)if(s.isIntersecting)this.loadScripts()},e),this.intersectionObserver.observe(this)}}customElements.define("scripts-injector",n);export{o as conditions,n as ScriptsInjector,i as ScriptInjectorEvents};
1
+ var B;((k)=>k.DATA_LOADED="data-loaded")(B||={});var C=["on:visible","on:idle","on:interaction"];class w extends HTMLElement{intersectionObserver=null;scriptsToLoad=[];registeredEvents=[];conditionsMap={"on:visible":this.onVisible.bind(this),"on:idle":this.onIdle.bind(this),"on:interaction":this.onInteraction.bind(this)};constructor(){super();this.loadScripts=this.loadScripts.bind(this),this.listenToDataLoaded=this.listenToDataLoaded.bind(this)}connectedCallback(){let h=this.getAttribute("scripts");this.scriptsToLoad=h?h.split(",").map((k)=>k.trim()).filter(Boolean):[],document.addEventListener("data-loaded",this.listenToDataLoaded),this.applyConditions()}disconnectedCallback(){if(document.removeEventListener("data-loaded",this.listenToDataLoaded),this.unregisterEvents(),this.intersectionObserver)this.intersectionObserver.disconnect(),this.intersectionObserver=null}notifyInjectors(){document.dispatchEvent(new CustomEvent("data-loaded",{detail:{loadedScripts:this.scriptsToLoad}}))}applyConditions(){let h=Object.keys(this.conditionsMap);for(let k of h)if(this.hasAttribute(k))this.conditionsMap[k]()}onVisible(){this.setupIntersectionObserver()}onIdle(){this.loadScripts()}onInteraction(){let h=this.getAttribute("on:interaction");if(!h)return;for(let k of h.split(",")){let q=k.trim();if(q)this.addEventListener(q,this.loadScripts),this.registeredEvents.push({type:q,listener:this.loadScripts})}}listenToDataLoaded(h){if(this.hasAttribute("data-loaded"))return;let{loadedScripts:k}=h.detail;if(this.scriptsToLoad=this.scriptsToLoad.filter((q)=>!k.includes(q)),this.scriptsToLoad.length===0)this.setAttribute("data-loaded",""),this.unregisterEvents()}unregisterEvents(){this.intersectionObserver?.disconnect();for(let{type:h,listener:k}of this.registeredEvents)this.removeEventListener(h,k);this.registeredEvents=[]}loadScripts(){if(this.hasAttribute("data-loaded"))return;try{for(let h of this.scriptsToLoad)if(!this.isScriptLoaded(h))this.loadScript(h)}catch(h){console.error("[scripts-injector] Error loading scripts:",h)}finally{this.setAttribute("data-loaded",""),this.unregisterEvents(),this.notifyInjectors()}}isScriptLoaded(h){return document.querySelector(`script[src="${h}"]`)!==null}loadScript(h){let k=document.createElement("script");k.src=h,k.type="module",k.async=!0,k.onerror=(q)=>{console.error(`[scripts-injector] Failed to load script: ${h}`,q)},document.head.appendChild(k)}setupIntersectionObserver(){let h=this.getAttribute("on:visible"),q={rootMargin:h&&h!==""&&h!=="true"?h:"50px 0px",threshold:0.1};this.intersectionObserver=new IntersectionObserver((x)=>{for(let z of x)if(z.isIntersecting)this.loadScripts()},q),this.intersectionObserver.observe(this)}}if(!customElements.get("scripts-injector"))customElements.define("scripts-injector",w);export{C as conditions,w as ScriptsInjector,B as ScriptInjectorEvents};