Skip to content

Commit e604426

Browse files
committed
0.5.2: Simplify type definitions.
1 parent 121e2f9 commit e604426

20 files changed

+492
-440
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Sample code:
154154
_Requires Deno `1.35` or later._
155155
156156
```typescript
157-
import van from "https://deno.land/x/[email protected].0/src/van-plate.js"
157+
import van from "https://deno.land/x/[email protected].2/src/van-plate.js"
158158

159159
const {a, body, li, p, ul} = van.tags
160160

@@ -195,7 +195,7 @@ _Requires Deno `1.35` or later._
195195
196196
```typescript
197197
import { DOMParser } from "https://deno.land/x/[email protected]/deno-dom-wasm.ts"
198-
import van from "https://deno.land/x/[email protected].0/src/mini-van.js"
198+
import van from "https://deno.land/x/[email protected].2/src/mini-van.js"
199199

200200
const document = new DOMParser().parseFromString("", "text/html")!
201201
const {tags, html} = van.vanWithDoc(document)
@@ -235,16 +235,16 @@ Preview via [CodeSandbox](https://codesandbox.io/p/sandbox/github/vanjs-org/vanj
235235
To get started on the client side, add the line below to your script:
236236
237237
```javascript
238-
import van from "https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.0.min.js"
238+
import van from "https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.2.min.js"
239239
```
240240
241241
To code without ES6 modules, add the following line to your HTML file instead:
242242
243243
```html
244-
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.0.nomodule.min.js"></script>
244+
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/vanjs-org/mini-van/public/mini-van-0.5.2.nomodule.min.js"></script>
245245
```
246246
247-
Alternative, you can download the files ([`mini-van-0.5.0.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.0.min.js), [`mini-van-0.5.0.nomodule.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.0.nomodule.min.js)) and serve them locally.
247+
Alternative, you can download the files ([`mini-van-0.5.2.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.2.min.js), [`mini-van-0.5.2.nomodule.min.js`](https://vanjs.org/autodownload?file=mini-van-0.5.2.nomodule.min.js)) and serve them locally.
248248
249249
You can find all relevant **Mini-Van** files in this [Download Table](https://vanjs.org/minivan#download-table).
250250

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mini-van-plate",
3-
"version": "0.5.0",
3+
"version": "0.5.2",
44
"description": "A minimalist template engine for DOM generation and manipulation, working for both client-side and server-side rendering",
55
"files": [
66
"src/mini-van.js",

public/mini-van-0.5.1.d.ts

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

public/mini-van-0.5.1.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 }}, objProto = protoOf(stateProto)
9+
10+
let state = initVal => ({__proto__: stateProto, val: initVal})
11+
12+
let val = s => protoOf(s ?? 0) === stateProto ? s.val : s
13+
14+
let plainValue = (k, v) => {
15+
let protoOfV = protoOf(v ?? 0)
16+
return protoOfV === stateProto ? v.val :
17+
protoOfV === funcProto && (!k?.startsWith("on") || v._isBindingFunc) ? v() : v
18+
}
19+
20+
let add = (dom, ...children) =>
21+
(dom.append(...children.flat(Infinity)
22+
.map(plainValue.bind(_undefined, _undefined))
23+
.filter(c => c != _undefined)),
24+
dom)
25+
26+
let vanWithDoc = doc => {
27+
let tagsNS = ns => new Proxy((name, ...args) => {
28+
let [props, ...children] = protoOf(args[0] ?? 0) === objProto ? args : [{}, ...args]
29+
let dom = ns ? doc.createElementNS(ns, name) : doc.createElement(name)
30+
for (let [k, v] of Object.entries(props)) {
31+
let plainV = plainValue(k, v)
32+
// Disable setting attribute for function-valued properties (mostly event handlers),
33+
// as they're usually not useful for SSR (server-side rendering).
34+
protoOf(plainV) !== funcProto && dom.setAttribute(k, plainV)
35+
}
36+
return add(dom, ...children)
37+
}, {get: (tag, name) => tag.bind(null, name)})
38+
39+
let tags = tagsNS()
40+
41+
return {
42+
add, _: f => (f._isBindingFunc = 1, f), tags, tagsNS, state,
43+
val, oldVal: val, derive: f => state(f()),
44+
html: (...args) => "<!DOCTYPE html>" + tags.html(...args).outerHTML,
45+
}
46+
}
47+
48+
export default {"vanWithDoc": vanWithDoc,
49+
...vanWithDoc(typeof window !== "undefined" ? window.document : null)}

public/mini-van-0.5.1.min.d.ts

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

public/mini-van-0.5.1.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mini-van-0.5.1.nomodule.js

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

public/mini-van-0.5.1.nomodule.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mini-van-0.5.2.d.ts

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

0 commit comments

Comments
 (0)