@postact/jsx 0.0.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ # @postact/jsx
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.3.5. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
@@ -0,0 +1 @@
1
+ export { type JSX, jsx as jsxDEV, Fragment } from ".//jsx-runtime";
package/jsx-runtime.ts ADDED
@@ -0,0 +1,131 @@
1
+ import {
2
+ createVf,
3
+ PostactIdentifier,
4
+ transformArgToVirtualItem,
5
+ type Ref,
6
+ type Subscribable,
7
+ type VirtualElement,
8
+ type VirtualFragment,
9
+ type VirtualItem,
10
+ } from "@postact/core";
11
+
12
+ export declare namespace JSX {
13
+ type Element = VirtualItem;
14
+ type AnyChildren =
15
+ | Element
16
+ | string
17
+ | boolean
18
+ | number
19
+ | bigint
20
+ | null
21
+ | undefined
22
+ | Subscribable<AnyChildren>;
23
+
24
+ interface IntrinsicElementsProps {
25
+ /**
26
+ * Takes a `Ref` object so that you can interact with its
27
+ * native APIs whenever this element is rendered.
28
+ *
29
+ * The `Ref` object is a subscribable, subscribable via `.subscribe()`.
30
+ */
31
+ ref?: Ref<any>;
32
+ children?: AnyChildren | AnyChildren[];
33
+ }
34
+
35
+ type EventHandlers<T> = {
36
+ [K in keyof GlobalEventHandlersEventMap as `on${K}`]?: (
37
+ this: T,
38
+ event: GlobalEventHandlersEventMap[K],
39
+ ) => any;
40
+ };
41
+
42
+ type ElementAttributes<T> = {
43
+ [K in keyof T]?: T[K];
44
+ };
45
+ type ReadonlyDOMProps =
46
+ | "offsetWidth"
47
+ | "offsetHeight"
48
+ | "offsetTop"
49
+ | "offsetLeft"
50
+ | "clientWidth"
51
+ | "clientHeight"
52
+ | "clientTop"
53
+ | "clientLeft"
54
+ | "scrollWidth"
55
+ | "scrollHeight"
56
+ | "nodeName"
57
+ | "nodeType"
58
+ | "nodeValue"
59
+ | "parentNode"
60
+ | "childNodes"
61
+ | "firstChild"
62
+ | "lastChild"
63
+ | "previousSibling"
64
+ | "nextSibling"
65
+ | "attributes"
66
+ | "ownerDocument"
67
+ | "namespaceURI"
68
+ | "tagName"
69
+ | "innerHTML"
70
+ | "outerHTML"
71
+ | "textContent";
72
+
73
+ type ExcludedHTMLProps = "children" | "style" | ReadonlyDOMProps | keyof EventHandlers<any>;
74
+
75
+ type ExtractProps<T> = {
76
+ [K in keyof T]: T[K] extends Function ? never : K;
77
+ }[keyof T];
78
+
79
+ type ElementProps<T extends HTMLElement> = Partial<
80
+ Omit<Pick<T, ExtractProps<T>>, ExcludedHTMLProps>
81
+ >;
82
+
83
+ type HTMLAttributes<T extends HTMLElement> = ElementProps<T> &
84
+ EventHandlers<T> &
85
+ IntrinsicElementsProps & { ref?: Ref<T> };
86
+
87
+ type IntrinsicElementsBase = {
88
+ [K in keyof HTMLElementTagNameMap]: HTMLAttributes<HTMLElementTagNameMap[K]>;
89
+ };
90
+
91
+ interface IntrinsicElements extends IntrinsicElementsBase {
92
+ [elemName: string]: IntrinsicElementsProps;
93
+ }
94
+ }
95
+
96
+ const Fragment = Symbol();
97
+
98
+ function mapChildren(items: any[]) {
99
+ return items.map((item) => transformArgToVirtualItem(item));
100
+ }
101
+
102
+ function jsx(type: Symbol | Function | string, props: any): JSX.Element {
103
+ const hasChildren = typeof props.children !== "undefined" && props.children !== null;
104
+ const children = !hasChildren
105
+ ? []
106
+ : Array.isArray(props.children)
107
+ ? props.children
108
+ : [props.children];
109
+
110
+ if (type === Fragment) {
111
+ return createVf(mapChildren(children));
112
+ } else if (typeof type === "string") {
113
+ if (hasChildren) {
114
+ delete props["children"];
115
+ }
116
+
117
+ return {
118
+ __p: PostactIdentifier.VirtualElement,
119
+ tag: type,
120
+ attributes: props,
121
+ children: mapChildren(children),
122
+ listeners: [],
123
+ } satisfies VirtualElement;
124
+ } else if (typeof type === "function") {
125
+ return type(props);
126
+ } else {
127
+ throw new TypeError("unknown type");
128
+ }
129
+ }
130
+
131
+ export { jsx, jsx as jsxs, Fragment };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@postact/jsx",
3
+ "version": "0.0.1",
4
+ "description": "JSX support for Postact.",
5
+ "author": {
6
+ "name": "AWeirdDev",
7
+ "email": "awdjared@gmail.com",
8
+ "url": "https://github.com/AWeirdDev"
9
+ },
10
+ "license": "MIT",
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.cjs",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./jsx-runtime": {
20
+ "types": "./dist/jsx-runtime.d.ts",
21
+ "import": "./dist/jsx-runtime.mjs",
22
+ "require": "./dist/jsx-runtime.cjs",
23
+ "default": "./dist/jsx-runtime.js"
24
+ },
25
+ "./jsx-dev-runtime": {
26
+ "types": "./dist/jsx-runtime.d.ts",
27
+ "import": "./dist/jsx-runtime.mjs",
28
+ "require": "./dist/jsx-runtime.cjs",
29
+ "default": "./dist/jsx-runtime.js"
30
+ }
31
+ },
32
+ "scripts": {
33
+ "build": "tsc && rolldown -m -c",
34
+ "fmt": "oxfmt jsx-runtime.ts jsx-dev-runtime.ts test.tsx"
35
+ },
36
+ "dependencies": {
37
+ "@postact/core": "0.0.8"
38
+ },
39
+ "devDependencies": {
40
+ "@types/bun": "latest",
41
+ "oxfmt": "^0.24.0"
42
+ },
43
+ "peerDependencies": {
44
+ "typescript": "^5"
45
+ },
46
+ "private": false
47
+ }
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from "rolldown";
2
+
3
+ export default defineConfig({
4
+ input: ["./jsx-runtime.ts", "./jsx-dev-runtime.ts"],
5
+ output: [
6
+ {
7
+ dir: "./dist",
8
+ format: "esm",
9
+ sourcemap: true,
10
+ },
11
+ ],
12
+ external: [],
13
+ });
package/test.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var core_1 = require("@postact/core");
4
+ function Counter() {
5
+ var $count = (0, core_1.state)(0);
6
+ return <button>asdf</button>;
7
+ }
package/test.prod.js ADDED
@@ -0,0 +1 @@
1
+ var l=class extends Error{constructor(q){super(q)}};function g(){if(typeof window>"u")throw new l("expected the runtime context to be in the browser (window is undefined)")}var X=function(q){return q[q.Dependent=0]="Dependent",q[q.State=1]="State",q[q.VirtualElement=2]="VirtualElement",q[q.VirtualFragment=3]="VirtualFragment",q[q.VirtualTextNode=4]="VirtualTextNode",q[q.CSSPaper=5]="CSSPaper",q[q.ComponentPointer=6]="ComponentPointer",q[q.ComponentInstance=7]="ComponentInstance",q[q.Ref=8]="Ref",q}({});function H(q){return q===null?!1:!!(typeof q=="object"&&Object.hasOwn(q,"__p"))}function N(q,z){return z===null?!1:!!((typeof z=="object"||typeof z=="function")&&Object.hasOwn(z,"__p")&&z.__p==q)}function B(q=6){return Array.from({length:q},()=>Math.floor(Math.random()*16).toString(16)).join("")}function h(q){return N(X.VirtualElement,q)}function x(q){return N(X.VirtualFragment,q)}function P(q,z){return{__p:X.VirtualFragment,children:q,subscribable:z}}function d(q){return N(X.VirtualTextNode,q)}function Q(q,z){return{__p:X.VirtualTextNode,data:q,subscribable:z}}var u=class{value;#q;constructor(q){this.value=q,this.#q=new Map}subscribe(q){this.#q.set(q,q)}unsubscribe(q){this.#q.delete(q)}emit(){let q=this.value;this.#q.forEach((z)=>z(q))}};function T(q){return N(X.Dependent,q)||N(X.State,q)||N(X.Ref,q)}var t=class{__p=X.Ref;value;#q;constructor(){this.value=null,this.#q=new Map}subscribe(q){this.#q.set(q,q)}unsubscribe(q){this.#q.delete(q)}emit(){let q=this.value;if(q===null)throw Error("ref is currently null");this.#q.forEach((z)=>z(q))}};function o(q){return N(X.Ref,q)}function C(q,z){let G=window.document.createDocumentFragment();if(q==null)return G;if(typeof q=="string")return G.appendChild(window.document.createTextNode(q)),G;if(d(q)){let K=q,J=window.document.createTextNode(K.data);if(K.subscribable){let O=z.debug?B():"",Y=window.document.createComment(z.debug?`${O}`:""),M=window.document.createComment(z.debug?`/${O}`:""),U=new E(Y,M,G);K.subscribable.subscribe((Z)=>{J.parentNode&&U.setParent(J.parentNode);let m=_(Z,z);U.spreadAndReplace(m)}),G.append(Y,J,M)}else G.appendChild(J);return G}if(h(q)){let K=window.document.createElement(q.tag);return q.attributes.entries().forEach(([J,O])=>{if(O!=null){if(o(O)&&(O.value=K,O.emit()),T(O))return O.subscribe((Y)=>{L(K,J,Y)}),L(K,J,O.value);K.setAttribute(J,O.toString())}}),q.listeners.forEach(([J,O])=>K.addEventListener(J,O)),q.subscribable&&q.subscribable.subscribe((J)=>{let O=_(J,z);K.replaceChildren(O)}),K.append(...q.children.map((J)=>C(J,z))),G.appendChild(K),G}if(x(q)){let K=q.children.reduce((J,O)=>(J.append(C(O,z)),J),window.document.createDocumentFragment());if(q.subscribable){let J=z.debug?B():"",O=window.document.createComment(z.debug?`${J}`:""),Y=window.document.createComment(z.debug?`/${J}`:""),M=new E(O,Y,G);q.subscribable.subscribe((U)=>{O.parentNode&&M.setParent(O.parentNode);let Z=_(U,z);M.spreadAndReplace(Z)}),G.append(O,K,Y)}else G.appendChild(K);return G}else throw Error("unknown virtual item")}function _(q,z){if(q==null)return window.document.createDocumentFragment();if(F(q)){let G=window.document.createDocumentFragment();return G.appendChild(window.document.createTextNode(q.toString())),G}else return C(q,z)}var E=class{#q;#z;#G;constructor(q,z,G){this.#q=q,this.#z=z,this.#G=G}setParent(q){this.#G=q}spreadAndReplace(q){let z=this.#q.nextSibling;for(;z&&!this.#z.isEqualNode(z);){let G=z.nextSibling;this.#G.removeChild(z),z=G}this.#G.insertBefore(q,this.#z)}};function L(q,z,G){G==null?q.removeAttribute(z):q.setAttribute(z,G.toString())}var I={debug:!1};function b(q,z=I){return g(),C(q,z)}function c(q,z){q.replaceChildren(b(z))}function f(q){let z=window.document.querySelector(q);if(!z)throw ReferenceError(`could not find any element matching query: ${q}`);return z.render=(G)=>{c(z,G)},z}var p=Object.freeze({"&amp;":"&","&lt;":"<","&gt;":">","&quot;":'"',"&#39;":"'"}),k=/&(?:amp|lt|gt|quot|#39);/g;function A(q){return q&&k.test(q)?q.replace(k,(z)=>p[z]):q}function F(q){return["string","number","bigint","boolean"].includes(typeof q)}function D(q,z){return typeof z=="function"?z(q):z}var R=class{value;__p=X.State;#q;#z;constructor(q){this.value=q,this.#q=new Map,this.#z=[]}update(q){let z=D(this.value,q);for(let G of this.#z)if(!G(this.value,z))return;this.value=z,this.emit()}set(q){this.value=D(this.value,q)}subscribe(q){this.#q.set(q,q)}unsubscribe(q){this.#q.delete(q)}emit(){let q=this.value;this.#q.forEach((z,G)=>G(q))}withChecker(q){return this.#z.push(q),this}withCheckers(q){return this.#z.push(...q),this}};function j(q){return new R(q)}var e=class{__p=X.Dependent;#q;#z;#G;constructor(q,z,G){this.#z=G||z(Array.isArray(q)?q.map((K)=>K.value):q.value),this.#q=z,this.#G=new Map,Array.isArray(q)?q.forEach((K)=>{K.subscribe(()=>{let J=this.#q(q.map((O)=>O.value));this.#z=J,this.#G.forEach((O,Y)=>Y(J))})}):q.subscribe(()=>{let K=this.#q(q.value);this.#z=K,this.#G.forEach((J,O)=>O(K))})}get value(){return this.#z}subscribe(q){this.#G.set(q,q)}unsubscribe(q){this.#G.delete(q)}};var q0=class extends R{#q;#z;ok;constructor(q){super(null),this.#q=q,this.ok=!1,this.#z=()=>{},this.#q.then((z)=>this.update(z)).catch((z)=>this.#z&&this.#z(z))}catch(q){return this.#z=q,this}};var $=function(q){return q[q.Empty=0]="Empty",q[q.Text=1]="Text",q[q.Subscribable=2]="Subscribable",q[q.VirtualItem=3]="VirtualItem",q[q.Function=4]="Function",q}({});function v(q){return q==null?$.Empty:typeof q=="function"?$.Function:F(q)?$.Text:T(q)?$.Subscribable:$.VirtualItem}function S(q){switch(v(q)){case $.Empty:return null;case $.Text:return Q(q.toString());case $.Subscribable:let z=q,G=z.value;return G==null?Q("",z):F(G)?Q(G.toString(),z):P([G],z);case $.VirtualItem:return q;case $.Function:let K=q();if(K==null)return null;if(F(K))return Q(K.toString());if(!H(K))throw Error(`unresolvable value in children after function calling. value: ${K}`);return K}}function w(q){return N(X.ComponentPointer,q)}function a(q){return N(X.ComponentInstance,q)}var W=class q extends Error{constructor(z){super(z)}static noInsertInTagNames(){return new q("`${...}` is not allowed in tag names")}static noInsertInAttrNames(){return new q("`${...}` is not allowed in attribute names")}static invalidCharacterInTagName(z){return new q(`${z} is not a valid html tag character`)}static invalidCharacterInAttributeName(z){return new q(`${z} is not a valid html attribute character`)}static expectedQuote(){return new q('expected double quote (")')}static expectedAttrName(){return new q("expected attribute name, got empty")}static expectedAttrEqual(){return new q("expected equal sign (=) right after attribute name")}static expectedTagClosing(){return new q("expected tag to be closing (with a slash: /)")}static expectedTagOpening(){return new q("expected an opening tag")}static tagMismatch(z,G){return new q(`the starting and closing tags do not match: \`${z}\` and \`${G}\``)}static noBackslashBeforeInsert(){return new q("there should be no backslash (\\) before ${...}")}static typeCheckComponentProps(){return new q("**do not** add attributes like how you would in JSX.this may lead to runtime type inconsistencies. instead, run components (e.g., `Page`) with attributes like this: \n html`<${Page({ hello: 'world' })} />`\nthis ensures runtime type safety.")}},z0=class{#q;#z;#G;#J;constructor(q,z){this.#q=q,this.#z=z,this.#G=0,this.#J=0}next(){if(this.#G>=this.#q.length)return null;if(this.#q[this.#G].length==0)return this.#J=0,this.#G+=1,[!0,""];if(this.#J>=this.#q[this.#G].length)return null;let q=this.#q[this.#G],z=q[this.#J];return this.#J=(this.#J+1)%q.length,this.#G+=this.#J==0?1:0,[this.#J==0,z]}seek(){return this.#q[this.#G][this.#J]||null}getInsertion(){return this.#z[this.#G-1]??null}consume(){let q=[];for(;;){let z=this.next();if(!z)break;let[G,K]=z;if(K=="<"){if(G){let J=this.getInsertion();if(w(J)||a(J)){q.push(this.processComponent(J));continue}else throw W.noInsertInTagNames()}q.push(this.processElement())}else if(G){let J=S(this.getInsertion());J!==null&&q.push(J)}else if(!/\s/.test(K))throw W.expectedTagOpening()}return{__p:X.VirtualFragment,children:q}}processElement(){let[q,z,G,K]=this.consumeTag(),[J,O]=n(z);if(G)return{__p:X.VirtualElement,tag:q,attributes:O,children:[],listeners:J};let Y=this.consumeChildren(K),M=this.consumeEndTag();if(q!==M)throw W.tagMismatch(q,typeof M=="string"?M:"[component]");return{__p:X.VirtualElement,tag:q,attributes:O,children:Y,listeners:J}}processComponent(q){let[z,G,K]=this.consumeAttributes();if(z.size>0)throw W.typeCheckComponentProps();if(G)return q.ptr(w(q)?{}:q.props);let J=this.consumeChildren(K),O=this.consumeEndTag();if(typeof O!="function")throw W.tagMismatch("[component]",O);if(!w(O))throw TypeError("expected component pointer for end tag, got other functions instead");if(O.ptr!==q.ptr)throw W.tagMismatch("[component A]","[component B]");return q.ptr({children:P(J),...w(q)?{}:q.props})}consumeTag(){let q="";for(;;){let[z,G]=this.next();if(/\s/.test(G)){let[K,J,O]=this.consumeAttributes();return[q,K,J,O]}if(G==">")return[q,new Map,!1,z];if(z)throw W.noInsertInTagNames();if(!/[a-zA-Z0-9-]/.test(G))throw W.invalidCharacterInTagName(G);q+=G}}consumeAttributes(){let q=new Map,z="",G=0;for(;;){let[K,J]=this.next();if(J==">")return z&&q.set(z,"true"),[q,!1,K];if(J=="/"){let[Y,M]=this.consumeWhitespace();if(M!==">")throw W.expectedTagClosing();return z&&q.set(z,"true"),[q,!0,K]}if(G===0){if(!z&&(/\s/.test(J)||!J))continue;if(/\s|=/.test(J)){if(!z)throw W.expectedAttrName();G=1}else{if(K)throw W.noInsertInAttrNames();if(!/[a-zA-Z0-9-]/.test(J))throw W.invalidCharacterInAttributeName(J);z+=J;continue}}if(J===" "){q.set(z,"true"),z="",G=0;continue}if(J!=="=")throw W.expectedAttrEqual();let O=K?i(this.getInsertion()):this.consumeStringQuote();O!==null&&q.set(z,O),z="",G=0}}consumeEndTag(){let[q,z]=this.consumeWhitespace();if(z!=="/")throw W.expectedTagClosing();if(q){let J=this.getInsertion();if(typeof J=="function")return J;throw W.noInsertInTagNames()}let G="",K=0;for(;;){let[J,O]=this.next();if(O===">")return typeof G=="function"?G:G.trimEnd();if(J){let Y=this.getInsertion();if(typeof Y=="function"&&typeof G!="function")G=Y;else throw W.noInsertInTagNames()}else{if(typeof G=="function"||(/\s/.test(O)&&(K=1),K==0&&!/[a-zA-Z0-9-]/.test(O)))throw W.invalidCharacterInTagName(O);G+=O}}}consumeWhitespace(){let[q,z]=this.next();return/\s/.test(z)?this.consumeWhitespace():[q,z]}consumeStringQuote(){let q="",[z,G]=this.consumeWhitespace();if(G!=='"')throw W.expectedQuote();for(q+='"',z&&(q+=this.getInsertion().toString());;){let[K,J]=this.next();if(J=="\\"){let[O,Y]=this.next();if(O)throw W.noBackslashBeforeInsert();q+="\\"+Y;continue}if(J=='"')break;q+=J,K&&(q+=this.getInsertion().toString())}return JSON.parse(q+'"')}consumeChildren(q){let z="",G=[];for(q&&G.push(S(this.getInsertion()));;){let[K,J]=this.next();if(J==="<"){if(this.seek()==="/")break;z.trim()&&G.push(Q(A(z))),z="",G.push(this.processElement());continue}if(z+=J,K){z.trim()&&G.push(Q(A(z))),z="";let O=this.getInsertion();G.push(S(O))}}return z.trim()&&G.push(Q(A(z))),G}};function i(q){switch(v(q)){case $.Empty:return null;case $.Text:return q.toString();case $.Subscribable:return q;case $.VirtualItem:return null;case $.Function:return q}}function n(q){let z=[];for(let[G,K]of Object.entries(q))G.startsWith("on")&&typeof K=="function"&&(q.delete(G),z.push([G.slice(2),K]));return[z,q]}var G0=new u({pathname:"",hash:" "});var s=Symbol();function y(q){return q.map((z)=>S(z))}function V(q,z){let G=typeof z.children<"u"&&z.children!==null,K=!G?[]:Array.isArray(z.children)?z.children:[z.children];if(q===s)return P(y(K));else if(typeof q==="string"){if(G)delete z.children;return{__p:X.VirtualElement,tag:q,attributes:z,children:y(K),listeners:[]}}else if(typeof q==="function")return q(z)}function r(){let q=j(0);return V("button",{onclick:()=>q.update((z)=>z+1),children:["Current: ",q]})}f("#app").render(V(r,{}));
package/test.tsx ADDED
@@ -0,0 +1,8 @@
1
+ import { state, select } from "@postact/core";
2
+
3
+ function Counter() {
4
+ const $count = state(0);
5
+ return <button onclick={() => $count.update((v) => v + 1)}>Current: {$count}</button>;
6
+ }
7
+
8
+ select("#app").render(<Counter />);
package/tsconfig.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
4
+ "target": "ESNext",
5
+ "module": "ESNext",
6
+ "moduleDetection": "force",
7
+ "allowJs": true,
8
+
9
+ "jsx": "react-jsx",
10
+ "jsxImportSource": "./",
11
+
12
+ "declaration": true,
13
+ "declarationMap": true,
14
+ "emitDeclarationOnly": true,
15
+ "composite": true,
16
+
17
+ "outDir": "./dist",
18
+
19
+ "moduleResolution": "bundler",
20
+ "verbatimModuleSyntax": true,
21
+ "noEmit": false,
22
+ "strict": true,
23
+ "skipLibCheck": true,
24
+ "noFallthroughCasesInSwitch": true,
25
+ "noUncheckedIndexedAccess": true,
26
+ "noImplicitOverride": true,
27
+ "noUnusedLocals": false,
28
+ "noUnusedParameters": false,
29
+ "noPropertyAccessFromIndexSignature": false,
30
+ },
31
+ "exclude": ["./dist/**", "rolldown.config.ts"]
32
+ }