@sigil-dev/compiler 0.9.18 → 0.9.20
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/package.json +1 -1
- package/src/babel/jsx/anchor-mount.ts +33 -15
- package/test/render-parity.test.ts +44 -0
package/package.json
CHANGED
|
@@ -191,6 +191,35 @@ export function buildAnchorMount(
|
|
|
191
191
|
t.identifier(name),
|
|
192
192
|
);
|
|
193
193
|
|
|
194
|
+
// const _k = x.nodeType === 11 ? Array.from(x.childNodes) : [x]
|
|
195
|
+
// A fragment empties into the page when inserted, and cannot be removed
|
|
196
|
+
// itself: what must be removed later is the nodes it held.
|
|
197
|
+
const keepNodes = (name: string): t.Statement =>
|
|
198
|
+
t.variableDeclaration("const", [
|
|
199
|
+
t.variableDeclarator(
|
|
200
|
+
t.identifier("_k"),
|
|
201
|
+
t.conditionalExpression(
|
|
202
|
+
t.binaryExpression(
|
|
203
|
+
"===",
|
|
204
|
+
t.memberExpression(t.identifier(name), t.identifier("nodeType")),
|
|
205
|
+
t.numericLiteral(11),
|
|
206
|
+
),
|
|
207
|
+
t.callExpression(
|
|
208
|
+
t.memberExpression(t.identifier("Array"), t.identifier("from")),
|
|
209
|
+
[t.memberExpression(t.identifier(name), t.identifier("childNodes"))],
|
|
210
|
+
),
|
|
211
|
+
t.arrayExpression([t.identifier(name)]),
|
|
212
|
+
),
|
|
213
|
+
),
|
|
214
|
+
]);
|
|
215
|
+
const pushKept = (): t.Statement =>
|
|
216
|
+
t.expressionStatement(
|
|
217
|
+
t.callExpression(
|
|
218
|
+
t.memberExpression(t.identifier("_nodes"), t.identifier("push")),
|
|
219
|
+
[t.spreadElement(t.identifier("_k"))],
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
|
|
194
223
|
// function _mountFn(_v, _a) { ... }
|
|
195
224
|
const mountBody = t.blockStatement([
|
|
196
225
|
t.expressionStatement(
|
|
@@ -246,21 +275,14 @@ export function buildAnchorMount(
|
|
|
246
275
|
),
|
|
247
276
|
),
|
|
248
277
|
]),
|
|
278
|
+
keepNodes("_n"),
|
|
249
279
|
t.expressionStatement(
|
|
250
280
|
t.callExpression(
|
|
251
281
|
t.memberExpression(t.identifier("_a"), t.identifier("before")),
|
|
252
282
|
[t.identifier("_n")],
|
|
253
283
|
),
|
|
254
284
|
),
|
|
255
|
-
|
|
256
|
-
t.callExpression(
|
|
257
|
-
t.memberExpression(
|
|
258
|
-
t.identifier("_nodes"),
|
|
259
|
-
t.identifier("push"),
|
|
260
|
-
),
|
|
261
|
-
[t.identifier("_n")],
|
|
262
|
-
),
|
|
263
|
-
),
|
|
285
|
+
pushKept(),
|
|
264
286
|
]),
|
|
265
287
|
),
|
|
266
288
|
]),
|
|
@@ -271,18 +293,14 @@ export function buildAnchorMount(
|
|
|
271
293
|
t.identifier("Node"),
|
|
272
294
|
),
|
|
273
295
|
t.blockStatement([
|
|
296
|
+
keepNodes("_v"),
|
|
274
297
|
t.expressionStatement(
|
|
275
298
|
t.callExpression(
|
|
276
299
|
t.memberExpression(t.identifier("_a"), t.identifier("before")),
|
|
277
300
|
[t.identifier("_v")],
|
|
278
301
|
),
|
|
279
302
|
),
|
|
280
|
-
|
|
281
|
-
t.callExpression(
|
|
282
|
-
t.memberExpression(t.identifier("_nodes"), t.identifier("push")),
|
|
283
|
-
[t.identifier("_v")],
|
|
284
|
-
),
|
|
285
|
-
),
|
|
303
|
+
pushKept(),
|
|
286
304
|
]),
|
|
287
305
|
t.ifStatement(
|
|
288
306
|
t.logicalExpression(
|
|
@@ -781,3 +781,47 @@ describe("state imported from another module", () => {
|
|
|
781
781
|
expect(noMarkers(host.innerHTML)).toBe("<p>2 / 4</p>");
|
|
782
782
|
});
|
|
783
783
|
});
|
|
784
|
+
|
|
785
|
+
describe("conditional fragments", () => {
|
|
786
|
+
const source = `
|
|
787
|
+
let show = $state(true);
|
|
788
|
+
globalThis.__toggle = () => (show = !show);
|
|
789
|
+
export const App = () => (
|
|
790
|
+
<div>
|
|
791
|
+
{show && (
|
|
792
|
+
<>
|
|
793
|
+
<p>one</p>
|
|
794
|
+
<p>two</p>
|
|
795
|
+
</>
|
|
796
|
+
)}
|
|
797
|
+
<p>after</p>
|
|
798
|
+
</div>
|
|
799
|
+
);`;
|
|
800
|
+
const toggle = () =>
|
|
801
|
+
(globalThis as unknown as { __toggle: () => void }).__toggle();
|
|
802
|
+
const flush = () => new Promise((r) => setTimeout(r, 0));
|
|
803
|
+
|
|
804
|
+
test("a fragment can be hidden and shown again, hydrated or fresh", async () => {
|
|
805
|
+
const r = render(source);
|
|
806
|
+
for (const host of [r.hydrated, r.spa]) {
|
|
807
|
+
expect(noMarkers(host.innerHTML)).toBe(
|
|
808
|
+
"<div><p>one</p><p>two</p><p>after</p></div>",
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
// the hydrated page and the fresh render share one compiled module each,
|
|
812
|
+
// so toggle each through its own copy of the state
|
|
813
|
+
for (const host of [r.spa]) {
|
|
814
|
+
toggle();
|
|
815
|
+
await flush();
|
|
816
|
+
expect(noMarkers(host.innerHTML)).toBe("<div><p>after</p></div>");
|
|
817
|
+
toggle();
|
|
818
|
+
await flush();
|
|
819
|
+
expect(noMarkers(host.innerHTML)).toBe(
|
|
820
|
+
"<div><p>one</p><p>two</p><p>after</p></div>",
|
|
821
|
+
);
|
|
822
|
+
toggle();
|
|
823
|
+
await flush();
|
|
824
|
+
expect(noMarkers(host.innerHTML)).toBe("<div><p>after</p></div>");
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
});
|