@atom.io/spa 0.0.0-stage → 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.
Files changed (106) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +401 -2
  3. package/README.mdx +150 -0
  4. package/dist/astro-application.d.ts +15 -0
  5. package/dist/astro-application.d.ts.map +1 -0
  6. package/dist/astro-application.js +62 -0
  7. package/dist/astro-application.js.map +1 -0
  8. package/dist/astro-client.d.ts +9 -0
  9. package/dist/astro-client.d.ts.map +1 -0
  10. package/dist/astro-client.js +98 -0
  11. package/dist/astro-client.js.map +1 -0
  12. package/dist/astro-context-OdlexHR4.js +79 -0
  13. package/dist/astro-context-OdlexHR4.js.map +1 -0
  14. package/dist/astro-middleware.d.ts +4 -0
  15. package/dist/astro-middleware.d.ts.map +1 -0
  16. package/dist/astro-middleware.js +49 -0
  17. package/dist/astro-middleware.js.map +1 -0
  18. package/dist/astro-request-DRKvDjsO.js +70 -0
  19. package/dist/astro-request-DRKvDjsO.js.map +1 -0
  20. package/dist/astro-server.d.ts +4 -0
  21. package/dist/astro-server.d.ts.map +1 -0
  22. package/dist/astro-server.js +30 -0
  23. package/dist/astro-server.js.map +1 -0
  24. package/dist/astro-shared-BT45auwR.js +29 -0
  25. package/dist/astro-shared-BT45auwR.js.map +1 -0
  26. package/dist/astro-shared-DkZaqX7L.d.ts +31 -0
  27. package/dist/astro-shared-DkZaqX7L.d.ts.map +1 -0
  28. package/dist/astro.d.ts +6 -0
  29. package/dist/astro.d.ts.map +1 -0
  30. package/dist/astro.js +26 -0
  31. package/dist/astro.js.map +1 -0
  32. package/dist/client.min.d.ts +1 -0
  33. package/dist/client.min.js +1 -0
  34. package/dist/core-BhiA9fFi.d.ts +44 -0
  35. package/dist/core-BhiA9fFi.d.ts.map +1 -0
  36. package/dist/core-Dvqm7w2y.js +231 -0
  37. package/dist/core-Dvqm7w2y.js.map +1 -0
  38. package/dist/development/astro-application.js +62 -0
  39. package/dist/development/astro-application.js.map +1 -0
  40. package/dist/development/astro-client.js +98 -0
  41. package/dist/development/astro-client.js.map +1 -0
  42. package/dist/development/astro-context-OdlexHR4.js +79 -0
  43. package/dist/development/astro-context-OdlexHR4.js.map +1 -0
  44. package/dist/development/astro-middleware.js +49 -0
  45. package/dist/development/astro-middleware.js.map +1 -0
  46. package/dist/development/astro-request-DRKvDjsO.js +70 -0
  47. package/dist/development/astro-request-DRKvDjsO.js.map +1 -0
  48. package/dist/development/astro-server.js +30 -0
  49. package/dist/development/astro-server.js.map +1 -0
  50. package/dist/development/astro-shared-BT45auwR.js +29 -0
  51. package/dist/development/astro-shared-BT45auwR.js.map +1 -0
  52. package/dist/development/astro.js +26 -0
  53. package/dist/development/astro.js.map +1 -0
  54. package/dist/development/core-DB3sQz4h.js +234 -0
  55. package/dist/development/core-DB3sQz4h.js.map +1 -0
  56. package/dist/development/dom-D9cY6l1R.js +194 -0
  57. package/dist/development/dom-D9cY6l1R.js.map +1 -0
  58. package/dist/development/index.js +40 -0
  59. package/dist/development/index.js.map +1 -0
  60. package/dist/development/jsx-runtime.js +25 -0
  61. package/dist/development/jsx-runtime.js.map +1 -0
  62. package/dist/development/server.js +87 -0
  63. package/dist/development/server.js.map +1 -0
  64. package/dist/development/vite-CQchIQ_-.js +126 -0
  65. package/dist/development/vite-CQchIQ_-.js.map +1 -0
  66. package/dist/development/vite.js +1 -0
  67. package/dist/dom-BR9o9wZW.js +188 -0
  68. package/dist/dom-BR9o9wZW.js.map +1 -0
  69. package/dist/index.d.ts +24 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +40 -0
  72. package/dist/index.js.map +1 -0
  73. package/dist/jsx-runtime-BzGV18VO.d.ts +965 -0
  74. package/dist/jsx-runtime-BzGV18VO.d.ts.map +1 -0
  75. package/dist/jsx-runtime.d.ts +2 -0
  76. package/dist/jsx-runtime.js +25 -0
  77. package/dist/jsx-runtime.js.map +1 -0
  78. package/dist/server.d.ts +3 -0
  79. package/dist/server.d.ts.map +1 -0
  80. package/dist/server.js +87 -0
  81. package/dist/server.js.map +1 -0
  82. package/dist/size.json +30 -0
  83. package/dist/vite-CQchIQ_-.js +126 -0
  84. package/dist/vite-CQchIQ_-.js.map +1 -0
  85. package/dist/vite.d.ts +6 -0
  86. package/dist/vite.d.ts.map +1 -0
  87. package/dist/vite.js +1 -0
  88. package/package.json +129 -3
  89. package/src/astro-application.ts +71 -0
  90. package/src/astro-client.ts +136 -0
  91. package/src/astro-context.ts +102 -0
  92. package/src/astro-middleware.ts +48 -0
  93. package/src/astro-request.ts +79 -0
  94. package/src/astro-server.ts +43 -0
  95. package/src/astro-shared.ts +78 -0
  96. package/src/astro.ts +47 -0
  97. package/src/compiler.ts +190 -0
  98. package/src/core.ts +385 -0
  99. package/src/dom.ts +385 -0
  100. package/src/env.d.ts +1 -0
  101. package/src/index.ts +24 -0
  102. package/src/jsx-runtime.ts +63 -0
  103. package/src/jsx-types.d.ts +909 -0
  104. package/src/server.ts +155 -0
  105. package/src/use-loadable.ts +67 -0
  106. package/src/vite.ts +48 -0
