@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.
- package/LICENSE +373 -0
- package/README.md +401 -2
- package/README.mdx +150 -0
- package/dist/astro-application.d.ts +15 -0
- package/dist/astro-application.d.ts.map +1 -0
- package/dist/astro-application.js +62 -0
- package/dist/astro-application.js.map +1 -0
- package/dist/astro-client.d.ts +9 -0
- package/dist/astro-client.d.ts.map +1 -0
- package/dist/astro-client.js +98 -0
- package/dist/astro-client.js.map +1 -0
- package/dist/astro-context-OdlexHR4.js +79 -0
- package/dist/astro-context-OdlexHR4.js.map +1 -0
- package/dist/astro-middleware.d.ts +4 -0
- package/dist/astro-middleware.d.ts.map +1 -0
- package/dist/astro-middleware.js +49 -0
- package/dist/astro-middleware.js.map +1 -0
- package/dist/astro-request-DRKvDjsO.js +70 -0
- package/dist/astro-request-DRKvDjsO.js.map +1 -0
- package/dist/astro-server.d.ts +4 -0
- package/dist/astro-server.d.ts.map +1 -0
- package/dist/astro-server.js +30 -0
- package/dist/astro-server.js.map +1 -0
- package/dist/astro-shared-BT45auwR.js +29 -0
- package/dist/astro-shared-BT45auwR.js.map +1 -0
- package/dist/astro-shared-DkZaqX7L.d.ts +31 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +1 -0
- package/dist/astro.d.ts +6 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +26 -0
- package/dist/astro.js.map +1 -0
- package/dist/client.min.d.ts +1 -0
- package/dist/client.min.js +1 -0
- package/dist/core-BhiA9fFi.d.ts +44 -0
- package/dist/core-BhiA9fFi.d.ts.map +1 -0
- package/dist/core-Dvqm7w2y.js +231 -0
- package/dist/core-Dvqm7w2y.js.map +1 -0
- package/dist/development/astro-application.js +62 -0
- package/dist/development/astro-application.js.map +1 -0
- package/dist/development/astro-client.js +98 -0
- package/dist/development/astro-client.js.map +1 -0
- package/dist/development/astro-context-OdlexHR4.js +79 -0
- package/dist/development/astro-context-OdlexHR4.js.map +1 -0
- package/dist/development/astro-middleware.js +49 -0
- package/dist/development/astro-middleware.js.map +1 -0
- package/dist/development/astro-request-DRKvDjsO.js +70 -0
- package/dist/development/astro-request-DRKvDjsO.js.map +1 -0
- package/dist/development/astro-server.js +30 -0
- package/dist/development/astro-server.js.map +1 -0
- package/dist/development/astro-shared-BT45auwR.js +29 -0
- package/dist/development/astro-shared-BT45auwR.js.map +1 -0
- package/dist/development/astro.js +26 -0
- package/dist/development/astro.js.map +1 -0
- package/dist/development/core-DB3sQz4h.js +234 -0
- package/dist/development/core-DB3sQz4h.js.map +1 -0
- package/dist/development/dom-D9cY6l1R.js +194 -0
- package/dist/development/dom-D9cY6l1R.js.map +1 -0
- package/dist/development/index.js +40 -0
- package/dist/development/index.js.map +1 -0
- package/dist/development/jsx-runtime.js +25 -0
- package/dist/development/jsx-runtime.js.map +1 -0
- package/dist/development/server.js +87 -0
- package/dist/development/server.js.map +1 -0
- package/dist/development/vite-CQchIQ_-.js +126 -0
- package/dist/development/vite-CQchIQ_-.js.map +1 -0
- package/dist/development/vite.js +1 -0
- package/dist/dom-BR9o9wZW.js +188 -0
- package/dist/dom-BR9o9wZW.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-runtime-BzGV18VO.d.ts +965 -0
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -0
- package/dist/jsx-runtime.js +25 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +87 -0
- package/dist/server.js.map +1 -0
- package/dist/size.json +30 -0
- package/dist/vite-CQchIQ_-.js +126 -0
- package/dist/vite-CQchIQ_-.js.map +1 -0
- package/dist/vite.d.ts +6 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +1 -0
- package/package.json +129 -3
- package/src/astro-application.ts +71 -0
- package/src/astro-client.ts +136 -0
- package/src/astro-context.ts +102 -0
- package/src/astro-middleware.ts +48 -0
- package/src/astro-request.ts +79 -0
- package/src/astro-server.ts +43 -0
- package/src/astro-shared.ts +78 -0
- package/src/astro.ts +47 -0
- package/src/compiler.ts +190 -0
- package/src/core.ts +385 -0
- package/src/dom.ts +385 -0
- package/src/env.d.ts +1 -0
- package/src/index.ts +24 -0
- package/src/jsx-runtime.ts +63 -0
- package/src/jsx-types.d.ts +909 -0
- package/src/server.ts +155 -0
- package/src/use-loadable.ts +67 -0
- package/src/vite.ts +48 -0
package/src/server.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
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
|
+
({ "&": `&`, "<": `<`, ">": `>`, '"': `"`, "'": `'` })[
|
|
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 credentialless default defer disabled disablepictureinpicture disableremoteplayback formnovalidate inert ismap itemscope loop multiple muted nomodule novalidate open playsinline readonly required reversed selected shadowrootclonable shadowrootdelegatesfocus shadowrootserializable`.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 (ns === `html`) {
|
|
94
|
+
if (key === `defaultValue` && (type === `input` || type === `textarea`)) {
|
|
95
|
+
if (props[`value`] != null) continue
|
|
96
|
+
key = `value`
|
|
97
|
+
} else if (key === `defaultChecked` && type === `input`) {
|
|
98
|
+
if (props[`checked`] != null) continue
|
|
99
|
+
key = `checked`
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (key === `className`) key = `class`
|
|
103
|
+
if (key === `htmlFor`) key = `for`
|
|
104
|
+
if (!/^[^\s"'<>/=]+$/.test(key)) throw new Error(`spa: invalid attribute`)
|
|
105
|
+
if (key === `value` && (type === `select` || type === `textarea`)) {
|
|
106
|
+
textValue = attribute ?? textValue
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
if (key === `style` && attribute != null && typeof attribute !== `string`)
|
|
110
|
+
throw new TypeError(`spa: CSS string required`)
|
|
111
|
+
const boolean =
|
|
112
|
+
ns === `html` &&
|
|
113
|
+
(booleans.has(key.toLowerCase()) ||
|
|
114
|
+
(/^(capture|download|hidden)$/.test(key) &&
|
|
115
|
+
typeof attribute === `boolean`))
|
|
116
|
+
if (attribute == null || (boolean && !attribute)) continue
|
|
117
|
+
attrs += boolean ? ` ${key}=""` : ` ${key}="${escape(attribute)}"`
|
|
118
|
+
}
|
|
119
|
+
if (
|
|
120
|
+
type === `option` &&
|
|
121
|
+
selectValue !== undefined &&
|
|
122
|
+
String(props[`value`]) === String(selectValue)
|
|
123
|
+
)
|
|
124
|
+
attrs += ` selected=""`
|
|
125
|
+
if (ns === `html` && voidTags.test(type)) return `<${type}${attrs}>`
|
|
126
|
+
if (ns === `html` && /^(textarea|title|style|script)$/.test(type)) {
|
|
127
|
+
let text = plain(
|
|
128
|
+
type === `textarea` ? (textValue ?? props[`children`]) : props[`children`],
|
|
129
|
+
)
|
|
130
|
+
if (type === `textarea` && text.startsWith(`\n`)) text = `\n${text}`
|
|
131
|
+
if (/^(script|style)$/.test(type) && new RegExp(`</${type}`, `i`).test(text))
|
|
132
|
+
throw new Error(`spa: unsafe raw text`)
|
|
133
|
+
return `<${type}${attrs}>${/^(script|style)$/.test(type) ? text : escape(text)}</${type}>`
|
|
134
|
+
}
|
|
135
|
+
const children = stringify(
|
|
136
|
+
() => props[`children`],
|
|
137
|
+
owner,
|
|
138
|
+
type === `foreignObject` ? `html` : ns,
|
|
139
|
+
type === `select` ? textValue : selectValue,
|
|
140
|
+
)
|
|
141
|
+
return `<${type}${attrs}>${children}</${type}>`
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function renderToString(
|
|
145
|
+
setup: () => Child,
|
|
146
|
+
options: RootOptions = {},
|
|
147
|
+
): string {
|
|
148
|
+
const root = createRoot(options, true)
|
|
149
|
+
if (/--|[<>]/.test(root._prefix)) throw new Error(`spa: invalid idPrefix`)
|
|
150
|
+
try {
|
|
151
|
+
return `<!--w:${root._prefix}-->${within(root, () => stringify(untrack(setup), root))}<!--/w-->`
|
|
152
|
+
} finally {
|
|
153
|
+
dispose(root)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -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
|
+
}
|