@sigil-dev/compiler 0.9.21 → 0.9.23
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 +162 -162
- package/package.json +14 -13
- package/src/analysis/hydration.ts +899 -0
- package/src/babel/handlers/dom/state.ts +106 -106
- package/src/babel/index.ts +685 -667
- package/src/babel/jsx/anchor-mount.ts +426 -426
- package/src/babel/jsx/children.ts +116 -116
- package/src/babel/jsx/element.ts +1027 -960
- package/src/babel/jsx/fragment.ts +271 -271
- package/src/babel/jsx/keyed-list.ts +317 -317
- package/src/babel/jsx/ssr.ts +307 -297
- package/src/babel/jsx/utils.ts +355 -333
- package/src/babel/util/bind.ts +143 -143
- package/src/babel/util/compile-state.ts +5 -0
- package/src/babel/util/css.ts +208 -208
- package/src/babel/util/props.ts +256 -256
- package/src/babel/util/reactive-fns.ts +110 -0
- package/src/babel/util/static-assertion.ts +51 -0
- package/src/bun-plugin.ts +103 -103
- package/src/vite/index.ts +61 -61
- package/test/bun-plugin.test.ts +45 -45
- package/test/hydration-analysis.test.ts +133 -0
- package/test/hydration.test.ts +277 -277
- package/test/jsx.test.ts +254 -254
- package/test/reactive-helpers.test.ts +135 -0
- package/test/render-parity.test.ts +827 -827
- package/test/scoped-css.test.ts +166 -166
- package/test/ssr-boolean-attrs.test.ts +56 -56
- package/test/ssr-raw-text.test.ts +68 -68
- package/test/ssr.test.ts +130 -130
- package/test/static-assertion.test.ts +123 -0
- package/test/static-skip.test.ts +292 -0
- package/test/style-object.test.ts +36 -36
package/src/babel/util/props.ts
CHANGED
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Names of props objects in the file being compiled (reset per file by the
|
|
5
|
-
* plugin). Children code uses it to tell `{title}`, a prop that may hold
|
|
6
|
-
* nodes, from `{count}`, which is always text.
|
|
7
|
-
*/
|
|
8
|
-
export const propObjects = new Set<string>();
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Values imported from the project's own modules (relative or alias paths),
|
|
12
|
-
* reset per file. `cart.items.length` where `cart` is an exported `$store`
|
|
13
|
-
* must update like local state; the store's proxy tracks the read, so the
|
|
14
|
-
* compiler only has to give the expression an effect. Components (PascalCase)
|
|
15
|
-
* and type imports aren't included, and neither are calls: `format(x)` may
|
|
16
|
-
* return nodes and keeps its old handling.
|
|
17
|
-
*/
|
|
18
|
-
export const importRoots = new Set<string>();
|
|
19
|
-
|
|
20
|
-
export function collectImportRoots(program: NodePath<t.Program>): void {
|
|
21
|
-
importRoots.clear();
|
|
22
|
-
for (const node of program.node.body) {
|
|
23
|
-
if (!t.isImportDeclaration(node) || node.importKind === "type") continue;
|
|
24
|
-
if (!/^[./$~@]/.test(node.source.value)) continue;
|
|
25
|
-
// @scope/package imports are npm packages, not project modules
|
|
26
|
-
if (
|
|
27
|
-
/^@[^/]+\/[^/]/.test(node.source.value) &&
|
|
28
|
-
!node.source.value.startsWith("@/")
|
|
29
|
-
)
|
|
30
|
-
continue;
|
|
31
|
-
for (const spec of node.specifiers) {
|
|
32
|
-
if (t.isImportSpecifier(spec) && spec.importKind === "type") continue;
|
|
33
|
-
const name = spec.local.name;
|
|
34
|
-
if (/^[A-Z]/.test(name)) continue;
|
|
35
|
-
importRoots.add(name);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function rootName(expr: t.Node): string | null {
|
|
41
|
-
let node = expr;
|
|
42
|
-
while (t.isMemberExpression(node)) node = node.object;
|
|
43
|
-
return t.isIdentifier(node) ? node.name : null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** `cart.items.length` where `cart` is imported from the project. */
|
|
47
|
-
export function isImportRead(expr: t.Node): boolean {
|
|
48
|
-
if (!t.isMemberExpression(expr)) return false;
|
|
49
|
-
const root = rootName(expr);
|
|
50
|
-
return root !== null && importRoots.has(root);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** `readStore(count)`, inserted by `rewriteImportReads`. */
|
|
54
|
-
export function isStoreRead(expr: t.Node): boolean {
|
|
55
|
-
return (
|
|
56
|
-
t.isCallExpression(expr) &&
|
|
57
|
-
t.isIdentifier(expr.callee, { name: "readStore" }) &&
|
|
58
|
-
expr.arguments.length === 1 &&
|
|
59
|
-
t.isIdentifier(expr.arguments[0]) &&
|
|
60
|
-
importRoots.has(expr.arguments[0].name)
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Wrap bare reads of project imports in `readStore(…)`.
|
|
66
|
-
*
|
|
67
|
-
* An exported `$store` holding a primitive reaches other modules as its
|
|
68
|
-
* signal getter, since the compiler only rewrites reads in the declaring
|
|
69
|
-
* file. `readStore` calls a signal and returns anything else unchanged, so
|
|
70
|
-
* `{count}` and `count * 2` see the number and track it. Callees, member
|
|
71
|
-
* objects (object stores forward member reads already), JSX tags, exports
|
|
72
|
-
* and types are left alone. Returns whether anything changed.
|
|
73
|
-
*/
|
|
74
|
-
export function rewriteImportReads(program: NodePath<t.Program>): boolean {
|
|
75
|
-
if (importRoots.size === 0) return false;
|
|
76
|
-
let changed = false;
|
|
77
|
-
program.traverse({
|
|
78
|
-
Identifier(path) {
|
|
79
|
-
const name = path.node.name;
|
|
80
|
-
if (!importRoots.has(name)) return;
|
|
81
|
-
if (!path.isReferencedIdentifier()) return;
|
|
82
|
-
const binding = path.scope.getBinding(name);
|
|
83
|
-
if (binding?.kind !== "module") return;
|
|
84
|
-
const parent = path.parent;
|
|
85
|
-
if (
|
|
86
|
-
(t.isCallExpression(parent) || t.isNewExpression(parent)) &&
|
|
87
|
-
parent.callee === path.node
|
|
88
|
-
)
|
|
89
|
-
return;
|
|
90
|
-
if (t.isMemberExpression(parent) && parent.object === path.node) return;
|
|
91
|
-
if (t.isOptionalMemberExpression(parent) && parent.object === path.node)
|
|
92
|
-
return;
|
|
93
|
-
if (t.isTaggedTemplateExpression(parent)) return;
|
|
94
|
-
if (t.isExportSpecifier(parent)) return;
|
|
95
|
-
if (path.findParent((p) => p.isTSType())) return;
|
|
96
|
-
if (t.isCallExpression(parent) && isStoreRead(parent)) return;
|
|
97
|
-
path.replaceWith(
|
|
98
|
-
t.callExpression(t.identifier("readStore"), [t.identifier(name)]),
|
|
99
|
-
);
|
|
100
|
-
path.skip();
|
|
101
|
-
changed = true;
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
return changed;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* A read whose value may be text or nodes and may change: `props.title`,
|
|
109
|
-
* `_props.item.name`, or a member read on an imported store.
|
|
110
|
-
*/
|
|
111
|
-
export function isPropRead(expr: t.Node): boolean {
|
|
112
|
-
if (isStoreRead(expr)) return true;
|
|
113
|
-
const root = rootName(expr);
|
|
114
|
-
if (root === null) return false;
|
|
115
|
-
return propObjects.has(root) || isImportRead(expr);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
type Fn =
|
|
119
|
-
| t.ArrowFunctionExpression
|
|
120
|
-
| t.FunctionExpression
|
|
121
|
-
| t.FunctionDeclaration;
|
|
122
|
-
|
|
123
|
-
interface Prop {
|
|
124
|
-
key: string;
|
|
125
|
-
local: string;
|
|
126
|
-
fallback?: t.Expression;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Keep destructured props live.
|
|
131
|
-
*
|
|
132
|
-
* `({ title }) => <h1>{title}</h1>` reads `title` once, when the component is
|
|
133
|
-
* called, so a prop the caller passes as a getter (see element.ts) would
|
|
134
|
-
* freeze at its first value. This rewrites the pattern into reads through the
|
|
135
|
-
* props object, `(_props) => <h1>{_props.title}</h1>`, and registers
|
|
136
|
-
* `_props` as reactive so JSX that reads it gets an effect.
|
|
137
|
-
*
|
|
138
|
-
* A plain `(props) =>` parameter needs no rewrite, only the registration.
|
|
139
|
-
*
|
|
140
|
-
* Only simple patterns are rewritten: `{ a, b: c, d = 1 }`. Anything else (a
|
|
141
|
-
* rest element, nested patterns, computed keys, a prop reassigned in the body,
|
|
142
|
-
* a prop used as a JSX tag) is left as it was, with the old read-once
|
|
143
|
-
* behaviour.
|
|
144
|
-
*/
|
|
145
|
-
export function liftDestructuredProps(
|
|
146
|
-
program: NodePath<t.Program>,
|
|
147
|
-
reactive: Set<string>,
|
|
148
|
-
): void {
|
|
149
|
-
program.traverse({
|
|
150
|
-
"ArrowFunctionExpression|FunctionExpression|FunctionDeclaration"(
|
|
151
|
-
path: NodePath<Fn>,
|
|
152
|
-
) {
|
|
153
|
-
const param = path.node.params[0];
|
|
154
|
-
if (!param || !isComponent(path) || !returnsJsx(path)) return;
|
|
155
|
-
// `(props) => <p>{props.title}</p>` is already a live read
|
|
156
|
-
if (t.isIdentifier(param)) {
|
|
157
|
-
reactive.add(param.name);
|
|
158
|
-
propObjects.add(param.name);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
if (!t.isObjectPattern(param)) return;
|
|
162
|
-
|
|
163
|
-
const props: Prop[] = [];
|
|
164
|
-
for (const p of param.properties) {
|
|
165
|
-
if (!t.isObjectProperty(p) || p.computed || !t.isIdentifier(p.key))
|
|
166
|
-
return;
|
|
167
|
-
if (t.isIdentifier(p.value)) {
|
|
168
|
-
props.push({ key: p.key.name, local: p.value.name });
|
|
169
|
-
} else if (
|
|
170
|
-
t.isAssignmentPattern(p.value) &&
|
|
171
|
-
t.isIdentifier(p.value.left)
|
|
172
|
-
) {
|
|
173
|
-
props.push({
|
|
174
|
-
key: p.key.name,
|
|
175
|
-
local: p.value.left.name,
|
|
176
|
-
fallback: p.value.right,
|
|
177
|
-
});
|
|
178
|
-
} else return;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const bindings = props.map((p) => path.scope.getBinding(p.local));
|
|
182
|
-
for (const b of bindings) {
|
|
183
|
-
if (!b?.constant) return;
|
|
184
|
-
if (b.referencePaths.some((r) => r.isJSXIdentifier())) return;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const propsId = path.scope.generateUidIdentifier("props");
|
|
188
|
-
if (param.typeAnnotation) propsId.typeAnnotation = param.typeAnnotation;
|
|
189
|
-
|
|
190
|
-
props.forEach((prop, i) => {
|
|
191
|
-
for (const ref of bindings[i]!.referencePaths) {
|
|
192
|
-
const read = () =>
|
|
193
|
-
t.memberExpression(
|
|
194
|
-
t.identifier(propsId.name),
|
|
195
|
-
t.identifier(prop.key),
|
|
196
|
-
);
|
|
197
|
-
const value = prop.fallback
|
|
198
|
-
? t.conditionalExpression(
|
|
199
|
-
t.binaryExpression("===", read(), t.identifier("undefined")),
|
|
200
|
-
t.cloneNode(prop.fallback),
|
|
201
|
-
read(),
|
|
202
|
-
)
|
|
203
|
-
: read();
|
|
204
|
-
const parent = ref.parentPath;
|
|
205
|
-
if (parent?.isObjectProperty() && parent.node.shorthand) {
|
|
206
|
-
parent.node.shorthand = false;
|
|
207
|
-
}
|
|
208
|
-
ref.replaceWith(value);
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
path.node.params[0] = propsId;
|
|
213
|
-
path.scope.crawl();
|
|
214
|
-
reactive.add(propsId.name);
|
|
215
|
-
propObjects.add(propsId.name);
|
|
216
|
-
},
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Components are PascalCase functions (`function Card`, `const Card = …`) or
|
|
222
|
-
* a module's default export (route components are often anonymous). A
|
|
223
|
-
* lowercase callback such as `items.map((item) => <li>…</li>)` is not one;
|
|
224
|
-
* list rows get their own item reactivity in keyed-list.ts.
|
|
225
|
-
*/
|
|
226
|
-
function isComponent(path: NodePath<Fn>): boolean {
|
|
227
|
-
const pascal = (name?: string) => !!name && /^[A-Z]/.test(name);
|
|
228
|
-
if (path.isFunctionDeclaration()) {
|
|
229
|
-
return (
|
|
230
|
-
pascal(path.node.id?.name) || path.parentPath.isExportDefaultDeclaration()
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
const parent = path.parentPath;
|
|
234
|
-
if (parent.isExportDefaultDeclaration()) return true;
|
|
235
|
-
if (parent.isVariableDeclarator() && t.isIdentifier(parent.node.id)) {
|
|
236
|
-
return pascal(parent.node.id.name);
|
|
237
|
-
}
|
|
238
|
-
if (path.isFunctionExpression() && pascal(path.node.id?.name)) return true;
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** A function whose own body (not a nested function) produces JSX. */
|
|
243
|
-
function returnsJsx(path: NodePath<Fn>): boolean {
|
|
244
|
-
let found = false;
|
|
245
|
-
path.traverse({
|
|
246
|
-
JSXElement(p) {
|
|
247
|
-
if (p.getFunctionParent() === path) found = true;
|
|
248
|
-
p.skip();
|
|
249
|
-
},
|
|
250
|
-
JSXFragment(p) {
|
|
251
|
-
if (p.getFunctionParent() === path) found = true;
|
|
252
|
-
p.skip();
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
|
-
return found;
|
|
256
|
-
}
|
|
1
|
+
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Names of props objects in the file being compiled (reset per file by the
|
|
5
|
+
* plugin). Children code uses it to tell `{title}`, a prop that may hold
|
|
6
|
+
* nodes, from `{count}`, which is always text.
|
|
7
|
+
*/
|
|
8
|
+
export const propObjects = new Set<string>();
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Values imported from the project's own modules (relative or alias paths),
|
|
12
|
+
* reset per file. `cart.items.length` where `cart` is an exported `$store`
|
|
13
|
+
* must update like local state; the store's proxy tracks the read, so the
|
|
14
|
+
* compiler only has to give the expression an effect. Components (PascalCase)
|
|
15
|
+
* and type imports aren't included, and neither are calls: `format(x)` may
|
|
16
|
+
* return nodes and keeps its old handling.
|
|
17
|
+
*/
|
|
18
|
+
export const importRoots = new Set<string>();
|
|
19
|
+
|
|
20
|
+
export function collectImportRoots(program: NodePath<t.Program>): void {
|
|
21
|
+
importRoots.clear();
|
|
22
|
+
for (const node of program.node.body) {
|
|
23
|
+
if (!t.isImportDeclaration(node) || node.importKind === "type") continue;
|
|
24
|
+
if (!/^[./$~@]/.test(node.source.value)) continue;
|
|
25
|
+
// @scope/package imports are npm packages, not project modules
|
|
26
|
+
if (
|
|
27
|
+
/^@[^/]+\/[^/]/.test(node.source.value) &&
|
|
28
|
+
!node.source.value.startsWith("@/")
|
|
29
|
+
)
|
|
30
|
+
continue;
|
|
31
|
+
for (const spec of node.specifiers) {
|
|
32
|
+
if (t.isImportSpecifier(spec) && spec.importKind === "type") continue;
|
|
33
|
+
const name = spec.local.name;
|
|
34
|
+
if (/^[A-Z]/.test(name)) continue;
|
|
35
|
+
importRoots.add(name);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function rootName(expr: t.Node): string | null {
|
|
41
|
+
let node = expr;
|
|
42
|
+
while (t.isMemberExpression(node)) node = node.object;
|
|
43
|
+
return t.isIdentifier(node) ? node.name : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `cart.items.length` where `cart` is imported from the project. */
|
|
47
|
+
export function isImportRead(expr: t.Node): boolean {
|
|
48
|
+
if (!t.isMemberExpression(expr)) return false;
|
|
49
|
+
const root = rootName(expr);
|
|
50
|
+
return root !== null && importRoots.has(root);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `readStore(count)`, inserted by `rewriteImportReads`. */
|
|
54
|
+
export function isStoreRead(expr: t.Node): boolean {
|
|
55
|
+
return (
|
|
56
|
+
t.isCallExpression(expr) &&
|
|
57
|
+
t.isIdentifier(expr.callee, { name: "readStore" }) &&
|
|
58
|
+
expr.arguments.length === 1 &&
|
|
59
|
+
t.isIdentifier(expr.arguments[0]) &&
|
|
60
|
+
importRoots.has(expr.arguments[0].name)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Wrap bare reads of project imports in `readStore(…)`.
|
|
66
|
+
*
|
|
67
|
+
* An exported `$store` holding a primitive reaches other modules as its
|
|
68
|
+
* signal getter, since the compiler only rewrites reads in the declaring
|
|
69
|
+
* file. `readStore` calls a signal and returns anything else unchanged, so
|
|
70
|
+
* `{count}` and `count * 2` see the number and track it. Callees, member
|
|
71
|
+
* objects (object stores forward member reads already), JSX tags, exports
|
|
72
|
+
* and types are left alone. Returns whether anything changed.
|
|
73
|
+
*/
|
|
74
|
+
export function rewriteImportReads(program: NodePath<t.Program>): boolean {
|
|
75
|
+
if (importRoots.size === 0) return false;
|
|
76
|
+
let changed = false;
|
|
77
|
+
program.traverse({
|
|
78
|
+
Identifier(path) {
|
|
79
|
+
const name = path.node.name;
|
|
80
|
+
if (!importRoots.has(name)) return;
|
|
81
|
+
if (!path.isReferencedIdentifier()) return;
|
|
82
|
+
const binding = path.scope.getBinding(name);
|
|
83
|
+
if (binding?.kind !== "module") return;
|
|
84
|
+
const parent = path.parent;
|
|
85
|
+
if (
|
|
86
|
+
(t.isCallExpression(parent) || t.isNewExpression(parent)) &&
|
|
87
|
+
parent.callee === path.node
|
|
88
|
+
)
|
|
89
|
+
return;
|
|
90
|
+
if (t.isMemberExpression(parent) && parent.object === path.node) return;
|
|
91
|
+
if (t.isOptionalMemberExpression(parent) && parent.object === path.node)
|
|
92
|
+
return;
|
|
93
|
+
if (t.isTaggedTemplateExpression(parent)) return;
|
|
94
|
+
if (t.isExportSpecifier(parent)) return;
|
|
95
|
+
if (path.findParent((p) => p.isTSType())) return;
|
|
96
|
+
if (t.isCallExpression(parent) && isStoreRead(parent)) return;
|
|
97
|
+
path.replaceWith(
|
|
98
|
+
t.callExpression(t.identifier("readStore"), [t.identifier(name)]),
|
|
99
|
+
);
|
|
100
|
+
path.skip();
|
|
101
|
+
changed = true;
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
return changed;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A read whose value may be text or nodes and may change: `props.title`,
|
|
109
|
+
* `_props.item.name`, or a member read on an imported store.
|
|
110
|
+
*/
|
|
111
|
+
export function isPropRead(expr: t.Node): boolean {
|
|
112
|
+
if (isStoreRead(expr)) return true;
|
|
113
|
+
const root = rootName(expr);
|
|
114
|
+
if (root === null) return false;
|
|
115
|
+
return propObjects.has(root) || isImportRead(expr);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
type Fn =
|
|
119
|
+
| t.ArrowFunctionExpression
|
|
120
|
+
| t.FunctionExpression
|
|
121
|
+
| t.FunctionDeclaration;
|
|
122
|
+
|
|
123
|
+
interface Prop {
|
|
124
|
+
key: string;
|
|
125
|
+
local: string;
|
|
126
|
+
fallback?: t.Expression;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Keep destructured props live.
|
|
131
|
+
*
|
|
132
|
+
* `({ title }) => <h1>{title}</h1>` reads `title` once, when the component is
|
|
133
|
+
* called, so a prop the caller passes as a getter (see element.ts) would
|
|
134
|
+
* freeze at its first value. This rewrites the pattern into reads through the
|
|
135
|
+
* props object, `(_props) => <h1>{_props.title}</h1>`, and registers
|
|
136
|
+
* `_props` as reactive so JSX that reads it gets an effect.
|
|
137
|
+
*
|
|
138
|
+
* A plain `(props) =>` parameter needs no rewrite, only the registration.
|
|
139
|
+
*
|
|
140
|
+
* Only simple patterns are rewritten: `{ a, b: c, d = 1 }`. Anything else (a
|
|
141
|
+
* rest element, nested patterns, computed keys, a prop reassigned in the body,
|
|
142
|
+
* a prop used as a JSX tag) is left as it was, with the old read-once
|
|
143
|
+
* behaviour.
|
|
144
|
+
*/
|
|
145
|
+
export function liftDestructuredProps(
|
|
146
|
+
program: NodePath<t.Program>,
|
|
147
|
+
reactive: Set<string>,
|
|
148
|
+
): void {
|
|
149
|
+
program.traverse({
|
|
150
|
+
"ArrowFunctionExpression|FunctionExpression|FunctionDeclaration"(
|
|
151
|
+
path: NodePath<Fn>,
|
|
152
|
+
) {
|
|
153
|
+
const param = path.node.params[0];
|
|
154
|
+
if (!param || !isComponent(path) || !returnsJsx(path)) return;
|
|
155
|
+
// `(props) => <p>{props.title}</p>` is already a live read
|
|
156
|
+
if (t.isIdentifier(param)) {
|
|
157
|
+
reactive.add(param.name);
|
|
158
|
+
propObjects.add(param.name);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (!t.isObjectPattern(param)) return;
|
|
162
|
+
|
|
163
|
+
const props: Prop[] = [];
|
|
164
|
+
for (const p of param.properties) {
|
|
165
|
+
if (!t.isObjectProperty(p) || p.computed || !t.isIdentifier(p.key))
|
|
166
|
+
return;
|
|
167
|
+
if (t.isIdentifier(p.value)) {
|
|
168
|
+
props.push({ key: p.key.name, local: p.value.name });
|
|
169
|
+
} else if (
|
|
170
|
+
t.isAssignmentPattern(p.value) &&
|
|
171
|
+
t.isIdentifier(p.value.left)
|
|
172
|
+
) {
|
|
173
|
+
props.push({
|
|
174
|
+
key: p.key.name,
|
|
175
|
+
local: p.value.left.name,
|
|
176
|
+
fallback: p.value.right,
|
|
177
|
+
});
|
|
178
|
+
} else return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const bindings = props.map((p) => path.scope.getBinding(p.local));
|
|
182
|
+
for (const b of bindings) {
|
|
183
|
+
if (!b?.constant) return;
|
|
184
|
+
if (b.referencePaths.some((r) => r.isJSXIdentifier())) return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const propsId = path.scope.generateUidIdentifier("props");
|
|
188
|
+
if (param.typeAnnotation) propsId.typeAnnotation = param.typeAnnotation;
|
|
189
|
+
|
|
190
|
+
props.forEach((prop, i) => {
|
|
191
|
+
for (const ref of bindings[i]!.referencePaths) {
|
|
192
|
+
const read = () =>
|
|
193
|
+
t.memberExpression(
|
|
194
|
+
t.identifier(propsId.name),
|
|
195
|
+
t.identifier(prop.key),
|
|
196
|
+
);
|
|
197
|
+
const value = prop.fallback
|
|
198
|
+
? t.conditionalExpression(
|
|
199
|
+
t.binaryExpression("===", read(), t.identifier("undefined")),
|
|
200
|
+
t.cloneNode(prop.fallback),
|
|
201
|
+
read(),
|
|
202
|
+
)
|
|
203
|
+
: read();
|
|
204
|
+
const parent = ref.parentPath;
|
|
205
|
+
if (parent?.isObjectProperty() && parent.node.shorthand) {
|
|
206
|
+
parent.node.shorthand = false;
|
|
207
|
+
}
|
|
208
|
+
ref.replaceWith(value);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
path.node.params[0] = propsId;
|
|
213
|
+
path.scope.crawl();
|
|
214
|
+
reactive.add(propsId.name);
|
|
215
|
+
propObjects.add(propsId.name);
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Components are PascalCase functions (`function Card`, `const Card = …`) or
|
|
222
|
+
* a module's default export (route components are often anonymous). A
|
|
223
|
+
* lowercase callback such as `items.map((item) => <li>…</li>)` is not one;
|
|
224
|
+
* list rows get their own item reactivity in keyed-list.ts.
|
|
225
|
+
*/
|
|
226
|
+
function isComponent(path: NodePath<Fn>): boolean {
|
|
227
|
+
const pascal = (name?: string) => !!name && /^[A-Z]/.test(name);
|
|
228
|
+
if (path.isFunctionDeclaration()) {
|
|
229
|
+
return (
|
|
230
|
+
pascal(path.node.id?.name) || path.parentPath.isExportDefaultDeclaration()
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
const parent = path.parentPath;
|
|
234
|
+
if (parent.isExportDefaultDeclaration()) return true;
|
|
235
|
+
if (parent.isVariableDeclarator() && t.isIdentifier(parent.node.id)) {
|
|
236
|
+
return pascal(parent.node.id.name);
|
|
237
|
+
}
|
|
238
|
+
if (path.isFunctionExpression() && pascal(path.node.id?.name)) return true;
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A function whose own body (not a nested function) produces JSX. */
|
|
243
|
+
function returnsJsx(path: NodePath<Fn>): boolean {
|
|
244
|
+
let found = false;
|
|
245
|
+
path.traverse({
|
|
246
|
+
JSXElement(p) {
|
|
247
|
+
if (p.getFunctionParent() === path) found = true;
|
|
248
|
+
p.skip();
|
|
249
|
+
},
|
|
250
|
+
JSXFragment(p) {
|
|
251
|
+
if (p.getFunctionParent() === path) found = true;
|
|
252
|
+
p.skip();
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
return found;
|
|
256
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
+
import type { SignalScope } from "../jsx/utils.ts";
|
|
3
|
+
import { getMacro } from "./helpers.ts";
|
|
4
|
+
|
|
5
|
+
/** Macros whose result is a signal. `$state.snapshot` and friends are plain reads. */
|
|
6
|
+
const isSignalMacro = (name: string) =>
|
|
7
|
+
name === "$state" ||
|
|
8
|
+
name === "$state.raw" ||
|
|
9
|
+
name === "$store" ||
|
|
10
|
+
name === "$derived" ||
|
|
11
|
+
name === "$derived.by";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Find the functions in this file that read a signal, directly or by calling
|
|
15
|
+
* another function that does, and record them on `signals.reactiveFns`.
|
|
16
|
+
*
|
|
17
|
+
* Without this the compiler decides whether `{expr}` is reactive by looking
|
|
18
|
+
* for a signal name inside `expr`, so `{label()}` with
|
|
19
|
+
* `const label = () => "n=" + count` compiled to a one-time `insert` and
|
|
20
|
+
* silently never updated: the read is real, but it sits behind a call whose
|
|
21
|
+
* arguments show nothing. With this, a call to such a function is treated like
|
|
22
|
+
* a direct read and gets an effect.
|
|
23
|
+
*
|
|
24
|
+
* Matching is by name and over-approximates on purpose. A false positive costs
|
|
25
|
+
* one effect that tracks nothing; a false negative is a value that never
|
|
26
|
+
* updates. Writes (`count = x`) are not reads and do not count.
|
|
27
|
+
*
|
|
28
|
+
* What it cannot see: a function imported from another module that reads
|
|
29
|
+
* another module's state. Wrap that call in `$derived`, which tracks whatever
|
|
30
|
+
* runs inside it.
|
|
31
|
+
*/
|
|
32
|
+
export function collectReactiveFns(
|
|
33
|
+
program: NodePath<t.Program>,
|
|
34
|
+
signals: SignalScope,
|
|
35
|
+
): void {
|
|
36
|
+
// Every name that is a signal: what the file declares plus what was
|
|
37
|
+
// already known (destructured props lifted to `props.x` reads).
|
|
38
|
+
const reads = new Set<string>(signals);
|
|
39
|
+
const fns = new Map<string, NodePath>();
|
|
40
|
+
|
|
41
|
+
program.traverse({
|
|
42
|
+
VariableDeclarator(path) {
|
|
43
|
+
const id = path.node.id;
|
|
44
|
+
if (!t.isIdentifier(id)) return;
|
|
45
|
+
const macro = getMacro(path);
|
|
46
|
+
if (macro && isSignalMacro(macro.name)) reads.add(id.name);
|
|
47
|
+
const init = path.get("init");
|
|
48
|
+
if (init.isArrowFunctionExpression() || init.isFunctionExpression()) {
|
|
49
|
+
fns.set(id.name, init);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
FunctionDeclaration(path) {
|
|
53
|
+
if (path.node.id) fns.set(path.node.id.name, path);
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
if (!reads.size || !fns.size) return;
|
|
57
|
+
|
|
58
|
+
// A function that builds JSX returns nodes, and those nodes carry their own
|
|
59
|
+
// effects for whatever they read. Calling it once and inserting the result
|
|
60
|
+
// is already correct; making the call itself reactive would stringify the
|
|
61
|
+
// node (`String(badge())`) instead of inserting it.
|
|
62
|
+
const buildsJsx = (fn: NodePath): boolean => {
|
|
63
|
+
let found = false;
|
|
64
|
+
fn.traverse({
|
|
65
|
+
JSXElement(path) {
|
|
66
|
+
found = true;
|
|
67
|
+
path.stop();
|
|
68
|
+
},
|
|
69
|
+
JSXFragment(path) {
|
|
70
|
+
found = true;
|
|
71
|
+
path.stop();
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
return found;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const readsSignal = (fn: NodePath): boolean => {
|
|
78
|
+
let found = false;
|
|
79
|
+
fn.traverse({
|
|
80
|
+
Identifier(path) {
|
|
81
|
+
if (found) return;
|
|
82
|
+
if (path.isReferencedIdentifier() && reads.has(path.node.name)) {
|
|
83
|
+
found = true;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
CallExpression(path) {
|
|
87
|
+
if (found) return;
|
|
88
|
+
const callee = path.node.callee;
|
|
89
|
+
if (t.isIdentifier(callee) && signals.reactiveFns.has(callee.name)) {
|
|
90
|
+
found = true;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
return found;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Fixed point: a function that calls a reactive function is reactive, and
|
|
98
|
+
// a helper can be declared after the one that calls it.
|
|
99
|
+
let changed = true;
|
|
100
|
+
while (changed) {
|
|
101
|
+
changed = false;
|
|
102
|
+
for (const [name, fn] of fns) {
|
|
103
|
+
if (signals.reactiveFns.has(name) || buildsJsx(fn)) continue;
|
|
104
|
+
if (readsSignal(fn)) {
|
|
105
|
+
signals.reactiveFns.add(name);
|
|
106
|
+
changed = true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
+
import { checkStaticUse, FORCE_HYDRATE } from "../../analysis/hydration.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `<Footer static />` asserts that this use needs no JavaScript in the
|
|
6
|
+
* browser, and fails the build when it does not.
|
|
7
|
+
*
|
|
8
|
+
* An assertion and not a hint on purpose: a hint the compiler is free to
|
|
9
|
+
* ignore turns into a page that looks right and does nothing, and a compile
|
|
10
|
+
* error that names the line is far cheaper than that. The attribute is removed
|
|
11
|
+
* either way, so the component never receives a `static` prop.
|
|
12
|
+
*
|
|
13
|
+
* Runs before anything else in the file is rewritten, in every mode (the
|
|
14
|
+
* server build must reach the same verdict as the browser build).
|
|
15
|
+
*/
|
|
16
|
+
export function applyStaticAssertions(
|
|
17
|
+
program: NodePath<t.Program>,
|
|
18
|
+
filename: string,
|
|
19
|
+
source: string,
|
|
20
|
+
): void {
|
|
21
|
+
program.traverse({
|
|
22
|
+
JSXAttribute(attr) {
|
|
23
|
+
if (!t.isJSXIdentifier(attr.node.name, { name: "static" })) return;
|
|
24
|
+
const opening = attr.parentPath;
|
|
25
|
+
if (!opening?.isJSXOpeningElement()) return;
|
|
26
|
+
const element = opening.parentPath as NodePath<t.JSXElement>;
|
|
27
|
+
|
|
28
|
+
// `static={false}` is the override the other way: this use hydrates,
|
|
29
|
+
// whatever the analysis would have said
|
|
30
|
+
const v = attr.node.value;
|
|
31
|
+
const off =
|
|
32
|
+
t.isJSXExpressionContainer(v) &&
|
|
33
|
+
t.isBooleanLiteral(v.expression, { value: false });
|
|
34
|
+
if (!off) {
|
|
35
|
+
const problems = checkStaticUse(element, filename, source);
|
|
36
|
+
if (problems.length) {
|
|
37
|
+
const tag = t.isJSXIdentifier(opening.node.name)
|
|
38
|
+
? opening.node.name.name
|
|
39
|
+
: "component";
|
|
40
|
+
const lines = problems.map((p) => ` line ${p.line}: ${p.detail}`);
|
|
41
|
+
throw attr.buildCodeFrameError(
|
|
42
|
+
`<${tag} static> is not static:\n${lines.join("\n")}\n` +
|
|
43
|
+
"Remove `static`, or make what it depends on static.",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (off) FORCE_HYDRATE.add(element.node);
|
|
48
|
+
attr.remove();
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|