package/src/dom.ts ADDED
@@ -0,0 +1,385 @@
1
+ import type { Accessor, Child, RootOptions, Scope, VNode } from "./core.ts"
2
+ import {
3
+ createRoot,
4
+ createScope,
5
+ dispose,
6
+ fail,
7
+ invalidate,
8
+ observe,
9
+ plain,
10
+ untrack,
11
+ within,
12
+ } from "./core.ts"
13
+ import { Fragment, merge } from "./jsx-runtime.ts"
14
+
15
+ const HTML = `http://www.w3.org/1999/xhtml`
16
+ const SVG = `http://www.w3.org/2000/svg`
17
+ type Cursor = { node: Node | null }
18
+ type RecordNode = { _value: Child; _scope: Scope; _start: Node; _end: Node }
19
+
20
+ function take(
21
+ node: Node,
22
+ parent: Node,
23
+ before: Node | null,
24
+ owner: Scope,
25
+ cursor?: Cursor,
26
+ ): Node {
27
+ if (
28
+ cursor &&
29
+ !(node.nodeType === 3 && !node.nodeValue && cursor.node?.nodeType !== 3)
30
+ ) {
31
+ const found = cursor.node
32
+ if (
33
+ !found ||
34
+ found.nodeType !== node.nodeType ||
35
+ found.nodeName !== node.nodeName ||
36
+ (found as Element).namespaceURI !== (node as Element).namespaceURI ||
37
+ (node.nodeType !== 1 && found.nodeValue !== node.nodeValue)
38
+ )
39
+ throw new Error(
40
+ import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
41
+ )
42
+ node = found
43
+ cursor.node = found.nextSibling
44
+ } else parent.insertBefore(node, cursor ? cursor.node : before)
45
+ owner._cleanups.add(() => {
46
+ ;(node as ChildNode).remove()
47
+ })
48
+ return node
49
+ }
50
+ function comment(
51
+ value: string,
52
+ parent: Node,
53
+ before: Node | null,
54
+ owner: Scope,
55
+ cursor?: Cursor,
56
+ ): Node {
57
+ return take(
58
+ parent.ownerDocument!.createComment(value),
59
+ parent,
60
+ before,
61
+ owner,
62
+ cursor,
63
+ )
64
+ }
65
+ function property(
66
+ element: Element,
67
+ key: string,
68
+ value: any,
69
+ previous: any,
70
+ ): void {
71
+ if (/^(key|children|ref)$/.test(key)) return
72
+ if (key === `innerHTML` && element.localName === `astro-slot`) return
73
+ if (
74
+ /^(innerHTML|outerHTML|textContent|innerText|dangerouslySetInnerHTML)$/.test(
75
+ key,
76
+ )
77
+ )
78
+ throw new Error(import.meta.env.DEV ? `spa: owned content` : undefined)
79
+ if (key.startsWith(`on`)) {
80
+ const capture = /(?<!Pointer)Capture$/.test(key)
81
+ const event = key
82
+ .slice(2, capture ? -7 : undefined)
83
+ .toLowerCase()
84
+ .replace(`doubleclick`, `dblclick`)
85
+ if (previous) element.removeEventListener(event, previous, capture)
86
+ if (value != null) {
87
+ if (import.meta.env.DEV && typeof value !== `function`)
88
+ throw new Error(`spa: event handler`)
89
+ element.addEventListener(event, value, capture)
90
+ }
91
+ return
92
+ }
93
+ if (key === `className`) key = `class`
94
+ if (key === `htmlFor`) key = `for`
95
+ if (
96
+ import.meta.env.DEV &&
97
+ key === `style` &&
98
+ value != null &&
99
+ typeof value !== `string`
100
+ )
101
+ throw new Error(`spa: CSS string required`)
102
+ if (
103
+ element.namespaceURI === HTML &&
104
+ key in element &&
105
+ !/^(style|form|list)$/.test(key)
106
+ ) {
107
+ const target = element as any
108
+ target[key] = value ?? (typeof target[key] === `boolean` ? false : ``)
109
+ } else if (value == null || (value === false && !/^(aria-|data-)/.test(key)))
110
+ element.removeAttribute(key)
111
+ else
112
+ element.setAttribute(
113
+ key,
114
+ value === true && !/^(aria-|data-)/.test(key) ? `` : String(value),
115
+ )
116
+ }
117
+
118
+ function mount(
119
+ value: Child,
120
+ parent: Node,
121
+ before: Node | null,
122
+ owner: Scope,
123
+ namespace: string,
124
+ cursor?: Cursor,
125
+ source = () => value,
126
+ ): void {
127
+ if (typeof value === `function`) {
128
+ comment(`{`, parent, before, owner, cursor)
129
+ let end: Node | null = null
130
+ let records = new Map<unknown, RecordNode>()
131
+ let initial = cursor
132
+ observe(
133
+ owner,
134
+ () => (source() as () => Child)(),
135
+ (next) => {
136
+ const values = ([next] as unknown[]).flat(Infinity) as Child[]
137
+ const kept = new Map<unknown, RecordNode>()
138
+ const focus = parent.ownerDocument!.activeElement as HTMLElement | null
139
+ for (let index = 0; index < values.length; index++) {
140
+ const child = values[index]
141
+ const vnode = child as VNode | null
142
+ const explicit = vnode?.props?.[`key`]
143
+ const key = explicit == null ? index : String(explicit)
144
+ if (import.meta.env.DEV && kept.has(key))
145
+ throw new Error(`spa: duplicate key`)
146
+ let record = records.get(key)
147
+ if (
148
+ record &&
149
+ (typeof record._value !== typeof child ||
150
+ (record._value as VNode | null)?.type !== vnode?.type)
151
+ ) {
152
+ dispose(record._scope)
153
+ record = undefined
154
+ }
155
+ if (!record) {
156
+ const childOwner = createScope(owner)
157
+ record = {
158
+ _value: child,
159
+ _scope: childOwner,
160
+ _start: comment(`[`, parent, end ?? before, childOwner, initial),
161
+ _end: null!,
162
+ }
163
+ const held = record
164
+ mount(
165
+ child,
166
+ parent,
167
+ end ?? before,
168
+ childOwner,
169
+ namespace,
170
+ initial,
171
+ () => held._value,
172
+ )
173
+ record._end = comment(
174
+ `]`,
175
+ parent,
176
+ end ?? before,
177
+ childOwner,
178
+ initial,
179
+ )
180
+ } else {
181
+ record._value = child
182
+ invalidate(record._scope)
183
+ }
184
+ kept.set(key, record)
185
+ }
186
+ for (const [key, record] of records)
187
+ if (!kept.has(key)) dispose(record._scope)
188
+ if (!initial) {
189
+ let at = end ?? before
190
+ for (const record of [...kept.values()].reverse()) {
191
+ if (record._end.nextSibling !== at) {
192
+ const range = parent.ownerDocument!.createRange()
193
+ range.setStartBefore(record._start)
194
+ range.setEndAfter(record._end)
195
+ parent.insertBefore(range.extractContents(), at)
196
+ }
197
+ at = record._start
198
+ }
199
+ if (
200
+ focus?.isConnected &&
201
+ focus !== parent.ownerDocument!.activeElement
202
+ )
203
+ focus.focus({ preventScroll: true })
204
+ }
205
+ initial = undefined
206
+ records = kept
207
+ },
208
+ )
209
+ end = comment(`}`, parent, before, owner, cursor)
210
+ return
211
+ }
212
+ if (Array.isArray(value)) {
213
+ for (const child of value)
214
+ mount(child, parent, before, owner, namespace, cursor)
215
+ return
216
+ }
217
+ if (value == null || typeof value === `boolean`) return
218
+ if (typeof value !== `object`) {
219
+ comment(`t`, parent, before, owner, cursor)
220
+ const text = take(
221
+ parent.ownerDocument!.createTextNode(String(value)),
222
+ parent,
223
+ before,
224
+ owner,
225
+ cursor,
226
+ )
227
+ observe(owner, source as Accessor<string | number | bigint>, (next) => {
228
+ const data = String(next)
229
+ if (text.nodeValue !== data) text.nodeValue = data
230
+ })
231
+ return
232
+ }
233
+ if (import.meta.env.DEV && (!(`type` in value) || !(`props` in value)))
234
+ throw new Error(`spa: invalid child`)
235
+ const vnode = value as VNode
236
+ const props = merge(() => (source() as VNode).props)
237
+ if (!vnode.type || vnode.type === Fragment) {
238
+ mount(() => props[`children`], parent, before, owner, namespace, cursor)
239
+ return
240
+ }
241
+ if (typeof vnode.type === `function`) {
242
+ const childOwner = createScope(owner)
243
+ const output = within(childOwner, () =>
244
+ untrack(() => (vnode.type as Function)(props)),
245
+ )
246
+ mount(output, parent, before, childOwner, namespace, cursor)
247
+ return
248
+ }
249
+ const ns = vnode.type === `svg` ? SVG : namespace
250
+ const element = take(
251
+ parent.ownerDocument!.createElementNS(ns, vnode.type),
252
+ parent,
253
+ before,
254
+ owner,
255
+ cursor,
256
+ ) as Element
257
+ if (vnode.type === `astro-slot`) {
258
+ if (!cursor) element.innerHTML = props[`innerHTML`] ?? ``
259
+ } else if (ns === HTML && /^(textarea|title|style|script)$/.test(vnode.type)) {
260
+ observe(
261
+ owner,
262
+ () =>
263
+ plain(
264
+ vnode.type === `textarea`
265
+ ? (props[`value`] ?? props[`children`])
266
+ : props[`children`],
267
+ ),
268
+ (text) => {
269
+ if (element.textContent !== text) element.textContent = text
270
+ },
271
+ )
272
+ } else {
273
+ const children = cursor ? { node: element.firstChild } : undefined
274
+ if (
275
+ !/^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/.test(
276
+ vnode.type,
277
+ ) ||
278
+ ns !== HTML
279
+ )
280
+ mount(
281
+ () => props[`children`],
282
+ element,
283
+ null,
284
+ owner,
285
+ vnode.type === `foreignObject` ? HTML : ns,
286
+ children,
287
+ )
288
+ if (children?.node)
289
+ throw new Error(
290
+ import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
291
+ )
292
+ }
293
+ let previous: Record<string, any> = {}
294
+ owner._cleanups.add(() => {
295
+ for (const key in previous)
296
+ if (key.startsWith(`on`)) property(element, key, null, previous[key])
297
+ })
298
+ observe(
299
+ owner,
300
+ () => ({ ...props }),
301
+ (next) => {
302
+ for (const key in { ...previous, ...next })
303
+ if (
304
+ !Object.is(previous[key], next[key]) ||
305
+ typeof next[key] === `object`
306
+ ) {
307
+ property(element, key, next[key], previous[key])
308
+ previous[key] = next[key]
309
+ }
310
+ previous = next
311
+ },
312
+ )
313
+ const ref = untrack(() => props[`ref`])
314
+ if (ref) {
315
+ owner._root._mounts!.unshift(() => {
316
+ if (!owner._dead) {
317
+ owner._cleanups.add(() => ref(null))
318
+ ref(element)
319
+ }
320
+ })
321
+ }
322
+ }
323
+
324
+ export interface SpaRoot {
325
+ dispose(): void
326
+ flush(): void
327
+ }
328
+ function start(
329
+ setup: () => Child,
330
+ host: Element,
331
+ options: RootOptions,
332
+ hydrating: boolean,
333
+ ): SpaRoot {
334
+ const cursor = hydrating ? { node: host.firstChild } : undefined
335
+ const prefix = cursor?.node?.nodeValue?.slice(2)
336
+ if (
337
+ hydrating &&
338
+ (prefix === undefined ||
339
+ (options.idPrefix !== undefined && prefix !== options.idPrefix))
340
+ )
341
+ throw new Error(import.meta.env.DEV ? `spa: hydration mismatch` : undefined)
342
+ const root = createRoot(options)
343
+ if (prefix !== undefined) root._prefix = prefix
344
+ try {
345
+ comment(`w:${root._prefix}`, host, null, root, cursor)
346
+ within(root, () => {
347
+ mount(
348
+ untrack(setup),
349
+ host,
350
+ null,
351
+ root,
352
+ host.namespaceURI === SVG ? SVG : HTML,
353
+ cursor,
354
+ )
355
+ })
356
+ comment(`/w`, host, null, root, cursor)
357
+ if (cursor?.node)
358
+ throw new Error(
359
+ import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
360
+ )
361
+ root.flush()
362
+ } catch (error) {
363
+ fail(root, error)
364
+ }
365
+ return {
366
+ dispose: () => {
367
+ dispose(root)
368
+ },
369
+ flush: root.flush,
370
+ }
371
+ }
372
+ export function render(
373
+ setup: () => Child,
374
+ host: Element,
375
+ options: RootOptions = {},
376
+ ): SpaRoot {
377
+ return start(setup, host, options, false)
378
+ }
379
+ export function hydrate(
380
+ setup: () => Child,
381
+ host: Element,
382
+ options: RootOptions = {},
383
+ ): SpaRoot {
384
+ return start(setup, host, options, true)
385
+ }
package/src/env.d.ts ADDED
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ export type {
2
+ Accessor,
3
+ Child,
4
+ Component,
5
+ RootOptions,
6
+ TimelineValue,
7
+ VNode,
8
+ } from "./core.ts"
9
+ export {
10
+ onCleanup,
11
+ onMount,
12
+ useId,
13
+ useRef,
14
+ useSetter,
15
+ useStore,
16
+ useTimeline,
17
+ useTransaction,
18
+ useValue,
19
+ } from "./core.ts"
20
+ export type { SpaRoot } from "./dom.ts"
21
+ export { hydrate, render } from "./dom.ts"
22
+ export { Fragment } from "./jsx-runtime.ts"
23
+ export type { LoadableValue } from "./use-loadable.ts"
24
+ export { useLoadable } from "./use-loadable.ts"
@@ -0,0 +1,63 @@
1
+ import type { Child, Component, VNode } from "./core.ts"
2
+ import type { HTMLAttributes, NativeElements } from "./jsx-types.d.ts"
3
+
4
+ export type {
5
+ AriaAttributes,
6
+ DOMAttributes,
7
+ EventAttributes,
8
+ EventHandler,
9
+ HTMLAttributes,
10
+ Ref,
11
+ SVGAttributes,
12
+ TargetedEvent,
13
+ } from "./jsx-types.d.ts"
14
+
15
+ export const Fragment = (props: { children?: Child }): Child => props.children
16
+ export function jsx(
17
+ type: VNode[`type`],
18
+ props: VNode[`props`] | null,
19
+ key?: unknown,
20
+ ): VNode {
21
+ return {
22
+ type,
23
+ props: key === undefined ? (props ?? {}) : merge(props ?? {}, { key }),
24
+ }
25
+ }
26
+ export const jsxs = jsx
27
+ export const jsxDEV = jsx
28
+
29
+ /** Preserve lazy props and spread precedence, including keys added by later updates. */
30
+ export function merge(
31
+ ...sources: (Record<string, any> | (() => Record<string, any>))[]
32
+ ): Record<string, any> {
33
+ const values = () =>
34
+ sources.map(
35
+ (source) => (typeof source === `function` ? source() : source) ?? {},
36
+ )
37
+ return new Proxy(
38
+ {},
39
+ {
40
+ get: (_, key) =>
41
+ values()
42
+ .reverse()
43
+ .find((source) => Object.hasOwn(source, key))?.[key],
44
+ ownKeys: () => [...new Set(values().flatMap(Object.keys))],
45
+ getOwnPropertyDescriptor: () => ({ enumerable: true, configurable: true }),
46
+ },
47
+ )
48
+ }
49
+
50
+ export namespace JSX {
51
+ export type Element = VNode
52
+ export type ElementType = keyof IntrinsicElements | Component
53
+ export interface ElementChildrenAttribute {
54
+ children: {}
55
+ }
56
+ export interface IntrinsicAttributes {
57
+ key?: string | number
58
+ }
59
+ /** Augment this interface to declare attributes for registered custom elements. */
60
+ export interface IntrinsicElements extends NativeElements {
61
+ [tag: `${string}-${string}`]: HTMLAttributes<HTMLElement>
62
+ }
63
+ }