@fluixi/compiler 1.0.0-alpha.92 → 1.0.0-alpha.94
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/dist/analyze/intrinsics.cjs +31 -1
- package/dist/analyze/intrinsics.d.cts +14 -0
- package/dist/analyze/intrinsics.d.ts +14 -0
- package/dist/analyze/intrinsics.d.ts.map +1 -1
- package/dist/analyze/intrinsics.js +35 -0
- package/dist/analyze/intrinsics.mjs +31 -1
- package/dist/analyze/reactive-bindings.cjs +18 -0
- package/dist/analyze/reactive-bindings.mjs +18 -0
- package/dist/analyze/static-graph.cjs +22 -4
- package/dist/analyze/static-graph.d.ts.map +1 -1
- package/dist/analyze/static-graph.js +6 -4
- package/dist/analyze/static-graph.mjs +22 -4
- package/dist/{babel-GI375YZ2.mjs → babel-RJFQVFJW.mjs} +4 -4
- package/dist/{chunk-QZ4CJPYP.mjs → chunk-DP2QDWWH.mjs} +1 -1
- package/dist/{chunk-DWNWN5RH.mjs → chunk-E2RWTIYE.mjs} +21 -15
- package/dist/{chunk-XJ5H2MXY.mjs → chunk-HCCBEJEZ.mjs} +31 -1
- package/dist/{chunk-VOUI6XPV.mjs → chunk-HIZU7WI2.mjs} +1 -1
- package/dist/{chunk-VC6X44FS.mjs → chunk-ZR2QHTEX.mjs} +505 -68
- package/dist/codegen/backends/imperative.cjs +24 -6
- package/dist/codegen/backends/imperative.d.ts.map +1 -1
- package/dist/codegen/backends/imperative.js +20 -4
- package/dist/codegen/backends/imperative.mjs +24 -6
- package/dist/frontend/babel/build-ir-lit.cjs +470 -63
- package/dist/frontend/babel/build-ir-lit.mjs +470 -63
- package/dist/frontend/babel/build-ir.cjs +470 -63
- package/dist/frontend/babel/build-ir.mjs +470 -63
- package/dist/frontend/babel/index.cjs +471 -64
- package/dist/frontend/babel/index.mjs +471 -64
- package/dist/frontend/babel/lower-template.cjs +470 -63
- package/dist/frontend/babel/lower-template.mjs +470 -63
- package/dist/frontend/babel/plugin.cjs +470 -63
- package/dist/frontend/babel/plugin.mjs +470 -63
- package/dist/index.cjs +360 -58
- package/dist/index.mjs +372 -58
- package/dist/integrations.cjs +1347 -98
- package/dist/integrations.d.ts.map +1 -1
- package/dist/integrations.js +44 -2
- package/dist/integrations.mjs +205 -19
- package/dist/lower/compile-template.cjs +354 -57
- package/dist/lower/compile-template.d.cts +13 -0
- package/dist/lower/compile-template.d.ts +13 -0
- package/dist/lower/compile-template.d.ts.map +1 -1
- package/dist/lower/compile-template.js +3 -0
- package/dist/lower/compile-template.mjs +366 -57
- package/dist/lower/entities.cjs +288 -0
- package/dist/lower/entities.d.cts +1 -0
- package/dist/lower/entities.d.ts +2 -0
- package/dist/lower/entities.d.ts.map +1 -0
- package/dist/lower/entities.js +54 -0
- package/dist/lower/entities.mjs +267 -0
- package/dist/lower/jsx.cjs +567 -7
- package/dist/lower/jsx.d.cts +8 -0
- package/dist/lower/jsx.d.ts +8 -0
- package/dist/lower/jsx.d.ts.map +1 -1
- package/dist/lower/jsx.js +205 -7
- package/dist/lower/jsx.mjs +567 -7
- package/dist/lower/template.cjs +332 -56
- package/dist/lower/template.d.ts.map +1 -1
- package/dist/lower/template.js +57 -49
- package/dist/lower/template.mjs +332 -56
- package/dist/resolve/builtins.cjs +1 -1
- package/dist/resolve/builtins.mjs +1 -1
- package/dist/resolve/component-globals.cjs +20 -1
- package/dist/resolve/component-globals.js +2 -2
- package/dist/resolve/component-globals.mjs +20 -1
- package/dist/resolve/write-globals.cjs +20 -1
- package/dist/resolve/write-globals.mjs +20 -1
- package/dist/{server-fns-BBZRZUPP.mjs → server-fns-JLSTKWPS.mjs} +4 -4
- package/dist/{server-functions-VEGLIFCE.mjs → server-functions-BGY3RD4R.mjs} +3 -3
- package/dist/transform/index.cjs +1123 -77
- package/dist/transform/index.mjs +1141 -91
- package/dist/transform/intrinsics.cjs +18 -0
- package/dist/transform/intrinsics.mjs +18 -0
- package/dist/transform/props.mjs +19 -14
- package/dist/transform/server-fns.mjs +19 -14
- package/dist/transform/source-locations.cjs +64 -3
- package/dist/transform/source-locations.d.cts +7 -2
- package/dist/transform/source-locations.d.ts +7 -2
- package/dist/transform/source-locations.d.ts.map +1 -1
- package/dist/transform/source-locations.js +68 -3
- package/dist/transform/source-locations.mjs +64 -3
- package/dist/transform/styles.cjs +588 -0
- package/dist/transform/styles.d.cts +211 -0
- package/dist/transform/styles.d.ts +212 -0
- package/dist/transform/styles.d.ts.map +1 -0
- package/dist/transform/styles.js +714 -0
- package/dist/transform/styles.mjs +653 -0
- package/dist/transform/templates.cjs +1122 -76
- package/dist/transform/templates.d.cts +13 -0
- package/dist/transform/templates.d.ts +13 -0
- package/dist/transform/templates.d.ts.map +1 -1
- package/dist/transform/templates.js +51 -14
- package/dist/transform/templates.mjs +1140 -90
- package/dist/{transform-UWZ7ZGJ2.mjs → transform-GBP6Y4UI.mjs} +603 -19
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/vite-styles.cjs +180 -0
- package/dist/vite-styles.d.cts +59 -0
- package/dist/vite-styles.d.ts +60 -0
- package/dist/vite-styles.d.ts.map +1 -0
- package/dist/vite-styles.js +198 -0
- package/dist/vite-styles.mjs +159 -0
- package/package.json +10 -3
package/dist/lower/jsx.mjs
CHANGED
|
@@ -305,6 +305,435 @@ function paren(code, position) {
|
|
|
305
305
|
return topLevelComma(src) === false ? code : `(${code})`;
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
// src/analyze/intrinsics.ts
|
|
309
|
+
var REACTIVE_MODULE = "@fluixi/reactive/signal";
|
|
310
|
+
var INTRINSICS = {
|
|
311
|
+
$signal: { export: "signal", module: REACTIVE_MODULE, returns: "signal-handle" },
|
|
312
|
+
$memo: { export: "memo", module: REACTIVE_MODULE, returns: "memo-handle" },
|
|
313
|
+
$effect: { export: "effect", module: REACTIVE_MODULE, returns: "effect" },
|
|
314
|
+
$store: { export: "store", module: "@fluixi/reactive/store", returns: "store-handle" },
|
|
315
|
+
$resource: { export: "resource", module: REACTIVE_MODULE, returns: "resource-handle" },
|
|
316
|
+
$cachedResource: { export: "cachedResource", module: "@fluixi/reactive/cached-resource", returns: "resource-handle" },
|
|
317
|
+
// No short runtime form for these yet, so the intrinsic stands for the `create*`
|
|
318
|
+
// name. Still worth having: no import to write, and when a shorter form is
|
|
319
|
+
// designed, the change is this line rather than a compiler pass.
|
|
320
|
+
$selector: { export: "createSelector", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
321
|
+
$deferred: { export: "createDeferred", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
322
|
+
// Not value constructors, but the compiler has to know them: they change what a read
|
|
323
|
+
// does. `untrack` suppresses dependency collection, which is exactly the kind of thing
|
|
324
|
+
// dependency analysis must not guess at.
|
|
325
|
+
$untrack: { export: "untrack", module: REACTIVE_MODULE, returns: "plain" },
|
|
326
|
+
$untrackStore: { export: "untrackStore", module: "@fluixi/reactive/store", returns: "plain" },
|
|
327
|
+
// Scopes, scheduling and lifecycle: no value to classify, but the same authoring promise,
|
|
328
|
+
// no import to write. `$cleanup` registers a callback on the current owner (`onCleanup`).
|
|
329
|
+
$batch: { export: "batch", module: REACTIVE_MODULE, returns: "plain" },
|
|
330
|
+
$cleanup: { export: "onCleanup", module: REACTIVE_MODULE, returns: "plain" },
|
|
331
|
+
$root: { export: "root", module: REACTIVE_MODULE, returns: "plain" },
|
|
332
|
+
$context: { export: "context", module: REACTIVE_MODULE, returns: "plain" },
|
|
333
|
+
$renderEffect: { export: "renderEffect", module: REACTIVE_MODULE, returns: "effect" },
|
|
334
|
+
$computed: { export: "computed", module: REACTIVE_MODULE, returns: "effect" },
|
|
335
|
+
// Storage for where the code runs, and a signal kept in it. `$persisted` is a signal
|
|
336
|
+
// handle as far as reads go: the same accessor with `set` on it.
|
|
337
|
+
$storage: { export: "useStorage", module: "@fluixi/core/storage", returns: "plain" },
|
|
338
|
+
$asyncStorage: { export: "useAsyncStorage", module: "@fluixi/core/storage", returns: "plain" },
|
|
339
|
+
$persisted: { export: "persisted", module: "@fluixi/core/storage", returns: "signal-handle" },
|
|
340
|
+
// An environment variable, public or server-only; the browser build refuses a secret.
|
|
341
|
+
$env: { export: "env", module: "@fluixi/start/env", returns: "plain" }
|
|
342
|
+
};
|
|
343
|
+
var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
|
|
344
|
+
|
|
345
|
+
// src/transform/source-locations.ts
|
|
346
|
+
var SOURCE_MARKER = "__fx_source";
|
|
347
|
+
var HOLE_MARKER = "__fx_hole";
|
|
348
|
+
var markHole = (accessor, line, column, name) => `(globalThis.${HOLE_MARKER} ?? ((f) => f))(${accessor},${line},${column}${name === void 0 ? "" : `,${JSON.stringify(name)}`})`;
|
|
349
|
+
var PROPS_MARKER = "__fx_props";
|
|
350
|
+
var markProps = (object, at) => `(globalThis.${PROPS_MARKER} ?? ((p) => p))(${object},${JSON.stringify(at)})`;
|
|
351
|
+
|
|
352
|
+
// src/codegen/serialize-static.ts
|
|
353
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
354
|
+
"area",
|
|
355
|
+
"base",
|
|
356
|
+
"br",
|
|
357
|
+
"col",
|
|
358
|
+
"embed",
|
|
359
|
+
"hr",
|
|
360
|
+
"img",
|
|
361
|
+
"input",
|
|
362
|
+
"link",
|
|
363
|
+
"meta",
|
|
364
|
+
"param",
|
|
365
|
+
"source",
|
|
366
|
+
"track",
|
|
367
|
+
"wbr"
|
|
368
|
+
]);
|
|
369
|
+
var HTML_NAME = {
|
|
370
|
+
className: "class",
|
|
371
|
+
htmlFor: "for"
|
|
372
|
+
};
|
|
373
|
+
var PROPERTY_ONLY = /* @__PURE__ */ new Set([
|
|
374
|
+
"innerHTML",
|
|
375
|
+
"outerHTML",
|
|
376
|
+
"innerText",
|
|
377
|
+
"textContent",
|
|
378
|
+
"value",
|
|
379
|
+
"checked",
|
|
380
|
+
"selected",
|
|
381
|
+
"muted",
|
|
382
|
+
"defaultValue",
|
|
383
|
+
"defaultChecked",
|
|
384
|
+
"children",
|
|
385
|
+
"ref"
|
|
386
|
+
]);
|
|
387
|
+
var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
|
|
388
|
+
function isRawTextElement(tag) {
|
|
389
|
+
return RAW_TEXT_ELEMENTS.has(tag);
|
|
390
|
+
}
|
|
391
|
+
function htmlAttrName(name) {
|
|
392
|
+
return HTML_NAME[name] ?? name;
|
|
393
|
+
}
|
|
394
|
+
function isPlainLiteralAttr(prop) {
|
|
395
|
+
if (prop.kind !== "attr") return false;
|
|
396
|
+
if (prop.expr !== void 0) return false;
|
|
397
|
+
if (prop.name.includes(":")) return false;
|
|
398
|
+
return !PROPERTY_ONLY.has(prop.name);
|
|
399
|
+
}
|
|
400
|
+
function isSerializable(node) {
|
|
401
|
+
if (!node.static) return false;
|
|
402
|
+
if (isRawTextElement(node.tag)) return false;
|
|
403
|
+
for (const prop of node.props) {
|
|
404
|
+
if (!isPlainLiteralAttr(prop)) return false;
|
|
405
|
+
}
|
|
406
|
+
for (const child of node.children) {
|
|
407
|
+
if (child.kind === "text") continue;
|
|
408
|
+
if (child.kind === "element" && isSerializable(child)) continue;
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
413
|
+
function serializeStatic(node) {
|
|
414
|
+
const attrs = node.props.map(serializeProp).filter(Boolean).join("");
|
|
415
|
+
const open = `<${node.tag}${attrs}>`;
|
|
416
|
+
if (VOID_ELEMENTS.has(node.tag)) return open;
|
|
417
|
+
return `${open}${node.children.map(serializeChild).join("")}</${node.tag}>`;
|
|
418
|
+
}
|
|
419
|
+
function serializeChild(child) {
|
|
420
|
+
if (child.kind === "text") return escapeText(child.value);
|
|
421
|
+
if (child.kind !== "element") throw new Error(`serializeStatic: unexpected ${child.kind}`);
|
|
422
|
+
return serializeStatic(child);
|
|
423
|
+
}
|
|
424
|
+
function serializeProp(prop) {
|
|
425
|
+
const name = HTML_NAME[prop.name] ?? prop.name;
|
|
426
|
+
const value = prop.literal;
|
|
427
|
+
if (value === true || value === void 0) return ` ${name}`;
|
|
428
|
+
if (value === false || value === null) return "";
|
|
429
|
+
return ` ${name}="${escapeAttr(String(value))}"`;
|
|
430
|
+
}
|
|
431
|
+
function escapeAttr(value) {
|
|
432
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
433
|
+
}
|
|
434
|
+
function escapeText(value) {
|
|
435
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
436
|
+
}
|
|
437
|
+
function templatePositions(root) {
|
|
438
|
+
const pos = [];
|
|
439
|
+
let file;
|
|
440
|
+
const walk = (el) => {
|
|
441
|
+
file ??= el.at?.file;
|
|
442
|
+
pos.push(el.at?.line ?? 0, el.at?.column ?? 0);
|
|
443
|
+
for (const child of el.children) if (child.kind === "element") walk(child);
|
|
444
|
+
};
|
|
445
|
+
walk(root);
|
|
446
|
+
return file && pos.some((n) => n !== 0) ? { file, pos } : void 0;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/codegen/partial-template.ts
|
|
450
|
+
var HOLE_START = "<!--fx-->";
|
|
451
|
+
var HOLE_END = "<!--fx/-->";
|
|
452
|
+
function isHole(node) {
|
|
453
|
+
return node.kind === "expr" || node.kind === "component" || node.kind === "control";
|
|
454
|
+
}
|
|
455
|
+
function isTemplateChild(node) {
|
|
456
|
+
return node.kind === "text" || node.kind === "element" && isTemplateElement(node);
|
|
457
|
+
}
|
|
458
|
+
function isTemplateElement(node) {
|
|
459
|
+
if (node.svg) return false;
|
|
460
|
+
if (isRawTextElement(node.tag)) return false;
|
|
461
|
+
if (!node.props.every((p) => isPlainLiteralAttr(p))) return false;
|
|
462
|
+
return node.children.every((c) => isTemplateChild(c) || isHole(c));
|
|
463
|
+
}
|
|
464
|
+
function containsHole(node) {
|
|
465
|
+
if (isHole(node)) return true;
|
|
466
|
+
const children = node.children;
|
|
467
|
+
return children ? children.some(containsHole) : false;
|
|
468
|
+
}
|
|
469
|
+
function addressable(el) {
|
|
470
|
+
for (let i = 0; i < el.children.length; i++) {
|
|
471
|
+
const child = el.children[i];
|
|
472
|
+
if (child.kind === "text") {
|
|
473
|
+
if (child.value === "") return false;
|
|
474
|
+
if (el.children[i + 1]?.kind === "text") return false;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return el.children.every((c) => c.kind !== "element" || addressable(c));
|
|
478
|
+
}
|
|
479
|
+
function planPartialTemplate(root) {
|
|
480
|
+
if (!isTemplateElement(root)) return null;
|
|
481
|
+
if (!addressable(root)) return null;
|
|
482
|
+
if (!root.children.some(containsHole)) return null;
|
|
483
|
+
const steps = [];
|
|
484
|
+
const holes = [];
|
|
485
|
+
let refCount = 0;
|
|
486
|
+
const declare = (expr) => {
|
|
487
|
+
const ref = `_n$${refCount++}`;
|
|
488
|
+
steps.push({ ref, expr });
|
|
489
|
+
return ref;
|
|
490
|
+
};
|
|
491
|
+
walk(root, "_el$");
|
|
492
|
+
return { html: serializeWithMarkers(root), tag: root.tag, steps, holes };
|
|
493
|
+
function walk(el, elementRef) {
|
|
494
|
+
let last = -1;
|
|
495
|
+
el.children.forEach((child, i) => {
|
|
496
|
+
if (containsHole(child)) last = i;
|
|
497
|
+
});
|
|
498
|
+
let previous = null;
|
|
499
|
+
for (let i = 0; i <= last; i++) {
|
|
500
|
+
const child = el.children[i];
|
|
501
|
+
const expr = previous ? `${previous}.nextSibling` : `${elementRef}.firstChild`;
|
|
502
|
+
if (isHole(child)) {
|
|
503
|
+
const startRef = declare(expr);
|
|
504
|
+
const endRef = declare(`holeEnd(${startRef})`);
|
|
505
|
+
holes.push({ parentRef: elementRef, startRef, endRef, node: child });
|
|
506
|
+
previous = endRef;
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
const ref = declare(expr);
|
|
510
|
+
if (child.kind === "element" && containsHole(child)) walk(child, ref);
|
|
511
|
+
previous = ref;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function serializeWithMarkers(el) {
|
|
516
|
+
return serializeStatic(withSentinels(el)).split(SENTINEL).join(HOLE_START + HOLE_END);
|
|
517
|
+
}
|
|
518
|
+
function withSentinels(el) {
|
|
519
|
+
return {
|
|
520
|
+
...el,
|
|
521
|
+
children: el.children.map(
|
|
522
|
+
(child) => isHole(child) ? { kind: "text", value: SENTINEL } : child.kind === "element" ? withSentinels(child) : child
|
|
523
|
+
)
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
var SENTINEL = "\0fx-hole\0";
|
|
527
|
+
|
|
528
|
+
// src/codegen/contract.ts
|
|
529
|
+
var RUNTIME_CONTRACT_V1 = {
|
|
530
|
+
version: 1,
|
|
531
|
+
module: "@fluixi/dom",
|
|
532
|
+
symbols: [
|
|
533
|
+
"createNativeElement",
|
|
534
|
+
"insert",
|
|
535
|
+
"spread",
|
|
536
|
+
"setAttribute",
|
|
537
|
+
"delegateEvents",
|
|
538
|
+
"createComponent",
|
|
539
|
+
"createMemo",
|
|
540
|
+
"untrack",
|
|
541
|
+
"Show",
|
|
542
|
+
"For",
|
|
543
|
+
"Index",
|
|
544
|
+
"Switch",
|
|
545
|
+
"Match",
|
|
546
|
+
"Dynamic",
|
|
547
|
+
"ErrorBoundary"
|
|
548
|
+
]
|
|
549
|
+
};
|
|
550
|
+
var RUNTIME_CONTRACT_V2 = {
|
|
551
|
+
version: 2,
|
|
552
|
+
module: "@fluixi/dom",
|
|
553
|
+
symbols: [...RUNTIME_CONTRACT_V1.symbols, "template", "cloneTemplate", "walk"]
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// src/codegen/backends/imperative.ts
|
|
557
|
+
var CONTROL_COMPONENT = {
|
|
558
|
+
show: "Show",
|
|
559
|
+
for: "For",
|
|
560
|
+
index: "Index",
|
|
561
|
+
switch: "Switch",
|
|
562
|
+
match: "Match",
|
|
563
|
+
dynamic: "Dynamic",
|
|
564
|
+
errorBoundary: "ErrorBoundary",
|
|
565
|
+
suspense: "Suspense"
|
|
566
|
+
};
|
|
567
|
+
function isIdent(name) {
|
|
568
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
569
|
+
}
|
|
570
|
+
function propKey(name) {
|
|
571
|
+
return isIdent(name) ? name : JSON.stringify(name);
|
|
572
|
+
}
|
|
573
|
+
function propValue(p) {
|
|
574
|
+
if (p.expr !== void 0) {
|
|
575
|
+
if (p.name === "ref" && p.at)
|
|
576
|
+
return markHole(paren(p.expr, "value"), p.at.line, p.at.column, p.expr);
|
|
577
|
+
if (!p.reactive) return paren(p.expr, "value");
|
|
578
|
+
const accessor = `() => ${paren(p.expr, "arrowBody")}`;
|
|
579
|
+
return p.at ? markHole(accessor, p.at.line, p.at.column, p.directive) : accessor;
|
|
580
|
+
}
|
|
581
|
+
return JSON.stringify(p.literal ?? true);
|
|
582
|
+
}
|
|
583
|
+
function componentPropValue(p) {
|
|
584
|
+
if (p.expr !== void 0) return paren(p.expr, "value");
|
|
585
|
+
return JSON.stringify(p.literal ?? true);
|
|
586
|
+
}
|
|
587
|
+
function emitProps(props, childrenExpr, used) {
|
|
588
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
589
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
590
|
+
const entries = normal.map((p) => `${propKey(p.name)}: ${propValue(p)}`);
|
|
591
|
+
if (childrenExpr != null) entries.push(`children: ${childrenExpr}`);
|
|
592
|
+
const obj = `{ ${entries.join(", ")} }`;
|
|
593
|
+
if (spreads.length > 0) {
|
|
594
|
+
used.add("mergeProps");
|
|
595
|
+
return `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
596
|
+
}
|
|
597
|
+
return obj;
|
|
598
|
+
}
|
|
599
|
+
function markBind(code, at) {
|
|
600
|
+
return at ? markHole(code, at.line, at.column) : code;
|
|
601
|
+
}
|
|
602
|
+
function emitChildren(children, used, templates) {
|
|
603
|
+
if (children.length === 1) return emitNode(children[0], used, templates);
|
|
604
|
+
return `[${children.map((c) => emitNode(c, used, templates)).join(", ")}]`;
|
|
605
|
+
}
|
|
606
|
+
function emitComponent(name, props, children, used, templates, at) {
|
|
607
|
+
used.add("createMemo");
|
|
608
|
+
used.add("createComponent");
|
|
609
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
610
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
611
|
+
const bodies = normal.map((p) => [p.name, componentPropValue(p)]);
|
|
612
|
+
if (children.length > 0) {
|
|
613
|
+
bodies.push(["children", emitChildren(children, used, templates)]);
|
|
614
|
+
}
|
|
615
|
+
let obj = `{ ${bodies.map(([name2, body]) => `get ${propKey(name2)}() { return ${body}; }`).join(", ")} }`;
|
|
616
|
+
if (bodies.some(([, body]) => /\bthis\b/.test(body))) {
|
|
617
|
+
const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => (${body})`);
|
|
618
|
+
const getters = bodies.map(([name2]) => `get ${propKey(name2)}() { return _$t[${JSON.stringify(name2)}](); }`);
|
|
619
|
+
obj = `((_$t) => ({ ${getters.join(", ")} }))({ ${thunks.join(", ")} })`;
|
|
620
|
+
}
|
|
621
|
+
if (spreads.length > 0) {
|
|
622
|
+
used.add("mergeProps");
|
|
623
|
+
obj = `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
624
|
+
}
|
|
625
|
+
const propAt = {};
|
|
626
|
+
for (const p of normal) if (p.at) propAt[p.name] = [p.at.line, p.at.column];
|
|
627
|
+
if (Object.keys(propAt).length) obj = markProps(obj, propAt);
|
|
628
|
+
const mark = at ? `globalThis.${SOURCE_MARKER}?.(${JSON.stringify(at.file)},${at.line},${at.column},${JSON.stringify(name)}), ` : "";
|
|
629
|
+
return `createMemo(${paren(`${mark}() => createComponent(${name}, ${obj})`, "value")})`;
|
|
630
|
+
}
|
|
631
|
+
function staticAttrAssignment(el, prop) {
|
|
632
|
+
const name = htmlAttrName(prop.name);
|
|
633
|
+
const value = prop.literal;
|
|
634
|
+
if (value === true || value === void 0) return `${el}.setAttribute(${JSON.stringify(name)}, "");`;
|
|
635
|
+
if (value === false || value === null) return "";
|
|
636
|
+
return `${el}.setAttribute(${JSON.stringify(name)}, ${JSON.stringify(String(value))});`;
|
|
637
|
+
}
|
|
638
|
+
var partial = false;
|
|
639
|
+
function emitNode(node, used, templates) {
|
|
640
|
+
switch (node.kind) {
|
|
641
|
+
case "text":
|
|
642
|
+
return JSON.stringify(node.value);
|
|
643
|
+
case "expr": {
|
|
644
|
+
if (!node.reactive) return paren(node.code, "value");
|
|
645
|
+
const accessor = `() => ${paren(node.code, "arrowBody")}`;
|
|
646
|
+
return node.at ? markHole(accessor, node.at.line, node.at.column) : accessor;
|
|
647
|
+
}
|
|
648
|
+
case "fragment":
|
|
649
|
+
if (node.children.length === 0) return "null";
|
|
650
|
+
return emitChildren(node.children, used, templates);
|
|
651
|
+
case "component":
|
|
652
|
+
return markBind(emitComponent(node.name, node.props, node.children, used, templates, node.at), node.bindAt ?? node.at);
|
|
653
|
+
case "control": {
|
|
654
|
+
const name = CONTROL_COMPONENT[node.control] ?? node.control;
|
|
655
|
+
used.add(name);
|
|
656
|
+
return markBind(emitComponent(name, node.props, node.children, used, templates), node.bindAt);
|
|
657
|
+
}
|
|
658
|
+
case "element": {
|
|
659
|
+
if (templates && node.static && !node.svg && isSerializable(node)) {
|
|
660
|
+
used.add("templateNode");
|
|
661
|
+
const id = `_tmpl$${templates.length}`;
|
|
662
|
+
templates.push({ id, html: serializeStatic(node), tag: node.tag, svg: false });
|
|
663
|
+
const at = templatePositions(node);
|
|
664
|
+
return at ? `templateNode(${id}, ${JSON.stringify(node.tag)}, false, false, ${JSON.stringify(at)})` : `templateNode(${id}, ${JSON.stringify(node.tag)})`;
|
|
665
|
+
}
|
|
666
|
+
if (templates && partial) {
|
|
667
|
+
const plan = planPartialTemplate(node);
|
|
668
|
+
if (plan) {
|
|
669
|
+
used.add("templateNode");
|
|
670
|
+
used.add("insert");
|
|
671
|
+
used.add("holeEnd");
|
|
672
|
+
used.add("holeContent");
|
|
673
|
+
used.add("holeScope");
|
|
674
|
+
const id = `_tmpl$${templates.length}`;
|
|
675
|
+
templates.push({ id, html: plan.html, tag: plan.tag, svg: false });
|
|
676
|
+
const at = templatePositions(node);
|
|
677
|
+
const lines = [
|
|
678
|
+
`const _el$ = templateNode(${id}, ${JSON.stringify(plan.tag)}, true${at ? `, false, ${JSON.stringify(at)}` : ""});`
|
|
679
|
+
];
|
|
680
|
+
for (const step of plan.steps) lines.push(`const ${step.ref} = ${step.expr};`);
|
|
681
|
+
for (const hole of plan.holes) {
|
|
682
|
+
lines.push(
|
|
683
|
+
`insert(${hole.parentRef}, holeScope(${hole.startRef}, () => ${paren(emitNode(hole.node, used, templates), "arrowBody")}), ${hole.endRef}, holeContent(${hole.startRef}, ${hole.endRef}));`
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
lines.push("return _el$;");
|
|
687
|
+
return `(() => { ${lines.join(" ")} })()`;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
used.add("createNativeElement");
|
|
691
|
+
const tag = JSON.stringify(node.tag);
|
|
692
|
+
const el = "_el$";
|
|
693
|
+
const parts = [];
|
|
694
|
+
const createArgs = node.at ? `${tag}, ${node.svg}, ${JSON.stringify(node.at)}` : node.svg ? `${tag}, true` : tag;
|
|
695
|
+
parts.push(`const ${el} = createNativeElement(${createArgs});`);
|
|
696
|
+
if (node.props.length > 0) {
|
|
697
|
+
if (!node.svg && node.props.every(isPlainLiteralAttr)) {
|
|
698
|
+
for (const p of node.props) parts.push(staticAttrAssignment(el, p));
|
|
699
|
+
} else {
|
|
700
|
+
used.add("spread");
|
|
701
|
+
const svgFlag = node.svg ? ", isSVG: true" : "";
|
|
702
|
+
const accessors = node.props.filter((p) => p.kind !== "spread" && p.expr !== void 0 && p.reactive && p.name !== "ref").map((p) => p.name);
|
|
703
|
+
const customFlag = node.tag.includes("-") ? `, isCustomElement: true, accessors: ${JSON.stringify(accessors)}` : "";
|
|
704
|
+
parts.push(
|
|
705
|
+
`spread({ element: ${el}, props: ${emitProps(node.props, null, used)}${svgFlag}${customFlag} });`
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
for (const child of node.children) {
|
|
710
|
+
used.add("insert");
|
|
711
|
+
const childCode = emitNode(child, used, templates);
|
|
712
|
+
const isFn = child.kind === "expr" && child.reactive || child.kind === "component" || child.kind === "control";
|
|
713
|
+
parts.push(isFn ? `insert(${el}, ${childCode}, null);` : `insert(${el}, ${childCode});`);
|
|
714
|
+
}
|
|
715
|
+
parts.push(`return ${el};`);
|
|
716
|
+
return `(() => { ${parts.join(" ")} })()`;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
function inlineTemplates(code, templates) {
|
|
721
|
+
if (!templates || templates.length === 0) return code;
|
|
722
|
+
const byId = new Map(templates.map((t) => [t.id, JSON.stringify(t.html)]));
|
|
723
|
+
return code.replace(/_tmpl\$\d+/g, (id) => byId.get(id) ?? id);
|
|
724
|
+
}
|
|
725
|
+
var imperativeBackend = {
|
|
726
|
+
name: "imperative",
|
|
727
|
+
contract: RUNTIME_CONTRACT_V1,
|
|
728
|
+
emit(node, options) {
|
|
729
|
+
const used = /* @__PURE__ */ new Set();
|
|
730
|
+
const templates = options?.templateClone !== false ? [] : void 0;
|
|
731
|
+
partial = options?.partialTemplates === true;
|
|
732
|
+
const code = emitNode(node, used, templates);
|
|
733
|
+
return { code, imports: Array.from(used), templates };
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
|
|
308
737
|
// src/lower/jsx.ts
|
|
309
738
|
var SVG_TAGS = /* @__PURE__ */ new Set([
|
|
310
739
|
"svg",
|
|
@@ -336,6 +765,22 @@ var DELEGATED = /* @__PURE__ */ new Set([
|
|
|
336
765
|
"mouseup"
|
|
337
766
|
]);
|
|
338
767
|
var isReactive = (node) => isReactiveShape(shapeOf(node));
|
|
768
|
+
var CONTAINER = "container";
|
|
769
|
+
function directive(node, name) {
|
|
770
|
+
if (node.type !== "JSXElement") return void 0;
|
|
771
|
+
const opening = node.openingElement;
|
|
772
|
+
if (opening.name.type !== "JSXIdentifier" || opening.name.name[0] !== opening.name.name[0].toLowerCase()) return void 0;
|
|
773
|
+
return opening.attributes.find((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === name);
|
|
774
|
+
}
|
|
775
|
+
function directiveExpr(attr) {
|
|
776
|
+
const v = attr.value;
|
|
777
|
+
return v?.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression" ? v.expression : void 0;
|
|
778
|
+
}
|
|
779
|
+
function emitIR(ir, ctx) {
|
|
780
|
+
const { code, imports, templates } = imperativeBackend.emit(ir, {});
|
|
781
|
+
for (const s of imports) ctx.used.add(s);
|
|
782
|
+
return inlineTemplates(code, templates);
|
|
783
|
+
}
|
|
339
784
|
var posOf = (node, ctx) => {
|
|
340
785
|
if (!ctx.sourceFile) return void 0;
|
|
341
786
|
const at = node.loc?.start;
|
|
@@ -413,9 +858,12 @@ function attrExprCode(attr, ctx) {
|
|
|
413
858
|
}
|
|
414
859
|
return null;
|
|
415
860
|
}
|
|
416
|
-
function buildProps(attrs, ctx) {
|
|
861
|
+
function buildProps(attrs, ctx, element = false) {
|
|
417
862
|
const props = [];
|
|
418
863
|
const usePairs = [];
|
|
864
|
+
const classEntries = [];
|
|
865
|
+
const styleEntries = [];
|
|
866
|
+
let styleAt;
|
|
419
867
|
for (const a of attrs) {
|
|
420
868
|
if (a.type === "JSXSpreadAttribute") {
|
|
421
869
|
props.push({ name: "", kind: "spread", expr: ctx.code(a.argument) });
|
|
@@ -431,6 +879,27 @@ function buildProps(attrs, ctx) {
|
|
|
431
879
|
usePairs.push(opts != null ? `[${dir}, () => ${paren(opts, "arrowBody")}]` : `[${dir}]`);
|
|
432
880
|
continue;
|
|
433
881
|
}
|
|
882
|
+
if (element && (ns === "class" || ns === "style")) {
|
|
883
|
+
const key = JSON.stringify(a.name.name.name);
|
|
884
|
+
const v = a.value;
|
|
885
|
+
let value;
|
|
886
|
+
if (v == null) value = "true";
|
|
887
|
+
else if (isString(v)) value = JSON.stringify(literalOf(v));
|
|
888
|
+
else if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
|
|
889
|
+
value = keyValue(v.expression, ctx);
|
|
890
|
+
if (ns === "style") styleAt ??= posOf(v.expression, ctx);
|
|
891
|
+
} else continue;
|
|
892
|
+
(ns === "class" ? classEntries : styleEntries).push(`${key}: ${value}`);
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (element && a.name.type === "JSXIdentifier" && a.name.name === "style" && a.value?.type === "JSXExpressionContainer") {
|
|
896
|
+
const entries = objectEntries(a.value.expression, ctx);
|
|
897
|
+
if (entries) {
|
|
898
|
+
styleEntries.unshift(...entries);
|
|
899
|
+
styleAt = posOf(a.value.expression, ctx);
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
434
903
|
if (ns === "oncapture") {
|
|
435
904
|
const evt = a.name.name.name.toLowerCase();
|
|
436
905
|
const h = attrExprCode(a, ctx) ?? "undefined";
|
|
@@ -439,12 +908,49 @@ function buildProps(attrs, ctx) {
|
|
|
439
908
|
}
|
|
440
909
|
props.push(buildAttr(a, ctx));
|
|
441
910
|
}
|
|
911
|
+
if (classEntries.length > 0) props.push({ name: "classList", kind: "attr", expr: `{ ${classEntries.join(", ")} }`, reactive: false });
|
|
912
|
+
if (styleEntries.length > 0) {
|
|
913
|
+
const entries = `{ ${styleEntries.join(", ")} }`;
|
|
914
|
+
const base = props.findIndex((p) => p.name === "style" && p.kind === "attr");
|
|
915
|
+
if (base === -1) props.push({ name: "style", kind: "attr", expr: entries, reactive: false, ...styleAt ? { at: styleAt } : {} });
|
|
916
|
+
else {
|
|
917
|
+
const prop = props[base];
|
|
918
|
+
const value = prop.literal !== void 0 ? JSON.stringify(prop.literal) : prop.reactive ? `() => ${paren(prop.expr, "arrowBody")}` : prop.expr;
|
|
919
|
+
props[base] = { name: "style", kind: "attr", expr: `[${value}, ${entries}]`, reactive: false, ...prop.at ? { at: prop.at } : {} };
|
|
920
|
+
}
|
|
921
|
+
}
|
|
442
922
|
if (usePairs.length > 0) props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
|
|
443
923
|
return props;
|
|
444
924
|
}
|
|
925
|
+
function keyValue(expr, ctx) {
|
|
926
|
+
const code = ctx.code(expr);
|
|
927
|
+
if (!isReactive(expr)) return code;
|
|
928
|
+
const accessor = `() => ${paren(code, "arrowBody")}`;
|
|
929
|
+
const at = posOf(expr, ctx);
|
|
930
|
+
return at ? markHole(accessor, at.line, at.column) : accessor;
|
|
931
|
+
}
|
|
932
|
+
function objectEntries(expr, ctx) {
|
|
933
|
+
if (expr?.type !== "ObjectExpression") return null;
|
|
934
|
+
const entries = [];
|
|
935
|
+
for (const p of expr.properties) {
|
|
936
|
+
if (p.type !== "ObjectProperty") return null;
|
|
937
|
+
const key = p.computed ? `[${ctx.code(p.key)}]` : p.key.type === "Identifier" ? JSON.stringify(p.key.name) : JSON.stringify(String(p.key.value));
|
|
938
|
+
entries.push(`${key}: ${keyValue(p.value, ctx)}`);
|
|
939
|
+
}
|
|
940
|
+
return entries;
|
|
941
|
+
}
|
|
445
942
|
function buildChildren(children, ctx) {
|
|
446
943
|
const out = [];
|
|
447
|
-
for (
|
|
944
|
+
for (let i = 0; i < children.length; i++) {
|
|
945
|
+
const child = children[i];
|
|
946
|
+
if (directive(child, "if")) {
|
|
947
|
+
let next = i + 1;
|
|
948
|
+
while (next < children.length && children[next].type === "JSXText" && !cleanTemplateText(children[next].value)) next++;
|
|
949
|
+
const fallback = directive(children[next], "else") ? children[next] : null;
|
|
950
|
+
out.push(buildIf(child, fallback, ctx));
|
|
951
|
+
if (fallback) i = next;
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
448
954
|
if (child.type === "JSXText") {
|
|
449
955
|
const text = cleanTemplateText(child.value);
|
|
450
956
|
if (text) out.push({ kind: "text", value: text });
|
|
@@ -460,6 +966,7 @@ function buildChildren(children, ctx) {
|
|
|
460
966
|
});
|
|
461
967
|
}
|
|
462
968
|
} else if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
969
|
+
if (ctx.dropStyles?.has(child.start)) continue;
|
|
463
970
|
out.push(buildNode(child, ctx));
|
|
464
971
|
} else if (child.type === "JSXSpreadChild") {
|
|
465
972
|
out.push({ kind: "expr", code: ctx.code(child.expression), reactive: false });
|
|
@@ -481,19 +988,72 @@ function readLoad(attrs) {
|
|
|
481
988
|
}
|
|
482
989
|
return void 0;
|
|
483
990
|
}
|
|
991
|
+
function buildIf(node, fallback, ctx) {
|
|
992
|
+
const cond = directiveExpr(directive(node, "if"));
|
|
993
|
+
const props = [
|
|
994
|
+
cond ? { name: "when", kind: "attr", expr: ctx.code(cond), reactive: isReactive(cond), at: posOf(cond, ctx) } : { name: "when", kind: "attr", literal: true }
|
|
995
|
+
];
|
|
996
|
+
if (fallback) {
|
|
997
|
+
props.push({ name: "fallback", kind: "attr", expr: emitIR(buildElement(fallback, ctx, ["else"]), ctx), jsxElement: true });
|
|
998
|
+
}
|
|
999
|
+
return { kind: "component", name: "Show", props, children: [buildElement(node, ctx, ["if"])] };
|
|
1000
|
+
}
|
|
1001
|
+
function buildEach(node, ctx) {
|
|
1002
|
+
const attr = directive(node, "each");
|
|
1003
|
+
if (!attr) return null;
|
|
1004
|
+
const list = directiveExpr(attr);
|
|
1005
|
+
const props = [
|
|
1006
|
+
list ? { name: "each", kind: "attr", expr: ctx.code(list), reactive: isReactive(list), at: posOf(list, ctx) } : { name: "each", kind: "attr", expr: "[]" }
|
|
1007
|
+
];
|
|
1008
|
+
const key = directive(node, "key");
|
|
1009
|
+
if (key) {
|
|
1010
|
+
const keyExpr = directiveExpr(key);
|
|
1011
|
+
const by = isString(key.value) ? `(item) => item[${JSON.stringify(literalOf(key.value))}]` : keyExpr ? ctx.code(keyExpr) : void 0;
|
|
1012
|
+
if (by) props.push({ name: "by", kind: "attr", expr: by });
|
|
1013
|
+
}
|
|
1014
|
+
const content = node.children.filter((c) => !(c.type === "JSXText" && !cleanTemplateText(c.value)));
|
|
1015
|
+
const only = content.length === 1 && content[0].type === "JSXExpressionContainer" ? content[0].expression : null;
|
|
1016
|
+
let render;
|
|
1017
|
+
if (only && (only.type === "ArrowFunctionExpression" || only.type === "FunctionExpression")) {
|
|
1018
|
+
const fn = ctx.code(only);
|
|
1019
|
+
const live = only.type === "ArrowFunctionExpression" && only.body.type !== "BlockStatement" && isReactive(only.body);
|
|
1020
|
+
render = live ? `(...args) => () => (${fn})(...args)` : fn;
|
|
1021
|
+
} else {
|
|
1022
|
+
const built = buildChildren(node.children, ctx);
|
|
1023
|
+
const ir = built.length === 1 ? built[0] : { kind: "fragment", children: built };
|
|
1024
|
+
render = `() => ${paren(emitIR(ir, ctx), "arrowBody")}`;
|
|
1025
|
+
}
|
|
1026
|
+
return { kind: "component", name: "For", props, children: [{ kind: "expr", code: render, reactive: false }] };
|
|
1027
|
+
}
|
|
1028
|
+
function buildElement(node, ctx, handled) {
|
|
1029
|
+
const skip = new Set(handled);
|
|
1030
|
+
const directives = /* @__PURE__ */ new Set(["if", "else", "each", ...directive(node, "each") ? ["key"] : []]);
|
|
1031
|
+
const attributes = node.openingElement.attributes.filter(
|
|
1032
|
+
(a) => !(a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && (skip.has(a.name.name) || directives.has(a.name.name)))
|
|
1033
|
+
);
|
|
1034
|
+
const { tag } = tagInfo(node.openingElement.name, ctx);
|
|
1035
|
+
const each = buildEach(node, ctx);
|
|
1036
|
+
const children = each ? [each] : buildChildren(node.children, ctx);
|
|
1037
|
+
if (tag === CONTAINER) return children.length === 1 ? children[0] : { kind: "fragment", children };
|
|
1038
|
+
const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
|
|
1039
|
+
const props = buildProps(attributes, ctx, true);
|
|
1040
|
+
const scope = ctx.scopes?.find((s) => node.start >= s.start && node.end <= s.end);
|
|
1041
|
+
if (scope) props.unshift({ name: "data-fx-s", kind: "attr", literal: scope.value });
|
|
1042
|
+
return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
|
|
1043
|
+
}
|
|
484
1044
|
function buildNode(node, ctx) {
|
|
485
1045
|
if (node.type === "JSXFragment") {
|
|
486
1046
|
return { kind: "fragment", children: buildChildren(node.children, ctx) };
|
|
487
1047
|
}
|
|
1048
|
+
if (directive(node, "if")) return buildIf(node, null, ctx);
|
|
1049
|
+
if (ctx.dropStyles?.has(node.start)) return { kind: "fragment", children: [] };
|
|
488
1050
|
const { tag, component } = tagInfo(node.openingElement.name, ctx);
|
|
1051
|
+
if (!component) return buildElement(node, ctx, []);
|
|
489
1052
|
const props = buildProps(node.openingElement.attributes, ctx);
|
|
490
1053
|
const children = buildChildren(node.children, ctx);
|
|
491
1054
|
const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
|
|
495
|
-
}
|
|
496
|
-
return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
|
|
1055
|
+
const load = readLoad(node.openingElement.attributes);
|
|
1056
|
+
return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
|
|
497
1057
|
}
|
|
498
1058
|
function buildJsxIR(node, ctx) {
|
|
499
1059
|
const ir = buildNode(node, ctx);
|