@stagepass/loader 1.1.1 → 1.1.3

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/dist/loader.js CHANGED
@@ -110,7 +110,7 @@ var Stagepass = (() => {
110
110
  env,
111
111
  domain: dom || hostname,
112
112
  timestamp: sessionStartTime,
113
- version: "1.1.0"
113
+ version: "1.1.2"
114
114
  });
115
115
  let readyResolve = null;
116
116
  let readyReject = null;
@@ -172,30 +172,67 @@ var Stagepass = (() => {
172
172
  }
173
173
  const modulesToLoad = modulesParam === "" ? ["all"] : modulesParam.split(",").map((m) => m.trim()).filter(Boolean);
174
174
  if (modulesToLoad.length === 0) return;
175
- let baseUrl = window.location.origin;
176
- if (loaderScript && loaderScript.src) {
177
- try {
178
- const scriptUrl = new URL(loaderScript.src);
179
- const pathParts = scriptUrl.pathname.split("/");
180
- pathParts.pop();
181
- baseUrl = `${scriptUrl.origin}${pathParts.join("/")}`;
182
- } catch (e) {
183
- baseUrl = loaderScript.src.replace(/\/[^\/\?]+(\?.*)?$/, "");
175
+ let baseUrl;
176
+ const existingModuleScripts = document.querySelectorAll('script[src*="modules"], script[src*="all.min.js"], script[src*="inject.min.js"]');
177
+ let useLocalModules = false;
178
+ for (const script of Array.from(existingModuleScripts)) {
179
+ const src = script.src;
180
+ if (src && !src.includes("loader.min.js") && (src.includes("modules.sp") || src.includes("localhost"))) {
181
+ try {
182
+ const scriptUrl = new URL(src);
183
+ const pathParts = scriptUrl.pathname.split("/");
184
+ pathParts.pop();
185
+ baseUrl = `${scriptUrl.origin}${pathParts.join("/")}`;
186
+ useLocalModules = true;
187
+ if (env !== "production") {
188
+ splog(`\u{1F4E6} Using locally hosted modules from: ${baseUrl}`);
189
+ }
190
+ break;
191
+ } catch (e) {
192
+ }
193
+ }
194
+ }
195
+ if (!useLocalModules) {
196
+ let version = "@1";
197
+ if (loaderScript && loaderScript.src) {
198
+ try {
199
+ const versionMatch = loaderScript.src.match(/@stagepass\/loader(@[^\/\?]+)/);
200
+ if (versionMatch) {
201
+ version = versionMatch[1];
202
+ }
203
+ } catch (e) {
204
+ }
205
+ }
206
+ baseUrl = `https://cdn.jsdelivr.net/npm/@stagepass/modules${version}/dist`;
207
+ if (env !== "production") {
208
+ splog(`\u{1F4E6} Loading modules from CDN: ${baseUrl}`);
184
209
  }
185
210
  }
186
211
  let currentIndex = 0;
212
+ let hasErrors = false;
187
213
  function loadNext() {
188
214
  if (currentIndex >= modulesToLoad.length) {
189
- if (env !== "production") {
190
- splog("\u2705 All modules loaded");
191
- }
192
- if (readyResolve) {
193
- readyResolve();
215
+ if (hasErrors) {
216
+ if (env !== "production") {
217
+ sperror("\u26A0\uFE0F Some modules failed to load");
218
+ }
219
+ if (readyReject) {
220
+ readyReject(new Error("One or more modules failed to load"));
221
+ }
222
+ } else {
223
+ if (env !== "production") {
224
+ splog("\u2705 All modules loaded");
225
+ }
226
+ if (readyResolve) {
227
+ readyResolve();
228
+ }
194
229
  }
195
230
  return;
196
231
  }
197
232
  const moduleName = modulesToLoad[currentIndex];
198
- const moduleUrl = `${baseUrl}/${moduleName}.min.js`;
233
+ const isLocalModule = baseUrl.includes(".sp") || baseUrl.includes("localhost");
234
+ const cacheBustParam = swap && isLocalModule ? `?_cb=${cacheBust}` : "";
235
+ const moduleUrl = `${baseUrl}/${moduleName}.min.js${cacheBustParam}`;
199
236
  const script = document.createElement("script");
200
237
  script.onload = () => {
201
238
  if (env !== "production") {
@@ -205,8 +242,9 @@ var Stagepass = (() => {
205
242
  loadNext();
206
243
  };
207
244
  script.onerror = () => {
245
+ hasErrors = true;
208
246
  if (env !== "production") {
209
- sperror(`Failed to load module: ${moduleName}`);
247
+ sperror(`Failed to load module: ${moduleName} from ${moduleUrl}`);
210
248
  }
211
249
  currentIndex++;
212
250
  loadNext();
@@ -1,2 +1,2 @@
1
- var Stagepass=(()=>{(function(){let m="stagepass_domain",v="stagepass",A=t=>t==="debug"||t.endsWith(".sp")||t==="localhost"||t.startsWith("localhost:"),b=new URLSearchParams(window.location.search),l=document.querySelector('script[src*="loader"]'),y=null,M=null;if(l&&l.src)try{let t=new URL(l.src);y=t.searchParams.get("modules"),M=t.searchParams.get("silent")}catch{let s=l.src.match(/[?&]modules(=([^&]*))?/);s&&(y=s[2]!==void 0?s[2]:""),l.src.match(/[?&]silent/)&&(M="")}function P(t,...s){var r;if(!localStorage.getItem(m))return;let d=new Date().toTimeString().split(" ")[0],n="";try{let u=(r=new Error().stack)==null?void 0:r.split(`
2
- `);if(u){let w=u.find((i,g)=>g>2&&!i.includes("log"));if(w){let i=w.match(/:([0-9]+):/);i&&(n=`:${i[1]}`)}}}catch{}let e=`[\u{1F3AB} ${d}${n?" \u{1F4CD}"+n:""}]`;t==="error"?console.log(`%c${e}`,"color: red; font-weight: bold;",...s):console[t]?console[t](e,...s):console.log(e,t,...s)}let p=(...t)=>P("log",...t),k=(...t)=>P("warn",...t),T=(...t)=>P("error",...t);window.splog=p,window.spwarn=k,window.sperror=T;let x=t=>t?t.replace(/^https?:\/\//,"").replace(/\/$/,""):null,C=!1,a=localStorage.getItem(m);if(b.has(v)){let t=b.get(v);if(t==="off"||t==="0"||t==="false")localStorage.removeItem(m),a=null,p("\u{1F50C} Stagepass disconnected.");else if(t){let o=t==="debug"?"debug":x(t);o&&A(o)?(localStorage.setItem(m,o),a=o,p(o==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${o}`),C=!0):o&&k(`Invalid domain: ${o}. Only .sp domains are allowed.`)}b.delete(v);let s=b.toString();window.history.replaceState({},"",window.location.pathname+(s?`?${s}`:"")+window.location.hash)}a&&!A(a)&&(localStorage.removeItem(m),a=null),a&&!C&&a!=="debug"&&p(`\u{1F50C} Stagepass connected to: ${a}`);let R=!!a,S=!!a&&a!=="debug",E=S?x(a):null,I=S?Date.now():0,W=I,B=I,f=window.location.hostname,N=f==="localhost"||f==="127.0.0.1"||f.startsWith("localhost:")||f.startsWith("127.0.0.1:"),U=S||N,h=U?"local":f.endsWith(".webflow.io")?"staging":"production";!(a&&A(a))&&!b.has(v)&&(h==="production"||h==="staging"&&M!==null)&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});let L=window.stagepass||{};L.vars=Object.freeze({isLocal:U,env:h,domain:E||f,timestamp:B,version:"1.1.0"});let $=null,H=null,V=new Promise((t,s)=>{$=t,H=s});L.ready=V,window.stagepass=L,globalThis.stagepass=L;function j(t,s,o,d,n){document.querySelectorAll(`${t}:not([data-stagepass-processed])`).forEach(e=>{e.setAttribute("data-stagepass-processed","true");let r=e.getAttribute(s),u=e.getAttribute(o);if(!u)return;if(!r&&u&&S&&E)try{let c=new URL(u).pathname.split("/");r=c[c.length-1]||c[c.length-2]}catch{let c=u.match(/\/([^\/\?#]+)(?:\?|#|$)/);c&&(r=c[1])}let w=S&&r&&E?`https://${E}/${r.startsWith("/")?r.substring(1):r}?_cb=${W}`:u;if(!w)return;let i=document.createElement(d);d==="script"?(i.src=w,Array.from(e.attributes).forEach(g=>{let c=g;n.includes(c.name)||i.setAttribute(c.name,c.value)}),!e.hasAttribute("async")&&!e.hasAttribute("defer")&&i.setAttribute("defer","")):(i.rel="stylesheet",i.href=w,Array.from(e.attributes).forEach(g=>{let c=g;n.includes(c.name)||i.setAttribute(c.name,c.value)})),e.parentNode?(e.parentNode.insertBefore(i,e),e.remove()):T(`${d} injection failed: No parent node`)})}function F(){if(y===null){$&&$();return}let t=y===""?["all"]:y.split(",").map(n=>n.trim()).filter(Boolean);if(t.length===0)return;let s=window.location.origin;if(l&&l.src)try{let n=new URL(l.src),e=n.pathname.split("/");e.pop(),s=`${n.origin}${e.join("/")}`}catch{s=l.src.replace(/\/[^\/\?]+(\?.*)?$/,"")}let o=0;function d(){if(o>=t.length){h!=="production"&&p("\u2705 All modules loaded"),$&&$();return}let n=t[o],e=`${s}/${n}.min.js`,r=document.createElement("script");r.onload=()=>{h!=="production"&&p(`\u{1F4E6} Module loaded: ${n}`),o++,d()},r.onerror=()=>{h!=="production"&&T(`Failed to load module: ${n}`),o++,d()},r.src=e,document.head.appendChild(r)}d()}let D=()=>{j("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path"]),j('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path"])};F(),D(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",D);function O(){if(!R)return;let t=document.createElement("div");t.id="stagepass-flag",t.textContent=a==="debug"?"STAGEPASS DEBUG":"STAGEPASS ACTIVE",t.style.cssText='font:12px/1 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;font-weight:bold;color:#fff;background:#D92662;padding:4px 6px;z-index:9999999999;position:fixed;top:0;right:0;cursor:pointer;border-radius:0 0 0 4px;user-select:none;',t.addEventListener("click",()=>{localStorage.removeItem(m),p("\u{1F50C} Stagepass disconnected."),window.location.reload()}),document.body?document.body.appendChild(t):document.addEventListener("DOMContentLoaded",()=>{document.body&&document.body.appendChild(t)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",O):O()})();})();
1
+ var Stagepass=(()=>{(function(){let h="stagepass_domain",v="stagepass",M=t=>t==="debug"||t.endsWith(".sp")||t==="localhost"||t.startsWith("localhost:"),y=new URLSearchParams(window.location.search),f=document.querySelector('script[src*="loader"]'),S=null,P=null;if(f&&f.src)try{let t=new URL(f.src);S=t.searchParams.get("modules"),P=t.searchParams.get("silent")}catch{let e=f.src.match(/[?&]modules(=([^&]*))?/);e&&(S=e[2]!==void 0?e[2]:""),f.src.match(/[?&]silent/)&&(P="")}function j(t,...e){var i;if(!localStorage.getItem(h))return;let g=new Date().toTimeString().split(" ")[0],u="";try{let s=(i=new Error().stack)==null?void 0:i.split(`
2
+ `);if(s){let a=s.find((r,d)=>d>2&&!r.includes("log"));if(a){let r=a.match(/:([0-9]+):/);r&&(u=`:${r[1]}`)}}}catch{}let n=`[\u{1F3AB} ${g}${u?" \u{1F4CD}"+u:""}]`;t==="error"?console.log(`%c${n}`,"color: red; font-weight: bold;",...e):console[t]?console[t](n,...e):console.log(n,t,...e)}let p=(...t)=>j("log",...t),k=(...t)=>j("warn",...t),E=(...t)=>j("error",...t);window.splog=p,window.spwarn=k,window.sperror=E;let x=t=>t?t.replace(/^https?:\/\//,"").replace(/\/$/,""):null,C=!1,c=localStorage.getItem(h);if(y.has(v)){let t=y.get(v);if(t==="off"||t==="0"||t==="false")localStorage.removeItem(h),c=null,p("\u{1F50C} Stagepass disconnected.");else if(t){let l=t==="debug"?"debug":x(t);l&&M(l)?(localStorage.setItem(h,l),c=l,p(l==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${l}`),C=!0):l&&k(`Invalid domain: ${l}. Only .sp domains are allowed.`)}y.delete(v);let e=y.toString();window.history.replaceState({},"",window.location.pathname+(e?`?${e}`:"")+window.location.hash)}c&&!M(c)&&(localStorage.removeItem(h),c=null),c&&!C&&c!=="debug"&&p(`\u{1F50C} Stagepass connected to: ${c}`);let N=!!c,w=!!c&&c!=="debug",L=w?x(c):null,U=w?Date.now():0,I=U,W=U,b=window.location.hostname,H=b==="localhost"||b==="127.0.0.1"||b.startsWith("localhost:")||b.startsWith("127.0.0.1:"),D=w||H,m=D?"local":b.endsWith(".webflow.io")?"staging":"production";!(c&&M(c))&&!y.has(v)&&(m==="production"||m==="staging"&&P!==null)&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});let A=window.stagepass||{};A.vars=Object.freeze({isLocal:D,env:m,domain:L||b,timestamp:W,version:"1.1.2"});let $=null,T=null,V=new Promise((t,e)=>{$=t,T=e});A.ready=V,window.stagepass=A,globalThis.stagepass=A;function O(t,e,l,g,u){document.querySelectorAll(`${t}:not([data-stagepass-processed])`).forEach(n=>{n.setAttribute("data-stagepass-processed","true");let i=n.getAttribute(e),s=n.getAttribute(l);if(!s)return;if(!i&&s&&w&&L)try{let o=new URL(s).pathname.split("/");i=o[o.length-1]||o[o.length-2]}catch{let o=s.match(/\/([^\/\?#]+)(?:\?|#|$)/);o&&(i=o[1])}let a=w&&i&&L?`https://${L}/${i.startsWith("/")?i.substring(1):i}?_cb=${I}`:s;if(!a)return;let r=document.createElement(g);g==="script"?(r.src=a,Array.from(n.attributes).forEach(d=>{let o=d;u.includes(o.name)||r.setAttribute(o.name,o.value)}),!n.hasAttribute("async")&&!n.hasAttribute("defer")&&r.setAttribute("defer","")):(r.rel="stylesheet",r.href=a,Array.from(n.attributes).forEach(d=>{let o=d;u.includes(o.name)||r.setAttribute(o.name,o.value)})),n.parentNode?(n.parentNode.insertBefore(r,n),n.remove()):E(`${g} injection failed: No parent node`)})}function q(){if(S===null){$&&$();return}let t=S===""?["all"]:S.split(",").map(s=>s.trim()).filter(Boolean);if(t.length===0)return;let e,l=document.querySelectorAll('script[src*="modules"], script[src*="all.min.js"], script[src*="inject.min.js"]'),g=!1;for(let s of Array.from(l)){let a=s.src;if(a&&!a.includes("loader.min.js")&&(a.includes("modules.sp")||a.includes("localhost")))try{let r=new URL(a),d=r.pathname.split("/");d.pop(),e=`${r.origin}${d.join("/")}`,g=!0,m!=="production"&&p(`\u{1F4E6} Using locally hosted modules from: ${e}`);break}catch{}}if(!g){let s="@1";if(f&&f.src)try{let a=f.src.match(/@stagepass\/loader(@[^\/\?]+)/);a&&(s=a[1])}catch{}e=`https://cdn.jsdelivr.net/npm/@stagepass/modules${s}/dist`,m!=="production"&&p(`\u{1F4E6} Loading modules from CDN: ${e}`)}let u=0,n=!1;function i(){if(u>=t.length){n?(m!=="production"&&E("\u26A0\uFE0F Some modules failed to load"),T&&T(new Error("One or more modules failed to load"))):(m!=="production"&&p("\u2705 All modules loaded"),$&&$());return}let s=t[u],a=e.includes(".sp")||e.includes("localhost"),r=w&&a?`?_cb=${I}`:"",d=`${e}/${s}.min.js${r}`,o=document.createElement("script");o.onload=()=>{m!=="production"&&p(`\u{1F4E6} Module loaded: ${s}`),u++,i()},o.onerror=()=>{n=!0,m!=="production"&&E(`Failed to load module: ${s} from ${d}`),u++,i()},o.src=d,document.head.appendChild(o)}i()}let R=()=>{O("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path"]),O('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path"])};q(),R(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",R);function B(){if(!N)return;let t=document.createElement("div");t.id="stagepass-flag",t.textContent=c==="debug"?"STAGEPASS DEBUG":"STAGEPASS ACTIVE",t.style.cssText='font:12px/1 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;font-weight:bold;color:#fff;background:#D92662;padding:4px 6px;z-index:9999999999;position:fixed;top:0;right:0;cursor:pointer;border-radius:0 0 0 4px;user-select:none;',t.addEventListener("click",()=>{localStorage.removeItem(h),p("\u{1F50C} Stagepass disconnected."),window.location.reload()}),document.body?document.body.appendChild(t):document.addEventListener("DOMContentLoaded",()=>{document.body&&document.body.appendChild(t)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",B):B()})();})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stagepass/loader",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Universal loader for Stagepass - injects local files into Webflow projects",
5
5
  "main": "dist/loader.min.js",
6
6
  "type": "module",