UNPKG

4.63 kBJavaScriptView Raw
1const PartytownSnippet = "/* Partytown 0.10.1 - MIT builder.io */\nconst t={preserveBehavior:!1},e=e=>{if(\"string\"==typeof e)return[e,t];const[n,r=t]=e;return[n,{...t,...r}]},n=Object.freeze((t=>{const e=new Set;let n=[];do{Object.getOwnPropertyNames(n).forEach((t=>{\"function\"==typeof n[t]&&e.add(t)}))}while((n=Object.getPrototypeOf(n))!==Object.prototype);return Array.from(e)})());!function(t,r,o,i,a,s,c,d,l,p,u=t,f){function h(){f||(f=1,\"/\"==(c=(s.lib||\"/~partytown/\")+(s.debug?\"debug/\":\"\"))[0]&&(l=r.querySelectorAll('script[type=\"text/partytown\"]'),i!=t?i.dispatchEvent(new CustomEvent(\"pt1\",{detail:t})):(d=setTimeout(v,1e4),r.addEventListener(\"pt0\",w),a?y(1):o.serviceWorker?o.serviceWorker.register(c+(s.swPath||\"partytown-sw.js\"),{scope:c}).then((function(t){t.active?y():t.installing&&t.installing.addEventListener(\"statechange\",(function(t){\"activated\"==t.target.state&&y()}))}),console.error):v())))}function y(e){p=r.createElement(e?\"script\":\"iframe\"),t._pttab=Date.now(),e||(p.style.display=\"block\",p.style.width=\"0\",p.style.height=\"0\",p.style.border=\"0\",p.style.visibility=\"hidden\",p.setAttribute(\"aria-hidden\",!0)),p.src=c+\"partytown-\"+(e?\"atomics.js?v=0.10.1\":\"sandbox-sw.html?\"+t._pttab),r.querySelector(s.sandboxParent||\"body\").appendChild(p)}function v(n,o){for(w(),i==t&&(s.forward||[]).map((function(n){const[r]=e(n);delete t[r.split(\".\")[0]]})),n=0;n<l.length;n++)(o=r.createElement(\"script\")).innerHTML=l[n].innerHTML,o.nonce=s.nonce,r.head.appendChild(o);p&&p.parentNode.removeChild(p)}function w(){clearTimeout(d)}s=t.partytown||{},i==t&&(s.forward||[]).map((function(r){const[o,{preserveBehavior:i}]=e(r);u=t,o.split(\".\").map((function(e,r,o){var a;u=u[o[r]]=r+1<o.length?u[o[r]]||(a=o[r+1],n.includes(a)?[]:{}):(()=>{let e=null;if(i){const{methodOrProperty:n,thisObject:r}=((t,e)=>{let n=t;for(let t=0;t<e.length-1;t+=1)n=n[e[t]];return{thisObject:n,methodOrProperty:e.length>0?n[e[e.length-1]]:void 0}})(t,o);\"function\"==typeof n&&(e=(...t)=>n.apply(r,...t))}return function(){let n;return e&&(n=e(arguments)),(t._ptf=t._ptf||[]).push(o,arguments),n}})()}))})),\"complete\"==r.readyState?h():(t.addEventListener(\"DOMContentLoaded\",h),t.addEventListener(\"load\",h))}(window,document,navigator,top,window.crossOriginIsolated);";
2
3/**
4 * The `type` attribute for Partytown scripts, which does two things:
5 *
6 * 1. Prevents the `<script>` from executing on the main thread.
7 * 2. Is used as a selector so the Partytown library can find all scripts to execute in a web worker.
8 *
9 * @public
10 */
11const SCRIPT_TYPE = `text/partytown`;
12const getMethods = (obj) => {
13 const properties = new Set();
14 let currentObj = obj;
15 do {
16 Object.getOwnPropertyNames(currentObj).forEach((item) => {
17 if (typeof currentObj[item] === 'function') {
18 properties.add(item);
19 }
20 });
21 } while ((currentObj = Object.getPrototypeOf(currentObj)) !== Object.prototype);
22 return Array.from(properties);
23};
24Object.freeze(getMethods([]));
25function serializeConfig(config) {
26 return JSON.stringify(config, (key, value) => {
27 if (typeof value === 'function') {
28 value = String(value);
29 if (value.startsWith(key + '(')) {
30 value = 'function ' + value;
31 }
32 }
33 if (key === 'loadScriptsOnMainThread') {
34 value = value.map((scriptUrl) => Array.isArray(scriptUrl)
35 ? scriptUrl
36 : [
37 typeof scriptUrl === 'string' ? 'string' : 'regexp',
38 typeof scriptUrl === 'string' ? scriptUrl : scriptUrl.source,
39 ]);
40 }
41 return value;
42 });
43}
44
45const createSnippet = (config, snippetCode) => {
46 const { forward = [], ...filteredConfig } = config || {};
47 const configStr = serializeConfig(filteredConfig);
48 return [
49 `!(function(w,p,f,c){`,
50 `if(!window.crossOriginIsolated && !navigator.serviceWorker) return;`,
51 Object.keys(filteredConfig).length > 0
52 ? `c=w[p]=Object.assign(w[p]||{},${configStr});`
53 : `c=w[p]=w[p]||{};`,
54 `c[f]=(c[f]||[])`,
55 forward.length > 0 ? `.concat(${JSON.stringify(forward)})` : ``,
56 `})(window,'partytown','forward');`,
57 snippetCode,
58 ].join('');
59};
60
61/**
62 * Function that returns the Partytown snippet as a string, which can be
63 * used as the innerHTML of the inlined Partytown script in the head.
64 *
65 * @public
66 */
67const partytownSnippet = (config) => createSnippet(config, PartytownSnippet);
68
69export { SCRIPT_TYPE, partytownSnippet };