Skip to content

Commit 68b7eee

Browse files
committed
Upgrade Mini-Van to 0.5.7
1 parent 4bba1ff commit 68b7eee

35 files changed

+296
-43
lines changed

code/mini-van-0.5.7.d.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export interface State<T> {
2+
val: T
3+
readonly oldVal: T
4+
readonly rawVal: T
5+
}
6+
7+
// Defining readonly view of State<T> for covariance.
8+
// Basically we want StateView<string> to implement StateView<string | number>
9+
export type StateView<T> = Readonly<State<T>>
10+
11+
export type Primitive = string | number | boolean | bigint
12+
13+
export type PropValue = Primitive | ((e: any) => void) | null
14+
15+
export type Props = Record<string, PropValue | StateView<PropValue> | (() => PropValue)>
16+
17+
interface HasFirstChild {firstChild?: unknown}
18+
19+
type NodeType<ElementType extends HasFirstChild> =
20+
Omit<ElementType["firstChild"], "after" | "before" | "remove" | "replaceWith">
21+
22+
export type ValidChildDomValue<ElementType extends HasFirstChild, TextNodeType> =
23+
Primitive | ElementType | NodeType<ElementType> | TextNodeType | null | undefined
24+
25+
export type BindingFunc<ElementType extends HasFirstChild, TextNodeType> =
26+
| ((dom?: ElementType | TextNodeType) => ValidChildDomValue<ElementType, TextNodeType>)
27+
| ((dom?: ElementType) => ElementType)
28+
29+
export type ChildDom<ElementType extends HasFirstChild, TextNodeType> =
30+
| ValidChildDomValue<ElementType, TextNodeType>
31+
| StateView<Primitive | null | undefined>
32+
| BindingFunc<ElementType, TextNodeType>
33+
| readonly ChildDom<ElementType, TextNodeType>[]
34+
35+
type AddFunc<ElementType extends HasFirstChild, TextNodeType> =
36+
(dom: ElementType, ...children: readonly ChildDom<ElementType, TextNodeType>[]) => ElementType
37+
38+
export type TagFunc<ElementType extends HasFirstChild, TextNodeType, ResultType = ElementType> =
39+
(first?: Props | ChildDom<ElementType, TextNodeType>,
40+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => ResultType
41+
42+
type Tags<ElementType extends HasFirstChild, TextNodeType> =
43+
Readonly<Record<string, TagFunc<ElementType, TextNodeType>>>
44+
45+
// Tags type in browser context, which contains the signatures to tag functions that return
46+
// specialized DOM elements.
47+
type BrowserTags = Tags<Element, Text> & {
48+
[K in keyof HTMLElementTagNameMap]: TagFunc<Element, Text, HTMLElementTagNameMap[K]>
49+
}
50+
51+
declare function state<T>(): State<T>
52+
declare function state<T>(initVal: T): State<T>
53+
54+
export interface VanObj<ElementType extends HasFirstChild, TextNodeType> {
55+
readonly state: typeof state
56+
readonly derive: <T>(f: () => T) => State<T>
57+
readonly add: AddFunc<ElementType, TextNodeType>
58+
readonly tags: Tags<ElementType, TextNodeType> & ((namespaceURI: string) => Tags<ElementType, TextNodeType>)
59+
60+
// Mini-Van specific API
61+
html: (first?: Props | ChildDom<ElementType, TextNodeType>,
62+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => string
63+
}
64+
65+
export interface Van extends VanObj<Element, Text> {
66+
readonly vanWithDoc: <ElementType extends HasFirstChild, TextNodeType>(doc: {
67+
createElement(s: any): ElementType,
68+
createTextNode(s: any): TextNodeType,
69+
}) => VanObj<ElementType, TextNodeType>
70+
readonly tags: BrowserTags & ((namespaceURI: string) => Tags<Element, Text>)
71+
}
72+
73+
declare const van: Van
74+
75+
export default van

code/mini-van-0.5.7.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// <reference types="./mini-van.d.ts" />
2+
3+
// This file consistently uses `let` keyword instead of `const` for reducing the bundle size.
4+
5+
// Aliasing some builtin symbols to reduce the bundle size.
6+
let protoOf = Object.getPrototypeOf, _undefined, funcProto = protoOf(protoOf)
7+
8+
let stateProto = {get oldVal() { return this.val }, get rawVal() { return this.val }}
9+
let objProto = protoOf(stateProto)
10+
11+
let state = initVal => ({__proto__: stateProto, val: initVal})
12+
13+
let plainValue = (k, v) => {
14+
let protoOfV = protoOf(v ?? 0)
15+
return protoOfV === stateProto ? v.val :
16+
protoOfV !== funcProto || k?.startsWith("on") ? v : v()
17+
}
18+
19+
let add = (dom, ...children) =>
20+
(dom.append(...children.flat(Infinity)
21+
.map(plainValue.bind(_undefined, _undefined))
22+
.filter(c => c != _undefined)),
23+
dom)
24+
25+
let vanWithDoc = doc => {
26+
let tag = (ns, name, ...args) => {
27+
let [props, ...children] = protoOf(args[0] ?? 0) === objProto ? args : [{}, ...args]
28+
let dom = ns ? doc.createElementNS(ns, name) : doc.createElement(name)
29+
for (let [k, v] of Object.entries(props)) {
30+
let plainV = plainValue(k, v)
31+
// Disable setting attribute for function-valued properties (mostly event handlers),
32+
// as they're usually not useful for SSR (server-side rendering).
33+
protoOf(plainV) !== funcProto && dom.setAttribute(k, plainV)
34+
}
35+
return add(dom, ...children)
36+
}
37+
38+
let handler = ns => ({get: (_, name) => tag.bind(_undefined, ns, name)})
39+
let tags = new Proxy(ns => new Proxy(tag, handler(ns)), handler())
40+
41+
return {
42+
add, tags, state, derive: f => state(f()),
43+
html: (...args) => "<!DOCTYPE html>" + tags.html(...args).outerHTML,
44+
}
45+
}
46+
47+
export default {"vanWithDoc": vanWithDoc,
48+
...vanWithDoc(typeof window !== "undefined" ? window.document : null)}

code/mini-van-0.5.7.min.d.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export interface State<T> {
2+
val: T
3+
readonly oldVal: T
4+
readonly rawVal: T
5+
}
6+
7+
// Defining readonly view of State<T> for covariance.
8+
// Basically we want StateView<string> to implement StateView<string | number>
9+
export type StateView<T> = Readonly<State<T>>
10+
11+
export type Primitive = string | number | boolean | bigint
12+
13+
export type PropValue = Primitive | ((e: any) => void) | null
14+
15+
export type Props = Record<string, PropValue | StateView<PropValue> | (() => PropValue)>
16+
17+
interface HasFirstChild {firstChild?: unknown}
18+
19+
type NodeType<ElementType extends HasFirstChild> =
20+
Omit<ElementType["firstChild"], "after" | "before" | "remove" | "replaceWith">
21+
22+
export type ValidChildDomValue<ElementType extends HasFirstChild, TextNodeType> =
23+
Primitive | ElementType | NodeType<ElementType> | TextNodeType | null | undefined
24+
25+
export type BindingFunc<ElementType extends HasFirstChild, TextNodeType> =
26+
| ((dom?: ElementType | TextNodeType) => ValidChildDomValue<ElementType, TextNodeType>)
27+
| ((dom?: ElementType) => ElementType)
28+
29+
export type ChildDom<ElementType extends HasFirstChild, TextNodeType> =
30+
| ValidChildDomValue<ElementType, TextNodeType>
31+
| StateView<Primitive | null | undefined>
32+
| BindingFunc<ElementType, TextNodeType>
33+
| readonly ChildDom<ElementType, TextNodeType>[]
34+
35+
type AddFunc<ElementType extends HasFirstChild, TextNodeType> =
36+
(dom: ElementType, ...children: readonly ChildDom<ElementType, TextNodeType>[]) => ElementType
37+
38+
export type TagFunc<ElementType extends HasFirstChild, TextNodeType, ResultType = ElementType> =
39+
(first?: Props | ChildDom<ElementType, TextNodeType>,
40+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => ResultType
41+
42+
type Tags<ElementType extends HasFirstChild, TextNodeType> =
43+
Readonly<Record<string, TagFunc<ElementType, TextNodeType>>>
44+
45+
// Tags type in browser context, which contains the signatures to tag functions that return
46+
// specialized DOM elements.
47+
type BrowserTags = Tags<Element, Text> & {
48+
[K in keyof HTMLElementTagNameMap]: TagFunc<Element, Text, HTMLElementTagNameMap[K]>
49+
}
50+
51+
declare function state<T>(): State<T>
52+
declare function state<T>(initVal: T): State<T>
53+
54+
export interface VanObj<ElementType extends HasFirstChild, TextNodeType> {
55+
readonly state: typeof state
56+
readonly derive: <T>(f: () => T) => State<T>
57+
readonly add: AddFunc<ElementType, TextNodeType>
58+
readonly tags: Tags<ElementType, TextNodeType> & ((namespaceURI: string) => Tags<ElementType, TextNodeType>)
59+
60+
// Mini-Van specific API
61+
html: (first?: Props | ChildDom<ElementType, TextNodeType>,
62+
...rest: readonly ChildDom<ElementType, TextNodeType>[]) => string
63+
}
64+
65+
export interface Van extends VanObj<Element, Text> {
66+
readonly vanWithDoc: <ElementType extends HasFirstChild, TextNodeType>(doc: {
67+
createElement(s: any): ElementType,
68+
createTextNode(s: any): TextNodeType,
69+
}) => VanObj<ElementType, TextNodeType>
70+
readonly tags: BrowserTags & ((namespaceURI: string) => Tags<Element, Text>)
71+
}
72+
73+
declare const van: Van
74+
75+
export default van

code/mini-van-0.5.7.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/mini-van-0.5.7.nomodule.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(() => {
2+
// mini-van.js
3+
var protoOf = Object.getPrototypeOf;
4+
var _undefined;
5+
var funcProto = protoOf(protoOf);
6+
var stateProto = { get oldVal() {
7+
return this.val;
8+
}, get rawVal() {
9+
return this.val;
10+
} };
11+
var objProto = protoOf(stateProto);
12+
var state = (initVal) => ({ __proto__: stateProto, val: initVal });
13+
var plainValue = (k, v) => {
14+
let protoOfV = protoOf(v ?? 0);
15+
return protoOfV === stateProto ? v.val : protoOfV !== funcProto || k?.startsWith("on") ? v : v();
16+
};
17+
var add = (dom, ...children) => (dom.append(...children.flat(Infinity).map(plainValue.bind(_undefined, _undefined)).filter((c) => c != _undefined)), dom);
18+
var vanWithDoc = (doc) => {
19+
let tag = (ns, name, ...args) => {
20+
let [props, ...children] = protoOf(args[0] ?? 0) === objProto ? args : [{}, ...args];
21+
let dom = ns ? doc.createElementNS(ns, name) : doc.createElement(name);
22+
for (let [k, v] of Object.entries(props)) {
23+
let plainV = plainValue(k, v);
24+
protoOf(plainV) !== funcProto && dom.setAttribute(k, plainV);
25+
}
26+
return add(dom, ...children);
27+
};
28+
let handler = (ns) => ({ get: (_, name) => tag.bind(_undefined, ns, name) });
29+
let tags = new Proxy((ns) => new Proxy(tag, handler(ns)), handler());
30+
return {
31+
add,
32+
tags,
33+
state,
34+
derive: (f) => state(f()),
35+
html: (...args) => "<!DOCTYPE html>" + tags.html(...args).outerHTML
36+
};
37+
};
38+
var mini_van_default = {
39+
"vanWithDoc": vanWithDoc,
40+
...vanWithDoc(typeof window !== "undefined" ? window.document : null)
41+
};
42+
43+
// mini-van.forbundle.js
44+
window.van = mini_van_default;
45+
})();

code/mini-van-0.5.7.nomodule.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/mini-van-latest.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface State<T> {
22
val: T
33
readonly oldVal: T
4+
readonly rawVal: T
45
}
56

67
// Defining readonly view of State<T> for covariance.

code/mini-van-latest.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// Aliasing some builtin symbols to reduce the bundle size.
66
let protoOf = Object.getPrototypeOf, _undefined, funcProto = protoOf(protoOf)
77

8-
let stateProto = {get oldVal() { return this.val }}, objProto = protoOf(stateProto)
8+
let stateProto = {get oldVal() { return this.val }, get rawVal() { return this.val }}
9+
let objProto = protoOf(stateProto)
910

1011
let state = initVal => ({__proto__: stateProto, val: initVal})
1112

code/mini-van-latest.min.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface State<T> {
22
val: T
33
readonly oldVal: T
4+
readonly rawVal: T
45
}
56

67
// Defining readonly view of State<T> for covariance.

code/mini-van-latest.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/mini-van-latest.nomodule.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
var funcProto = protoOf(protoOf);
66
var stateProto = { get oldVal() {
77
return this.val;
8+
}, get rawVal() {
9+
return this.val;
810
} };
911
var objProto = protoOf(stateProto);
1012
var state = (initVal) => ({ __proto__: stateProto, val: initVal });

code/mini-van-latest.nomodule.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/mini-van.version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.6
1+
0.5.7

hydration-example/package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hydration-example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"dependencies": {
1919
"finalhandler": "^1.2.0",
20-
"mini-van-plate": "^0.5.6",
20+
"mini-van-plate": "^0.5.7",
2121
"serve-static": "^1.15.0",
2222
"vanjs-core": "^1.5.0"
2323
}

jsfiddle/minivan/hello/demo.details

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
authors:
44
- Tao Xin
55
resources:
6-
- https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.6.nomodule.min.js
6+
- https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.7.nomodule.min.js
77
panel_js: 2
88
...
99
*/

0 commit comments

Comments
 (0)