@stagepass/loader 1.1.4 → 1.2.0
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/README.md +55 -0
- package/dist/loader.js +134 -1
- package/dist/loader.min.js +2 -2
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# `@stagepass/loader`
|
|
2
|
+
|
|
3
|
+
Universal browser runtime for Stagepass.
|
|
4
|
+
|
|
5
|
+
The loader reads Stagepass session state and swaps tagged production assets with local `.sp` assets during development. It also exposes a runtime API via `sp` / `stagepass`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @stagepass/loader
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## CDN Usage
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<script src="https://cdn.jsdelivr.net/npm/@stagepass/loader@1/dist/loader.min.js"></script>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## What it does
|
|
20
|
+
|
|
21
|
+
- Processes `script[data-stagepass]` and `link[rel="stylesheet"][data-stagepass]`.
|
|
22
|
+
- Uses `data-src` / `data-href` as source URL.
|
|
23
|
+
- Uses `data-stagepass-path` as optional local path override.
|
|
24
|
+
- Adds cache busting in local mode via `?_cb=<timestamp>`.
|
|
25
|
+
- Optional production cache busting with `data-stagepass-cachebust="true"`.
|
|
26
|
+
|
|
27
|
+
## Runtime API
|
|
28
|
+
|
|
29
|
+
Preferred global is `sp` (aliases: `stagepass`, `window.stagepass`).
|
|
30
|
+
|
|
31
|
+
- `sp.vars.isLocal`
|
|
32
|
+
- `sp.vars.env`
|
|
33
|
+
- `sp.vars.domain`
|
|
34
|
+
- `sp.vars.timestamp`
|
|
35
|
+
- `sp.vars.version`
|
|
36
|
+
- `sp.vars.client.*` (theme/browser/os/device/viewport/locale/capabilities)
|
|
37
|
+
- `sp.resolveLocalUrl(relativePath)`
|
|
38
|
+
- `sp.log(...)`, `sp.warn(...)`, `sp.error(...)`
|
|
39
|
+
|
|
40
|
+
Deprecated aliases (still available): `splog`, `spwarn`, `sperror`.
|
|
41
|
+
|
|
42
|
+
## URL Modes
|
|
43
|
+
|
|
44
|
+
- `?stagepass=my-project.sp` - activate local swapping.
|
|
45
|
+
- `?stagepass=debug` - logging only.
|
|
46
|
+
- `?stagepass=off` - deactivate session.
|
|
47
|
+
|
|
48
|
+
## Related Packages
|
|
49
|
+
|
|
50
|
+
- `@stagepass/cli` - creates and manages `.sp` infrastructure.
|
|
51
|
+
- `@stagepass/modules` - optional modules that extend the loader.
|
|
52
|
+
|
|
53
|
+
## Repository
|
|
54
|
+
|
|
55
|
+
- Full docs and examples: [stagepass repository](https://github.com/arobertherz/stagepass)
|
package/dist/loader.js
CHANGED
|
@@ -101,13 +101,110 @@ var Stagepass = (() => {
|
|
|
101
101
|
};
|
|
102
102
|
console.log = console.warn = console.error = console.info = console.debug = noop;
|
|
103
103
|
}
|
|
104
|
+
function getBrowserInfo(ua) {
|
|
105
|
+
let family = "unknown";
|
|
106
|
+
let major = null;
|
|
107
|
+
let m = null;
|
|
108
|
+
if (/Edg\/(\d+)/.test(ua)) {
|
|
109
|
+
family = "edge";
|
|
110
|
+
m = ua.match(/Edg\/(\d+)/);
|
|
111
|
+
} else if (/OPR\/(\d+)/.test(ua)) {
|
|
112
|
+
family = "opera";
|
|
113
|
+
m = ua.match(/OPR\/(\d+)/);
|
|
114
|
+
} else if (/Firefox\/(\d+)/.test(ua)) {
|
|
115
|
+
family = "firefox";
|
|
116
|
+
m = ua.match(/Firefox\/(\d+)/);
|
|
117
|
+
} else if (/Chrome\/(\d+)/.test(ua)) {
|
|
118
|
+
family = "chrome";
|
|
119
|
+
m = ua.match(/Chrome\/(\d+)/);
|
|
120
|
+
} else if (/Version\/(\d+).+Safari\//.test(ua)) {
|
|
121
|
+
family = "safari";
|
|
122
|
+
m = ua.match(/Version\/(\d+)/);
|
|
123
|
+
}
|
|
124
|
+
if (m && m[1]) major = Number(m[1]);
|
|
125
|
+
return { family, major: Number.isFinite(major) ? major : null, userAgent: ua };
|
|
126
|
+
}
|
|
127
|
+
function getOsInfo(ua) {
|
|
128
|
+
if (/Android/i.test(ua)) return { family: "android" };
|
|
129
|
+
if (/iPhone|iPad|iPod/i.test(ua)) return { family: "ios" };
|
|
130
|
+
if (/Mac OS X|Macintosh/i.test(ua)) return { family: "macos" };
|
|
131
|
+
if (/Windows NT/i.test(ua)) return { family: "windows" };
|
|
132
|
+
if (/Linux/i.test(ua)) return { family: "linux" };
|
|
133
|
+
return { family: "unknown" };
|
|
134
|
+
}
|
|
135
|
+
function getDeviceInfo(ua) {
|
|
136
|
+
const touch = "ontouchstart" in window || (navigator.maxTouchPoints || 0) > 0;
|
|
137
|
+
let type = "desktop";
|
|
138
|
+
if (/iPad|Tablet/i.test(ua)) type = "tablet";
|
|
139
|
+
else if (/Mobi|Android.+Mobile|iPhone/i.test(ua)) type = "mobile";
|
|
140
|
+
else if (/Android/i.test(ua) && touch) type = "tablet";
|
|
141
|
+
return { type, touch };
|
|
142
|
+
}
|
|
143
|
+
function getThemeInfo() {
|
|
144
|
+
let preferred = "no-preference";
|
|
145
|
+
let current = "unknown";
|
|
146
|
+
try {
|
|
147
|
+
if (window.matchMedia) {
|
|
148
|
+
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
149
|
+
preferred = "dark";
|
|
150
|
+
current = "dark";
|
|
151
|
+
} else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
|
|
152
|
+
preferred = "light";
|
|
153
|
+
current = "light";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} catch (_) {
|
|
157
|
+
}
|
|
158
|
+
return { preferred, current };
|
|
159
|
+
}
|
|
160
|
+
function hasStorage(type) {
|
|
161
|
+
try {
|
|
162
|
+
const storage = window[type];
|
|
163
|
+
const key = "__stagepass_test__";
|
|
164
|
+
storage.setItem(key, "1");
|
|
165
|
+
storage.removeItem(key);
|
|
166
|
+
return true;
|
|
167
|
+
} catch (_) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function getClientInfo() {
|
|
172
|
+
const ua = navigator.userAgent || "";
|
|
173
|
+
let timezone = null;
|
|
174
|
+
try {
|
|
175
|
+
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || null;
|
|
176
|
+
} catch (_) {
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
theme: getThemeInfo(),
|
|
180
|
+
browser: getBrowserInfo(ua),
|
|
181
|
+
os: getOsInfo(ua),
|
|
182
|
+
device: getDeviceInfo(ua),
|
|
183
|
+
viewport: {
|
|
184
|
+
width: window.innerWidth || 0,
|
|
185
|
+
height: window.innerHeight || 0,
|
|
186
|
+
dpr: window.devicePixelRatio || 1
|
|
187
|
+
},
|
|
188
|
+
locale: {
|
|
189
|
+
language: navigator.language || "unknown",
|
|
190
|
+
timezone
|
|
191
|
+
},
|
|
192
|
+
capabilities: {
|
|
193
|
+
cookiesEnabled: navigator.cookieEnabled === true,
|
|
194
|
+
localStorage: hasStorage("localStorage"),
|
|
195
|
+
sessionStorage: hasStorage("sessionStorage")
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
104
199
|
const stagepassObj = window.stagepass || {};
|
|
200
|
+
const clientInfo = getClientInfo();
|
|
105
201
|
stagepassObj.vars = Object.freeze({
|
|
106
202
|
isLocal,
|
|
107
203
|
env,
|
|
108
204
|
domain: dom || hostname,
|
|
109
205
|
timestamp: sessionStartTime,
|
|
110
|
-
version: "1.
|
|
206
|
+
version: "1.2.0",
|
|
207
|
+
client: clientInfo
|
|
111
208
|
});
|
|
112
209
|
stagepassObj.log = spLog;
|
|
113
210
|
stagepassObj.warn = spWarn;
|
|
@@ -122,6 +219,42 @@ var Stagepass = (() => {
|
|
|
122
219
|
globalThis.stagepass = stagepassObj;
|
|
123
220
|
window.sp = stagepassObj;
|
|
124
221
|
globalThis.sp = stagepassObj;
|
|
222
|
+
try {
|
|
223
|
+
if (window.matchMedia) {
|
|
224
|
+
const darkMq = window.matchMedia("(prefers-color-scheme: dark)");
|
|
225
|
+
const lightMq = window.matchMedia("(prefers-color-scheme: light)");
|
|
226
|
+
const updateTheme = () => {
|
|
227
|
+
var _a, _b;
|
|
228
|
+
if (!((_b = (_a = stagepassObj.vars) == null ? void 0 : _a.client) == null ? void 0 : _b.theme)) return;
|
|
229
|
+
if (darkMq.matches) {
|
|
230
|
+
stagepassObj.vars.client.theme.current = "dark";
|
|
231
|
+
stagepassObj.vars.client.theme.preferred = "dark";
|
|
232
|
+
} else if (lightMq.matches) {
|
|
233
|
+
stagepassObj.vars.client.theme.current = "light";
|
|
234
|
+
stagepassObj.vars.client.theme.preferred = "light";
|
|
235
|
+
} else {
|
|
236
|
+
stagepassObj.vars.client.theme.current = "unknown";
|
|
237
|
+
stagepassObj.vars.client.theme.preferred = "no-preference";
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
if (typeof darkMq.addEventListener === "function") {
|
|
241
|
+
darkMq.addEventListener("change", updateTheme);
|
|
242
|
+
lightMq.addEventListener("change", updateTheme);
|
|
243
|
+
} else if (typeof darkMq.addListener === "function") {
|
|
244
|
+
darkMq.addListener(updateTheme);
|
|
245
|
+
lightMq.addListener(updateTheme);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch (_) {
|
|
249
|
+
}
|
|
250
|
+
const updateViewport = () => {
|
|
251
|
+
var _a, _b;
|
|
252
|
+
if (!((_b = (_a = stagepassObj.vars) == null ? void 0 : _a.client) == null ? void 0 : _b.viewport)) return;
|
|
253
|
+
stagepassObj.vars.client.viewport.width = window.innerWidth || 0;
|
|
254
|
+
stagepassObj.vars.client.viewport.height = window.innerHeight || 0;
|
|
255
|
+
stagepassObj.vars.client.viewport.dpr = window.devicePixelRatio || 1;
|
|
256
|
+
};
|
|
257
|
+
window.addEventListener("resize", updateViewport);
|
|
125
258
|
function swapEl(sel, pathAttr, srcAttr, type, skipAttrs) {
|
|
126
259
|
document.querySelectorAll(`${sel}:not([data-stagepass-processed])`).forEach((el) => {
|
|
127
260
|
el.setAttribute("data-stagepass-processed", "true");
|
package/dist/loader.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Stagepass=(()=>{(function(){let
|
|
2
|
-
`);if(!
|
|
1
|
+
var Stagepass=(()=>{(function(){let f="stagepass_domain",S="stagepass",k=e=>e==="debug"||e.endsWith(".sp")||e==="localhost"||e.startsWith("localhost:"),m=new URLSearchParams(window.location.search);function O(){var e;try{let t=(e=new Error().stack)==null?void 0:e.split(`
|
|
2
|
+
`);if(!t)return"";let n=/loader(?:\.min)?\.js/i;for(let o=3;o<t.length;o++){let a=t[o].match(/([^/]+\.(?:js|ts))(?:[?#][^:]*)?:(\d+)/);if(a){let c=a[1];if(!n.test(c))return` ${c}:${a[2]}`}}}catch{}return""}function p(e,...t){if(!localStorage.getItem(f))return;let o=new Date().toTimeString().split(" ")[0],l=O(),a=`[\u{1F3AB} ${o}${l?" |"+l:""}]`;e==="error"?console.log(`%c${a}`,"color: red; font-weight: bold;",...t):console[e]?console[e](a,...t):console.log(a,e,...t)}let w=(...e)=>p("log",...e),E=(...e)=>p("warn",...e),L=(...e)=>p("error",...e);window.splog=w,window.spwarn=E,window.sperror=L;function D(...e){let t="log";e.length>0&&(e[0]==="warn"||e[0]==="error")&&(t=e[0],e=e.slice(1)),p(t,...e)}let W=(...e)=>p("warn",...e),j=(...e)=>p("error",...e),A=e=>e?e.replace(/^https?:\/\//,"").replace(/\/$/,""):null,C=!1,i=localStorage.getItem(f);if(m.has(S)){let e=m.get(S);if(e==="off"||e==="0"||e==="false")localStorage.removeItem(f),i=null,w("\u{1F50C} Stagepass disconnected.");else if(e){let n=e==="debug"?"debug":A(e);n&&k(n)?(localStorage.setItem(f,n),i=n,w(n==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${n}`),C=!0):n&&E(`Invalid domain: ${n}. Only .sp domains are allowed.`)}m.delete(S);let t=m.toString();window.history.replaceState({},"",window.location.pathname+(t?`?${t}`:"")+window.location.hash)}i&&!k(i)&&(localStorage.removeItem(f),i=null),i&&!C&&i!=="debug"&&w(`\u{1F50C} Stagepass connected to: ${i}`);let R=!!i,b=!!i&&i!=="debug",v=b?A(i):null,I=b?Date.now():0,V=I,F=I,h=window.location.hostname,N=h==="localhost"||h==="127.0.0.1"||h.startsWith("localhost:")||h.startsWith("127.0.0.1:"),T=b||N,M=T?"local":h.endsWith(".webflow.io")?"staging":"production";!(i&&k(i))&&!m.has(S)&&M==="production"&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});function z(e){let t="unknown",n=null,o=null;return/Edg\/(\d+)/.test(e)?(t="edge",o=e.match(/Edg\/(\d+)/)):/OPR\/(\d+)/.test(e)?(t="opera",o=e.match(/OPR\/(\d+)/)):/Firefox\/(\d+)/.test(e)?(t="firefox",o=e.match(/Firefox\/(\d+)/)):/Chrome\/(\d+)/.test(e)?(t="chrome",o=e.match(/Chrome\/(\d+)/)):/Version\/(\d+).+Safari\//.test(e)&&(t="safari",o=e.match(/Version\/(\d+)/)),o&&o[1]&&(n=Number(o[1])),{family:t,major:Number.isFinite(n)?n:null,userAgent:e}}function B(e){return/Android/i.test(e)?{family:"android"}:/iPhone|iPad|iPod/i.test(e)?{family:"ios"}:/Mac OS X|Macintosh/i.test(e)?{family:"macos"}:/Windows NT/i.test(e)?{family:"windows"}:/Linux/i.test(e)?{family:"linux"}:{family:"unknown"}}function H(e){let t="ontouchstart"in window||(navigator.maxTouchPoints||0)>0,n="desktop";return/iPad|Tablet/i.test(e)?n="tablet":/Mobi|Android.+Mobile|iPhone/i.test(e)?n="mobile":/Android/i.test(e)&&t&&(n="tablet"),{type:n,touch:t}}function U(){let e="no-preference",t="unknown";try{window.matchMedia&&(window.matchMedia("(prefers-color-scheme: dark)").matches?(e="dark",t="dark"):window.matchMedia("(prefers-color-scheme: light)").matches&&(e="light",t="light"))}catch{}return{preferred:e,current:t}}function x(e){try{let t=window[e],n="__stagepass_test__";return t.setItem(n,"1"),t.removeItem(n),!0}catch{return!1}}function q(){let e=navigator.userAgent||"",t=null;try{t=Intl.DateTimeFormat().resolvedOptions().timeZone||null}catch{}return{theme:U(),browser:z(e),os:B(e),device:H(e),viewport:{width:window.innerWidth||0,height:window.innerHeight||0,dpr:window.devicePixelRatio||1},locale:{language:navigator.language||"unknown",timezone:t},capabilities:{cookiesEnabled:navigator.cookieEnabled===!0,localStorage:x("localStorage"),sessionStorage:x("sessionStorage")}}}let s=window.stagepass||{},G=q();s.vars=Object.freeze({isLocal:T,env:M,domain:v||h,timestamp:F,version:"1.2.0",client:G}),s.log=D,s.warn=W,s.error=j,s.resolveLocalUrl=e=>{let t=s.vars;if(!t.isLocal||!t.domain)return"";let n=e.startsWith("/")?e.substring(1):e;return n?`https://${t.domain}/${n}?_cb=${t.timestamp}`:""},window.stagepass=s,globalThis.stagepass=s,window.sp=s,globalThis.sp=s;try{if(window.matchMedia){let e=window.matchMedia("(prefers-color-scheme: dark)"),t=window.matchMedia("(prefers-color-scheme: light)"),n=()=>{var o,l;(l=(o=s.vars)==null?void 0:o.client)!=null&&l.theme&&(e.matches?(s.vars.client.theme.current="dark",s.vars.client.theme.preferred="dark"):t.matches?(s.vars.client.theme.current="light",s.vars.client.theme.preferred="light"):(s.vars.client.theme.current="unknown",s.vars.client.theme.preferred="no-preference"))};typeof e.addEventListener=="function"?(e.addEventListener("change",n),t.addEventListener("change",n)):typeof e.addListener=="function"&&(e.addListener(n),t.addListener(n))}}catch{}let K=()=>{var e,t;(t=(e=s.vars)==null?void 0:e.client)!=null&&t.viewport&&(s.vars.client.viewport.width=window.innerWidth||0,s.vars.client.viewport.height=window.innerHeight||0,s.vars.client.viewport.dpr=window.devicePixelRatio||1)};window.addEventListener("resize",K);function $(e,t,n,o,l){document.querySelectorAll(`${e}:not([data-stagepass-processed])`).forEach(a=>{a.setAttribute("data-stagepass-processed","true");let c=a.getAttribute(t),d=a.getAttribute(n);if(!d)return;if(!c&&d&&b&&v)try{let r=new URL(d).pathname.split("/");c=r[r.length-1]||r[r.length-2]}catch{let r=d.match(/\/([^\/\?#]+)(?:\?|#|$)/);r&&(c=r[1])}let u;if(b&&c&&v?u=`https://${v}/${c.startsWith("/")?c.substring(1):c}?_cb=${V}`:a.getAttribute("data-stagepass-cachebust")==="true"?u=d+(d.includes("?")?"&":"?")+"_cb="+Date.now():u=d,!u)return;let g=document.createElement(o);o==="script"?(g.src=u,Array.from(a.attributes).forEach(y=>{let r=y;l.includes(r.name)||g.setAttribute(r.name,r.value)}),!a.hasAttribute("async")&&!a.hasAttribute("defer")&&g.setAttribute("defer","")):(g.rel="stylesheet",g.href=u,Array.from(a.attributes).forEach(y=>{let r=y;l.includes(r.name)||g.setAttribute(r.name,r.value)})),a.parentNode?(a.parentNode.insertBefore(g,a),a.remove()):L(`${o} injection failed: No parent node`)})}let P=()=>{$("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path","data-stagepass-cachebust"]),$('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path","data-stagepass-cachebust"])};P(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",P);function _(){if(!R)return;let e=document.createElement("div");e.id="stagepass-flag",e.textContent=i==="debug"?"STAGEPASS DEBUG":"STAGEPASS ACTIVE",e.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;',e.addEventListener("click",()=>{localStorage.removeItem(f),w("\u{1F50C} Stagepass disconnected."),window.location.reload()}),document.body?document.body.appendChild(e):document.addEventListener("DOMContentLoaded",()=>{document.body&&document.body.appendChild(e)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",_):_()})();})();
|