@snupo/jmc 1.0.0

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.
Files changed (58) hide show
  1. package/coverage/base.css +224 -0
  2. package/coverage/base.ts.html +172 -0
  3. package/coverage/block-navigation.js +87 -0
  4. package/coverage/coverage-final.json +7 -0
  5. package/coverage/favicon.png +0 -0
  6. package/coverage/h.ts.html +490 -0
  7. package/coverage/index.html +191 -0
  8. package/coverage/index.ts.html +97 -0
  9. package/coverage/jmc.ts.html +562 -0
  10. package/coverage/jsx.ts.html +2446 -0
  11. package/coverage/lib.ts.html +157 -0
  12. package/coverage/prettify.css +1 -0
  13. package/coverage/prettify.js +2 -0
  14. package/coverage/sort-arrow-sprite.png +0 -0
  15. package/coverage/sorter.js +210 -0
  16. package/dist/app.js +65602 -0
  17. package/dist/magic-string.es-hwNd2_8t.js +1012 -0
  18. package/dist/rolldown-runtime-C_rDcCAf.js +43 -0
  19. package/dist/src/base.d.ts +7 -0
  20. package/dist/src/base.d.ts.map +1 -0
  21. package/dist/src/base.js +23 -0
  22. package/dist/src/base.js.map +1 -0
  23. package/dist/src/h.d.ts +65 -0
  24. package/dist/src/h.d.ts.map +1 -0
  25. package/dist/src/h.js +21 -0
  26. package/dist/src/h.js.map +1 -0
  27. package/dist/src/index.d.ts +6 -0
  28. package/dist/src/index.d.ts.map +1 -0
  29. package/dist/src/index.js +6 -0
  30. package/dist/src/index.js.map +1 -0
  31. package/dist/src/jmc.d.ts +5 -0
  32. package/dist/src/jmc.d.ts.map +1 -0
  33. package/dist/src/jmc.js +123 -0
  34. package/dist/src/jmc.js.map +1 -0
  35. package/dist/src/jsx.d.ts +697 -0
  36. package/dist/src/jsx.d.ts.map +1 -0
  37. package/dist/src/jsx.js +21 -0
  38. package/dist/src/jsx.js.map +1 -0
  39. package/dist/src/lib.d.ts +21 -0
  40. package/dist/src/lib.d.ts.map +1 -0
  41. package/dist/src/lib.js +19 -0
  42. package/dist/src/lib.js.map +1 -0
  43. package/package.json +29 -0
  44. package/src/base.ts +27 -0
  45. package/src/h.ts +118 -0
  46. package/src/index.ts +5 -0
  47. package/src/jmc.ts +139 -0
  48. package/src/jsx.ts +787 -0
  49. package/src/lib.ts +25 -0
  50. package/tests/classcomponent.test.tsx +170 -0
  51. package/tests/functioncomponent.test.tsx +41 -0
  52. package/tests/h.test.tsx +312 -0
  53. package/tests/jmc.test.tsx +329 -0
  54. package/tsconfig.build.json +12 -0
  55. package/tsconfig.json +42 -0
  56. package/tsdown.config.ts +5 -0
  57. package/vite.config.mts +31 -0
  58. package/vitest.config.mts +34 -0
