@stagepass/loader 1.1.3 → 1.1.4
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 +49 -133
- package/dist/loader.min.js +2 -2
- package/package.json +1 -1
package/dist/loader.js
CHANGED
|
@@ -5,43 +5,30 @@ var Stagepass = (() => {
|
|
|
5
5
|
const P = "stagepass";
|
|
6
6
|
const isValidDomain = (d) => d === "debug" || d.endsWith(".sp") || d === "localhost" || d.startsWith("localhost:");
|
|
7
7
|
const p = new URLSearchParams(window.location.search);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let silentParam = null;
|
|
11
|
-
if (loaderScript && loaderScript.src) {
|
|
8
|
+
function getCallerLocation() {
|
|
9
|
+
var _a;
|
|
12
10
|
try {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
silentParam = "";
|
|
11
|
+
const stack = (_a = new Error().stack) == null ? void 0 : _a.split("\n");
|
|
12
|
+
if (!stack) return "";
|
|
13
|
+
const loaderPattern = /loader(?:\.min)?\.js/i;
|
|
14
|
+
for (let i = 3; i < stack.length; i++) {
|
|
15
|
+
const line = stack[i];
|
|
16
|
+
const match = line.match(/([^/]+\.(?:js|ts))(?:[?#][^:]*)?:(\d+)/);
|
|
17
|
+
if (match) {
|
|
18
|
+
const file = match[1];
|
|
19
|
+
if (!loaderPattern.test(file)) return ` ${file}:${match[2]}`;
|
|
20
|
+
}
|
|
24
21
|
}
|
|
22
|
+
} catch (_) {
|
|
25
23
|
}
|
|
24
|
+
return "";
|
|
26
25
|
}
|
|
27
26
|
function log(level, ...args) {
|
|
28
|
-
var _a;
|
|
29
27
|
const v = localStorage.getItem(K);
|
|
30
28
|
if (!v) return;
|
|
31
29
|
const t = (/* @__PURE__ */ new Date()).toTimeString().split(" ")[0];
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const s = (_a = new Error().stack) == null ? void 0 : _a.split("\n");
|
|
35
|
-
if (s) {
|
|
36
|
-
const l = s.find((x, i) => i > 2 && !x.includes("log"));
|
|
37
|
-
if (l) {
|
|
38
|
-
const m = l.match(/:([0-9]+):/);
|
|
39
|
-
if (m) c = `:${m[1]}`;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} catch (e) {
|
|
43
|
-
}
|
|
44
|
-
const pre = `[\u{1F3AB} ${t}${c ? " \u{1F4CD}" + c : ""}]`;
|
|
30
|
+
const loc = getCallerLocation();
|
|
31
|
+
const pre = `[\u{1F3AB} ${t}${loc ? " |" + loc : ""}]`;
|
|
45
32
|
if (level === "error") {
|
|
46
33
|
console.log(`%c${pre}`, "color: red; font-weight: bold;", ...args);
|
|
47
34
|
} else if (console[level]) {
|
|
@@ -56,6 +43,16 @@ var Stagepass = (() => {
|
|
|
56
43
|
window.splog = splog;
|
|
57
44
|
window.spwarn = spwarn;
|
|
58
45
|
window.sperror = sperror;
|
|
46
|
+
function spLog(...args) {
|
|
47
|
+
let level = "log";
|
|
48
|
+
if (args.length > 0 && (args[0] === "warn" || args[0] === "error")) {
|
|
49
|
+
level = args[0];
|
|
50
|
+
args = args.slice(1);
|
|
51
|
+
}
|
|
52
|
+
log(level, ...args);
|
|
53
|
+
}
|
|
54
|
+
const spWarn = (...args) => log("warn", ...args);
|
|
55
|
+
const spError = (...args) => log("error", ...args);
|
|
59
56
|
const clean = (d) => d ? d.replace(/^https?:\/\//, "").replace(/\/$/, "") : null;
|
|
60
57
|
let paramProcessed = false;
|
|
61
58
|
let sv = localStorage.getItem(K);
|
|
@@ -99,7 +96,7 @@ var Stagepass = (() => {
|
|
|
99
96
|
const env = isLocal ? "local" : hostname.endsWith(".webflow.io") ? "staging" : "production";
|
|
100
97
|
const hasValidSession = sv && isValidDomain(sv);
|
|
101
98
|
const shouldSuppress = !hasValidSession && !p.has(P);
|
|
102
|
-
if (shouldSuppress &&
|
|
99
|
+
if (shouldSuppress && env === "production") {
|
|
103
100
|
const noop = () => {
|
|
104
101
|
};
|
|
105
102
|
console.log = console.warn = console.error = console.info = console.debug = noop;
|
|
@@ -110,17 +107,21 @@ var Stagepass = (() => {
|
|
|
110
107
|
env,
|
|
111
108
|
domain: dom || hostname,
|
|
112
109
|
timestamp: sessionStartTime,
|
|
113
|
-
version: "1.1.
|
|
114
|
-
});
|
|
115
|
-
let readyResolve = null;
|
|
116
|
-
let readyReject = null;
|
|
117
|
-
const readyPromise = new Promise((resolve, reject) => {
|
|
118
|
-
readyResolve = resolve;
|
|
119
|
-
readyReject = reject;
|
|
110
|
+
version: "1.1.4"
|
|
120
111
|
});
|
|
121
|
-
stagepassObj.
|
|
112
|
+
stagepassObj.log = spLog;
|
|
113
|
+
stagepassObj.warn = spWarn;
|
|
114
|
+
stagepassObj.error = spError;
|
|
115
|
+
stagepassObj.resolveLocalUrl = (relativePath) => {
|
|
116
|
+
const v = stagepassObj.vars;
|
|
117
|
+
if (!v.isLocal || !v.domain) return "";
|
|
118
|
+
const clean2 = relativePath.startsWith("/") ? relativePath.substring(1) : relativePath;
|
|
119
|
+
return clean2 ? `https://${v.domain}/${clean2}?_cb=${v.timestamp}` : "";
|
|
120
|
+
};
|
|
122
121
|
window.stagepass = stagepassObj;
|
|
123
122
|
globalThis.stagepass = stagepassObj;
|
|
123
|
+
window.sp = stagepassObj;
|
|
124
|
+
globalThis.sp = stagepassObj;
|
|
124
125
|
function swapEl(sel, pathAttr, srcAttr, type, skipAttrs) {
|
|
125
126
|
document.querySelectorAll(`${sel}:not([data-stagepass-processed])`).forEach((el) => {
|
|
126
127
|
el.setAttribute("data-stagepass-processed", "true");
|
|
@@ -137,7 +138,14 @@ var Stagepass = (() => {
|
|
|
137
138
|
if (match) lp = match[1];
|
|
138
139
|
}
|
|
139
140
|
}
|
|
140
|
-
|
|
141
|
+
let url;
|
|
142
|
+
if (swap && lp && dom) {
|
|
143
|
+
url = `https://${dom}/${lp.startsWith("/") ? lp.substring(1) : lp}?_cb=${cacheBust}`;
|
|
144
|
+
} else if (el.getAttribute("data-stagepass-cachebust") === "true") {
|
|
145
|
+
url = ps + (ps.includes("?") ? "&" : "?") + "_cb=" + Date.now();
|
|
146
|
+
} else {
|
|
147
|
+
url = ps;
|
|
148
|
+
}
|
|
141
149
|
if (!url) return;
|
|
142
150
|
const n = document.createElement(type);
|
|
143
151
|
if (type === "script") {
|
|
@@ -163,102 +171,10 @@ var Stagepass = (() => {
|
|
|
163
171
|
}
|
|
164
172
|
});
|
|
165
173
|
}
|
|
166
|
-
function loadModules() {
|
|
167
|
-
if (modulesParam === null) {
|
|
168
|
-
if (readyResolve) {
|
|
169
|
-
readyResolve();
|
|
170
|
-
}
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const modulesToLoad = modulesParam === "" ? ["all"] : modulesParam.split(",").map((m) => m.trim()).filter(Boolean);
|
|
174
|
-
if (modulesToLoad.length === 0) return;
|
|
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}`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
let currentIndex = 0;
|
|
212
|
-
let hasErrors = false;
|
|
213
|
-
function loadNext() {
|
|
214
|
-
if (currentIndex >= modulesToLoad.length) {
|
|
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
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
const moduleName = modulesToLoad[currentIndex];
|
|
233
|
-
const isLocalModule = baseUrl.includes(".sp") || baseUrl.includes("localhost");
|
|
234
|
-
const cacheBustParam = swap && isLocalModule ? `?_cb=${cacheBust}` : "";
|
|
235
|
-
const moduleUrl = `${baseUrl}/${moduleName}.min.js${cacheBustParam}`;
|
|
236
|
-
const script = document.createElement("script");
|
|
237
|
-
script.onload = () => {
|
|
238
|
-
if (env !== "production") {
|
|
239
|
-
splog(`\u{1F4E6} Module loaded: ${moduleName}`);
|
|
240
|
-
}
|
|
241
|
-
currentIndex++;
|
|
242
|
-
loadNext();
|
|
243
|
-
};
|
|
244
|
-
script.onerror = () => {
|
|
245
|
-
hasErrors = true;
|
|
246
|
-
if (env !== "production") {
|
|
247
|
-
sperror(`Failed to load module: ${moduleName} from ${moduleUrl}`);
|
|
248
|
-
}
|
|
249
|
-
currentIndex++;
|
|
250
|
-
loadNext();
|
|
251
|
-
};
|
|
252
|
-
script.src = moduleUrl;
|
|
253
|
-
document.head.appendChild(script);
|
|
254
|
-
}
|
|
255
|
-
loadNext();
|
|
256
|
-
}
|
|
257
174
|
const process = () => {
|
|
258
|
-
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path"]);
|
|
259
|
-
swapEl('link[rel="stylesheet"][data-stagepass]', "data-stagepass-path", "data-href", "link", ["href", "data-href", "data-stagepass", "data-stagepass-path"]);
|
|
175
|
+
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path", "data-stagepass-cachebust"]);
|
|
176
|
+
swapEl('link[rel="stylesheet"][data-stagepass]', "data-stagepass-path", "data-href", "link", ["href", "data-href", "data-stagepass", "data-stagepass-path", "data-stagepass-cachebust"]);
|
|
260
177
|
};
|
|
261
|
-
loadModules();
|
|
262
178
|
process();
|
|
263
179
|
if (document.readyState === "loading") {
|
|
264
180
|
document.addEventListener("DOMContentLoaded", process);
|
package/dist/loader.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Stagepass=(()=>{(function(){let
|
|
2
|
-
`);if(
|
|
1
|
+
var Stagepass=(()=>{(function(){let p="stagepass_domain",S="stagepass",v=t=>t==="debug"||t.endsWith(".sp")||t==="localhost"||t.startsWith("localhost:"),m=new URLSearchParams(window.location.search);function W(){var t;try{let e=(t=new Error().stack)==null?void 0:t.split(`
|
|
2
|
+
`);if(!e)return"";let n=/loader(?:\.min)?\.js/i;for(let c=3;c<e.length;c++){let s=e[c].match(/([^/]+\.(?:js|ts))(?:[?#][^:]*)?:(\d+)/);if(s){let i=s[1];if(!n.test(i))return` ${i}:${s[2]}`}}}catch{}return""}function g(t,...e){if(!localStorage.getItem(p))return;let c=new Date().toTimeString().split(" ")[0],f=W(),s=`[\u{1F3AB} ${c}${f?" |"+f:""}]`;t==="error"?console.log(`%c${s}`,"color: red; font-weight: bold;",...e):console[t]?console[t](s,...e):console.log(s,t,...e)}let b=(...t)=>g("log",...t),E=(...t)=>g("warn",...t),$=(...t)=>g("error",...t);window.splog=b,window.spwarn=E,window.sperror=$;function j(...t){let e="log";t.length>0&&(t[0]==="warn"||t[0]==="error")&&(e=t[0],t=t.slice(1)),g(e,...t)}let M=(...t)=>g("warn",...t),P=(...t)=>g("error",...t),A=t=>t?t.replace(/^https?:\/\//,"").replace(/\/$/,""):null,k=!1,o=localStorage.getItem(p);if(m.has(S)){let t=m.get(S);if(t==="off"||t==="0"||t==="false")localStorage.removeItem(p),o=null,b("\u{1F50C} Stagepass disconnected.");else if(t){let n=t==="debug"?"debug":A(t);n&&v(n)?(localStorage.setItem(p,n),o=n,b(n==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${n}`),k=!0):n&&E(`Invalid domain: ${n}. Only .sp domains are allowed.`)}m.delete(S);let e=m.toString();window.history.replaceState({},"",window.location.pathname+(e?`?${e}`:"")+window.location.hash)}o&&!v(o)&&(localStorage.removeItem(p),o=null),o&&!k&&o!=="debug"&&b(`\u{1F50C} Stagepass connected to: ${o}`);let U=!!o,w=!!o&&o!=="debug",L=w?A(o):null,T=w?Date.now():0,_=T,B=T,u=window.location.hostname,V=u==="localhost"||u==="127.0.0.1"||u.startsWith("localhost:")||u.startsWith("127.0.0.1:"),C=w||V,D=C?"local":u.endsWith(".webflow.io")?"staging":"production";!(o&&v(o))&&!m.has(S)&&D==="production"&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});let r=window.stagepass||{};r.vars=Object.freeze({isLocal:C,env:D,domain:L||u,timestamp:B,version:"1.1.4"}),r.log=j,r.warn=M,r.error=P,r.resolveLocalUrl=t=>{let e=r.vars;if(!e.isLocal||!e.domain)return"";let n=t.startsWith("/")?t.substring(1):t;return n?`https://${e.domain}/${n}?_cb=${e.timestamp}`:""},window.stagepass=r,globalThis.stagepass=r,window.sp=r,globalThis.sp=r;function I(t,e,n,c,f){document.querySelectorAll(`${t}:not([data-stagepass-processed])`).forEach(s=>{s.setAttribute("data-stagepass-processed","true");let i=s.getAttribute(e),l=s.getAttribute(n);if(!l)return;if(!i&&l&&w&&L)try{let a=new URL(l).pathname.split("/");i=a[a.length-1]||a[a.length-2]}catch{let a=l.match(/\/([^\/\?#]+)(?:\?|#|$)/);a&&(i=a[1])}let h;if(w&&i&&L?h=`https://${L}/${i.startsWith("/")?i.substring(1):i}?_cb=${_}`:s.getAttribute("data-stagepass-cachebust")==="true"?h=l+(l.includes("?")?"&":"?")+"_cb="+Date.now():h=l,!h)return;let d=document.createElement(c);c==="script"?(d.src=h,Array.from(s.attributes).forEach(y=>{let a=y;f.includes(a.name)||d.setAttribute(a.name,a.value)}),!s.hasAttribute("async")&&!s.hasAttribute("defer")&&d.setAttribute("defer","")):(d.rel="stylesheet",d.href=h,Array.from(s.attributes).forEach(y=>{let a=y;f.includes(a.name)||d.setAttribute(a.name,a.value)})),s.parentNode?(s.parentNode.insertBefore(d,s),s.remove()):$(`${c} injection failed: No parent node`)})}let x=()=>{I("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path","data-stagepass-cachebust"]),I('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path","data-stagepass-cachebust"])};x(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",x);function O(){if(!U)return;let t=document.createElement("div");t.id="stagepass-flag",t.textContent=o==="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(p),b("\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()})();})();
|