@sigil-dev/compiler 0.3.0

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.
@@ -0,0 +1,520 @@
1
+ import { types as t } from "@babel/core";
2
+ import { buildBind } from "../util/bind";
3
+ import { buildAnchorMount } from "./anchor-mount";
4
+ import { collectChildren } from "./children";
5
+ import { processFragment } from "./fragment";
6
+ import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
7
+ import { buildTextNode } from "./text-node";
8
+ import {
9
+ ATTR_MAP,
10
+ buildHydrationScope,
11
+ containsSignal,
12
+ getCreateElement,
13
+ isPrimitive,
14
+ } from "./utils";
15
+
16
+ /**
17
+ * Add classList.add(hash) statement for scoped CSS.
18
+ */
19
+ function addScopedClass(
20
+ varName: string,
21
+ hash: string,
22
+ statements: t.Statement[],
23
+ ): void {
24
+ statements.push(
25
+ t.expressionStatement(
26
+ t.callExpression(
27
+ t.memberExpression(
28
+ t.memberExpression(t.identifier(varName), t.identifier("classList")),
29
+ t.identifier("add"),
30
+ ),
31
+ [t.stringLiteral(hash)],
32
+ ),
33
+ ),
34
+ );
35
+ }
36
+
37
+ export function processElement(
38
+ node: t.JSXElement,
39
+ statements: t.Statement[],
40
+ genId: () => string,
41
+ signals: Set<string>,
42
+ hash?: string,
43
+ hydrate?: boolean,
44
+ nodesVar?: string,
45
+ parentVar?: string,
46
+ ): string {
47
+ const varName = genId();
48
+ const tag = (node.openingElement.name as t.JSXIdentifier).name;
49
+ const isComponent = /^[A-Z]/.test(tag);
50
+
51
+ if (isComponent) {
52
+ // Component: call the function with a props object
53
+ const propsObj = t.objectExpression([]);
54
+
55
+ // Add attributes to props (including spread)
56
+ for (const attr of node.openingElement.attributes) {
57
+ if (t.isJSXSpreadAttribute(attr)) {
58
+ // {…obj} — spread into props
59
+ propsObj.properties.push(t.spreadElement(attr.argument));
60
+ continue;
61
+ }
62
+ if (!t.isJSXAttribute(attr)) continue;
63
+ const attrName = (attr.name as t.JSXIdentifier).name;
64
+
65
+ if (t.isStringLiteral(attr.value)) {
66
+ propsObj.properties.push(
67
+ t.objectProperty(t.identifier(attrName), attr.value),
68
+ );
69
+ } else if (t.isJSXExpressionContainer(attr.value)) {
70
+ const expr = attr.value.expression as t.Expression;
71
+ propsObj.properties.push(
72
+ t.objectProperty(t.identifier(attrName), expr),
73
+ );
74
+ }
75
+ }
76
+
77
+ // Add children to props
78
+ const childrenExpr = collectChildren(
79
+ node,
80
+ statements,
81
+ genId,
82
+ signals,
83
+ hash,
84
+ hydrate,
85
+ nodesVar,
86
+ );
87
+ if (childrenExpr) {
88
+ propsObj.properties.push(
89
+ t.objectProperty(t.identifier("children"), childrenExpr),
90
+ );
91
+ }
92
+
93
+ // const _el0 = TagName({ ...props })
94
+ statements.push(
95
+ t.variableDeclaration("const", [
96
+ t.variableDeclarator(
97
+ t.identifier(varName),
98
+ t.callExpression(t.identifier(tag), [propsObj]),
99
+ ),
100
+ ]),
101
+ );
102
+
103
+ return varName;
104
+ }
105
+
106
+ // Native element: claim (hydrate) or create (dom)
107
+ const currentNodesVar = nodesVar ?? "__nodes";
108
+ statements.push(
109
+ t.variableDeclaration("const", [
110
+ t.variableDeclarator(
111
+ t.identifier(varName),
112
+ getCreateElement(
113
+ tag,
114
+ !!hydrate,
115
+ currentNodesVar,
116
+ hydrate ? parentVar : undefined,
117
+ ),
118
+ ),
119
+ ]),
120
+ );
121
+
122
+ // In hydrate mode, scope to this element's children for descendants
123
+ let childNodesVar: string | undefined;
124
+ if (hydrate) {
125
+ childNodesVar = genId();
126
+ buildHydrationScope(varName, statements, childNodesVar);
127
+ }
128
+
129
+ // attributes
130
+ const seenAttrs = new Set<string>();
131
+ for (const attr of node.openingElement.attributes) {
132
+ if (t.isJSXSpreadAttribute(attr)) {
133
+ // {…obj} — spread onto native element
134
+ statements.push(
135
+ t.expressionStatement(
136
+ t.callExpression(t.identifier("Object.assign"), [
137
+ t.identifier(varName),
138
+ attr.argument as t.Expression,
139
+ ]),
140
+ ),
141
+ );
142
+ continue;
143
+ }
144
+ if (!t.isJSXAttribute(attr)) continue;
145
+ const attrName = (attr.name as t.JSXIdentifier).name;
146
+ const realAttr = ATTR_MAP[attrName] ?? attrName;
147
+
148
+ if (seenAttrs.has(realAttr)) {
149
+ throw new TypeError(
150
+ `Duplicate attribute "${realAttr}" (via "${attrName}"). Use class or className, not both.`,
151
+ );
152
+ }
153
+ seenAttrs.add(realAttr);
154
+ if (attrName.startsWith("use:")) {
155
+ // use:directive or use:directive={params}
156
+ const directiveName = attrName.slice(4);
157
+ const args: t.Expression[] = [
158
+ t.identifier(varName),
159
+ t.identifier(directiveName),
160
+ ];
161
+ if (t.isJSXExpressionContainer(attr.value)) {
162
+ args.push((attr.value as t.JSXExpressionContainer).expression as t.Expression);
163
+ }
164
+ statements.push(
165
+ t.expressionStatement(
166
+ t.callExpression(t.identifier("applyDirective"), args),
167
+ ),
168
+ );
169
+ } else if (/^on[A-Z]/.test(attrName)) {
170
+ // Standard JSX: onClick, onSubmit, onMouseenter, etc.
171
+ // Convert camelCase to lowercase: onClick -> click, onMouseenter -> mouseenter
172
+ const event = attrName.slice(2).toLowerCase();
173
+ const handler = (attr.value as t.JSXExpressionContainer)
174
+ .expression as t.Expression;
175
+ statements.push(
176
+ t.expressionStatement(
177
+ t.callExpression(
178
+ t.memberExpression(
179
+ t.identifier(varName),
180
+ t.identifier("addEventListener"),
181
+ ),
182
+ [t.stringLiteral(event), handler],
183
+ ),
184
+ ),
185
+ );
186
+ } else if (attrName.startsWith("bind")) {
187
+ buildBind(varName, attrName, attr, statements);
188
+ } else if (t.isStringLiteral(attr.value)) {
189
+ statements.push(
190
+ t.expressionStatement(
191
+ t.assignmentExpression(
192
+ "=",
193
+ t.memberExpression(t.identifier(varName), t.identifier(realAttr)),
194
+ attr.value,
195
+ ),
196
+ ),
197
+ );
198
+ } else if (t.isJSXExpressionContainer(attr.value)) {
199
+ const expr = attr.value.expression as t.Expression;
200
+ if (containsSignal(expr, signals)) {
201
+ // Dynamic class with scoped CSS: use classList.value to preserve hash
202
+ if (attrName === "class" && hash) {
203
+ statements.push(
204
+ t.expressionStatement(
205
+ t.callExpression(t.identifier("createEffect"), [
206
+ t.arrowFunctionExpression(
207
+ [],
208
+ t.blockStatement([
209
+ t.expressionStatement(
210
+ t.assignmentExpression(
211
+ "=",
212
+ t.memberExpression(
213
+ t.memberExpression(
214
+ t.identifier(varName),
215
+ t.identifier("classList"),
216
+ ),
217
+ t.identifier("value"),
218
+ ),
219
+ expr,
220
+ ),
221
+ ),
222
+ t.expressionStatement(
223
+ t.callExpression(
224
+ t.memberExpression(
225
+ t.memberExpression(
226
+ t.identifier(varName),
227
+ t.identifier("classList"),
228
+ ),
229
+ t.identifier("add"),
230
+ ),
231
+ [t.stringLiteral(hash)],
232
+ ),
233
+ ),
234
+ ]),
235
+ ),
236
+ ]),
237
+ ),
238
+ );
239
+ } else {
240
+ // createEffect(() => varName.realAttr = expr)
241
+ statements.push(
242
+ t.expressionStatement(
243
+ t.callExpression(t.identifier("createEffect"), [
244
+ t.arrowFunctionExpression(
245
+ [],
246
+ t.blockStatement([
247
+ t.expressionStatement(
248
+ t.assignmentExpression(
249
+ "=",
250
+ t.memberExpression(
251
+ t.identifier(varName),
252
+ t.identifier(realAttr),
253
+ ),
254
+ expr,
255
+ ),
256
+ ),
257
+ ]),
258
+ ),
259
+ ]),
260
+ ),
261
+ );
262
+ }
263
+ } else {
264
+ // varName.realAttr = expr
265
+ statements.push(
266
+ t.expressionStatement(
267
+ t.assignmentExpression(
268
+ "=",
269
+ t.memberExpression(t.identifier(varName), t.identifier(realAttr)),
270
+ expr,
271
+ ),
272
+ ),
273
+ );
274
+ }
275
+ }
276
+ }
277
+
278
+ // Add scoped CSS hash to classList (if hash is present)
279
+ if (hash) {
280
+ addScopedClass(varName, hash, statements);
281
+ }
282
+
283
+ // children
284
+ for (const child of node.children) {
285
+ if (t.isJSXElement(child)) {
286
+ const childVar = processElement(
287
+ child,
288
+ statements,
289
+ genId,
290
+ signals,
291
+ hash,
292
+ hydrate,
293
+ childNodesVar,
294
+ hydrate ? varName : undefined,
295
+ );
296
+ if (!hydrate) {
297
+ statements.push(
298
+ t.expressionStatement(
299
+ t.callExpression(
300
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
301
+ [t.identifier(childVar)],
302
+ ),
303
+ ),
304
+ );
305
+ }
306
+ } else if (t.isJSXFragment(child)) {
307
+ const childVar = processFragment(
308
+ child,
309
+ statements,
310
+ genId,
311
+ signals,
312
+ hash,
313
+ hydrate,
314
+ childNodesVar,
315
+ hydrate ? varName : undefined,
316
+ );
317
+ if (!hydrate) {
318
+ statements.push(
319
+ t.expressionStatement(
320
+ t.callExpression(
321
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
322
+ [t.identifier(childVar)],
323
+ ),
324
+ ),
325
+ );
326
+ }
327
+ } else if (t.isJSXText(child)) {
328
+ const text = child.value;
329
+ if (!text.trim()) continue;
330
+ if (hydrate) {
331
+ // SSR hydration: text already in DOM from SSR, do nothing.
332
+ // SPA navigation: childNodesVar pool is empty, create text node.
333
+ const poolLen = t.memberExpression(
334
+ t.identifier(childNodesVar!),
335
+ t.identifier("length"),
336
+ );
337
+ statements.push(
338
+ t.ifStatement(
339
+ t.binaryExpression("===", poolLen, t.numericLiteral(0)),
340
+ t.expressionStatement(
341
+ t.callExpression(
342
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
343
+ [
344
+ t.callExpression(
345
+ t.memberExpression(
346
+ t.identifier("document"),
347
+ t.identifier("createTextNode"),
348
+ ),
349
+ [t.stringLiteral(text.replace(/\s*\n\s*/g, " "))],
350
+ ),
351
+ ],
352
+ ),
353
+ ),
354
+ ),
355
+ );
356
+ } else {
357
+ statements.push(
358
+ t.expressionStatement(
359
+ t.callExpression(
360
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
361
+ [
362
+ t.callExpression(
363
+ t.memberExpression(
364
+ t.identifier("document"),
365
+ t.identifier("createTextNode"),
366
+ ),
367
+ [t.stringLiteral(text)],
368
+ ),
369
+ ],
370
+ ),
371
+ ),
372
+ );
373
+ }
374
+ } else if (t.isJSXExpressionContainer(child)) {
375
+ if (t.isJSXEmptyExpression(child.expression)) continue;
376
+ const expr = child.expression as t.Expression;
377
+ if (t.isJSXElement(expr)) {
378
+ const childVar = processElement(
379
+ expr,
380
+ statements,
381
+ genId,
382
+ signals,
383
+ hash,
384
+ hydrate,
385
+ childNodesVar,
386
+ hydrate ? varName : undefined,
387
+ );
388
+ if (!hydrate) {
389
+ statements.push(
390
+ t.expressionStatement(
391
+ t.callExpression(
392
+ t.memberExpression(
393
+ t.identifier(varName),
394
+ t.identifier("append"),
395
+ ),
396
+ [t.identifier(childVar)],
397
+ ),
398
+ ),
399
+ );
400
+ }
401
+ } else if (t.isJSXFragment(expr)) {
402
+ const childVar = processFragment(
403
+ expr,
404
+ statements,
405
+ genId,
406
+ signals,
407
+ hash,
408
+ hydrate,
409
+ childNodesVar,
410
+ hydrate ? varName : undefined,
411
+ );
412
+ if (!hydrate) {
413
+ statements.push(
414
+ t.expressionStatement(
415
+ t.callExpression(
416
+ t.memberExpression(
417
+ t.identifier(varName),
418
+ t.identifier("append"),
419
+ ),
420
+ [t.identifier(childVar)],
421
+ ),
422
+ ),
423
+ );
424
+ }
425
+ } else if (containsSignal(expr, signals)) {
426
+ if (isPrimitive(expr)) {
427
+ // fast path: text node + textContent
428
+ buildTextNode(
429
+ varName,
430
+ expr,
431
+ statements,
432
+ genId,
433
+ hydrate,
434
+ childNodesVar,
435
+ hydrate ? varName : undefined,
436
+ );
437
+ } else {
438
+ // Check for keyed list pattern
439
+ const keyed = findKeyedMapExpr(expr, signals);
440
+ if (keyed) {
441
+ buildKeyedList(
442
+ varName,
443
+ keyed,
444
+ statements,
445
+ genId,
446
+ signals,
447
+ processElement,
448
+ hash,
449
+ hydrate,
450
+ childNodesVar,
451
+ hydrate ? varName : undefined,
452
+ );
453
+ } else {
454
+ buildAnchorMount(
455
+ varName,
456
+ expr,
457
+ statements,
458
+ genId,
459
+ hydrate,
460
+ childNodesVar,
461
+ hydrate ? varName : undefined,
462
+ );
463
+ }
464
+ }
465
+ } else {
466
+ // non-reactive: set once
467
+ if (hydrate) {
468
+ const claimCommentArgs: t.Expression[] = [
469
+ t.identifier(childNodesVar!),
470
+ t.stringLiteral("g"),
471
+ ];
472
+ if (varName) claimCommentArgs.push(t.identifier(varName));
473
+ const poolLen = t.memberExpression(
474
+ t.identifier(childNodesVar!),
475
+ t.identifier("length"),
476
+ );
477
+ // Pool has elements (initial load): SSR content already in DOM, just consume delimiters
478
+ const claimG = t.expressionStatement(
479
+ t.callExpression(t.identifier("claimComment"), claimCommentArgs),
480
+ );
481
+ const claimSlashG = (() => {
482
+ const args: t.Expression[] = [
483
+ t.identifier(childNodesVar!),
484
+ t.stringLiteral("/g"),
485
+ ];
486
+ if (varName) args.push(t.identifier(varName));
487
+ return t.expressionStatement(
488
+ t.callExpression(t.identifier("claimComment"), args),
489
+ );
490
+ })();
491
+ // Pool empty (SPA nav): create and insert fresh elements
492
+ const insertCall = t.expressionStatement(
493
+ t.callExpression(t.identifier("insert"), [
494
+ t.identifier(varName),
495
+ expr,
496
+ ]),
497
+ );
498
+ statements.push(
499
+ t.ifStatement(
500
+ t.binaryExpression(">", poolLen, t.numericLiteral(0)),
501
+ t.blockStatement([claimG, claimSlashG]),
502
+ t.blockStatement([insertCall]),
503
+ ),
504
+ );
505
+ } else {
506
+ statements.push(
507
+ t.expressionStatement(
508
+ t.callExpression(t.identifier("insert"), [
509
+ t.identifier(varName),
510
+ expr,
511
+ ]),
512
+ ),
513
+ );
514
+ }
515
+ }
516
+ }
517
+ }
518
+
519
+ return varName;
520
+ }