@sigil-dev/compiler 0.9.20 → 0.9.22
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 +3 -2
- 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
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import type { NodePath } from "@babel/core";
|
|
2
|
-
import { types as t } from "@babel/core";
|
|
3
|
-
import { MAGIC } from "../../util/magic.ts";
|
|
4
|
-
|
|
5
|
-
export function handleState(
|
|
6
|
-
declaration: NodePath<t.VariableDeclaration>,
|
|
7
|
-
declarator: NodePath<t.VariableDeclarator>,
|
|
8
|
-
init: NodePath<t.CallExpression>,
|
|
9
|
-
signals: Set<string>,
|
|
10
|
-
dev?: boolean,
|
|
11
|
-
fileKey?: string, // e.g. "src/routes/counter.tsx"
|
|
12
|
-
raw = false, // $state.raw: same rewrites, no deep proxy
|
|
13
|
-
): void {
|
|
14
|
-
const factory = raw ? MAGIC.createRawSignal : MAGIC.createSignal;
|
|
15
|
-
const { id } = declarator.node;
|
|
16
|
-
if (!t.isIdentifier(id)) throw new Error("Destructured $state not supported");
|
|
17
|
-
const name = id.name;
|
|
18
|
-
|
|
19
|
-
// check for // @sigil:reset escape comment
|
|
20
|
-
const hasReset = declaration.node.leadingComments?.some(
|
|
21
|
-
(c) => c.value.trim() === "@sigil:reset",
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const setCall = (value: t.Expression) =>
|
|
25
|
-
t.callExpression(
|
|
26
|
-
t.memberExpression(t.identifier(name), t.identifier("set")),
|
|
27
|
-
[value],
|
|
28
|
-
);
|
|
29
|
-
const binding = declaration.scope.getBinding(name);
|
|
30
|
-
if (!binding) return;
|
|
31
|
-
const refs = [...binding.referencePaths];
|
|
32
|
-
const violations = [...binding.constantViolations];
|
|
33
|
-
|
|
34
|
-
const originalArgs = init.node.arguments;
|
|
35
|
-
|
|
36
|
-
if (dev && fileKey && !hasReset) {
|
|
37
|
-
const key = `${fileKey}#${name}`;
|
|
38
|
-
// createSignal(__hmrAdopt__("file#count", original))
|
|
39
|
-
init.replaceWith(
|
|
40
|
-
t.callExpression(t.identifier(factory), [
|
|
41
|
-
t.callExpression(t.identifier("__hmrAdopt__"), [
|
|
42
|
-
t.stringLiteral(key),
|
|
43
|
-
...(originalArgs as t.Expression[]),
|
|
44
|
-
]),
|
|
45
|
-
]),
|
|
46
|
-
);
|
|
47
|
-
// __hmrRegister__("file#count", count) — inserted after declaration
|
|
48
|
-
declaration.insertAfter(
|
|
49
|
-
t.expressionStatement(
|
|
50
|
-
t.callExpression(t.identifier("__hmrRegister__"), [
|
|
51
|
-
t.stringLiteral(key),
|
|
52
|
-
t.identifier(name),
|
|
53
|
-
]),
|
|
54
|
-
),
|
|
55
|
-
);
|
|
56
|
-
} else {
|
|
57
|
-
init.replaceWith(
|
|
58
|
-
t.callExpression(t.identifier(factory), originalArgs as t.Expression[]),
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
declaration.node.kind = "const";
|
|
63
|
-
// ... rest of rewrites unchanged
|
|
64
|
-
// rewrite writes
|
|
65
|
-
// violations go before refs becase UpdateExpression
|
|
66
|
-
// is both a read and a write and this prevents count()++
|
|
67
|
-
for (const violation of violations) {
|
|
68
|
-
if (violation.isUpdateExpression()) {
|
|
69
|
-
const op = violation.node.operator === "++" ? "+" : "-";
|
|
70
|
-
violation.replaceWith(
|
|
71
|
-
setCall(
|
|
72
|
-
t.binaryExpression(
|
|
73
|
-
op,
|
|
74
|
-
t.callExpression(t.identifier(name), []),
|
|
75
|
-
t.numericLiteral(1),
|
|
76
|
-
),
|
|
77
|
-
),
|
|
78
|
-
);
|
|
79
|
-
} else if (violation.isAssignmentExpression()) {
|
|
80
|
-
const { operator, right } = violation.node;
|
|
81
|
-
if (operator === "=") {
|
|
82
|
-
// count = 5 -> count.set(5)
|
|
83
|
-
violation.replaceWith(setCall(right));
|
|
84
|
-
} else {
|
|
85
|
-
// count += 2 -> count.set(count() + 2)
|
|
86
|
-
const baseOp = operator.slice(0, -1);
|
|
87
|
-
violation.replaceWith(
|
|
88
|
-
setCall(
|
|
89
|
-
t.binaryExpression(
|
|
90
|
-
baseOp as t.BinaryExpression["operator"],
|
|
91
|
-
t.callExpression(t.identifier(name), []),
|
|
92
|
-
right,
|
|
93
|
-
),
|
|
94
|
-
),
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
for (const ref of refs) {
|
|
101
|
-
if (!ref.isIdentifier()) continue;
|
|
102
|
-
// weird syntax, but just swaps the read with a call.
|
|
103
|
-
ref.replaceWith(t.callExpression(ref.node, []));
|
|
104
|
-
}
|
|
105
|
-
signals.add(name);
|
|
106
|
-
}
|
|
1
|
+
import type { NodePath } from "@babel/core";
|
|
2
|
+
import { types as t } from "@babel/core";
|
|
3
|
+
import { MAGIC } from "../../util/magic.ts";
|
|
4
|
+
|
|
5
|
+
export function handleState(
|
|
6
|
+
declaration: NodePath<t.VariableDeclaration>,
|
|
7
|
+
declarator: NodePath<t.VariableDeclarator>,
|
|
8
|
+
init: NodePath<t.CallExpression>,
|
|
9
|
+
signals: Set<string>,
|
|
10
|
+
dev?: boolean,
|
|
11
|
+
fileKey?: string, // e.g. "src/routes/counter.tsx"
|
|
12
|
+
raw = false, // $state.raw: same rewrites, no deep proxy
|
|
13
|
+
): void {
|
|
14
|
+
const factory = raw ? MAGIC.createRawSignal : MAGIC.createSignal;
|
|
15
|
+
const { id } = declarator.node;
|
|
16
|
+
if (!t.isIdentifier(id)) throw new Error("Destructured $state not supported");
|
|
17
|
+
const name = id.name;
|
|
18
|
+
|
|
19
|
+
// check for // @sigil:reset escape comment
|
|
20
|
+
const hasReset = declaration.node.leadingComments?.some(
|
|
21
|
+
(c) => c.value.trim() === "@sigil:reset",
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const setCall = (value: t.Expression) =>
|
|
25
|
+
t.callExpression(
|
|
26
|
+
t.memberExpression(t.identifier(name), t.identifier("set")),
|
|
27
|
+
[value],
|
|
28
|
+
);
|
|
29
|
+
const binding = declaration.scope.getBinding(name);
|
|
30
|
+
if (!binding) return;
|
|
31
|
+
const refs = [...binding.referencePaths];
|
|
32
|
+
const violations = [...binding.constantViolations];
|
|
33
|
+
|
|
34
|
+
const originalArgs = init.node.arguments;
|
|
35
|
+
|
|
36
|
+
if (dev && fileKey && !hasReset) {
|
|
37
|
+
const key = `${fileKey}#${name}`;
|
|
38
|
+
// createSignal(__hmrAdopt__("file#count", original))
|
|
39
|
+
init.replaceWith(
|
|
40
|
+
t.callExpression(t.identifier(factory), [
|
|
41
|
+
t.callExpression(t.identifier("__hmrAdopt__"), [
|
|
42
|
+
t.stringLiteral(key),
|
|
43
|
+
...(originalArgs as t.Expression[]),
|
|
44
|
+
]),
|
|
45
|
+
]),
|
|
46
|
+
);
|
|
47
|
+
// __hmrRegister__("file#count", count) — inserted after declaration
|
|
48
|
+
declaration.insertAfter(
|
|
49
|
+
t.expressionStatement(
|
|
50
|
+
t.callExpression(t.identifier("__hmrRegister__"), [
|
|
51
|
+
t.stringLiteral(key),
|
|
52
|
+
t.identifier(name),
|
|
53
|
+
]),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
init.replaceWith(
|
|
58
|
+
t.callExpression(t.identifier(factory), originalArgs as t.Expression[]),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declaration.node.kind = "const";
|
|
63
|
+
// ... rest of rewrites unchanged
|
|
64
|
+
// rewrite writes
|
|
65
|
+
// violations go before refs becase UpdateExpression
|
|
66
|
+
// is both a read and a write and this prevents count()++
|
|
67
|
+
for (const violation of violations) {
|
|
68
|
+
if (violation.isUpdateExpression()) {
|
|
69
|
+
const op = violation.node.operator === "++" ? "+" : "-";
|
|
70
|
+
violation.replaceWith(
|
|
71
|
+
setCall(
|
|
72
|
+
t.binaryExpression(
|
|
73
|
+
op,
|
|
74
|
+
t.callExpression(t.identifier(name), []),
|
|
75
|
+
t.numericLiteral(1),
|
|
76
|
+
),
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
} else if (violation.isAssignmentExpression()) {
|
|
80
|
+
const { operator, right } = violation.node;
|
|
81
|
+
if (operator === "=") {
|
|
82
|
+
// count = 5 -> count.set(5)
|
|
83
|
+
violation.replaceWith(setCall(right));
|
|
84
|
+
} else {
|
|
85
|
+
// count += 2 -> count.set(count() + 2)
|
|
86
|
+
const baseOp = operator.slice(0, -1);
|
|
87
|
+
violation.replaceWith(
|
|
88
|
+
setCall(
|
|
89
|
+
t.binaryExpression(
|
|
90
|
+
baseOp as t.BinaryExpression["operator"],
|
|
91
|
+
t.callExpression(t.identifier(name), []),
|
|
92
|
+
right,
|
|
93
|
+
),
|
|
94
|
+
),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (const ref of refs) {
|
|
101
|
+
if (!ref.isIdentifier()) continue;
|
|
102
|
+
// weird syntax, but just swaps the read with a call.
|
|
103
|
+
ref.replaceWith(t.callExpression(ref.node, []));
|
|
104
|
+
}
|
|
105
|
+
signals.add(name);
|
|
106
|
+
}
|