mini-jsx-test-pb 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/dist/index.d.ts +12 -0
- package/dist/index.js +83 -0
- package/package.json +25 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Effect = () => void;
|
|
2
|
+
declare function State<T>(initial: T): {
|
|
3
|
+
val: T;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
declare function effect(fn: Effect): void;
|
|
7
|
+
|
|
8
|
+
declare function h(type: any, props: any, ...children: any[]): any;
|
|
9
|
+
|
|
10
|
+
declare const version = "0.0.1";
|
|
11
|
+
|
|
12
|
+
export { State, effect, h, version };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/state.ts
|
|
2
|
+
var activeEffect = null;
|
|
3
|
+
function State(initial) {
|
|
4
|
+
let value = initial;
|
|
5
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
6
|
+
return {
|
|
7
|
+
get val() {
|
|
8
|
+
if (activeEffect) {
|
|
9
|
+
subscribers.add(activeEffect);
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
},
|
|
13
|
+
set val(next) {
|
|
14
|
+
if (Object.is(value, next)) return;
|
|
15
|
+
value = next;
|
|
16
|
+
subscribers.forEach((fn) => fn());
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function setActiveEffect(effect2) {
|
|
21
|
+
activeEffect = effect2;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/effect.ts
|
|
25
|
+
function effect(fn) {
|
|
26
|
+
const runner = () => {
|
|
27
|
+
setActiveEffect(runner);
|
|
28
|
+
fn();
|
|
29
|
+
setActiveEffect(null);
|
|
30
|
+
};
|
|
31
|
+
runner();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/jsx.ts
|
|
35
|
+
function h(type, props, ...children) {
|
|
36
|
+
if (typeof type === "function") {
|
|
37
|
+
return type({ ...props ?? {}, children });
|
|
38
|
+
}
|
|
39
|
+
const el = document.createElement(type);
|
|
40
|
+
if (props) {
|
|
41
|
+
for (const key in props) {
|
|
42
|
+
const value = props[key];
|
|
43
|
+
if (key.startsWith("on")) {
|
|
44
|
+
el.addEventListener(key.slice(2).toLowerCase(), value);
|
|
45
|
+
} else {
|
|
46
|
+
el.setAttribute(key, value);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
for (const child of children.flat()) {
|
|
51
|
+
mountChild(el, child);
|
|
52
|
+
}
|
|
53
|
+
return el;
|
|
54
|
+
}
|
|
55
|
+
function mountChild(parent, child) {
|
|
56
|
+
if (child == null || child === false) return;
|
|
57
|
+
if (typeof child === "function") {
|
|
58
|
+
const text = document.createTextNode("");
|
|
59
|
+
parent.appendChild(text);
|
|
60
|
+
effect(() => {
|
|
61
|
+
text.nodeValue = String(child());
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (typeof child === "object" && "val" in child) {
|
|
66
|
+
mountChild(parent, () => child.val);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (child instanceof Node) {
|
|
70
|
+
parent.appendChild(child);
|
|
71
|
+
} else {
|
|
72
|
+
parent.appendChild(document.createTextNode(String(child)));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/index.ts
|
|
77
|
+
var version = "0.0.1";
|
|
78
|
+
export {
|
|
79
|
+
State,
|
|
80
|
+
effect,
|
|
81
|
+
h,
|
|
82
|
+
version
|
|
83
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mini-jsx-test-pb",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
19
|
+
"dev": "tsup src/index.ts --format esm --dts --watch"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"tsup": "^8.5.1",
|
|
23
|
+
"typescript": "^5.9.3"
|
|
24
|
+
}
|
|
25
|
+
}
|