@atom.io/spa 0.0.0 → 0.0.2
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/README.md +201 -6
- package/README.mdx +62 -1
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +7 -4
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +4 -3
- package/dist/astro-server.js.map +1 -1
- package/dist/astro-shared-BT45auwR.js.map +1 -1
- package/dist/{astro-shared-DkZaqX7L.d.ts → astro-shared-DyzbUi6c.d.ts} +4 -2
- package/dist/astro-shared-DyzbUi6c.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/{core-Dvqm7w2y.js → capabilities-BuZUQpiD.js} +64 -2
- package/dist/capabilities-BuZUQpiD.js.map +1 -0
- package/dist/{core-BhiA9fFi.d.ts → capabilities-yC9TsHDI.d.ts} +20 -2
- package/dist/capabilities-yC9TsHDI.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/development/astro-client.js +7 -4
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +4 -3
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/astro-shared-BT45auwR.js.map +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/{core-DB3sQz4h.js → capabilities-DhOJmeoJ.js} +64 -2
- package/dist/development/capabilities-DhOJmeoJ.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-BKc6V_Kz.js} +8 -3
- package/dist/development/dom-BKc6V_Kz.js.map +1 -0
- package/dist/development/index.js +3 -38
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +121 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +217 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +177 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +17 -7
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-Dh-d6w4y.js +38 -0
- package/dist/development/src-Dh-d6w4y.js.map +1 -0
- package/dist/development/three.js +438 -0
- package/dist/development/three.js.map +1 -0
- package/dist/development/vite-CSHoxzXU.js +240 -0
- package/dist/development/vite-CSHoxzXU.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-BR9o9wZW.js → dom-B2rm8o2L.js} +8 -3
- package/dist/dom-B2rm8o2L.js.map +1 -0
- package/dist/index-BdwxmSKg.d.ts +24 -0
- package/dist/index-BdwxmSKg.d.ts.map +1 -0
- package/dist/index.d.ts +4 -24
- package/dist/index.js +3 -38
- package/dist/{jsx-runtime-BzGV18VO.d.ts → jsx-runtime-CZU7F8Qz.d.ts} +6 -4
- package/dist/jsx-runtime-CZU7F8Qz.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +32 -0
- package/dist/konva.d.ts.map +1 -0
- package/dist/konva.js +121 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +44 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +217 -0
- package/dist/motion.js.map +1 -0
- package/dist/renderer.d.ts +24 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +177 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +17 -7
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-BZxX4xbU.js +38 -0
- package/dist/src-BZxX4xbU.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +438 -0
- package/dist/three.js.map +1 -0
- package/dist/vite-CSHoxzXU.js +240 -0
- package/dist/vite-CSHoxzXU.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +176 -131
- package/src/astro-client.ts +1 -0
- package/src/astro-server.ts +1 -0
- package/src/astro-shared.ts +3 -0
- package/src/capabilities.ts +88 -0
- package/src/compiler.ts +251 -177
- package/src/core.ts +11 -0
- package/src/dom.ts +4 -1
- package/src/index.ts +5 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/jsx-types.d.ts +3 -1
- package/src/konva.ts +159 -0
- package/src/motion.ts +406 -0
- package/src/renderer.ts +241 -0
- package/src/server.ts +21 -13
- package/src/three-events.ts +330 -0
- package/src/three.ts +350 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts.map +0 -1
- package/dist/core-Dvqm7w2y.js.map +0 -1
- package/dist/development/core-DB3sQz4h.js.map +0 -1
- package/dist/development/dom-D9cY6l1R.js.map +0 -1
- package/dist/development/index.js.map +0 -1
- package/dist/development/vite-BadrFSIM.js +0 -126
- package/dist/development/vite-BadrFSIM.js.map +0 -1
- package/dist/dom-BR9o9wZW.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +0 -1
- package/dist/vite-BadrFSIM.js +0 -126
- package/dist/vite-BadrFSIM.js.map +0 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Cleanup, Scope } from "./core.ts"
|
|
2
|
+
|
|
3
|
+
/** Native renderers expose property access without importing capability engines. */
|
|
4
|
+
export interface CapabilityTarget {
|
|
5
|
+
kind: string
|
|
6
|
+
node: unknown
|
|
7
|
+
read?(key: string): unknown
|
|
8
|
+
write?(key: string, value: unknown): void
|
|
9
|
+
listen?(event: string, listener: () => void): Cleanup
|
|
10
|
+
}
|
|
11
|
+
export interface CapabilityBinding {
|
|
12
|
+
update(props: Record<string, unknown>): void
|
|
13
|
+
dispose(): void
|
|
14
|
+
}
|
|
15
|
+
export interface Capability {
|
|
16
|
+
namespace: string
|
|
17
|
+
scheduleFrame?(callback: (time: number) => void): Cleanup
|
|
18
|
+
attach(target: CapabilityTarget): CapabilityBinding
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function capabilityNamespace(key: string): string | undefined {
|
|
22
|
+
const colon = key.indexOf(`:`)
|
|
23
|
+
if (colon < 0) return undefined
|
|
24
|
+
const namespace = key.slice(0, colon)
|
|
25
|
+
return /^(xml|xmlns|xlink)$/.test(namespace) ? undefined : namespace
|
|
26
|
+
}
|
|
27
|
+
export function validateCapabilities(
|
|
28
|
+
scope: Scope,
|
|
29
|
+
props: Record<string, unknown>,
|
|
30
|
+
): void {
|
|
31
|
+
for (const key of Object.keys(props)) {
|
|
32
|
+
const namespace = capabilityNamespace(key)
|
|
33
|
+
if (namespace && !scope._root._capabilities.has(namespace))
|
|
34
|
+
throw new Error(
|
|
35
|
+
`spa: capability "${namespace}" is not registered for this root`,
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Separate framework properties from native attributes and own their bindings. */
|
|
41
|
+
export function capabilityProps(
|
|
42
|
+
scope: Scope,
|
|
43
|
+
target: CapabilityTarget,
|
|
44
|
+
): (props: Record<string, any>) => Record<string, any> {
|
|
45
|
+
const bindings = new Map<string, CapabilityBinding>()
|
|
46
|
+
const cleanup: Cleanup = () => {
|
|
47
|
+
const errors: unknown[] = []
|
|
48
|
+
for (const binding of [...bindings.values()].reverse()) {
|
|
49
|
+
try {
|
|
50
|
+
binding.dispose()
|
|
51
|
+
} catch (error) {
|
|
52
|
+
errors.push(error)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
bindings.clear()
|
|
56
|
+
if (errors.length) throw new AggregateError(errors)
|
|
57
|
+
}
|
|
58
|
+
scope._cleanups.add(cleanup)
|
|
59
|
+
return (props) => {
|
|
60
|
+
validateCapabilities(scope, props)
|
|
61
|
+
const native: Record<string, any> = {}
|
|
62
|
+
const groups = new Map<string, Record<string, unknown>>()
|
|
63
|
+
for (const key of Object.keys(props)) {
|
|
64
|
+
const namespace = capabilityNamespace(key)
|
|
65
|
+
if (!namespace) {
|
|
66
|
+
native[key] = props[key]
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
const group = groups.get(namespace) ?? {}
|
|
70
|
+
group[key.slice(namespace.length + 1)] = props[key]
|
|
71
|
+
groups.set(namespace, group)
|
|
72
|
+
}
|
|
73
|
+
for (const [namespace, values] of groups) {
|
|
74
|
+
let binding = bindings.get(namespace)
|
|
75
|
+
if (!binding) {
|
|
76
|
+
binding = scope._root._capabilities.get(namespace)!.attach(target)
|
|
77
|
+
bindings.set(namespace, binding)
|
|
78
|
+
}
|
|
79
|
+
binding.update(values)
|
|
80
|
+
}
|
|
81
|
+
for (const [namespace, binding] of bindings)
|
|
82
|
+
if (!groups.has(namespace)) {
|
|
83
|
+
bindings.delete(namespace)
|
|
84
|
+
binding.dispose()
|
|
85
|
+
}
|
|
86
|
+
return native
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/compiler.ts
CHANGED
|
@@ -1,189 +1,263 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { decode } from "html-entities"
|
|
2
|
+
import type { SourceMap } from "magic-string"
|
|
3
|
+
import MagicString from "magic-string"
|
|
4
|
+
import type {
|
|
5
|
+
Expression,
|
|
6
|
+
JSXElement,
|
|
7
|
+
JSXElementName,
|
|
8
|
+
JSXFragment,
|
|
9
|
+
Node,
|
|
10
|
+
} from "oxc-parser"
|
|
11
|
+
import { parseSync, visitorKeys } from "oxc-parser"
|
|
12
|
+
|
|
13
|
+
type Part = string | Node
|
|
14
|
+
|
|
15
|
+
const decodeJsx = (value: string): string =>
|
|
16
|
+
decode(value, { level: `html4`, scope: `strict` })
|
|
17
|
+
|
|
18
|
+
/** Match JSX's multiline whitespace rules without reprinting JavaScript. */
|
|
19
|
+
function text(value: string): string {
|
|
20
|
+
const lines = value.split(/\r\n|\n|\r/)
|
|
21
|
+
let last = 0
|
|
22
|
+
for (let i = 0; i < lines.length; i++) if (/[^ \t]/.test(lines[i])) last = i
|
|
23
|
+
return lines
|
|
24
|
+
.map((line, i) => {
|
|
25
|
+
line = line.replace(/\t/g, ` `)
|
|
26
|
+
if (i > 0) line = line.replace(/^ +/, ``)
|
|
27
|
+
if (i < lines.length - 1) line = line.replace(/ +$/, ``)
|
|
28
|
+
return line && i < last ? `${line} ` : line
|
|
29
|
+
})
|
|
30
|
+
.join(``)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function eager(node: Node, allowJsx = false): boolean {
|
|
34
|
+
if (node.type === `ParenthesizedExpression`)
|
|
35
|
+
return eager(node.expression, allowJsx)
|
|
36
|
+
return (
|
|
37
|
+
node.type === `Literal` ||
|
|
38
|
+
node.type === `FunctionExpression` ||
|
|
39
|
+
node.type === `ArrowFunctionExpression` ||
|
|
40
|
+
(allowJsx && (node.type === `JSXElement` || node.type === `JSXFragment`)) ||
|
|
41
|
+
(node.type === `TemplateLiteral` && node.expressions.length === 0)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
3
44
|
|
|
4
45
|
export function compile(
|
|
5
46
|
code: string,
|
|
6
47
|
filename: string,
|
|
7
|
-
): { code: string; map:
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
48
|
+
): { code: string; map: SourceMap } {
|
|
49
|
+
const parsed = parseSync(filename, code, {
|
|
50
|
+
lang: `tsx`,
|
|
51
|
+
preserveParens: true,
|
|
52
|
+
})
|
|
53
|
+
if (parsed.errors.length) {
|
|
54
|
+
const error = parsed.errors[0]
|
|
55
|
+
throw new SyntaxError(
|
|
56
|
+
[`${filename}: ${error.message}`, error.codeframe]
|
|
57
|
+
.filter(Boolean)
|
|
58
|
+
.join(`\n`),
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
const output = new MagicString(code)
|
|
62
|
+
const parents = new WeakMap<Node, Node>()
|
|
63
|
+
const identifiers = new Set<string>()
|
|
64
|
+
const elements: (JSXElement | JSXFragment)[] = []
|
|
65
|
+
const functions = new Set<Node>()
|
|
66
|
+
function walk(node: Node, parent?: Node, outerFunction?: Node): void {
|
|
67
|
+
if (parent) parents.set(node, parent)
|
|
68
|
+
output.addSourcemapLocation(node.start)
|
|
69
|
+
output.addSourcemapLocation(node.end)
|
|
70
|
+
if (node.type === `Identifier` || node.type === `JSXIdentifier`)
|
|
71
|
+
identifiers.add(node.name)
|
|
72
|
+
if (
|
|
73
|
+
!outerFunction &&
|
|
74
|
+
(node.type === `FunctionDeclaration` ||
|
|
75
|
+
node.type === `FunctionExpression` ||
|
|
76
|
+
node.type === `ArrowFunctionExpression`)
|
|
77
|
+
)
|
|
78
|
+
outerFunction = node
|
|
79
|
+
if (node.type === `JSXElement` || node.type === `JSXFragment`) {
|
|
80
|
+
elements.push(node)
|
|
81
|
+
if (outerFunction) functions.add(outerFunction)
|
|
82
|
+
}
|
|
83
|
+
for (const key of visitorKeys[node.type]) {
|
|
84
|
+
const value = (node as unknown as Record<string, unknown>)[key]
|
|
85
|
+
if (Array.isArray(value)) {
|
|
86
|
+
for (const child of value) if (child) walk(child, node, outerFunction)
|
|
87
|
+
} else if (value) walk(value as Node, node, outerFunction)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
walk(parsed.program)
|
|
91
|
+
function uid(base: string): string {
|
|
92
|
+
let name = `_${base}`
|
|
93
|
+
let suffix = 1
|
|
94
|
+
while (identifiers.has(name)) name = `_${base}${++suffix}`
|
|
95
|
+
identifiers.add(name)
|
|
96
|
+
return name
|
|
97
|
+
}
|
|
98
|
+
const create = uid(`spa`)
|
|
99
|
+
const merge = uid(`props`)
|
|
100
|
+
const join = (parts: Part[][], separator: string): Part[] =>
|
|
101
|
+
parts.flatMap((part, i) => (i ? [separator, ...part] : part))
|
|
102
|
+
const expression = (node: Expression): Part[] =>
|
|
103
|
+
eager(node, true) ? [`(`, node, `)`] : [`() => (`, node, `)`]
|
|
104
|
+
function tag(node: JSXElementName): string {
|
|
105
|
+
if (node.type === `JSXNamespacedName`)
|
|
106
|
+
return JSON.stringify(`${node.namespace.name}:${node.name.name}`)
|
|
107
|
+
if (node.type === `JSXMemberExpression`)
|
|
108
|
+
return code.slice(node.start, node.end)
|
|
109
|
+
return /^[a-z]/.test(node.name) || node.name.includes(`-`)
|
|
110
|
+
? JSON.stringify(node.name)
|
|
111
|
+
: node.name
|
|
112
|
+
}
|
|
113
|
+
/** Rewrite syntax around untouched expression spans, including nested JSX. */
|
|
114
|
+
function rewrite(node: JSXElement | JSXFragment, parts: Part[]): void {
|
|
115
|
+
let cursor = node.start
|
|
116
|
+
let replacement = ``
|
|
117
|
+
for (const part of parts) {
|
|
118
|
+
if (typeof part === `string`) {
|
|
119
|
+
replacement += part
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
if (cursor < part.start) output.update(cursor, part.start, replacement)
|
|
123
|
+
else output.appendLeft(cursor, replacement)
|
|
124
|
+
cursor = part.end
|
|
125
|
+
replacement = ``
|
|
126
|
+
}
|
|
127
|
+
// Every JSX node ends with closing syntax after its last expression span.
|
|
128
|
+
output.update(cursor, node.end, replacement)
|
|
129
|
+
}
|
|
130
|
+
for (const node of elements) {
|
|
131
|
+
const chunks: Part[][] = []
|
|
132
|
+
let properties: Part[][] = []
|
|
133
|
+
const flush = () => {
|
|
134
|
+
if (properties.length) chunks.push([`{`, ...join(properties, `, `), `}`])
|
|
135
|
+
properties = []
|
|
136
|
+
}
|
|
137
|
+
if (node.type === `JSXElement`) {
|
|
138
|
+
for (const attribute of node.openingElement.attributes) {
|
|
139
|
+
if (attribute.type === `JSXSpreadAttribute`) {
|
|
140
|
+
flush()
|
|
141
|
+
chunks.push([`() => (`, attribute.argument, `)`])
|
|
142
|
+
continue
|
|
143
|
+
}
|
|
144
|
+
const key = JSON.stringify(
|
|
145
|
+
attribute.name.type === `JSXNamespacedName`
|
|
146
|
+
? `${attribute.name.namespace.name}:${attribute.name.name.name}`
|
|
147
|
+
: attribute.name.name,
|
|
24
148
|
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
149
|
+
const value = attribute.value
|
|
150
|
+
if (!value) properties.push([`${key}: true`])
|
|
151
|
+
else if (value.type === `Literal`)
|
|
152
|
+
properties.push([`${key}: ${JSON.stringify(decodeJsx(value.value))}`])
|
|
153
|
+
else {
|
|
154
|
+
const expr =
|
|
155
|
+
value.type === `JSXExpressionContainer` ? value.expression : value
|
|
156
|
+
properties.push(
|
|
157
|
+
key === `"key"` || eager(expr)
|
|
158
|
+
? [`${key}: (`, expr, `)`]
|
|
159
|
+
: [`get ${key}(){ return (`, expr, `); }`],
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
30
163
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
(
|
|
164
|
+
const children: Part[][] = []
|
|
165
|
+
for (const child of node.children) {
|
|
166
|
+
if (child.type === `JSXText`) {
|
|
167
|
+
const value = text(decodeJsx(child.value))
|
|
168
|
+
if (value) children.push([JSON.stringify(value)])
|
|
169
|
+
} else if (child.type === `JSXElement` || child.type === `JSXFragment`)
|
|
170
|
+
children.push([child])
|
|
171
|
+
else if (child.type === `JSXExpressionContainer`) {
|
|
172
|
+
if (child.expression.type !== `JSXEmptyExpression`)
|
|
173
|
+
children.push(expression(child.expression))
|
|
174
|
+
} else
|
|
175
|
+
throw new SyntaxError(`${filename}: JSX spread children are unsupported`)
|
|
176
|
+
}
|
|
177
|
+
if (children.length)
|
|
178
|
+
properties.push([
|
|
179
|
+
`children: `,
|
|
180
|
+
...(children.length === 1
|
|
181
|
+
? children[0]
|
|
182
|
+
: [`[`, ...join(children, `, `), `]`]),
|
|
183
|
+
])
|
|
184
|
+
flush()
|
|
185
|
+
const spreads =
|
|
186
|
+
node.type === `JSXElement` &&
|
|
187
|
+
node.openingElement.attributes.some(
|
|
188
|
+
(attr) => attr.type === `JSXSpreadAttribute`,
|
|
35
189
|
)
|
|
190
|
+
const props = spreads
|
|
191
|
+
? [`${merge}(`, ...join(chunks, `, `), `)`]
|
|
192
|
+
: (chunks[0] ?? [`{}`])
|
|
193
|
+
rewrite(node, [
|
|
194
|
+
`${create}(${node.type === `JSXElement` ? tag(node.openingElement.name) : `null`}, `,
|
|
195
|
+
...props,
|
|
196
|
+
`)`,
|
|
197
|
+
])
|
|
198
|
+
}
|
|
199
|
+
const components = new Set<string>()
|
|
200
|
+
for (const fn of functions) {
|
|
201
|
+
let node = fn
|
|
202
|
+
let parent = parents.get(node)
|
|
203
|
+
while (parent?.type === `ParenthesizedExpression`) {
|
|
204
|
+
node = parent
|
|
205
|
+
parent = parents.get(node)
|
|
36
206
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
207
|
+
if (fn.type === `FunctionDeclaration` && fn.id) components.add(fn.id.name)
|
|
208
|
+
else if (
|
|
209
|
+
parent?.type === `VariableDeclarator` &&
|
|
210
|
+
parent.id.type === `Identifier`
|
|
211
|
+
)
|
|
212
|
+
components.add(parent.id.name)
|
|
213
|
+
else if (parent?.type === `ExportDefaultDeclaration`) {
|
|
214
|
+
const name = uid(`Component`)
|
|
215
|
+
if (fn.type === `FunctionDeclaration`) {
|
|
216
|
+
// Keep anonymous default function declarations hoisted.
|
|
217
|
+
const token = [
|
|
218
|
+
...code
|
|
219
|
+
.slice(fn.start, fn.body!.start)
|
|
220
|
+
.matchAll(/\/\*[\s\S]*?\*\/|\/\/[^\r\n]*|\bfunction\b|\*/g),
|
|
221
|
+
].find((match) => match[0] === (fn.generator ? `*` : `function`))!
|
|
222
|
+
output.appendLeft(fn.start + token.index + token[0].length, ` ${name}`)
|
|
223
|
+
} else {
|
|
224
|
+
// Replace only keywords so intervening comments and annotations survive.
|
|
225
|
+
const prefix = code.slice(parent.start, node.start)
|
|
226
|
+
for (const token of prefix.matchAll(
|
|
227
|
+
/\/\*[\s\S]*?\*\/|\/\/[^\r\n]*|\b(?:export|default)\b/g,
|
|
228
|
+
)) {
|
|
229
|
+
if (token[0] !== `export` && token[0] !== `default`) continue
|
|
230
|
+
output.update(
|
|
231
|
+
parent.start + token.index,
|
|
232
|
+
parent.start + token.index + token[0].length,
|
|
233
|
+
token[0] === `export` ? `const ${name} =` : ``,
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
output.appendLeft(parent.end, `;\nexport default ${name};`)
|
|
237
|
+
}
|
|
238
|
+
components.add(name)
|
|
41
239
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const id = path.scope.generateUidIdentifier(`Component`)
|
|
50
|
-
path.replaceWithMultiple([
|
|
51
|
-
t.variableDeclaration(`const`, [
|
|
52
|
-
t.variableDeclarator(id, path.node.declaration),
|
|
53
|
-
]),
|
|
54
|
-
t.exportDefaultDeclaration(id),
|
|
55
|
-
])
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
Program: {
|
|
59
|
-
enter(path) {
|
|
60
|
-
create = path.scope.generateUidIdentifier(`spa`)
|
|
61
|
-
merge = path.scope.generateUidIdentifier(`props`)
|
|
62
|
-
},
|
|
63
|
-
exit(path) {
|
|
64
|
-
path.unshiftContainer(
|
|
65
|
-
`body`,
|
|
66
|
-
t.importDeclaration(
|
|
67
|
-
[
|
|
68
|
-
t.importSpecifier(create, t.identifier(`jsx`)),
|
|
69
|
-
t.importSpecifier(merge, t.identifier(`merge`)),
|
|
70
|
-
],
|
|
71
|
-
t.stringLiteral(`@atom.io/spa/jsx-runtime`),
|
|
72
|
-
),
|
|
73
|
-
)
|
|
74
|
-
for (const component of components)
|
|
75
|
-
path.pushContainer(
|
|
76
|
-
`body`,
|
|
77
|
-
t.expressionStatement(
|
|
78
|
-
t.assignmentExpression(
|
|
79
|
-
`=`,
|
|
80
|
-
t.memberExpression(
|
|
81
|
-
t.identifier(component),
|
|
82
|
-
t.identifier(`spa`),
|
|
83
|
-
),
|
|
84
|
-
t.booleanLiteral(true),
|
|
85
|
-
),
|
|
86
|
-
),
|
|
87
|
-
)
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
"JSXElement|JSXFragment": {
|
|
91
|
-
exit(path: any) {
|
|
92
|
-
const node = path.node as t.JSXElement | t.JSXFragment
|
|
93
|
-
const chunks: t.Expression[] = []
|
|
94
|
-
let properties: (t.ObjectProperty | t.ObjectMethod)[] = []
|
|
95
|
-
const flush = () => {
|
|
96
|
-
if (properties.length) {
|
|
97
|
-
chunks.push(t.objectExpression(properties))
|
|
98
|
-
properties = []
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (t.isJSXElement(node))
|
|
102
|
-
for (const attribute of node.openingElement.attributes) {
|
|
103
|
-
if (t.isJSXSpreadAttribute(attribute)) {
|
|
104
|
-
flush()
|
|
105
|
-
chunks.push(t.arrowFunctionExpression([], attribute.argument))
|
|
106
|
-
continue
|
|
107
|
-
}
|
|
108
|
-
const key = t.isJSXNamespacedName(attribute.name)
|
|
109
|
-
? `${attribute.name.namespace.name}:${attribute.name.name.name}`
|
|
110
|
-
: attribute.name.name
|
|
111
|
-
const value =
|
|
112
|
-
attribute.value == null
|
|
113
|
-
? t.booleanLiteral(true)
|
|
114
|
-
: t.isJSXExpressionContainer(attribute.value)
|
|
115
|
-
? attribute.value.expression
|
|
116
|
-
: attribute.value
|
|
117
|
-
// The parser rejects empty attribute expressions.
|
|
118
|
-
const expression = value as t.Expression
|
|
119
|
-
properties.push(
|
|
120
|
-
key === `key` ||
|
|
121
|
-
literal(expression) ||
|
|
122
|
-
t.isFunction(expression)
|
|
123
|
-
? t.objectProperty(t.stringLiteral(key), expression)
|
|
124
|
-
: t.objectMethod(
|
|
125
|
-
`get`,
|
|
126
|
-
t.stringLiteral(key),
|
|
127
|
-
[],
|
|
128
|
-
t.blockStatement([t.returnStatement(expression)]),
|
|
129
|
-
),
|
|
130
|
-
)
|
|
131
|
-
}
|
|
132
|
-
const children = t.react
|
|
133
|
-
.buildChildren(node)
|
|
134
|
-
.map((value) => child(value as t.Expression))
|
|
135
|
-
if (children.length)
|
|
136
|
-
properties.push(
|
|
137
|
-
t.objectProperty(
|
|
138
|
-
t.identifier(`children`),
|
|
139
|
-
children.length === 1
|
|
140
|
-
? children[0]
|
|
141
|
-
: t.arrayExpression(children),
|
|
142
|
-
),
|
|
143
|
-
)
|
|
144
|
-
flush()
|
|
145
|
-
const props =
|
|
146
|
-
chunks.length > 1
|
|
147
|
-
? t.callExpression(merge, chunks)
|
|
148
|
-
: (chunks[0] ?? t.objectExpression([]))
|
|
149
|
-
const result = t.callExpression(create, [
|
|
150
|
-
t.isJSXElement(node)
|
|
151
|
-
? name(node.openingElement.name)
|
|
152
|
-
: t.nullLiteral(),
|
|
153
|
-
props,
|
|
154
|
-
])
|
|
155
|
-
generated.add(result)
|
|
156
|
-
let fn = path.findParent((parent: any) => parent.isFunction())
|
|
157
|
-
while (fn?.findParent((parent: any) => parent.isFunction()))
|
|
158
|
-
fn = fn.findParent((parent: any) => parent.isFunction())
|
|
159
|
-
if (fn) {
|
|
160
|
-
if (fn.node.id) components.add(fn.node.id.name)
|
|
161
|
-
else if (
|
|
162
|
-
fn.parentPath.isVariableDeclarator() &&
|
|
163
|
-
t.isIdentifier(fn.parentPath.node.id)
|
|
164
|
-
)
|
|
165
|
-
components.add(fn.parentPath.node.id.name)
|
|
166
|
-
else if (
|
|
167
|
-
fn.parentPath.isExportDefaultDeclaration() &&
|
|
168
|
-
t.isFunctionDeclaration(fn.node)
|
|
169
|
-
) {
|
|
170
|
-
fn.node.id = path.scope.generateUidIdentifier(`Component`)
|
|
171
|
-
components.add(fn.node.id.name)
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
path.replaceWith(result)
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
},
|
|
240
|
+
}
|
|
241
|
+
if (elements.length) {
|
|
242
|
+
// Keep hashbangs and directive prologues ahead of generated imports.
|
|
243
|
+
let position = parsed.program.hashbang?.end ?? 0
|
|
244
|
+
for (const statement of parsed.program.body) {
|
|
245
|
+
if (statement.type !== `ExpressionStatement` || !statement.directive) break
|
|
246
|
+
position = statement.end
|
|
178
247
|
}
|
|
248
|
+
output.appendLeft(
|
|
249
|
+
position,
|
|
250
|
+
`\nimport { jsx as ${create}, merge as ${merge} } from "@atom.io/spa/jsx-runtime";\n`,
|
|
251
|
+
)
|
|
252
|
+
for (const component of components)
|
|
253
|
+
output.append(`\n;${component}.spa = true;`)
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
code: output.toString(),
|
|
257
|
+
map: output.generateMap({
|
|
258
|
+
source: filename,
|
|
259
|
+
includeContent: true,
|
|
260
|
+
hires: true,
|
|
261
|
+
}),
|
|
179
262
|
}
|
|
180
|
-
const result = transformSync(code, {
|
|
181
|
-
filename,
|
|
182
|
-
babelrc: false,
|
|
183
|
-
configFile: false,
|
|
184
|
-
sourceMaps: true,
|
|
185
|
-
parserOpts: { plugins: [`jsx`, `typescript`] },
|
|
186
|
-
plugins: [plugin],
|
|
187
|
-
})!
|
|
188
|
-
return { code: result.code!, map: result.map }
|
|
189
263
|
}
|
package/src/core.ts
CHANGED
|
@@ -30,6 +30,8 @@ import {
|
|
|
30
30
|
timeTravelTransactionGroupInStore,
|
|
31
31
|
} from "atom.io/internal"
|
|
32
32
|
|
|
33
|
+
import type { Capability } from "./capabilities.ts"
|
|
34
|
+
|
|
33
35
|
export type Cleanup = () => void
|
|
34
36
|
// A callable interface permits the recursive Child union. A type alias does not.
|
|
35
37
|
/* oxlint-disable typescript/prefer-function-type -- A callable interface is required for recursive Child. */
|
|
@@ -53,6 +55,7 @@ export type Child =
|
|
|
53
55
|
| readonly Child[]
|
|
54
56
|
| Accessor<Child>
|
|
55
57
|
export interface RootOptions {
|
|
58
|
+
capabilities?: readonly Capability[]
|
|
56
59
|
store?: RootStore
|
|
57
60
|
idPrefix?: string
|
|
58
61
|
onError?: (error: unknown) => void
|
|
@@ -65,6 +68,7 @@ export interface Scope {
|
|
|
65
68
|
_dead: boolean
|
|
66
69
|
}
|
|
67
70
|
export interface Root extends Scope {
|
|
71
|
+
_capabilities: Map<string, Capability>
|
|
68
72
|
_store: RootStore
|
|
69
73
|
_prefix: string
|
|
70
74
|
_id: number
|
|
@@ -151,7 +155,14 @@ function enqueue(scope: Scope, run: Cleanup): void {
|
|
|
151
155
|
root._queue.add(run)
|
|
152
156
|
}
|
|
153
157
|
export function createRoot(options: RootOptions = {}, server = false): Root {
|
|
158
|
+
const capabilities = new Map<string, Capability>()
|
|
159
|
+
for (const capability of options.capabilities ?? []) {
|
|
160
|
+
if (capabilities.has(capability.namespace))
|
|
161
|
+
throw new Error(`spa: duplicate capability ${capability.namespace}`)
|
|
162
|
+
capabilities.set(capability.namespace, capability)
|
|
163
|
+
}
|
|
154
164
|
const root = {
|
|
165
|
+
_capabilities: capabilities,
|
|
155
166
|
_store: options.store ?? IMPLICIT.STORE,
|
|
156
167
|
_prefix: options.idPrefix ?? `w${serial++}`,
|
|
157
168
|
_id: 0,
|
package/src/dom.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { capabilityProps } from "./capabilities.ts"
|
|
1
2
|
import type { Accessor, Child, RootOptions, Scope, VNode } from "./core.ts"
|
|
2
3
|
import {
|
|
3
4
|
createRoot,
|
|
@@ -290,6 +291,7 @@ function mount(
|
|
|
290
291
|
import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
|
|
291
292
|
)
|
|
292
293
|
}
|
|
294
|
+
const nativeProps = capabilityProps(owner, { kind: `dom`, node: element })
|
|
293
295
|
let previous: Record<string, any> = {}
|
|
294
296
|
owner._cleanups.add(() => {
|
|
295
297
|
for (const key in previous)
|
|
@@ -298,7 +300,8 @@ function mount(
|
|
|
298
300
|
observe(
|
|
299
301
|
owner,
|
|
300
302
|
() => ({ ...props }),
|
|
301
|
-
(
|
|
303
|
+
(all) => {
|
|
304
|
+
const next = nativeProps(all)
|
|
302
305
|
for (const key in { ...previous, ...next })
|
|
303
306
|
if (
|
|
304
307
|
!Object.is(previous[key], next[key]) ||
|
package/src/index.ts
CHANGED
package/src/jsx-runtime.ts
CHANGED
|
@@ -12,6 +12,10 @@ export type {
|
|
|
12
12
|
TargetedEvent,
|
|
13
13
|
} from "./jsx-types.d.ts"
|
|
14
14
|
|
|
15
|
+
/** Optional integrations augment attributes without changing element identity. */
|
|
16
|
+
// oxlint-disable-next-line typescript/no-empty-interface -- Optional integrations augment this declaration.
|
|
17
|
+
export interface CapabilityAttributes<Kind extends string = string> {}
|
|
18
|
+
|
|
15
19
|
export const Fragment = (props: { children?: Child }): Child => props.children
|
|
16
20
|
export function jsx(
|
|
17
21
|
type: VNode[`type`],
|
package/src/jsx-types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Child } from "./core.ts"
|
|
2
|
+
import type { CapabilityAttributes } from "./jsx-runtime.ts"
|
|
2
3
|
|
|
3
4
|
/** Native events retain their target; only currentTarget is the attached element. */
|
|
4
5
|
export type TargetedEvent<E extends Element, T extends Event = Event> = Omit<
|
|
@@ -195,7 +196,8 @@ export type AriaAttributes = Attributes<{
|
|
|
195
196
|
[`aria-valuetext`]: string
|
|
196
197
|
}>
|
|
197
198
|
|
|
198
|
-
export type DOMAttributes<E extends Element> =
|
|
199
|
+
export type DOMAttributes<E extends Element> = CapabilityAttributes<`dom`> &
|
|
200
|
+
EventAttributes<E> &
|
|
199
201
|
AriaAttributes &
|
|
200
202
|
Attributes<{
|
|
201
203
|
children: Child
|