@atom.io/spa 0.0.0-stage → 0.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 (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 +78 -0
  63. package/dist/development/server.js.map +1 -0
  64. package/dist/development/vite-BadrFSIM.js +126 -0
  65. package/dist/development/vite-BadrFSIM.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 +78 -0
  81. package/dist/server.js.map +1 -0
  82. package/dist/size.json +30 -0
  83. package/dist/vite-BadrFSIM.js +126 -0
  84. package/dist/vite-BadrFSIM.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 +131 -5
  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 +189 -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 +150 -0
  105. package/src/use-loadable.ts +67 -0
  106. package/src/vite.ts +48 -0
package/src/server.ts ADDED
@@ -0,0 +1,150 @@
1
+ import type { Child, RootOptions, Scope, VNode } from "./core.ts"
2
+ import {
3
+ createRoot,
4
+ createScope,
5
+ dispose,
6
+ plain,
7
+ untrack,
8
+ within,
9
+ } from "./core.ts"
10
+ import { Fragment } from "./jsx-runtime.ts"
11
+
12
+ const escape = (value: unknown): string =>
13
+ String(value).replace(
14
+ /[&<>"']/g,
15
+ (char) =>
16
+ ({ "&": `&amp;`, "<": `&lt;`, ">": `&gt;`, '"': `&quot;`, "'": `&#39;` })[
17
+ char
18
+ ]!,
19
+ )
20
+ const voidTags =
21
+ /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/
22
+ const booleans = new Set(
23
+ `allowfullscreen async autofocus autoplay checked controls default defer disabled formnovalidate hidden inert ismap itemscope loop multiple muted nomodule novalidate open playsinline readonly required reversed selected`.split(
24
+ ` `,
25
+ ),
26
+ )
27
+ function stringify(
28
+ value: Child,
29
+ owner: Scope,
30
+ namespace = `html`,
31
+ selectValue?: string | number,
32
+ ): string {
33
+ if (typeof value === `function`) {
34
+ const values = ([untrack(value)] as unknown[]).flat(Infinity) as Child[]
35
+ const keys = new Set<unknown>()
36
+ return (
37
+ `<!--{-->` +
38
+ values
39
+ .map((child, index) => {
40
+ const explicit = (child as VNode | null)?.props?.[`key`]
41
+ const key = explicit == null ? index : String(explicit)
42
+ if (keys.has(key)) throw new Error(`spa: duplicate key`)
43
+ keys.add(key)
44
+ return `<!--[-->${within(owner, () => stringify(child, createScope(owner), namespace, selectValue))}<!--]-->`
45
+ })
46
+ .join(``) +
47
+ `<!--}-->`
48
+ )
49
+ }
50
+ if (Array.isArray(value))
51
+ return value
52
+ .map((child) => stringify(child, owner, namespace, selectValue))
53
+ .join(``)
54
+ if (value == null || typeof value === `boolean`) return ``
55
+ if (typeof value !== `object`) return `<!--t-->${escape(value)}`
56
+ if (!(`type` in value) || !(`props` in value))
57
+ throw new TypeError(`spa: invalid child`)
58
+ const { type, props } = value
59
+ if (!type || type === Fragment)
60
+ return stringify(() => props[`children`], owner, namespace, selectValue)
61
+ if (typeof type === `function`) {
62
+ const childOwner = createScope(owner)
63
+ return within(childOwner, () =>
64
+ stringify(
65
+ untrack(() => type(props)),
66
+ childOwner,
67
+ namespace,
68
+ selectValue,
69
+ ),
70
+ )
71
+ }
72
+ if (!/^[A-Za-z][\w.:-]*$/.test(type)) throw new Error(`spa: invalid tag`)
73
+ if (type === `astro-slot`)
74
+ return `<astro-slot${props[`name`] ? ` name="${escape(props[`name`])}"` : ``}>${props[`innerHTML`] ?? ``}</astro-slot>`
75
+ const ns = type === `svg` ? `svg` : namespace
76
+ let attrs = ``
77
+ let textValue: string | number | undefined
78
+ for (let key of Object.keys(props)) {
79
+ if (
80
+ key === `children` ||
81
+ key === `ref` ||
82
+ key === `key` ||
83
+ key.startsWith(`on`)
84
+ )
85
+ continue
86
+ if (
87
+ /^(innerHTML|outerHTML|textContent|innerText|dangerouslySetInnerHTML)$/.test(
88
+ key,
89
+ )
90
+ )
91
+ throw new Error(`spa: owned content`)
92
+ let attribute = props[key]
93
+ if (key === `className`) key = `class`
94
+ if (key === `htmlFor`) key = `for`
95
+ if (!/^[^\s"'<>/=]+$/.test(key)) throw new Error(`spa: invalid attribute`)
96
+ if (key === `value` && (type === `select` || type === `textarea`)) {
97
+ textValue = attribute
98
+ continue
99
+ }
100
+ if (key === `style` && attribute != null && typeof attribute !== `string`)
101
+ throw new TypeError(`spa: CSS string required`)
102
+ const boolean = ns === `html` && booleans.has(key.toLowerCase())
103
+ if (
104
+ attribute == null ||
105
+ (boolean && !attribute) ||
106
+ (attribute === false && !/^(aria-|data-)/.test(key))
107
+ )
108
+ continue
109
+ attrs +=
110
+ boolean || (attribute === true && !/^(aria-|data-)/.test(key))
111
+ ? ` ${key}=""`
112
+ : ` ${key}="${escape(attribute)}"`
113
+ }
114
+ if (
115
+ type === `option` &&
116
+ selectValue !== undefined &&
117
+ String(props[`value`]) === String(selectValue)
118
+ )
119
+ attrs += ` selected=""`
120
+ if (ns === `html` && voidTags.test(type)) return `<${type}${attrs}>`
121
+ if (ns === `html` && /^(textarea|title|style|script)$/.test(type)) {
122
+ let text = plain(
123
+ type === `textarea` ? (textValue ?? props[`children`]) : props[`children`],
124
+ )
125
+ if (type === `textarea` && text.startsWith(`\n`)) text = `\n${text}`
126
+ if (/^(script|style)$/.test(type) && new RegExp(`</${type}`, `i`).test(text))
127
+ throw new Error(`spa: unsafe raw text`)
128
+ return `<${type}${attrs}>${/^(script|style)$/.test(type) ? text : escape(text)}</${type}>`
129
+ }
130
+ const children = stringify(
131
+ () => props[`children`],
132
+ owner,
133
+ type === `foreignObject` ? `html` : ns,
134
+ type === `select` ? textValue : selectValue,
135
+ )
136
+ return `<${type}${attrs}>${children}</${type}>`
137
+ }
138
+
139
+ export function renderToString(
140
+ setup: () => Child,
141
+ options: RootOptions = {},
142
+ ): string {
143
+ const root = createRoot(options, true)
144
+ if (/--|[<>]/.test(root._prefix)) throw new Error(`spa: invalid idPrefix`)
145
+ try {
146
+ return `<!--w:${root._prefix}-->${within(root, () => stringify(untrack(setup), root))}<!--/w-->`
147
+ } finally {
148
+ dispose(root)
149
+ }
150
+ }
@@ -0,0 +1,67 @@
1
+ import type {
2
+ Loadable,
3
+ ReadableFamilyToken,
4
+ ReadableToken,
5
+ ViewOf,
6
+ } from "atom.io"
7
+ import type { Canonical } from "atom.io/foundations/canonical"
8
+ import { withdraw } from "atom.io/internal"
9
+
10
+ import { type Accessor, owner, resolveStateToken, useValue } from "./core.ts"
11
+
12
+ export type LoadableValue<T> = { loading: boolean; value: T }
13
+
14
+ export function useLoadable<T, E>(
15
+ token: ReadableToken<Loadable<T>, any, E>,
16
+ ): Accessor<`LOADING` | LoadableValue<ViewOf<T | E>>>
17
+ export function useLoadable<T, K extends Canonical, E>(
18
+ token: ReadableFamilyToken<Loadable<T>, K, E>,
19
+ key: NoInfer<K>,
20
+ ): Accessor<`LOADING` | LoadableValue<ViewOf<T | E>>>
21
+ export function useLoadable<T, E>(
22
+ token: ReadableToken<Loadable<T>, any, E>,
23
+ fallback: NoInfer<T>,
24
+ ): Accessor<LoadableValue<ViewOf<T>> & { error?: E }>
25
+ export function useLoadable<T, K extends Canonical, E>(
26
+ token: ReadableFamilyToken<Loadable<T>, K, E>,
27
+ key: NoInfer<K>,
28
+ fallback: NoInfer<T>,
29
+ ): Accessor<LoadableValue<ViewOf<T>> & { error?: E }>
30
+ export function useLoadable(
31
+ ...params:
32
+ | [ReadableToken<any, any, any>]
33
+ | [ReadableToken<any, any, any>, unknown]
34
+ | [ReadableFamilyToken<any, Canonical, any>, Canonical]
35
+ | [ReadableFamilyToken<any, Canonical, any>, Canonical, unknown]
36
+ ): Accessor<`LOADING` | (LoadableValue<unknown> & { error?: unknown })> {
37
+ const store = owner!._root._store
38
+ const [token] = params
39
+ const family = token.type.endsWith(`_family`)
40
+ const selected = resolveStateToken(token, params[1] as Canonical)
41
+ const read = useValue(selected)
42
+ const fallbackIndex = family ? 2 : 1
43
+ const fallback = params[fallbackIndex]
44
+ const hasFallback = params.length > fallbackIndex
45
+ let available = hasFallback
46
+ let previous = fallback
47
+ let error: unknown
48
+ return () => {
49
+ const value = read()
50
+ const loading = value instanceof Promise
51
+ if (!loading) {
52
+ previous = value
53
+ available = true
54
+ const state = withdraw(store, selected)
55
+ error =
56
+ hasFallback &&
57
+ `catch` in state &&
58
+ state.catch.some((ErrorType) => value instanceof ErrorType)
59
+ ? value
60
+ : undefined
61
+ }
62
+ if (!available) return `LOADING`
63
+ return error
64
+ ? { loading, value: fallback, error }
65
+ : { loading, value: previous }
66
+ }
67
+ }
package/src/vite.ts ADDED
@@ -0,0 +1,48 @@
1
+ import type { Plugin } from "vite"
2
+
3
+ import { compile } from "./compiler.ts"
4
+
5
+ export interface SpaPluginOptions {
6
+ include?: RegExp
7
+ }
8
+ export default function spa(options: SpaPluginOptions = {}): Plugin {
9
+ const include = options.include ?? /\.[jt]sx$/
10
+ const matches = (id: string): boolean => {
11
+ include.lastIndex = 0
12
+ return !id.includes(`/node_modules/`) && include.test(id.split(`?`)[0])
13
+ }
14
+ return {
15
+ name: `spa`,
16
+ enforce: `pre`,
17
+ config() {
18
+ return {
19
+ resolve: { dedupe: [`@atom.io/spa`, `atom.io`] },
20
+ optimizeDeps: { exclude: [`@atom.io/spa`, `atom.io`] },
21
+ }
22
+ },
23
+ configEnvironment: {
24
+ order: `post`,
25
+ handler(_name, config) {
26
+ // Astro and its adapters force renderer entries into the optimizer.
27
+ // Vite processes explicit includes even when their package is excluded.
28
+ // Keep the renderer, hooks, and store in one unbundled module graph.
29
+ if (config.optimizeDeps?.include) {
30
+ config.optimizeDeps.include = config.optimizeDeps.include.filter(
31
+ (id) => !/^(@atom\.io\/spa|atom\.io)(\/|$)/.test(id),
32
+ )
33
+ }
34
+ },
35
+ },
36
+ transform(code, id) {
37
+ if (!matches(id)) return
38
+ return compile(code, id)
39
+ },
40
+ // A reload disposes the whole document. No state-preserving HMR is promised.
41
+ handleHotUpdate({ server, file }) {
42
+ if (matches(file)) {
43
+ server.ws.send({ type: `full-reload` })
44
+ return []
45
+ }
46
+ },
47
+ }
48
+ }