@atom.io/spa 0.0.1 → 0.0.3
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 +254 -6
- package/README.mdx +83 -1
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-application.js +2 -2
- package/dist/astro-application.js.map +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +6 -3
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +2 -1
- 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-BkWvVZWZ.d.ts} +4 -2
- package/dist/astro-shared-BkWvVZWZ.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/capabilities-BkQ2i0vV.js +63 -0
- package/dist/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/capabilities-CFP-HIuY.d.ts +115 -0
- package/dist/capabilities-CFP-HIuY.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{core-Dvqm7w2y.js → core-DCmqLZQP.js} +23 -4
- package/dist/core-DCmqLZQP.js.map +1 -0
- package/dist/development/astro-application.js +2 -2
- package/dist/development/astro-application.js.map +1 -1
- package/dist/development/astro-client.js +6 -3
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +2 -1
- 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/capabilities-BkQ2i0vV.js +63 -0
- package/dist/development/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/development/{core-DB3sQz4h.js → core-p9vciX9C.js} +23 -4
- package/dist/development/core-p9vciX9C.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-Ci1XvbVJ.js} +21 -7
- package/dist/development/dom-Ci1XvbVJ.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 +143 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +601 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +190 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +4 -2
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-DG0xjuS5.js +38 -0
- package/dist/development/src-DG0xjuS5.js.map +1 -0
- package/dist/development/three.js +452 -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-dHQM0O7Q.js} +21 -7
- package/dist/dom-dHQM0O7Q.js.map +1 -0
- package/dist/index-CJJXcKaw.d.ts +24 -0
- package/dist/index-CJJXcKaw.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-CREZYKDC.d.ts} +6 -4
- package/dist/jsx-runtime-CREZYKDC.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 +143 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +45 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +601 -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 +190 -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 +4 -2
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-DCQa6qxs.js +38 -0
- package/dist/src-DCQa6qxs.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +452 -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 +62 -15
- package/src/astro-application.ts +2 -2
- 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 +107 -0
- package/src/compiler.ts +251 -178
- package/src/core.ts +38 -2
- package/src/dom.ts +38 -4
- 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 +186 -0
- package/src/motion-tween.ts +106 -0
- package/src/motion.ts +519 -0
- package/src/presence.ts +324 -0
- package/src/renderer.ts +278 -0
- package/src/server.ts +3 -0
- package/src/three-events.ts +342 -0
- package/src/three.ts +373 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts +0 -44
- 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-CQchIQ_-.js +0 -126
- package/dist/development/vite-CQchIQ_-.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-CQchIQ_-.js +0 -126
- package/dist/vite-CQchIQ_-.js.map +0 -1
package/src/compiler.ts
CHANGED
|
@@ -1,190 +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 || t.isArrowFunctionExpression(chunks[0])
|
|
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.isFunctionDeclaration() && fn.node.id)
|
|
161
|
-
components.add(fn.node.id.name)
|
|
162
|
-
else if (
|
|
163
|
-
fn.parentPath.isVariableDeclarator() &&
|
|
164
|
-
t.isIdentifier(fn.parentPath.node.id)
|
|
165
|
-
)
|
|
166
|
-
components.add(fn.parentPath.node.id.name)
|
|
167
|
-
else if (
|
|
168
|
-
fn.parentPath.isExportDefaultDeclaration() &&
|
|
169
|
-
t.isFunctionDeclaration(fn.node)
|
|
170
|
-
) {
|
|
171
|
-
fn.node.id = path.scope.generateUidIdentifier(`Component`)
|
|
172
|
-
components.add(fn.node.id.name)
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
path.replaceWith(result)
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
},
|
|
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
|
|
179
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
|
+
}),
|
|
180
262
|
}
|
|
181
|
-
const result = transformSync(code, {
|
|
182
|
-
filename,
|
|
183
|
-
babelrc: false,
|
|
184
|
-
configFile: false,
|
|
185
|
-
sourceMaps: true,
|
|
186
|
-
parserOpts: { plugins: [`jsx`, `typescript`] },
|
|
187
|
-
plugins: [plugin],
|
|
188
|
-
})!
|
|
189
|
-
return { code: result.code!, map: result.map }
|
|
190
263
|
}
|
package/src/core.ts
CHANGED
|
@@ -30,6 +30,13 @@ import {
|
|
|
30
30
|
timeTravelTransactionGroupInStore,
|
|
31
31
|
} from "atom.io/internal"
|
|
32
32
|
|
|
33
|
+
import type { Capability } from "./capabilities.ts"
|
|
34
|
+
import type {
|
|
35
|
+
ExitParticipant,
|
|
36
|
+
PresenceCoordinator,
|
|
37
|
+
PresenceTarget,
|
|
38
|
+
} from "./presence.ts"
|
|
39
|
+
|
|
33
40
|
export type Cleanup = () => void
|
|
34
41
|
// A callable interface permits the recursive Child union. A type alias does not.
|
|
35
42
|
/* oxlint-disable typescript/prefer-function-type -- A callable interface is required for recursive Child. */
|
|
@@ -53,11 +60,15 @@ export type Child =
|
|
|
53
60
|
| readonly Child[]
|
|
54
61
|
| Accessor<Child>
|
|
55
62
|
export interface RootOptions {
|
|
63
|
+
capabilities?: readonly Capability[]
|
|
56
64
|
store?: RootStore
|
|
57
65
|
idPrefix?: string
|
|
58
66
|
onError?: (error: unknown) => void
|
|
59
67
|
}
|
|
60
68
|
export interface Scope {
|
|
69
|
+
_exiting?: boolean
|
|
70
|
+
_exits?: Set<ExitParticipant>
|
|
71
|
+
_targets?: Set<PresenceTarget>
|
|
61
72
|
_root: Root
|
|
62
73
|
_parent?: Scope | undefined
|
|
63
74
|
_cleanups: Set<Cleanup | Scope>
|
|
@@ -65,6 +76,8 @@ export interface Scope {
|
|
|
65
76
|
_dead: boolean
|
|
66
77
|
}
|
|
67
78
|
export interface Root extends Scope {
|
|
79
|
+
_presence?: PresenceCoordinator
|
|
80
|
+
_capabilities: Map<string, Capability>
|
|
68
81
|
_store: RootStore
|
|
69
82
|
_prefix: string
|
|
70
83
|
_id: number
|
|
@@ -120,6 +133,11 @@ export function dispose(scope: Scope): void {
|
|
|
120
133
|
if (scope._dead) return
|
|
121
134
|
scope._dead = true
|
|
122
135
|
const errors: unknown[] = []
|
|
136
|
+
try {
|
|
137
|
+
scope._root._presence?.forget(scope)
|
|
138
|
+
} catch (error) {
|
|
139
|
+
errors.push(error)
|
|
140
|
+
}
|
|
123
141
|
for (const cleanup of [...scope._cleanups].reverse()) {
|
|
124
142
|
try {
|
|
125
143
|
typeof cleanup === `function` ? cleanup() : dispose(cleanup)
|
|
@@ -138,7 +156,7 @@ export function invalidate(scope: Scope): void {
|
|
|
138
156
|
if (typeof child !== `function`) invalidate(child)
|
|
139
157
|
}
|
|
140
158
|
function enqueue(scope: Scope, run: Cleanup): void {
|
|
141
|
-
if (scope
|
|
159
|
+
if (!isPresent(scope)) return
|
|
142
160
|
const root = scope._root
|
|
143
161
|
if (!root._queue.size)
|
|
144
162
|
queueMicrotask(() => {
|
|
@@ -151,7 +169,14 @@ function enqueue(scope: Scope, run: Cleanup): void {
|
|
|
151
169
|
root._queue.add(run)
|
|
152
170
|
}
|
|
153
171
|
export function createRoot(options: RootOptions = {}, server = false): Root {
|
|
172
|
+
const capabilities = new Map<string, Capability>()
|
|
173
|
+
for (const capability of options.capabilities ?? []) {
|
|
174
|
+
if (capabilities.has(capability.namespace))
|
|
175
|
+
throw new Error(`spa: duplicate capability ${capability.namespace}`)
|
|
176
|
+
capabilities.set(capability.namespace, capability)
|
|
177
|
+
}
|
|
154
178
|
const root = {
|
|
179
|
+
_capabilities: capabilities,
|
|
155
180
|
_store: options.store ?? IMPLICIT.STORE,
|
|
156
181
|
_prefix: options.idPrefix ?? `w${serial++}`,
|
|
157
182
|
_id: 0,
|
|
@@ -165,6 +190,11 @@ export function createRoot(options: RootOptions = {}, server = false): Root {
|
|
|
165
190
|
((error) => {
|
|
166
191
|
throw error
|
|
167
192
|
})
|
|
193
|
+
for (const capability of capabilities.values())
|
|
194
|
+
if (capability.presence) {
|
|
195
|
+
if (root._presence) throw new Error(`spa: multiple presence coordinators`)
|
|
196
|
+
root._presence = capability.presence(root)
|
|
197
|
+
}
|
|
168
198
|
root.flush = () => {
|
|
169
199
|
try {
|
|
170
200
|
let passes = 0
|
|
@@ -208,7 +238,7 @@ export function observe<T>(
|
|
|
208
238
|
): void {
|
|
209
239
|
const subscriptions = new Map<Subscribe, Cleanup>()
|
|
210
240
|
const run = () => {
|
|
211
|
-
if (scope
|
|
241
|
+
if (!isPresent(scope)) return
|
|
212
242
|
const used = new Set<Subscribe>()
|
|
213
243
|
const previous = tracking
|
|
214
244
|
tracking = (subscribe) => {
|
|
@@ -383,3 +413,9 @@ export function plain(value: Child): string {
|
|
|
383
413
|
// oxlint-disable-next-line typescript/no-base-to-string -- Valid text children are primitives here; development rejects malformed object children.
|
|
384
414
|
return String(value)
|
|
385
415
|
}
|
|
416
|
+
|
|
417
|
+
export function isPresent(scope: Scope): boolean {
|
|
418
|
+
for (let at: Scope | undefined = scope; at; at = at._parent)
|
|
419
|
+
if (at._dead || at._exiting) return false
|
|
420
|
+
return true
|
|
421
|
+
}
|
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,
|
|
@@ -128,6 +129,21 @@ function mount(
|
|
|
128
129
|
comment(`{`, parent, before, owner, cursor)
|
|
129
130
|
let end: Node | null = null
|
|
130
131
|
let records = new Map<unknown, RecordNode>()
|
|
132
|
+
const presence = owner._root._presence?.collection<RecordNode, Child>(
|
|
133
|
+
(record) => record._scope,
|
|
134
|
+
(record, child) =>
|
|
135
|
+
typeof record._value === typeof child &&
|
|
136
|
+
(record._value as VNode | null)?.type === (child as VNode | null)?.type,
|
|
137
|
+
)
|
|
138
|
+
const retire = (
|
|
139
|
+
record: RecordNode,
|
|
140
|
+
key: unknown,
|
|
141
|
+
eligible: boolean,
|
|
142
|
+
index: number,
|
|
143
|
+
) => {
|
|
144
|
+
if (presence) presence.remove(record, key, eligible, index, () => {})
|
|
145
|
+
else dispose(record._scope)
|
|
146
|
+
}
|
|
131
147
|
let initial = cursor
|
|
132
148
|
observe(
|
|
133
149
|
owner,
|
|
@@ -149,9 +165,15 @@ function mount(
|
|
|
149
165
|
(typeof record._value !== typeof child ||
|
|
150
166
|
(record._value as VNode | null)?.type !== vnode?.type)
|
|
151
167
|
) {
|
|
152
|
-
|
|
168
|
+
retire(
|
|
169
|
+
record,
|
|
170
|
+
key,
|
|
171
|
+
child == null || typeof child === `boolean`,
|
|
172
|
+
[...records.keys()].indexOf(key),
|
|
173
|
+
)
|
|
153
174
|
record = undefined
|
|
154
175
|
}
|
|
176
|
+
record ??= presence?.take(key, child)
|
|
155
177
|
if (!record) {
|
|
156
178
|
const childOwner = createScope(owner)
|
|
157
179
|
record = {
|
|
@@ -179,15 +201,20 @@ function mount(
|
|
|
179
201
|
)
|
|
180
202
|
} else {
|
|
181
203
|
record._value = child
|
|
204
|
+
owner._root._presence?.resume(record._scope)
|
|
182
205
|
invalidate(record._scope)
|
|
183
206
|
}
|
|
184
207
|
kept.set(key, record)
|
|
185
208
|
}
|
|
186
209
|
for (const [key, record] of records)
|
|
187
|
-
if (!kept.has(key))
|
|
210
|
+
if (!kept.has(key))
|
|
211
|
+
retire(record, key, true, [...records.keys()].indexOf(key))
|
|
188
212
|
if (!initial) {
|
|
189
213
|
let at = end ?? before
|
|
190
|
-
|
|
214
|
+
const rendered = presence?.merge([...kept.values()]) ?? [
|
|
215
|
+
...kept.values(),
|
|
216
|
+
]
|
|
217
|
+
for (const record of rendered.reverse()) {
|
|
191
218
|
if (record._end.nextSibling !== at) {
|
|
192
219
|
const range = parent.ownerDocument!.createRange()
|
|
193
220
|
range.setStartBefore(record._start)
|
|
@@ -198,6 +225,7 @@ function mount(
|
|
|
198
225
|
}
|
|
199
226
|
if (
|
|
200
227
|
focus?.isConnected &&
|
|
228
|
+
!focus.closest(`[inert]`) &&
|
|
201
229
|
focus !== parent.ownerDocument!.activeElement
|
|
202
230
|
)
|
|
203
231
|
focus.focus({ preventScroll: true })
|
|
@@ -290,6 +318,11 @@ function mount(
|
|
|
290
318
|
import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
|
|
291
319
|
)
|
|
292
320
|
}
|
|
321
|
+
const nativeProps = capabilityProps(
|
|
322
|
+
owner,
|
|
323
|
+
{ kind: `dom`, node: element },
|
|
324
|
+
() => ({ ...props }),
|
|
325
|
+
)
|
|
293
326
|
let previous: Record<string, any> = {}
|
|
294
327
|
owner._cleanups.add(() => {
|
|
295
328
|
for (const key in previous)
|
|
@@ -298,7 +331,8 @@ function mount(
|
|
|
298
331
|
observe(
|
|
299
332
|
owner,
|
|
300
333
|
() => ({ ...props }),
|
|
301
|
-
(
|
|
334
|
+
(all) => {
|
|
335
|
+
const next = nativeProps(all)
|
|
302
336
|
for (const key in { ...previous, ...next })
|
|
303
337
|
if (
|
|
304
338
|
!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
|