vaderjs 1.4.0 → 1.4.1-d560b9aa7f
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/@integrations/ssg.js +178 -0
- package/LICENSE +21 -0
- package/README.md +34 -57
- package/binaries/IPC/index.js +277 -0
- package/binaries/main.js +1318 -0
- package/binaries/watcher.js +70 -66
- package/binaries/win32/check.ps1 +7 -0
- package/client/index.js +15 -0
- package/config/index.js +36 -0
- package/package.json +8 -6
- package/runtime/router.js +1 -1
- package/runtime/vader.js +1 -1
- package/vader.js +212 -672
- package/binaries/generator.js +0 -201
package/binaries/watcher.js
CHANGED
|
@@ -1,70 +1,74 @@
|
|
|
1
1
|
/**
|
|
2
|
+
|
|
2
3
|
* @file watcher.js
|
|
4
|
+
|
|
3
5
|
* @description This file is used as a polyfill for missing functionality of bun.js fs watch on windows
|
|
6
|
+
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
8
|
+
|
|
9
|
+
import { watch } from "fs";
|
|
10
|
+
|
|
11
|
+
import IPC from "../binaries/IPC/index.js";
|
|
12
|
+
|
|
13
|
+
IPC.NOLISTEN = true;
|
|
14
|
+
|
|
15
|
+
const s = await IPC.newServer(IPC.typeEnums.WATCHER, { port: process.env.PORT || 3434 });
|
|
16
|
+
|
|
17
|
+
console = s.Console;
|
|
18
|
+
|
|
19
|
+
process.cwd = () => {
|
|
20
|
+
return process.env.PWD;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
let folders = process.env.FOLDERS.split(",");
|
|
24
|
+
|
|
25
|
+
let hasSent = false;
|
|
26
|
+
|
|
27
|
+
folders.forEach((folder) => {
|
|
28
|
+
watch(process.cwd() + "/" + folder, { recursive: true }, (event, filename) => {
|
|
29
|
+
switch (event) {
|
|
30
|
+
case "change":
|
|
31
|
+
if (!hasSent) {
|
|
32
|
+
console.log({ type: "change", filename });
|
|
33
|
+
|
|
34
|
+
hasSent = true;
|
|
35
|
+
|
|
36
|
+
setTimeout(() => {
|
|
37
|
+
hasSent = false;
|
|
38
|
+
}, 500);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
break;
|
|
42
|
+
|
|
43
|
+
case "add":
|
|
44
|
+
if (!hasSent) {
|
|
45
|
+
console.log({ type: "add", filename });
|
|
46
|
+
|
|
47
|
+
hasSent = true;
|
|
48
|
+
|
|
49
|
+
setTimeout(() => {
|
|
50
|
+
hasSent = false;
|
|
51
|
+
}, 500);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
break;
|
|
55
|
+
|
|
56
|
+
case "close":
|
|
57
|
+
if (!hasSent) {
|
|
58
|
+
console.log({ type: "close", filename });
|
|
59
|
+
|
|
60
|
+
hasSent = true;
|
|
61
|
+
|
|
62
|
+
setTimeout(() => {
|
|
63
|
+
hasSent = false;
|
|
64
|
+
}, 500);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
process.on("exit", function (code) {
|
|
73
|
+
console.log("About to exit with code:", code);
|
|
74
|
+
});
|
package/client/index.js
CHANGED
|
@@ -122,6 +122,21 @@ export const Mounted = (callback, /**@type {Component} */ component, runOnlyOnce
|
|
|
122
122
|
|
|
123
123
|
|
|
124
124
|
|
|
125
|
+
/**
|
|
126
|
+
* @function fontmatter
|
|
127
|
+
* @description This method allows you to get the frontmatter of a markdown file
|
|
128
|
+
* @param {string} path - Path to the markdown file
|
|
129
|
+
* @returns {{title: string, description: string, content: string}}
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
export const fontmatter = (path) => {
|
|
133
|
+
return {
|
|
134
|
+
title: '',
|
|
135
|
+
description: '',
|
|
136
|
+
content: Object.keys({}).includes('content') ? '' : ''
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
}
|
|
125
140
|
|
|
126
141
|
|
|
127
142
|
|
package/config/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @function defineConfig
|
|
3
|
+
* @description Define the configuration for the application
|
|
4
|
+
* @param {Object} config
|
|
5
|
+
* @param {Object} config.host - Important metadata
|
|
6
|
+
* @param {string} config.host.hostname - The hostname for your webapplication
|
|
7
|
+
* @param {Object} config.host.prod - Define data for production use
|
|
8
|
+
* @param {Number} config.host.prod.port - The production port for your webapp
|
|
9
|
+
* @param {('vercel'|'netlify'|'cloudflare')} config.host.provider - Helps vader to generate routes for your webapp
|
|
10
|
+
* @param {Object} config.dev - The development server configuration
|
|
11
|
+
* @param {Number} config.dev.port - The port to use for the development server
|
|
12
|
+
* @param {('localhost')} config.dev.host - The hostname to use for the development server
|
|
13
|
+
* @param {Array} config.integrations - Additional integrations to enhance vaderjs
|
|
14
|
+
* @param {Function} config.integrations[0] - The integration to use for the application
|
|
15
|
+
* @returns {Object} The defined configuration
|
|
16
|
+
*/
|
|
17
|
+
export const defineConfig = (config = {
|
|
18
|
+
host: {
|
|
19
|
+
provider: '',
|
|
20
|
+
hostname:'',
|
|
21
|
+
prod:{
|
|
22
|
+
port:3000,
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
dev: {
|
|
26
|
+
port: 3000, // Default port for the development server
|
|
27
|
+
host: 'localhost' // Default hostname for the development server
|
|
28
|
+
},
|
|
29
|
+
integrations: []
|
|
30
|
+
}) => {
|
|
31
|
+
return config;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default {
|
|
35
|
+
defineConfig
|
|
36
|
+
};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "vaderjs",
|
|
3
3
|
"description": "A Reactive library aimed to helping you build reactive applications inspired by react.js",
|
|
4
4
|
"module": "vader.js",
|
|
5
|
-
"version": "1.4.
|
|
5
|
+
"version": "1.4.1-d560b9aa7f",
|
|
6
6
|
"bin": {
|
|
7
7
|
"vader": "./vader.js"
|
|
8
8
|
},
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"nextjs",
|
|
13
13
|
"pages router",
|
|
14
14
|
"bun.js",
|
|
15
|
+
"severside generation",
|
|
16
|
+
"static site generation",
|
|
17
|
+
"vue",
|
|
15
18
|
"spa",
|
|
16
19
|
"vanillajs",
|
|
17
20
|
"vanilla js"
|
|
@@ -28,10 +31,9 @@
|
|
|
28
31
|
"email": "malikwhitterb@gmail.com"
|
|
29
32
|
},
|
|
30
33
|
"dependencies": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
|
|
34
|
+
"playwright": "latest",
|
|
35
|
+
"source-map": "latest",
|
|
36
|
+
"terser":"latest",
|
|
37
|
+
"ws": "latest"
|
|
36
38
|
}
|
|
37
39
|
}
|
package/runtime/router.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import Vader from"./vader.js";let middlewares=[];class Router{constructor(t,e){this.routes=[],this.middlewares=[],this.errorMiddlewares=[],this.listeners=[],this.basePath=t}get(t,e){this.routes.push({method:"get",path:t,handler:e})}use(t){this.middlewares.push(t)}matchingRoute(){return routes.find((t=>t.url===window.location.pathname||window.location.pathname.split("/")[1]===t.url.split("/")[1]||void 0))}listen(t,e){t||(t=Math.random().toString(36).substring(7)),this.listeners.push(t),window.onpopstate=async t=>{let e=window.location.pathname;if(e.includes("#noNavigation"))return;let r=`/${e.split("/")[1]}`,n=(new DOMParser).parseFromString(await fetch(r,{cache:"reload"}).then((t=>t.text())),"text/html").documentElement,s=(n.querySelector("head"),n.querySelector("body"));document.querySelector("#app").innerHTML=s.querySelector("#app").innerHTML;let
|
|
1
|
+
import Vader from"./vader.js";let middlewares=[];class Router{constructor(t,e){this.routes=[],this.middlewares=[],this.errorMiddlewares=[],this.listeners=[],this.basePath=t}get(t,e){this.routes.push({method:"get",path:t,handler:e})}use(t){this.middlewares.push(t)}matchingRoute(){return routes.find((t=>t.url===window.location.pathname||window.location.pathname.split("/")[1]===t.url.split("/")[1]||void 0))}listen(t,e){t||(t=Math.random().toString(36).substring(7)),this.listeners.push(t),window.onpopstate=async t=>{let e=window.location.pathname;if(e.includes("#noNavigation"))return;let r=`/${e.split("/")[1]}`,n=(new DOMParser).parseFromString(await fetch(r,{cache:"reload"}).then((t=>t.text())),"text/html").documentElement,s=(n.querySelector("head"),n.querySelector("body"));document.querySelector("#app").innerHTML=s.querySelector("#app").innerHTML;let a=document.createElement("script");a.id="router",a.innerHTML=s.querySelector('script[id="router"]').innerHTML,a.setAttribute("type","module"),document.body.removeChild(document.getElementById("router")),document.body.appendChild(a)},window.location.pathname.includes("#noNavigation")||(console.log("no navigation"),this.handleRoute(window.location.pathname)),e&&e()}render(t){document.querySelector("#app").innerHTML="",Vader.render(t,document.querySelector("#app"))}extractParams(t,e){const r=t.split("/").filter((t=>""!==t)),n=e.split("/").filter((t=>""!==t)),s={};return r.forEach(((t,e)=>{if(t.startsWith(":")){const r=t.slice(1);s[r]=n[e]}else if(t.startsWith("*")){n.slice(e).forEach(((t,e)=>{s[e]=t}))}})),s}extractQueryParams(t){const e=t.split("?")[1];if(!e)return{};const r={};return e.split("&").forEach((t=>{const[e,n]=t.split("=");r[e]=n})),r}checkroute(t){return(t=t.endsWith("/")?t.slice(0,-1):t).includes("index.html")&&(t=t.replace("index.html","")),t.includes("?")&&(t=t.split("?")[0]),this.routes.find((e=>{if(e.path===t)return!0;if(""===t&&"/"===e.path)return!0;if(e.path.includes("*")||e.path.includes(":")){const r=e.path.split("/").filter((t=>""!==t)),n=t.split("/").filter((t=>""!==t));if(this.basePath&&n.shift(),r.length!==n.length&&!e.path.endsWith("*"))return!1;for(let t=0;t<r.length;t++){const e=r[t],s=n[t];if(!e.startsWith(":")&&!e.startsWith("*")&&e!==s)return!1}return!0}const r=this.extractParams(e.path,t);return Object.keys(r).length>0}))}handleRoute(t){let e=200,r={},n=t,s=this.checkroute(t);s||(e=404,s=this.routes.find((t=>"*"===t.path)),s&&(r=this.extractParams(s.path,t)));const a=this.extractQueryParams(n),o=s&&s.path?this.extractParams(s.path,n):r;Object.keys(o).forEach((t=>{o[t]=o[t].split("?")?o[t].split("?")[0]:o[t]}));const i={headers:{},params:o,query:a,path:t,fileUrl:window.location.href.split(window.location.origin)[1],url:window.location.href,method:s?s.method:"get",pause:!1,timestamp:Date.now()};window.$CURRENT_URL=i.path,window.$basePath=" ",window.$FULL_URL=window.location.href.replace("#","");const l={status:e,log:t=>{void 0===t?console.log(`${i.path} ${i.method} ${l.status} ${i.timestamp}`):console.table({"Request Path":i.path,"Request Method":s.method,"Response Status":l.status,"Request Timestamp":i.timestamp})},refresh:()=>{this.handleRoute(window.location.pathname)},redirect:t=>{!t.startsWith("/")&&(t=`/${t}`),window.history.pushState({},"",t),window.dispatchEvent(new Event("popstate"))},render:async t=>{t?.default&&(t=t.default),document.querySelector("#app").innerHTML="",Vader.render(t,document.querySelector("#app"),{passProps:{router:{req:i,res:l}}})},setQuery:t=>{if($SERVER)return;let e="";Object.keys(t).forEach(((r,n)=>{e+=`${0===n?"?":"&"}${r}=${t[r]}`}));let r=window.location.pathname.split("?")[0];e=e.replace("/","-").replaceAll("/","-"),i.query={...i.query,...t},window.history.pushState({},"",`${r}${e}`)},getQuery:t=>{let e=window.location.search,r=new URLSearchParams(e);return t?r.get(t.key):r},send:t=>{document.querySelector("#app").innerHTML=t},json:t=>{const e=document.querySelector("#app");e.innerHTML="";const r=document.createElement("pre");r.textContent=JSON.stringify(t,null,2),e.appendChild(r)}};middlewares.forEach((t=>{t(i,l)})),s?l.render(s.handler):l.status(404).send("Not Found")}}export default Router;
|
package/runtime/vader.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let hasMounted=[],hasRendered=[];export const Mounted=(e,t,r=!1)=>{let s=setInterval((()=>{switch(!0){case r&&hasMounted.includes(t.key):return clearInterval(s);case r&&!hasMounted.includes(t.key):hasMounted.push(t.key),e();break;case!document.querySelector(`[key="${t.key}"]`):return;default:e(),clearInterval(s)}}),100)};export class Component{constructor(e){this.state={},this.__internalInstance=null,this.key=e?.key||Math.random(),this.checkMount(),this.mounted=!1}setState(e){this.state=Object.assign({},this.state,e),this.updateInstance(this.__internalInstance)}useState(e,t){this.state[e]||(this.state[e]=t);let r=this.state[e];return r=(()=>this.state[e]||t)(),[r,t=>{r=t,this.state[e]=t,this.updateInstance(this.__internalInstance)}]}useReducer(e,t,r){this.state[e]||(this.state[e]=r);let s=this.state[e];return s=(()=>this.state[e]||r)(),[s,r=>{const n=t(s,r);s=n,this.state[e]=n,this.updateInstance(this.__internalInstance)}]}useRef(e,t){return this.state[e]||(this.state[e]=t),{current:this.state[e]}}onMount(){}domDifference(e,t){let r=[];for(let s=0;s<e.length;s++){let n=e[s],a=t[s];if(n&&a&&n.childNodes.length>0&&a.childNodes.length>0){switch(!0){case n.attributes&&a.attributes&&n.attributes.length!==a.attributes.length:r.push({type:"attributeSwap",old:n,new:a});break;case!n.isEqualNode(a)&&n.nodeName===a.nodeName:r.push({type:"replace",old:n,new:a})}let e=this.domDifference(Array.from(n.childNodes),Array.from(a.childNodes));return r.push(...e),r}!n&&a&&r.push({type:"add",old:n,new:a})}return r}checkMount(){if(this.mounted)return;setInterval((()=>{document.querySelector(`[key="${this.key}"]`)&&!this.mounted&&(this.mounted=!0,this.onMount())}))}updateChangedElements(e){e.forEach((e=>{if(e)switch(e.type){case"replace":if(e.old.panrntNode&&"BODY"===e.old.parentNode.nodeName)return;e.old.replaceWith(e.new.cloneNode(!0));break;case"remove":e.old.remove();break;case"attributeSwap":let t=Array.from(e.old.attributes),r=Array.from(e.new.attributes);t.forEach((t=>{e.old.removeAttribute(t.name)})),r.forEach((t=>{e.old.setAttribute(t.name,t.value)}));break;case"add":e.old.appendChild(e.new.cloneNode(!0))}}))}async updateInstance(e){console.log(e);let t=document.querySelector(`[key="${this.key}"]`)||document.querySelector("#app").firstChild,r=await vjsxx.render(e,t,{return:!0});console.log(r);let s=this.domDifference(Array.from(t.childNodes),Array.from(r.childNodes));this.updateChangedElements(s)}parseStyle(e){let t="";return Object.keys(e).forEach((r=>{let s=e[r];r=r.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),t+=`${r}:${s};`})),t}}class vjsx{constructor(){this._vjsx=!0}isClass(e){return/^\s*class\s+/.test(e.toString())}instanizeClass(e,t={}){if("string"==typeof e||"object"==typeof e)return e;switch(this.isClass(e)){case!0:let r=new e(t);return r.$$typeof="vjsx",r.type=e.name,r.key=t?.key||r.name,r.props=t,r.__internalInstance=()=>r.render(),r.render();case!1:let s=new Component;s.key=e.name+Math.random(),s.__internalInstance=()=>e.bind(s)(t),e.$$typeof="vjsx",e.useState=s.useState.bind(s),e.setState=s.setState.bind(s),e.state=s.state;let n={},a={};return t?.router&&(n=t.router.req,a=t.router.res),e.apply?e.apply(s,[n,a,t]):e}}createElement(e,t,r,...s){let n="function"==typeof e,a=s.find((e=>"vjsx"===e?.$$typeof));if(n)return t.key=r||Math.random(),t.parent=a,this.instanizeClass(e,t);let o={type:e,RootParent:a,props:{...t,key:t?.key||Math.random(),children:Array.isArray(t.children)?t.children:[t.children]}};return"TEXT_ELEMENT"===("string"==typeof o?"TEXT_ELEMENT":o.type)?createTextElement(o):o}async render(e,t,r){let s=this.instanizeClass(e,r?.passProps);if(r&&r?.passProps&&r?.passProps?.router){let e=r.passProps.router.req,s=r.passProps.router.res;e.pause&&await new Promise((n=>{let a=setInterval((()=>{e.pause?s.render&&"function"==typeof s.render&&!hasRendered.includes(s.render.toString())&&(Vader.render(s.render(e,s,r.passProps),t),hasRendered.push(s.render.toString())):(clearInterval(a),n())}),0)}))}if(!s.type)return;let n="TEXT_ELEMENT"===s.type?document.createTextNode(""):document.createElement(s.type),isListener=e=>e.startsWith("on");if(s.props&&(s.props.key&&(s.key=s.props.key),Object.keys(s.props).filter(isListener).forEach((e=>{let t=e.toLowerCase().substring(2);n.addEventListener(t,s.props[e].bind(s))})),Object.keys(s.props).filter((e=>!isListener(e))).forEach((e=>{switch(e){case"classname":n.setAttribute("class",s.props[e]),delete s.props[e];break;case"ref":if(Array.isArray(s.props[e].current))return void s.props[e].current.push(n);s.props[e].current=n;break;case"htmlFor":n.setAttribute("for",s.props[e]);break;case"style":n.setAttribute("style",Component.prototype.parseStyle(s.props[e]));case"children":break;default:n.setAttribute(e,s.props[e])}"TEXT_ELEMENT"!==s.type||(n.nodeValue=s.props[e])}))),s.props&&s.props.children&&Array.isArray(s.props.children)&&s.props?.children&&s.props.children.forEach((e=>{if(Array.isArray(e))return void e.forEach((e=>{null!==e&&this.render(e,n,!0)}));if(!e)return;let t="function"==typeof e?this.instanizeClass(e):e;"TEXT_ELEMENT"!==(t?.type?t.type:"TEXT_ELEMENT")?this.render(t,n):n.appendChild(document.createTextNode(t))})),r?.return)return n;s.key&&n.setAttribute("key",s.key),!r?.return&&document.body.contains(t)&&(s?.RootParent?.onMount(),t.innerHTML=""),r?.return||t.appendChild(n)}}function createTextElement(e){return{type:"TEXT_ELEMENT",props:{nodeValue:e,children:[]}}}export const useState=e=>{this.state[key]||(this.state[key]=e);let t=this.state[key];return t=(()=>this.state[key]||e)(),[t,e=>{t=e,this.state[key]=e,this.updateInstance(this.__internalInstance)}]};export const useReducer=(e,t)=>{const[r,s]=useState(t);return[r,t=>{const n=e(r,t);s(n)}]};export const useRef=e=>{this.state[key]||(this.state[key]=e);let t=this.state[key];return t=(()=>this.state[key]||e)(),[t,e=>{t=e,this.state[key]=e,this.updateInstance(this.__internalInstance)}]};export class Html extends Component{constructor(e){super(e),this.key=e?.key||"DOCUMENT_ROOT",this.props=e,this.checkMount()}render(){return $SERVER?(this.props.lang&&document.documentElement.setAttribute("lang",this.props.lang),vjsxx.createElement("html",{children:this.props.children})):vjsxx.createElement("div",{children:this.props.children,key:this.key})}onMount(){this.props?.parent&&this.props?.parent&&this.props.parent.onMount()}}export class Head extends Component{constructor(e){super(e),this.head=document.createElement("head")}render(){if($SERVER||this.props.updateOnReload){Array.isArray(this.props.children)&&this.props.children.forEach((e=>{this.head.appendChild(vjsxx.render(e,this.head,{return:!0}))})),Object.keys(this.props).includes("children")&&!Array.isArray(this.props.children)&&this.head.appendChild(vjsxx.render(this.props.children,this.head,{return:!0}));let e=document.head;this.head.querySelectorAll("*").forEach((t=>{switch(t.tagName){case"TITLE":e.querySelector("title")&&e.querySelector("title").remove(),e.appendChild(t);break;case"META":if(this.props.updateOnReload)return;return void(e.querySelector(`meta[name="${t.name}"]`)&&e.querySelector(`meta[name="${t.name}"]`).remove());case"LINK":if($SERVER)return void(e.querySelector(`link[href="${t.href}"]`)||e.appendChild(t));case"SCRIPT":if(!t.hasAttribute("eager"))return void(document.querySelector(`script[src="${t.src}"]`)||e.appendChild(t));if(document.querySelector(`script[srcid="${t.src}"]`))return;fetch(t.src).then((e=>e.text())).then((r=>{let s=document.createElement("script");s.innerHTML=r,s.setAttribute("srcid",t.src),s.setAttribute("type",t.type),s.setAttribute("async",t.async||!1),s.setAttribute("defer",t.defer||!1),e.querySelector(`script[srcid="${t.src}"]`)||e.prepend(s)})).catch((e=>{console.warn("Error fetching script",e)}));break;default:console.warn("Unknown tag",t.tagName)}}))}return""}}export class Link extends Component{constructor(e){super(e),this.key=e?.key||Math.random(),this.props=e,this.checkMount()}render(){return vjsxx.createElement("a",{class:this.props.class||this.props.className,style:this.props.style,onClick:e=>{switch(e.preventDefault(),!0){case"outside"===this.props.action:e.preventDefault(),window.open(this.props.href,"_blank");break;case"function"==typeof this.props.action:this.props.action(e);break;default:e.preventDefault(),window.history.pushState({},"",this.props.href),window.dispatchEvent(new Event("popstate"))}},children:this.props.children},this.props?.key,null)}}let vjsxx=new vjsx,Vader={createElement:vjsxx.createElement,useState:useState,instanizeClass:vjsxx.instanizeClass,render:vjsxx.render,useRef:useRef,isClass:vjsxx.isClass,Head:Head,Html:Html,Mounted:Mounted,Component:Component,Link:Link};export default Vader;
|
|
1
|
+
let hasMounted=[],hasRendered=[],memoizes={},states={};export const Mounted=(e,t,s=!1)=>{let r=setInterval((()=>{switch(!0){case s&&hasMounted.includes(t.key):return clearInterval(r);case s&&!hasMounted.includes(t.key):hasMounted.push(t.key),e();break;case!document.querySelector(`[key="${t.key}"]`):return;default:e(),clearInterval(r)}}),100)};export class Component{constructor(e){this.state=states[this.constructor.name]||{},this.__internalInstance=null,this.key=e?.key||Component.name+Math.random(),this.checkMount(),this.mounted=!1,this.props={...e,key:this.key}}useKey(e){Object.defineProperty(this,"key",{value:e}),Object.defineProperty(this.props,"key",{value:e})}setState(e){this.state=Object.assign({},this.state,e),this.updateInstance(this.__internalInstance)}useState(e,t){this.state[e]||(this.state[e]=t);let s=this.state[e];const r=()=>this.state[e];return[r,t=>{this.state[e]=t,this.updateInstance(this.__internalInstance),s=r()}]}useRef(e,t){return this.state[e]||(this.state[e]=t),{current:this.state[e]}}onMount(){}domDifference(e,t){let s=[];for(let r=0;r<e.length;r++){let n=e[r],a=t[r];n&&a&&n.childNodes.length>0&&a.childNodes.length>0&&s.push(...this.domDifference(Array.from(n.childNodes),Array.from(a.childNodes))),n.nodeValue!==a.nodeValue&&s.push({type:"replace",old:n,new:a})}return s}checkMount(){if(this.mounted)return;setInterval((()=>{document.querySelector(`[key="${this.key}"]`)&&!this.mounted&&(this.mounted=!0,this.onMount())}))}updateChangedElements(e){e.forEach((e=>{if(e)switch(e.type){case"replace":if(e.old.panrntNode&&"BODY"===e.old.parentNode.nodeName)return;e.old.replaceWith(e.new.cloneNode(!0));break;case"remove":e.old.remove();break;case"attributeSwap":let t=Array.from(e.old.attributes),s=Array.from(e.new.attributes);t.forEach((t=>{e.old.removeAttribute(t.name)})),s.forEach((t=>{e.old.setAttribute(t.name,t.value)}));break;case"add":e.old.appendChild(e.new.cloneNode(!0))}}))}async updateInstance(e,t=!1){let s=document.querySelector(`[key="${this.key}"]`)||document.querySelector("#app").firstChild,r=await vjsxx.render(e,s,{return:!0}),n=t?[{type:"replace",old:s,new:r}]:this.domDifference(Array.from(s.childNodes),Array.from(r.childNodes));this.updateChangedElements(n)}parseStyle(e,t,s={useImportant:!1}){let r="",n=document.head.querySelector("#vader-stylesheet")||document.createElement("style");return n.setAttribute("id","vader-stylesheet"),Object.keys(e).forEach((a=>{let i=e[a],o="",l="";switch(!0){case a.includes("hover"):return o=a.split("hover(")[1].replace(/\)/g,""),l=`.${o}:hover{${this.parseStyle(i,t,{useImportant:!0})}};`,n.innerHTML.includes(l)||(n.innerHTML=l+n.innerHTML),void t.classList.add(o);case a.includes("transition"):let e=`transition:${i} ${s.useImportant?" !important":""};`;r+=e;break;case a.includes("@mobile"):o=a.split("@mobile")[1].replace(/\(/g,"").replace(/\)/g,"");let d=document.createElement("style");d.setAttribute("id","vader-mobile-stylesheet"),l=`@media (max-width: 768px){.${o}{${this.parseStyle(i,t,{useImportant:!0})}};}`,document.head.querySelector("#vader-mobile-stylesheet")||document.head.appendChild(d),document.querySelector("#vader-mobile-stylesheet")&&!document.querySelector("#vader-mobile-stylesheet").innerHTML.includes(l)&&(document.querySelector("#vader-mobile-stylesheet").innerHTML=l+document.querySelector("#vader-mobile-stylesheet").innerHTML),t.classList.add(o);break;case a.includes("@tablet"):o=a.split("@tablet")[1].replace(/\(/g,"").replace(/\)/g,""),l=`@media (min-width: 769px) and (max-width: 1024px){.${o}{${this.parseStyle(i,t,{useImportant:!0})}};}`;let c=document.createElement("style");c.setAttribute("id","vader-tablet-stylesheet"),document.head.querySelector("#vader-tablet-stylesheet")||document.head.appendChild(c),document.head.querySelector("#vader-tablet-stylesheet").innerHTML.includes(l)||(document.head.querySelector("#vader-tablet-stylesheet").innerHTML=l+document.head.querySelector("#vader-tablet-stylesheet").innerHTML),t.classList.add(o);break;case a.includes("@desktop"):o=a.split("@desktop")[1].replace(/\(/g,"").replace(/\)/g,"");let p=document.createElement("style");p.setAttribute("id","vader-desktop-stylesheet"),document.head.querySelector("#vader-desktop-stylesheet")||document.head.appendChild(p),l=`@media (min-width: 1025px){.${o}{${this.parseStyle(i,t,{useImportant:!0})}};}`,document.head.querySelector("#vader-desktop-stylesheet").innerHTML.includes(l)||(document.head.querySelector("#vader-desktop-stylesheet").innerHTML=l+document.head.querySelector("#vader-desktop-stylesheet").innerHTML),t.classList.add(o);break;case a.includes("transform"):let h=`transform:${i} ${s.useImportant?" !important":""};`;r+=h;break;case a.includes("animation"):r+=`animation:${i};`;break;case a.includes("active"):o=a.split("active(")[1].replace(/\)/g,""),l=`.${o}:active{${this.parseStyle(i,t,{useImportant:!0})}};`,n.innerHTML.includes(l)||(n.innerHTML+=l),t.classList.add(o);break;case a.includes("body"):let u=`body{${this.parseStyle(i,t)}};`;return void(document.head.querySelector("#vader-stylesheet").innerHTML.includes(`body{${this.parseStyle(i,t)}};`)||(document.head.querySelector("#vader-stylesheet").innerHTML+=u));case a.includes("focus"):o=a.split("focus(")[1].replace(/\)/g,"");let y=`.${o}:focus{${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(y)||(n.innerHTML+=y),t.classList.add(o);break;case a.includes("before"):o=a.split("before(")[1].replace(/\)/g,"");let m=`.${o}::before{${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(m)||(n.innerHTML+=m),t.classList.add(o);break;case a.includes("after"):o=a.split("after(")[1].replace(/\)/g,"");let f=`.${o}::after{${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(f)||(n.innerHTML+=f),t.classList.add(o);break;case a.includes("placeholder"):o=a.split("placeholder(")[1].replace(/\)/g,"");let k=`.${o}::placeholder{${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(k)||(n.innerHTML+=k),t.classList.add(o);break;case a.includes("first-child"):o=a.split("first-child(")[1].replace(/\)/g,"");let b=`.${o}:first-child{${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(b)||(n.innerHTML+=b),t.classList.add(o);break;case a.includes("last-child"):o=a.split("last-child(")[1].replace(/\)/g,"");let v=`.${o}:last-child{${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(v)||(n.innerHTML+=v),t.classList.add(o);break;case a.includes("nth-child"):o=a.split("nth-child(")[1].replace(/\)/g,"").split("(")[0];let E=`.${o}:nth-child(${a.split("nth-child(")[1].replace(/\)/g,"").split("(")[1]}){${this.parseStyle(i,t,{useImportant:!0,noReturn:!0})}};`;n.innerHTML.includes(E)||(n.innerHTML+=E),t.classList.add(o);break;case a.includes(">"):let L=`${a}{${this.parseStyle(i,t)}};`;n.innerHTML.includes(L)||(n.innerHTML+=L),t.classList.add(o)}a=a.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase(),r+=`${a}:${i} ${s.useImportant?" !important":""};`})),document.head.querySelector("#vader-stylesheet")||document.head.appendChild(n),r}}class vjsx{constructor(){this._vjsx=!0}isClass(e){return/^\s*class\s+/.test(e.toString())}instanizeClass(e,t={}){if("string"==typeof e||"object"==typeof e)return e;switch(this.isClass(e)){case!0:let s=new e(t);return states[e.name]=s.state,s.state=states[e.name],s.$$typeof="vjsx",s.type=e.name,s.key=t?.key||s?.key||s.name,s.props=t,s.__internalInstance=()=>s.render(s.props),s.__internalInstance.bind(s),s.render();case!1:let r=new Component;r.key=e.name+Math.random(),e.$$typeof="vjsx",e.useState=r.useState.bind(r),e.setState=r.setState.bind(r),states[e.name]=r.state,r.state=states[e.name],e.state=r.state;let n={},a={};return t?.router&&(n=t.router.req,a=t.router.res),r.__internalInstance=()=>e.bind(r)(t,n,a),e.apply?e.apply(r,[t,n,a]):e}}createElement(e,t,s,...r){let n="function"==typeof e,a=r.find((e=>"vjsx"===e?.$$typeof));if(n)return t.key=s||Math.random(),t.parent=a,this.instanizeClass(e,t);let i={type:e,RootParent:a,key:s,props:{...t,children:Array.isArray(t.children)?t.children:[t.children]}};return"TEXT_ELEMENT"===("string"==typeof i?"TEXT_ELEMENT":i.type)?createTextElement(i):i}async render(e,t,s){let r=this.instanizeClass(e,s?.passProps);if(s&&s?.passProps&&s?.passProps?.router){let i=s.passProps.router.req,o=s.passProps.router.res;if(i.pause){async function l(){return await new Promise((async e=>{let r=setInterval((()=>{i.pause?o.render&&"function"==typeof o.render&&!hasRendered.includes(o.render.toString())&&(Vader.render(o.render(i,o,s.passProps),t),hasRendered.push(o.render.toString())):(clearInterval(r),e())}),0)}))}await l()}}if(!r.type)return;let n="TEXT_ELEMENT"===r.type?document.createTextNode(""):document.createElement(r.type),a=e=>e.startsWith("on");if(r.props&&(r.props.key&&(r.key=r.props.key),Object.keys(r.props).filter(a).forEach((e=>{let t=e.toLowerCase().substring(2);n.addEventListener(t,r.props[e].bind(r))})),Object.keys(r.props).filter((e=>!a(e))).forEach((e=>{switch(!0){case"className"===e||"class"===e||"classname"===e:n.setAttribute("class",r.props[e]),delete r.props[e];break;case"ref"===e:if(Array.isArray(r.props[e].current))return void r.props[e].current.push(n);r.props[e].current=n,n.removeAttribute("ref");break;case"htmlFor"===e:n.setAttribute("for",r.props[e]);break;case"style"===e:n.setAttribute("style",Component.prototype.parseStyle(r.props[e],n));break;default:"children"!==e&&n.setAttribute(e,r.props[e])}"TEXT_ELEMENT"!==r.type||(n.nodeValue=r.props[e])}))),r.props&&r.props.children&&Array.isArray(r.props.children)&&r.props?.children&&r.props.children.forEach((async e=>{if("number"!=typeof e&&"string"!=typeof e||(e=e.toString()),Array.isArray(e))return void e.forEach((async e=>{null!==e&&await this.render(e,n,!0)}));if(!e)return;let t="function"==typeof e?this.instanizeClass(e):e;"TEXT_ELEMENT"!==(t?.type?t.type:"TEXT_ELEMENT")?await this.render(t,n):n.appendChild(document.createTextNode(t))})),s?.return)return n;r.key&&n.setAttribute("key",r.key),!s?.return&&document.body.contains(t)&&(r?.RootParent?.onMount(),t.innerHTML=""),t.appendChild(n)}}function createTextElement(e){return{type:"TEXT_ELEMENT",props:{nodeValue:e,children:[]}}}export const useState=e=>{this.state[key]||(this.state[key]=e);let t=this.state[key];const s=()=>this.state[key]||e;return t=s(),[s,e=>{t=e,this.state[key]=e,this.updateInstance(this.__internalInstance)}]};export const useReducer=(e,t)=>{const[s,r]=useState(t);return[s,t=>{const n=e(s,t);r(n)}]};export const useRef=e=>{this.state[key]||(this.state[key]=e);let t=this.state[key];return t=(()=>this.state[key]||e)(),[t,e=>{t=e,this.state[key]=e,this.updateInstance(this.__internalInstance)}]};export class Html extends Component{constructor(e){super(e),this.useKey(this?.props?.key||"HTML_ROOT"),this.props=e,this.checkMount()}render(){return $SERVER?(this.props.lang&&document.documentElement.setAttribute("lang",this.props.lang),vjsxx.createElement("div",{children:this.props.children,key:this.key})):vjsxx.createElement("div",{children:this.props.children,key:this.key})}onMount(){this.props?.parent&&this.props?.parent&&this.props.parent.onMount()}}export class Head extends Component{constructor(e){super(e),this.head=document.createElement("div"),this.useKey("head"),this.hasRefreshed=!1}render(){return($SERVER||this.props.refresh&&!memoizes[this.key])&&(async()=>{let e=document.createElement("div");if(Array.isArray(this.props.children))for(let t of this.props.children){let s=await vjsxx.render(t,e,{return:!0}),r=this.head.querySelectorAll("*");for(let e=0;e<r.length;e++)r[e].isEqualNode(s);e.appendChild(s)}Object.keys(this.props).includes("children")&&!Array.isArray(this.props.children)&&this.head.appendChild(vjsxx.render(this.props.children,this.head,{return:!0})),e.querySelectorAll("*").forEach((e=>{switch(e.tagName){case"TITLE":document.title=e.innerHTML;break;case"META":return void(document.querySelector(`meta[name="${e.getAttribute("name")}"]`)||document.head.appendChild(e));case"LINK":return void(document.querySelector(`link[href="${e.getAttribute("href")}"]`)||document.head.appendChild(e));case"SCRIPT":if(!e.hasAttribute("eager"))return void(document.querySelector(`script[src="${e.getAttribute("src")}"]`)||document.head.appendChild(e));if(document.querySelector(`script[srcid="${e.getAttribute("srcid")}"]`))return;fetch(e.src).then((e=>e.text())).then((t=>{let s=document.createElement("script");s.innerHTML=t,s.setAttribute("srcid",e.src),s.setAttribute("type",e.type),s.setAttribute("async",e.async||!1),s.setAttribute("defer",e.defer||!1),oldDocument.querySelector(`script[srcid="${e.src}"]`)||document.head.appendChild(s)})).catch((e=>{console.warn("Error fetching script",e)}));break;default:console.warn("Unknown tag",e.tagName)}}))})(),""}}export class Link extends Component{constructor(e){if(super(e),this.key=e?.key||Math.random(),this.props=e,this.checkMount(),"object"==typeof this.props?.href){let e=this.props?.href?.query?Object.keys(this.props?.href?.query).map((e=>`${e}=${this.props?.href?.query[e]}`)).join("&"):"";this.props.href=this.props?.href?.pathname+(e?`?${e}`:"")}}render(){let e={...this.props,href:this.props?.href,onClick:e=>{switch(e.preventDefault(),!0){case"outside"===this.props.action:e.preventDefault(),window.open(this.props.href,"_blank");break;case"function"==typeof this.props.action:this.props.action(e);break;default:e.preventDefault(),window.history.pushState({},"",this.props.href),window.dispatchEvent(new Event("popstate"))}},key:this.key};return vjsxx.createElement("a",e,this.props?.key,null)}}export class imgComponent extends Component{constructor(e){if(super(e),this.useKey(this?.props?.key||"img"),this.key=this?.props?.key||"img",this.props=e,this.checkMount(),this.isLoaded=!1,!this.key)throw new Error("Key is required for Image component for tracking")}render(){let e=new Image;e.src=this.props?.src,e.onload=()=>{this.props.isLoaded||(this.props.isLoaded=!0,this.isLoaded=!0,this.updateInstance(this.__internalInstance,!0))};let t=vjsxx.createElement("div",{style:{width:this.props?.width,height:this.props?.height,...this?.props?.loader?{}:{background:this.props?.loadColor||"#bcbcbc"},...this?.props?.style},key:this.key,children:[this.props?.loader||""]},this.key,null);return this.isLoaded?vjsxx.createElement("img",{...this.props,style:{ObjectFit:"cover",...this.props?.style},key:this.key},this.key,null):t}}let vjsxx=new vjsx,Vader={createElement:vjsxx.createElement,useState:useState,instanizeClass:vjsxx.instanizeClass,render:vjsxx.render,useRef:useRef,isClass:vjsxx.isClass,Head:Head,Html:Html,Mounted:Mounted,Component:Component,Link:Link,Image:imgComponent};export default Vader;
|