package/src/lib.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { Children, IClassComponent, IUpdateContext, H, J } from './h';
2
+ import { updateview } from './jmc'
3
+
4
+ export type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }
5
+
6
+ export const When = ({ cond }: { cond: boolean }, cn: Children) => cond ? { cn } : void 0;
7
+
8
+ export abstract class JMXComp<P extends J = {}> implements IClassComponent {
9
+
10
+ element!: HTMLElement // assigned by jmx framework
11
+ constructor(public props: P) { }
12
+
13
+ // overrides
14
+ mounted() { }
15
+ update(uc?: IUpdateContext): boolean | void { }
16
+ abstract view(): H
17
+
18
+ // utility: updates the component's view
19
+ updateview() { updateview(this.element) }
20
+ get ismounted():boolean { return this.element as any }
21
+ }
22
+
23
+ export function cc(...namesOrObjects: (string | any)[]): string {
24
+ return namesOrObjects.flatMap(n => (n.trim ? n : Object.keys(n).filter(k => n[k]))).join(' ') // n.trim distinguishes strings from objects
25
+ }
@@ -0,0 +1,170 @@
1
+ import { beforeEach, describe, expect, it, test, vitest } from 'vitest'
2
+ import { H, HtmlHTMLAttributes, IntrinsicElementAttributes, JMXComp, jsx, patch, updateview } from '../src';
3
+ import { Window } from 'happy-dom'
4
+
5
+ beforeEach(() => {
6
+ const window = new Window({ url: "http://localhost/" })
7
+ window.document.documentElement.innerHTML = `<html><body></body></html>`
8
+ // @ts-expect-error assigning globals
9
+ globalThis.window = window
10
+ // @ts-expect-error assigning globals
11
+ globalThis.document = window.document
12
+ })
13
+
14
+ describe('class components', () => {
15
+
16
+ test('class component renders', () => {
17
+
18
+ class C extends JMXComp {
19
+ view(): H {
20
+ return <div class="class component C">hi ho</div>
21
+ }
22
+ }
23
+
24
+ patch(document.body, <body><C /></body>)
25
+ expect(document.body.outerHTML).toBe('<body><div class="class component C">hi ho</div></body>')
26
+ })
27
+
28
+ test('class component underived renders', () => {
29
+
30
+ class C {
31
+ view(): H {
32
+ return <div class="class component C">hi ho</div>
33
+ }
34
+ }
35
+
36
+ patch(document.body, <body><C /></body>)
37
+ expect(document.body.outerHTML).toBe('<body><div class="class component C">hi ho</div></body>')
38
+ })
39
+
40
+ test('class component update with straight jsc update', () => {
41
+
42
+ let m = {
43
+ name: "lunar",
44
+ css: "mondo"
45
+ }
46
+
47
+ class C extends JMXComp<{ name: string, css: string }> {
48
+ view(): H {
49
+ return <bunny class={this.props.css} carrot={{ grade: 4, breed: "fluffy" }}>{this.props.name}</bunny>
50
+ }
51
+ }
52
+
53
+ patch(document.body, <body><C {...m} /></body>)
54
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo">lunar</bunny></body>')
55
+
56
+
57
+ m.name = "solar"
58
+ m.css = "jupiter"
59
+ updateview("bunny")
60
+ expect(document.body.outerHTML).toBe('<body><bunny class="jupiter">solar</bunny></body>')
61
+ })
62
+
63
+ test('class component update with straight jsc update', () => {
64
+
65
+ let m = {
66
+ name: "lunar",
67
+ css: "mondo"
68
+ }
69
+
70
+ class C extends JMXComp<{ name: string, css: string }> {
71
+ view(): H {
72
+ return <bunny class={this.props.css} carrot={{ grade: 4, breed: "fluffy" }}>{this.props.name}</bunny>
73
+ }
74
+ }
75
+
76
+ patch(document.body, <body><C {...m} /></body>)
77
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo">lunar</bunny></body>')
78
+
79
+
80
+ m.name = "solar"
81
+ m.css = "jupiter"
82
+ updateview("bunny")
83
+ expect(document.body.outerHTML).toBe('<body><bunny class="jupiter">solar</bunny></body>')
84
+ })
85
+
86
+ test('class component update with empty update returning true', () => {
87
+
88
+ let m = {
89
+ name: "lunar",
90
+ css: "mondo"
91
+ }
92
+
93
+ class C extends JMXComp<{ name: string, css: string }> {
94
+ view(): H {
95
+ return <bunny class={this.props.css} carrot={{ grade: 4, breed: "fluffy" }}>{this.props.name}</bunny>
96
+ }
97
+ update() {
98
+ return true // no update
99
+ }
100
+ }
101
+
102
+ patch(document.body, <body><C {...m} /></body>)
103
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo">lunar</bunny></body>')
104
+
105
+ m.name = "solar"
106
+ m.css = "jupiter"
107
+ updateview("bunny")
108
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo">lunar</bunny></body>')
109
+ })
110
+
111
+ test('class component update with empty update returning false', () => {
112
+
113
+ let m = {
114
+ name: "lunar",
115
+ css: "mondo"
116
+ }
117
+
118
+ class C extends JMXComp<{ name: string, css: string }> {
119
+ view(): H {
120
+ return <bunny class={this.props.css} carrot={{ grade: 4, breed: "fluffy" }}>{this.props.name}</bunny>
121
+ }
122
+ update() {
123
+ return false // no update
124
+ }
125
+ }
126
+
127
+ patch(document.body, <body><C {...m} /></body>)
128
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo">lunar</bunny></body>')
129
+
130
+ m.name = "solar"
131
+ m.css = "jupiter"
132
+ updateview("bunny")
133
+ expect(document.body.outerHTML).toBe('<body><bunny class="jupiter">solar</bunny></body>')
134
+ })
135
+
136
+ test('class component update with empty update modifying', () => {
137
+
138
+ let m = {
139
+ name: "lunar",
140
+ css: "mondo"
141
+ }
142
+
143
+ class C extends JMXComp<{ name: string, css: string }> {
144
+ view(): H {
145
+ return <bunny class={this.props.css} carrot={{ grade: 4, breed: "fluffy" }}>{this.props.name}</bunny>
146
+ }
147
+ update() {
148
+ this.element.replaceChildren()
149
+ this.element.setAttribute("changed", "now")
150
+ return true // no default update
151
+ }
152
+ }
153
+
154
+ patch(document.body, <body><C {...m} /></body>)
155
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo">lunar</bunny></body>')
156
+
157
+ m.name = "solar"
158
+ m.css = "jupiter"
159
+ updateview("bunny")
160
+ expect(document.body.outerHTML).toBe('<body><bunny class="mondo" changed="now"></bunny></body>')
161
+ })
162
+ })
163
+
164
+ declare global {
165
+ namespace JSX {
166
+ interface IntrinsicElements extends IntrinsicElementAttributes {
167
+ bunny: HtmlHTMLAttributes & { carrot: { grade: number, breed?: string } }
168
+ }
169
+ }
170
+ }
@@ -0,0 +1,41 @@
1
+ import { beforeEach, describe, expect, it, test, vitest } from 'vitest'
2
+ import { jsx, patch, updateview } from '../src';
3
+ import { Window } from 'happy-dom'
4
+
5
+ beforeEach(() => {
6
+ const window = new Window({ url: "http://localhost/" })
7
+ window.document.documentElement.innerHTML = `<html><body></body></html>`
8
+ // @ts-expect-error assigning globals
9
+ globalThis.window = window
10
+ // @ts-expect-error assigning globals
11
+ globalThis.document = window.document
12
+ })
13
+
14
+ describe('function components', () => {
15
+
16
+ test('creates correct html', () => {
17
+
18
+ let F = () => <div><b>bunny</b></div>
19
+
20
+ patch(document.body, <body><F /></body>)
21
+
22
+ expect(document.body.outerHTML).toBe('<body><div><b>bunny</b></div></body>')
23
+ });
24
+
25
+ test('updates correct html', () => {
26
+
27
+ let name = "buster"
28
+ let F = () => <div><b>{name} bunny</b></div>
29
+
30
+ patch(document.body, <body><F /></body>)
31
+ expect(document.body.outerHTML).toBe('<body><div><b>buster bunny</b></div></body>')
32
+
33
+ name = "babsi"
34
+ updateview("b")
35
+ expect(document.body.outerHTML, "b is below component, therefore does not update").toBe('<body><div><b>buster bunny</b></div></body>')
36
+
37
+ updateview("div")
38
+ expect(document.body.outerHTML, "div is the component, therefore updates").toBe('<body><div><b>babsi bunny</b></div></body>')
39
+ });
40
+
41
+ })
@@ -0,0 +1,312 @@
1
+ import {
2
+ jsx,
3
+ jsxf,
4
+ HElement,
5
+ HKind,
6
+ HFragment,
7
+ HClassComponent,
8
+ HFunctionComponent,
9
+ } from '../src/index'
10
+ import { describe, expect, it, test, vitest } from 'vitest'
11
+
12
+ describe('jsx hyperscript', () => {
13
+ it('HElement empty', () => {
14
+ let h: HElement = <div />
15
+
16
+ let e = {
17
+ kind: HKind.Element,
18
+ tag: 'div',
19
+ }
20
+
21
+ expect(h).toEqual(e)
22
+ })
23
+
24
+ it('HElement with properties', () => {
25
+ let h: HElement = <div class='red' title='snup' />
26
+
27
+ let e = {
28
+ kind: HKind.Element,
29
+ tag: 'div',
30
+ p: {
31
+ class: 'red',
32
+ title: 'snup',
33
+ },
34
+ }
35
+
36
+ expect(h).toEqual(e)
37
+ })
38
+
39
+ it('HElement with properties and children', () => {
40
+ let h: HElement = (
41
+ <div class='red' title='snup'>
42
+ <b>boldy</b>
43
+ </div>
44
+ )
45
+
46
+ let e = {
47
+ kind: HKind.Element,
48
+ tag: 'div',
49
+ p: {
50
+ class: 'red',
51
+ title: 'snup',
52
+ },
53
+ cn: [
54
+ {
55
+ kind: HKind.Element,
56
+ tag: 'b',
57
+ cn: ['boldy'],
58
+ },
59
+ ],
60
+ }
61
+
62
+ expect(h).toEqual(e)
63
+ })
64
+
65
+ it('HFragment empty', () => {
66
+ let h: HFragment = <></>
67
+
68
+ let e = {
69
+ kind: HKind.Fragment,
70
+ }
71
+
72
+ expect(h).toEqual(e)
73
+ })
74
+
75
+ it('HFragment with children', () => {
76
+ let h: HFragment = (
77
+ <>
78
+ <b>boldy</b>
79
+ </>
80
+ )
81
+
82
+ let e = {
83
+ kind: HKind.Fragment,
84
+ cn: [
85
+ {
86
+ kind: HKind.Element,
87
+ tag: 'b',
88
+ cn: ['boldy'],
89
+ },
90
+ ],
91
+ }
92
+
93
+ expect(h).toEqual(e)
94
+ })
95
+
96
+ it('HFragment nested', () => {
97
+ let h: HFragment = (
98
+ <>
99
+ <></>
100
+ </>
101
+ )
102
+
103
+ let e = {
104
+ kind: HKind.Fragment,
105
+ cn: [
106
+ {
107
+ kind: HKind.Fragment,
108
+ },
109
+ ],
110
+ }
111
+
112
+ expect(h).toEqual(e)
113
+ })
114
+
115
+ test('HClassComponent ', () => {
116
+ class C { }
117
+
118
+ let h: HClassComponent = <C />
119
+
120
+ let e = {
121
+ kind: HKind.ClassComponent,
122
+ tag: C,
123
+ }
124
+
125
+ expect(h).toEqual(e)
126
+ })
127
+
128
+ test('HClassComponent with props and children', () => {
129
+ class C { }
130
+
131
+ let h: HClassComponent = <C class='red'>hops</C>
132
+
133
+ let e = {
134
+ kind: HKind.ClassComponent,
135
+ tag: C,
136
+ p: { class: 'red' },
137
+ cn: ['hops'],
138
+ }
139
+
140
+ expect(h).toEqual(e)
141
+ })
142
+
143
+ test('HFunctionComponent ', () => {
144
+ function F() { }
145
+
146
+ let h: HFunctionComponent = <F />
147
+
148
+ let e = {
149
+ kind: HKind.FunctionComponent,
150
+ tag: F,
151
+ }
152
+
153
+ expect(h).toEqual(e)
154
+ })
155
+
156
+ test('HFunctionComponent with props and children', () => {
157
+ function F() { }
158
+
159
+ let h: HFunctionComponent = (
160
+ <F class='red'>
161
+ <i>italy</i>
162
+ </F>
163
+ )
164
+
165
+ let e = {
166
+ kind: HKind.FunctionComponent,
167
+ tag: F,
168
+ p: { class: 'red' },
169
+ cn: [
170
+ {
171
+ kind: HKind.Element,
172
+ tag: 'i',
173
+ cn: ['italy'],
174
+ },
175
+ ],
176
+ }
177
+
178
+ expect(h).toEqual(e)
179
+ })
180
+
181
+ test('jsx 1', () => {
182
+ let F = ({ a }: { a: number[] }) => (
183
+ <>
184
+ {a.map((x) => (
185
+ <div>{x}</div>
186
+ ))}
187
+ </>
188
+ )
189
+
190
+ let h = (
191
+ <body>
192
+ <F a={[1, 2, 3]} />
193
+ </body>
194
+ )
195
+
196
+ let e = {
197
+ kind: HKind.Element,
198
+ tag: 'body',
199
+ cn: [
200
+ {
201
+ kind: HKind.FunctionComponent,
202
+ tag: F,
203
+ p: {
204
+ a: [1, 2, 3],
205
+ },
206
+ },
207
+ ],
208
+ }
209
+
210
+ expect(h).toEqual(e)
211
+ })
212
+
213
+ test('adhoc 1', () => {
214
+ let F = <>
215
+ <b>1</b>
216
+ <b>2</b>
217
+ </>
218
+
219
+ let h = <body>
220
+ <F />
221
+ </body>
222
+
223
+ let f_is = jsx(jsxf, null, jsx('b', null, '1'), jsx('b', null, '2'))
224
+ let h_is = jsx('body', null, jsx(f_is, null))
225
+
226
+ let e = {
227
+ kind: 1,
228
+ tag: 'body',
229
+ cn: [
230
+ {
231
+ kind: 4,
232
+ cn: [
233
+ { kind: 1, tag: 'b', cn: ['1'] },
234
+ { kind: 1, tag: 'b', cn: ['2'] },
235
+ ],
236
+ },
237
+ ],
238
+ }
239
+
240
+ // test implementation
241
+ expect(h).toEqual(e)
242
+ })
243
+
244
+ test('adhoc 2', () => {
245
+
246
+ let F = <>hasen</>
247
+ let G = (
248
+ <>
249
+ <F />
250
+ </>
251
+ )
252
+ let h = (
253
+ <body>
254
+ <G />
255
+ hopp
256
+ </body>
257
+ )
258
+
259
+ // simple jsx -> js transform
260
+ // let f_is = jsx(jsxf, null, 'hasen')
261
+ // let g_is = jsx(jsxf, null, jsx(f_is, null))
262
+ // let h_is = jsx('body', null, jsx(g_is, null), 'hopp')
263
+
264
+ // // jsx transform as ocx does it with builtin ast folding
265
+ // let hh = jsx(
266
+ // 'body',
267
+ // null,
268
+ // jsx(jsx(jsxf, null, jsx(jsx(jsxf, null, 'hasen'), null)), null),
269
+ // 'hopp',
270
+ // )
271
+
272
+ console.dir(h, { depth: null });
273
+
274
+ let e = {
275
+ kind: 1,
276
+ tag: 'body',
277
+ cn: [
278
+ {
279
+ kind: 4,
280
+ cn: [{ kind: 4, cn: ['hasen'] }]
281
+ },
282
+ 'hopp'
283
+ ]
284
+ }
285
+
286
+ expect(h).toEqual(e)
287
+ })
288
+
289
+ // with functions, that wont work
290
+ test('adhoc 1 with funs, does not work for testing that simple', () => {
291
+
292
+ let F = () => (
293
+ <>
294
+ <b>1</b>
295
+ </>
296
+ )
297
+
298
+ let h = () => (
299
+ <body>
300
+ <F />
301
+ </body>
302
+ )
303
+
304
+ let f_is = () => jsx(jsxf, null, jsx('b', null, '1'))
305
+ let h_is = () => jsx('body', null, jsx(f_is, null))
306
+
307
+ console.log([F, h, f_is, h_is])
308
+ console.dir(h_is())
309
+
310
+ // h_is() returns { kind: 1, tag: 'body', cn: [ { kind: 2, tag: [Function: f_is] } ] }
311
+ })
312
+ })