@fluixi/compiler 1.0.0-alpha.93 → 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.
Files changed (90) hide show
  1. package/dist/analyze/intrinsics.cjs +13 -1
  2. package/dist/analyze/intrinsics.d.cts +14 -0
  3. package/dist/analyze/intrinsics.d.ts +14 -0
  4. package/dist/analyze/intrinsics.d.ts.map +1 -1
  5. package/dist/analyze/intrinsics.js +17 -0
  6. package/dist/analyze/intrinsics.mjs +13 -1
  7. package/dist/analyze/static-graph.cjs +1 -1
  8. package/dist/analyze/static-graph.mjs +1 -1
  9. package/dist/{babel-RJHJQVEM.mjs → babel-RJFQVFJW.mjs} +4 -4
  10. package/dist/{chunk-56QSOVVL.mjs → chunk-DP2QDWWH.mjs} +1 -1
  11. package/dist/{chunk-R7PPFBCL.mjs → chunk-E2RWTIYE.mjs} +21 -15
  12. package/dist/{chunk-OD53TMER.mjs → chunk-HCCBEJEZ.mjs} +13 -1
  13. package/dist/{chunk-TJIH7TLX.mjs → chunk-HIZU7WI2.mjs} +1 -1
  14. package/dist/{chunk-U7HCFLZ2.mjs → chunk-ZR2QHTEX.mjs} +356 -24
  15. package/dist/codegen/backends/imperative.cjs +1 -1
  16. package/dist/codegen/backends/imperative.d.ts.map +1 -1
  17. package/dist/codegen/backends/imperative.js +2 -1
  18. package/dist/codegen/backends/imperative.mjs +1 -1
  19. package/dist/frontend/babel/build-ir-lit.cjs +352 -22
  20. package/dist/frontend/babel/build-ir-lit.mjs +352 -22
  21. package/dist/frontend/babel/build-ir.cjs +352 -22
  22. package/dist/frontend/babel/build-ir.mjs +352 -22
  23. package/dist/frontend/babel/index.cjs +353 -23
  24. package/dist/frontend/babel/index.mjs +353 -23
  25. package/dist/frontend/babel/lower-template.cjs +352 -22
  26. package/dist/frontend/babel/lower-template.mjs +352 -22
  27. package/dist/frontend/babel/plugin.cjs +352 -22
  28. package/dist/frontend/babel/plugin.mjs +352 -22
  29. package/dist/index.cjs +321 -22
  30. package/dist/index.mjs +333 -22
  31. package/dist/integrations.cjs +1174 -49
  32. package/dist/integrations.d.ts.map +1 -1
  33. package/dist/integrations.js +44 -2
  34. package/dist/integrations.mjs +205 -19
  35. package/dist/lower/compile-template.cjs +315 -21
  36. package/dist/lower/compile-template.d.cts +13 -0
  37. package/dist/lower/compile-template.d.ts +13 -0
  38. package/dist/lower/compile-template.d.ts.map +1 -1
  39. package/dist/lower/compile-template.js +3 -0
  40. package/dist/lower/compile-template.mjs +327 -21
  41. package/dist/lower/entities.cjs +288 -0
  42. package/dist/lower/entities.d.cts +1 -0
  43. package/dist/lower/entities.d.ts +2 -0
  44. package/dist/lower/entities.d.ts.map +1 -0
  45. package/dist/lower/entities.js +54 -0
  46. package/dist/lower/entities.mjs +267 -0
  47. package/dist/lower/jsx.cjs +104 -47
  48. package/dist/lower/jsx.d.cts +8 -0
  49. package/dist/lower/jsx.d.ts +8 -0
  50. package/dist/lower/jsx.d.ts.map +1 -1
  51. package/dist/lower/jsx.js +92 -2
  52. package/dist/lower/jsx.mjs +104 -47
  53. package/dist/lower/template.cjs +293 -20
  54. package/dist/lower/template.d.ts.map +1 -1
  55. package/dist/lower/template.js +35 -19
  56. package/dist/lower/template.mjs +293 -20
  57. package/dist/resolve/builtins.cjs +1 -1
  58. package/dist/resolve/builtins.mjs +1 -1
  59. package/dist/resolve/component-globals.cjs +6 -1
  60. package/dist/resolve/component-globals.js +2 -2
  61. package/dist/resolve/component-globals.mjs +6 -1
  62. package/dist/resolve/write-globals.cjs +6 -1
  63. package/dist/resolve/write-globals.mjs +6 -1
  64. package/dist/{server-fns-ZFRDIQ2X.mjs → server-fns-JLSTKWPS.mjs} +4 -4
  65. package/dist/{server-functions-CTDXX67X.mjs → server-functions-BGY3RD4R.mjs} +3 -3
  66. package/dist/transform/index.cjs +956 -33
  67. package/dist/transform/index.mjs +974 -47
  68. package/dist/transform/props.mjs +19 -14
  69. package/dist/transform/server-fns.mjs +19 -14
  70. package/dist/transform/styles.cjs +588 -0
  71. package/dist/transform/styles.d.cts +211 -0
  72. package/dist/transform/styles.d.ts +212 -0
  73. package/dist/transform/styles.d.ts.map +1 -0
  74. package/dist/transform/styles.js +714 -0
  75. package/dist/transform/styles.mjs +653 -0
  76. package/dist/transform/templates.cjs +955 -32
  77. package/dist/transform/templates.d.cts +13 -0
  78. package/dist/transform/templates.d.ts +13 -0
  79. package/dist/transform/templates.d.ts.map +1 -1
  80. package/dist/transform/templates.js +49 -12
  81. package/dist/transform/templates.mjs +973 -46
  82. package/dist/{transform-QWWQDCXE.mjs → transform-GBP6Y4UI.mjs} +601 -17
  83. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  84. package/dist/vite-styles.cjs +180 -0
  85. package/dist/vite-styles.d.cts +59 -0
  86. package/dist/vite-styles.d.ts +60 -0
  87. package/dist/vite-styles.d.ts.map +1 -0
  88. package/dist/vite-styles.js +198 -0
  89. package/dist/vite-styles.mjs +159 -0
  90. package/package.json +4 -3
@@ -1,4 +1,4 @@
1
- /*! @fluixi/compiler v1.0.0-alpha.93 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
1
+ /*! @fluixi/compiler v1.0.0-alpha.94 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
2
  "use strict";
3
3
  var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
@@ -136,6 +136,11 @@ var init_builtins = __esm({
136
136
  });
137
137
 
138
138
  // src/analyze/intrinsics.ts
139
+ function tagIntrinsics(bound) {
140
+ const tags = /* @__PURE__ */ new Map();
141
+ for (const [name, tag] of Object.entries(TAG_INTRINSICS)) if (!bound.has(name)) tags.set(name, tag.export);
142
+ return tags;
143
+ }
139
144
  function mayHaveIntrinsic(code) {
140
145
  return INTRINSIC_CALL.test(code);
141
146
  }
@@ -145,7 +150,7 @@ function isIntrinsicName(name) {
145
150
  function isPrimitiveModule(source) {
146
151
  return PRIMITIVE_MODULES.includes(source);
147
152
  }
148
- var REACTIVE_MODULE, INTRINSICS, RUNTIME_PRIMITIVES, PRIMITIVE_MODULES, INTRINSIC_CALL;
153
+ var REACTIVE_MODULE, INTRINSICS, RUNTIME_PRIMITIVES, PRIMITIVE_MODULES, TAG_INTRINSICS, INTRINSIC_CALL;
149
154
  var init_intrinsics = __esm({
150
155
  "src/analyze/intrinsics.ts"() {
151
156
  "use strict";
@@ -208,6 +213,11 @@ var init_intrinsics = __esm({
208
213
  "@fluixi/reactive/cached-resource",
209
214
  "@fluixi/core"
210
215
  ];
216
+ TAG_INTRINSICS = {
217
+ $html: { export: "html", module: "@fluixi/core" },
218
+ $svg: { export: "svg", module: "@fluixi/core" },
219
+ $css: { export: "css", module: "@fluixi/core" }
220
+ };
211
221
  INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
212
222
  }
213
223
  });
@@ -1079,7 +1089,7 @@ function emitComponent(name, props, children, used, templates, at) {
1079
1089
  }
1080
1090
  let obj = `{ ${bodies.map(([name2, body]) => `get ${propKey(name2)}() { return ${body}; }`).join(", ")} }`;
1081
1091
  if (bodies.some(([, body]) => /\bthis\b/.test(body))) {
1082
- const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => ${body}`);
1092
+ const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => (${body})`);
1083
1093
  const getters = bodies.map(([name2]) => `get ${propKey(name2)}() { return _$t[${JSON.stringify(name2)}](); }`);
1084
1094
  obj = `((_$t) => ({ ${getters.join(", ")} }))({ ${thunks.join(", ")} })`;
1085
1095
  }
@@ -1244,6 +1254,277 @@ var init_text = __esm({
1244
1254
  }
1245
1255
  });
1246
1256
 
1257
+ // src/lower/entities.ts
1258
+ function decodeEntities(value) {
1259
+ if (!value.includes("&")) return value;
1260
+ return value.replace(REFERENCE, (match, hex, dec, name) => {
1261
+ const code = hex ? parseInt(hex, 16) : dec ? parseInt(dec, 10) : Object.prototype.hasOwnProperty.call(NAMED, name) ? NAMED[name] : void 0;
1262
+ return code !== void 0 && code <= 1114111 ? String.fromCodePoint(code) : match;
1263
+ });
1264
+ }
1265
+ var NAMED, REFERENCE;
1266
+ var init_entities = __esm({
1267
+ "src/lower/entities.ts"() {
1268
+ "use strict";
1269
+ NAMED = {
1270
+ quot: 34,
1271
+ amp: 38,
1272
+ apos: 39,
1273
+ lt: 60,
1274
+ gt: 62,
1275
+ nbsp: 160,
1276
+ iexcl: 161,
1277
+ cent: 162,
1278
+ pound: 163,
1279
+ curren: 164,
1280
+ yen: 165,
1281
+ brvbar: 166,
1282
+ sect: 167,
1283
+ uml: 168,
1284
+ copy: 169,
1285
+ ordf: 170,
1286
+ laquo: 171,
1287
+ not: 172,
1288
+ shy: 173,
1289
+ reg: 174,
1290
+ macr: 175,
1291
+ deg: 176,
1292
+ plusmn: 177,
1293
+ sup2: 178,
1294
+ sup3: 179,
1295
+ acute: 180,
1296
+ micro: 181,
1297
+ para: 182,
1298
+ middot: 183,
1299
+ cedil: 184,
1300
+ sup1: 185,
1301
+ ordm: 186,
1302
+ raquo: 187,
1303
+ frac14: 188,
1304
+ frac12: 189,
1305
+ frac34: 190,
1306
+ iquest: 191,
1307
+ Agrave: 192,
1308
+ Aacute: 193,
1309
+ Acirc: 194,
1310
+ Atilde: 195,
1311
+ Auml: 196,
1312
+ Aring: 197,
1313
+ AElig: 198,
1314
+ Ccedil: 199,
1315
+ Egrave: 200,
1316
+ Eacute: 201,
1317
+ Ecirc: 202,
1318
+ Euml: 203,
1319
+ Igrave: 204,
1320
+ Iacute: 205,
1321
+ Icirc: 206,
1322
+ Iuml: 207,
1323
+ ETH: 208,
1324
+ Ntilde: 209,
1325
+ Ograve: 210,
1326
+ Oacute: 211,
1327
+ Ocirc: 212,
1328
+ Otilde: 213,
1329
+ Ouml: 214,
1330
+ times: 215,
1331
+ Oslash: 216,
1332
+ Ugrave: 217,
1333
+ Uacute: 218,
1334
+ Ucirc: 219,
1335
+ Uuml: 220,
1336
+ Yacute: 221,
1337
+ THORN: 222,
1338
+ szlig: 223,
1339
+ agrave: 224,
1340
+ aacute: 225,
1341
+ acirc: 226,
1342
+ atilde: 227,
1343
+ auml: 228,
1344
+ aring: 229,
1345
+ aelig: 230,
1346
+ ccedil: 231,
1347
+ egrave: 232,
1348
+ eacute: 233,
1349
+ ecirc: 234,
1350
+ euml: 235,
1351
+ igrave: 236,
1352
+ iacute: 237,
1353
+ icirc: 238,
1354
+ iuml: 239,
1355
+ eth: 240,
1356
+ ntilde: 241,
1357
+ ograve: 242,
1358
+ oacute: 243,
1359
+ ocirc: 244,
1360
+ otilde: 245,
1361
+ ouml: 246,
1362
+ divide: 247,
1363
+ oslash: 248,
1364
+ ugrave: 249,
1365
+ uacute: 250,
1366
+ ucirc: 251,
1367
+ uuml: 252,
1368
+ yacute: 253,
1369
+ thorn: 254,
1370
+ yuml: 255,
1371
+ OElig: 338,
1372
+ oelig: 339,
1373
+ Scaron: 352,
1374
+ scaron: 353,
1375
+ Yuml: 376,
1376
+ fnof: 402,
1377
+ circ: 710,
1378
+ tilde: 732,
1379
+ Alpha: 913,
1380
+ Beta: 914,
1381
+ Gamma: 915,
1382
+ Delta: 916,
1383
+ Epsilon: 917,
1384
+ Zeta: 918,
1385
+ Eta: 919,
1386
+ Theta: 920,
1387
+ Iota: 921,
1388
+ Kappa: 922,
1389
+ Lambda: 923,
1390
+ Mu: 924,
1391
+ Nu: 925,
1392
+ Xi: 926,
1393
+ Omicron: 927,
1394
+ Pi: 928,
1395
+ Rho: 929,
1396
+ Sigma: 931,
1397
+ Tau: 932,
1398
+ Upsilon: 933,
1399
+ Phi: 934,
1400
+ Chi: 935,
1401
+ Psi: 936,
1402
+ Omega: 937,
1403
+ alpha: 945,
1404
+ beta: 946,
1405
+ gamma: 947,
1406
+ delta: 948,
1407
+ epsilon: 949,
1408
+ zeta: 950,
1409
+ eta: 951,
1410
+ theta: 952,
1411
+ iota: 953,
1412
+ kappa: 954,
1413
+ lambda: 955,
1414
+ mu: 956,
1415
+ nu: 957,
1416
+ xi: 958,
1417
+ omicron: 959,
1418
+ pi: 960,
1419
+ rho: 961,
1420
+ sigmaf: 962,
1421
+ sigma: 963,
1422
+ tau: 964,
1423
+ upsilon: 965,
1424
+ phi: 966,
1425
+ chi: 967,
1426
+ psi: 968,
1427
+ omega: 969,
1428
+ thetasym: 977,
1429
+ upsih: 978,
1430
+ piv: 982,
1431
+ ensp: 8194,
1432
+ emsp: 8195,
1433
+ thinsp: 8201,
1434
+ zwnj: 8204,
1435
+ zwj: 8205,
1436
+ lrm: 8206,
1437
+ rlm: 8207,
1438
+ ndash: 8211,
1439
+ mdash: 8212,
1440
+ lsquo: 8216,
1441
+ rsquo: 8217,
1442
+ sbquo: 8218,
1443
+ ldquo: 8220,
1444
+ rdquo: 8221,
1445
+ bdquo: 8222,
1446
+ dagger: 8224,
1447
+ Dagger: 8225,
1448
+ bull: 8226,
1449
+ hellip: 8230,
1450
+ permil: 8240,
1451
+ prime: 8242,
1452
+ Prime: 8243,
1453
+ lsaquo: 8249,
1454
+ rsaquo: 8250,
1455
+ oline: 8254,
1456
+ frasl: 8260,
1457
+ euro: 8364,
1458
+ image: 8465,
1459
+ weierp: 8472,
1460
+ real: 8476,
1461
+ trade: 8482,
1462
+ alefsym: 8501,
1463
+ larr: 8592,
1464
+ uarr: 8593,
1465
+ rarr: 8594,
1466
+ darr: 8595,
1467
+ harr: 8596,
1468
+ crarr: 8629,
1469
+ lArr: 8656,
1470
+ uArr: 8657,
1471
+ rArr: 8658,
1472
+ dArr: 8659,
1473
+ hArr: 8660,
1474
+ forall: 8704,
1475
+ part: 8706,
1476
+ exist: 8707,
1477
+ empty: 8709,
1478
+ nabla: 8711,
1479
+ isin: 8712,
1480
+ notin: 8713,
1481
+ ni: 8715,
1482
+ prod: 8719,
1483
+ sum: 8721,
1484
+ minus: 8722,
1485
+ lowast: 8727,
1486
+ radic: 8730,
1487
+ prop: 8733,
1488
+ infin: 8734,
1489
+ ang: 8736,
1490
+ and: 8743,
1491
+ or: 8744,
1492
+ cap: 8745,
1493
+ cup: 8746,
1494
+ int: 8747,
1495
+ there4: 8756,
1496
+ sim: 8764,
1497
+ cong: 8773,
1498
+ asymp: 8776,
1499
+ ne: 8800,
1500
+ equiv: 8801,
1501
+ le: 8804,
1502
+ ge: 8805,
1503
+ sub: 8834,
1504
+ sup: 8835,
1505
+ nsub: 8836,
1506
+ sube: 8838,
1507
+ supe: 8839,
1508
+ oplus: 8853,
1509
+ otimes: 8855,
1510
+ perp: 8869,
1511
+ sdot: 8901,
1512
+ lceil: 8968,
1513
+ rceil: 8969,
1514
+ lfloor: 8970,
1515
+ rfloor: 8971,
1516
+ lang: 9001,
1517
+ rang: 9002,
1518
+ loz: 9674,
1519
+ spades: 9824,
1520
+ clubs: 9827,
1521
+ hearts: 9829,
1522
+ diams: 9830
1523
+ };
1524
+ REFERENCE = /&(?:#[xX]([0-9a-fA-F]{1,6})|#([0-9]{1,7})|([a-zA-Z][a-zA-Z0-9]{1,31}));/g;
1525
+ }
1526
+ });
1527
+
1247
1528
  // src/lower/template.ts
1248
1529
  function escapeTemplate(s) {
1249
1530
  return s.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
@@ -1263,7 +1544,9 @@ var init_template = __esm({
1263
1544
  init_load_directive();
1264
1545
  init_imperative();
1265
1546
  init_text();
1547
+ init_entities();
1266
1548
  init_paren();
1549
+ init_source_locations();
1267
1550
  DELEGATED = /* @__PURE__ */ new Set([
1268
1551
  "click",
1269
1552
  "dblclick",
@@ -1343,7 +1626,7 @@ var init_template = __esm({
1343
1626
  switch (node.kind) {
1344
1627
  case "Text": {
1345
1628
  if (node.raw) return { kind: "text", value: node.value };
1346
- const cleaned = cleanTemplateText(node.value);
1629
+ const cleaned = decodeEntities(cleanTemplateText(node.value));
1347
1630
  return cleaned ? { kind: "text", value: cleaned } : null;
1348
1631
  }
1349
1632
  case "Comment":
@@ -1379,7 +1662,7 @@ var init_template = __esm({
1379
1662
  kind: "element",
1380
1663
  tag: el.tag,
1381
1664
  svg: el.namespace === "svg",
1382
- props: this.lowerAttributes(el.attributes),
1665
+ props: this.lowerAttributes(el.attributes, true),
1383
1666
  children,
1384
1667
  static: false,
1385
1668
  ...at ? { at } : {}
@@ -1492,13 +1775,22 @@ var init_template = __esm({
1492
1775
  return clone.kind === "Element" ? this.lowerElement(clone) : this.lowerComponent(clone);
1493
1776
  }
1494
1777
  // --- attributes ---------------------------------------------------------
1495
- lowerAttributes(attrs) {
1778
+ /**
1779
+ * `element`: a DOM element, where each class:/style: value that moves is its own accessor.
1780
+ * A component gets the object as before, rebuilt whole when any value moves.
1781
+ */
1782
+ lowerAttributes(attrs, element = false) {
1783
+ let anyReactive = false;
1784
+ const entry = (name, h) => {
1785
+ anyReactive ||= h.reactive;
1786
+ if (!element || !h.reactive) return `${JSON.stringify(name)}: ${h.code}`;
1787
+ const accessor = `() => ${paren(h.code, "arrowBody")}`;
1788
+ return `${JSON.stringify(name)}: ${h.at ? markHole(accessor, h.at.line, h.at.column) : accessor}`;
1789
+ };
1496
1790
  const props = [];
1497
1791
  const classEntries = [];
1498
- let classReactive = false;
1499
1792
  let classAt;
1500
1793
  const styleEntries = [];
1501
- let styleReactive = false;
1502
1794
  let styleAt;
1503
1795
  const usePairs = [];
1504
1796
  for (const a of attrs) {
@@ -1538,15 +1830,13 @@ var init_template = __esm({
1538
1830
  break;
1539
1831
  case "ClassDirective": {
1540
1832
  const h = this.hole(a.hole);
1541
- classEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
1542
- classReactive = classReactive || h.reactive;
1833
+ classEntries.push(entry(a.name, h));
1543
1834
  classAt ??= h.at;
1544
1835
  break;
1545
1836
  }
1546
1837
  case "StyleDirective": {
1547
1838
  const h = this.hole(a.hole);
1548
- styleEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
1549
- styleReactive = styleReactive || h.reactive;
1839
+ styleEntries.push(entry(a.name, h));
1550
1840
  styleAt ??= h.at;
1551
1841
  break;
1552
1842
  }
@@ -1566,18 +1856,19 @@ var init_template = __esm({
1566
1856
  name: "classList",
1567
1857
  kind: "attr",
1568
1858
  expr: `{ ${classEntries.join(", ")} }`,
1569
- reactive: classReactive,
1859
+ reactive: !element && anyReactive,
1570
1860
  ...classAt ? { at: classAt } : {}
1571
1861
  });
1572
1862
  }
1573
1863
  if (styleEntries.length > 0) {
1574
- props.push({
1575
- name: "style",
1576
- kind: "attr",
1577
- expr: `{ ${styleEntries.join(", ")} }`,
1578
- reactive: styleReactive,
1579
- ...styleAt ? { at: styleAt } : {}
1580
- });
1864
+ const entries = `{ ${styleEntries.join(", ")} }`;
1865
+ const base = props.findIndex((p) => p.name === "style");
1866
+ if (base === -1) props.push({ name: "style", kind: "attr", expr: entries, reactive: !element && anyReactive, ...styleAt ? { at: styleAt } : {} });
1867
+ else {
1868
+ const prop = props[base];
1869
+ const value = prop.expr === void 0 ? JSON.stringify(prop.literal) : prop.reactive ? `() => ${paren(prop.expr, "arrowBody")}` : prop.expr;
1870
+ props[base] = { name: "style", kind: "attr", expr: `[${value}, ${entries}]`, reactive: false, ...prop.at ? { at: prop.at } : {} };
1871
+ }
1581
1872
  }
1582
1873
  if (usePairs.length > 0) {
1583
1874
  props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
@@ -1666,7 +1957,7 @@ var init_template = __esm({
1666
1957
  return base;
1667
1958
  }
1668
1959
  if (v.kind === "static") {
1669
- base.literal = v.value;
1960
+ base.literal = decodeEntities(v.value);
1670
1961
  return base;
1671
1962
  }
1672
1963
  if (v.kind === "hole") {
@@ -1679,7 +1970,7 @@ var init_template = __esm({
1679
1970
  let reactive = false;
1680
1971
  let at;
1681
1972
  const body = v.parts.map((p) => {
1682
- if ("text" in p) return escapeTemplate(p.text);
1973
+ if ("text" in p) return escapeTemplate(decodeEntities(p.text));
1683
1974
  const h = this.hole(p.hole);
1684
1975
  reactive = reactive || h.reactive;
1685
1976
  at ??= h.at;
@@ -1990,9 +2281,12 @@ function attrExprCode(attr, ctx) {
1990
2281
  }
1991
2282
  return null;
1992
2283
  }
1993
- function buildProps(attrs, ctx) {
2284
+ function buildProps(attrs, ctx, element = false) {
1994
2285
  const props = [];
1995
2286
  const usePairs = [];
2287
+ const classEntries = [];
2288
+ const styleEntries = [];
2289
+ let styleAt;
1996
2290
  for (const a of attrs) {
1997
2291
  if (a.type === "JSXSpreadAttribute") {
1998
2292
  props.push({ name: "", kind: "spread", expr: ctx.code(a.argument) });
@@ -2008,6 +2302,27 @@ function buildProps(attrs, ctx) {
2008
2302
  usePairs.push(opts != null ? `[${dir}, () => ${paren(opts, "arrowBody")}]` : `[${dir}]`);
2009
2303
  continue;
2010
2304
  }
2305
+ if (element && (ns === "class" || ns === "style")) {
2306
+ const key = JSON.stringify(a.name.name.name);
2307
+ const v = a.value;
2308
+ let value;
2309
+ if (v == null) value = "true";
2310
+ else if (isString(v)) value = JSON.stringify(literalOf(v));
2311
+ else if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
2312
+ value = keyValue(v.expression, ctx);
2313
+ if (ns === "style") styleAt ??= posOf(v.expression, ctx);
2314
+ } else continue;
2315
+ (ns === "class" ? classEntries : styleEntries).push(`${key}: ${value}`);
2316
+ continue;
2317
+ }
2318
+ if (element && a.name.type === "JSXIdentifier" && a.name.name === "style" && a.value?.type === "JSXExpressionContainer") {
2319
+ const entries = objectEntries(a.value.expression, ctx);
2320
+ if (entries) {
2321
+ styleEntries.unshift(...entries);
2322
+ styleAt = posOf(a.value.expression, ctx);
2323
+ continue;
2324
+ }
2325
+ }
2011
2326
  if (ns === "oncapture") {
2012
2327
  const evt = a.name.name.name.toLowerCase();
2013
2328
  const h = attrExprCode(a, ctx) ?? "undefined";
@@ -2016,9 +2331,37 @@ function buildProps(attrs, ctx) {
2016
2331
  }
2017
2332
  props.push(buildAttr(a, ctx));
2018
2333
  }
2334
+ if (classEntries.length > 0) props.push({ name: "classList", kind: "attr", expr: `{ ${classEntries.join(", ")} }`, reactive: false });
2335
+ if (styleEntries.length > 0) {
2336
+ const entries = `{ ${styleEntries.join(", ")} }`;
2337
+ const base = props.findIndex((p) => p.name === "style" && p.kind === "attr");
2338
+ if (base === -1) props.push({ name: "style", kind: "attr", expr: entries, reactive: false, ...styleAt ? { at: styleAt } : {} });
2339
+ else {
2340
+ const prop = props[base];
2341
+ const value = prop.literal !== void 0 ? JSON.stringify(prop.literal) : prop.reactive ? `() => ${paren(prop.expr, "arrowBody")}` : prop.expr;
2342
+ props[base] = { name: "style", kind: "attr", expr: `[${value}, ${entries}]`, reactive: false, ...prop.at ? { at: prop.at } : {} };
2343
+ }
2344
+ }
2019
2345
  if (usePairs.length > 0) props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
2020
2346
  return props;
2021
2347
  }
2348
+ function keyValue(expr, ctx) {
2349
+ const code = ctx.code(expr);
2350
+ if (!isReactive(expr)) return code;
2351
+ const accessor = `() => ${paren(code, "arrowBody")}`;
2352
+ const at = posOf(expr, ctx);
2353
+ return at ? markHole(accessor, at.line, at.column) : accessor;
2354
+ }
2355
+ function objectEntries(expr, ctx) {
2356
+ if (expr?.type !== "ObjectExpression") return null;
2357
+ const entries = [];
2358
+ for (const p of expr.properties) {
2359
+ if (p.type !== "ObjectProperty") return null;
2360
+ const key = p.computed ? `[${ctx.code(p.key)}]` : p.key.type === "Identifier" ? JSON.stringify(p.key.name) : JSON.stringify(String(p.key.value));
2361
+ entries.push(`${key}: ${keyValue(p.value, ctx)}`);
2362
+ }
2363
+ return entries;
2364
+ }
2022
2365
  function buildChildren(children, ctx) {
2023
2366
  const out = [];
2024
2367
  for (let i = 0; i < children.length; i++) {
@@ -2046,6 +2389,7 @@ function buildChildren(children, ctx) {
2046
2389
  });
2047
2390
  }
2048
2391
  } else if (child.type === "JSXElement" || child.type === "JSXFragment") {
2392
+ if (ctx.dropStyles?.has(child.start)) continue;
2049
2393
  out.push(buildNode(child, ctx));
2050
2394
  } else if (child.type === "JSXSpreadChild") {
2051
2395
  out.push({ kind: "expr", code: ctx.code(child.expression), reactive: false });
@@ -2115,13 +2459,17 @@ function buildElement(node, ctx, handled) {
2115
2459
  const children = each ? [each] : buildChildren(node.children, ctx);
2116
2460
  if (tag === CONTAINER2) return children.length === 1 ? children[0] : { kind: "fragment", children };
2117
2461
  const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
2118
- return { kind: "element", tag, svg: SVG_TAGS.has(tag), props: buildProps(attributes, ctx), children, static: false, ...at ? { at } : {} };
2462
+ const props = buildProps(attributes, ctx, true);
2463
+ const scope = ctx.scopes?.find((s) => node.start >= s.start && node.end <= s.end);
2464
+ if (scope) props.unshift({ name: "data-fx-s", kind: "attr", literal: scope.value });
2465
+ return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
2119
2466
  }
2120
2467
  function buildNode(node, ctx) {
2121
2468
  if (node.type === "JSXFragment") {
2122
2469
  return { kind: "fragment", children: buildChildren(node.children, ctx) };
2123
2470
  }
2124
2471
  if (directive(node, "if")) return buildIf(node, null, ctx);
2472
+ if (ctx.dropStyles?.has(node.start)) return { kind: "fragment", children: [] };
2125
2473
  const { tag, component } = tagInfo(node.openingElement.name, ctx);
2126
2474
  if (!component) return buildElement(node, ctx, []);
2127
2475
  const props = buildProps(node.openingElement.attributes, ctx);
@@ -2145,6 +2493,7 @@ var init_jsx = __esm({
2145
2493
  init_analyze_static();
2146
2494
  init_text();
2147
2495
  init_paren();
2496
+ init_source_locations();
2148
2497
  init_imperative();
2149
2498
  SVG_TAGS = /* @__PURE__ */ new Set([
2150
2499
  "svg",
@@ -2793,6 +3142,569 @@ var init_babel = __esm({
2793
3142
  }
2794
3143
  });
2795
3144
 
3145
+ // src/transform/styles.ts
3146
+ function collectStyles(program, sourceName, options, tags = { html: /* @__PURE__ */ new Map([["html", "html"]]) }, module2) {
3147
+ const plan = {
3148
+ scopes: [],
3149
+ templateOwners: /* @__PURE__ */ new Map(),
3150
+ drop: /* @__PURE__ */ new Set(),
3151
+ styles: [],
3152
+ diagnostics: [],
3153
+ edits: [],
3154
+ literals: [],
3155
+ references: [],
3156
+ shadowClasses: /* @__PURE__ */ new Map()
3157
+ };
3158
+ if (!options) return plan;
3159
+ const literalsOf = findLiterals(program, options.cssModules ?? ["@fluixi/core", "@fluixi/dom"], plan, tags.css ?? []);
3160
+ plan.names = literalsOf;
3161
+ const classOf = /* @__PURE__ */ new Map();
3162
+ const shadowOf = (owner) => {
3163
+ const cls = classOf.get(owner);
3164
+ return cls ? plan.shadowClasses.get(cls) : void 0;
3165
+ };
3166
+ const resolved = /* @__PURE__ */ new Map();
3167
+ const piecesToNodes = (quasis, expressions, at, stack) => {
3168
+ const out = [];
3169
+ for (let i = 0; i < quasis.length; i++) {
3170
+ const text = quasis[i].value.cooked ?? quasis[i].value.raw;
3171
+ if (text.trim()) {
3172
+ try {
3173
+ const start = quasis[i].loc?.start;
3174
+ out.push(...options.process(text, sourceName).map((n) => start ? shiftLoc(n, sourceName, start) : n));
3175
+ } catch (error) {
3176
+ plan.diagnostics.push({ ...at, message: `CSS could not be processed: ${error.message}` });
3177
+ return null;
3178
+ }
3179
+ }
3180
+ const expr = expressions[i];
3181
+ if (!expr) continue;
3182
+ const value = expr.type === "Identifier" ? resolveLiteral(expr.name, at, stack) : void 0;
3183
+ if (value === void 0) {
3184
+ plan.diagnostics.push({
3185
+ ...at,
3186
+ message: "Only css`` values can be interpolated into styles, and only by name: ${base}. Dynamic values go through custom properties (style:--x={...} and var(--x))."
3187
+ });
3188
+ return null;
3189
+ }
3190
+ if (value === null) return null;
3191
+ out.push(...value);
3192
+ }
3193
+ return out;
3194
+ };
3195
+ function resolveLiteral(name, at, stack) {
3196
+ const imported = literalsOf.imports.get(name);
3197
+ if (imported) {
3198
+ const nodes = options?.imported?.get(`${imported.source}#${imported.name}`);
3199
+ if (nodes) return nodes.map(dropLoc);
3200
+ plan.references.push({ name: imported.name, source: imported.source, ...at });
3201
+ plan.diagnostics.push({
3202
+ ...at,
3203
+ message: options?.imported ? `${name} from ${imported.source} is not a css\`\` value the build could find.` : `${name} comes from ${imported.source}: style values from another module are resolved by the build.`
3204
+ });
3205
+ return null;
3206
+ }
3207
+ const literal = literalsOf.literals.get(name);
3208
+ if (!literal) return void 0;
3209
+ if (stack.includes(name)) {
3210
+ plan.diagnostics.push({ ...at, message: `css\`\` values include each other: ${[...stack, name].join(" → ")}.` });
3211
+ return null;
3212
+ }
3213
+ if (!resolved.has(name)) {
3214
+ resolved.set(name, piecesToNodes(literal.quasi.quasis, literal.quasi.expressions, at, [...stack, name]));
3215
+ }
3216
+ return resolved.get(name);
3217
+ }
3218
+ for (const [name, literal] of literalsOf.literals) {
3219
+ const at = { line: literal.loc?.start.line ?? 0, column: literal.loc?.start.column ?? 0 };
3220
+ const nodes = resolveLiteral(name, at, []);
3221
+ const id = hash(`${sourceName}:css:${name}`);
3222
+ plan.edits.push({ start: literal.start, end: literal.end, code: `({ $fxStyle: ${JSON.stringify(id)} })` });
3223
+ if (nodes) plan.literals.push({ id, name, css: printStyle(nodes, options.minify) });
3224
+ }
3225
+ const scopeOf = /* @__PURE__ */ new Map();
3226
+ const names = /* @__PURE__ */ new Map();
3227
+ const named = /* @__PURE__ */ new Map();
3228
+ const visit = (node, owners) => {
3229
+ if (!node || typeof node !== "object") return;
3230
+ if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.init && FUNCTIONS.has(node.init.type)) {
3231
+ named.set(node.init, node.id.name);
3232
+ }
3233
+ if ((node.type === "ClassDeclaration" || node.type === "ClassExpression") && node.id) {
3234
+ for (const member of node.body.body) if (member.type === "ClassMethod") named.set(member, node.id.name);
3235
+ }
3236
+ if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
3237
+ for (const member of node.body.body) if (member.type === "ClassMethod") classOf.set(member, node);
3238
+ const shadow = staticMember(node, "shadow");
3239
+ if (shadow?.value && !(shadow.value.type === "BooleanLiteral" && shadow.value.value === false)) {
3240
+ plan.shadowClasses.set(node, { node, styles: staticMember(node, "styles"), indexes: [] });
3241
+ }
3242
+ }
3243
+ if (node.type === "TaggedTemplateExpression" && node.tag.type === "Identifier" && tags.html.get(node.tag.name) === "html") {
3244
+ const text = node.quasi.quasis.map((q) => q.value.raw).join("");
3245
+ if (/<style[\s>]/.test(text)) {
3246
+ const at = { line: node.loc?.start.line ?? 0, column: node.loc?.start.column ?? 0 };
3247
+ const owner = owners[0];
3248
+ if (!owner) plan.diagnostics.push({ ...at, message: "<style> has to be written inside a component." });
3249
+ else {
3250
+ const id = idFor(owner);
3251
+ const shadow = shadowOf(owner);
3252
+ plan.templateOwners.set(node.start, { id, component: functionName(owner, named) });
3253
+ for (const block of templateBlocks(node.quasi)) compileBlock(block, block.scoped, owner, at);
3254
+ }
3255
+ }
3256
+ }
3257
+ const inner = FUNCTIONS.has(node.type) ? [...owners, node] : owners;
3258
+ if (node.type === "JSXElement" && isStyle(node)) {
3259
+ handle(node, owners);
3260
+ return;
3261
+ }
3262
+ for (const key of Object.keys(node)) {
3263
+ if (key === "loc" || key === "leadingComments" || key === "trailingComments" || key === "innerComments") continue;
3264
+ const value = node[key];
3265
+ if (Array.isArray(value)) for (const v of value) visit(v, inner);
3266
+ else if (value && typeof value === "object" && "type" in value) visit(value, inner);
3267
+ }
3268
+ };
3269
+ const handle = (node, owners) => {
3270
+ const at = { line: node.loc?.start.line ?? 0, column: node.loc?.start.column ?? 0 };
3271
+ const pieces = blockPieces(node);
3272
+ if (pieces === null) {
3273
+ plan.diagnostics.push({
3274
+ ...at,
3275
+ message: "<style> needs its CSS as one template literal, <style>{`...`}</style>: JSX reads a bare `{` as an expression."
3276
+ });
3277
+ return;
3278
+ }
3279
+ const owner = owners[0];
3280
+ if (!owner) {
3281
+ plan.diagnostics.push({ ...at, message: "<style> has to be written inside a component." });
3282
+ return;
3283
+ }
3284
+ compileBlock(pieces, isScoped(node), owner, at);
3285
+ plan.drop.add(node.start);
3286
+ };
3287
+ const compileBlock = (pieces, scoped, owner, at) => {
3288
+ const component = functionName(owner, named);
3289
+ const id = idFor(owner);
3290
+ const processed = piecesToNodes(pieces.quasis, pieces.expressions, at, []);
3291
+ if (!processed) return;
3292
+ let nodes = processed;
3293
+ const shadow = shadowOf(owner);
3294
+ if (scoped && !shadow) {
3295
+ if (!plan.scopes.some((s) => s.value === id)) plan.scopes.push({ start: owner.start, end: owner.end, value: id });
3296
+ nodes = scopeNodes(nodes, id, (message) => plan.diagnostics.push({ ...at, message }));
3297
+ }
3298
+ const printed = module2 ? printStyleWithMap(nodes, options.minify, sourceName, module2) : { css: printStyle(nodes, options.minify) };
3299
+ plan.styles.push({ id, scoped: scoped && !shadow, component, ...printed, ...shadow ? { shadow: true } : {}, ...at });
3300
+ if (shadow) shadow.indexes.push(plan.styles.length - 1);
3301
+ };
3302
+ function idFor(owner) {
3303
+ let id = scopeOf.get(owner);
3304
+ if (!id) {
3305
+ const component = functionName(owner, named);
3306
+ const seen = names.get(component) ?? 0;
3307
+ names.set(component, seen + 1);
3308
+ id = hash(`${sourceName}:${component}${seen ? ":" + seen : ""}`);
3309
+ scopeOf.set(owner, id);
3310
+ }
3311
+ return id;
3312
+ }
3313
+ visit(program, []);
3314
+ return plan;
3315
+ }
3316
+ function applyTemplateStyles(ir, t4) {
3317
+ const walk8 = (node) => {
3318
+ const children = node.children;
3319
+ if (children) {
3320
+ for (let i = children.length - 1; i >= 0; i--) {
3321
+ const child = children[i];
3322
+ if (child.kind === "element" && child.tag === "style" && t4.owner) children.splice(i, 1);
3323
+ else walk8(child);
3324
+ }
3325
+ }
3326
+ if (t4.scope && node.kind === "element" && node.tag !== "style" && node.props && !node.props.some((p) => p.name === SCOPE_ATTR)) {
3327
+ node.props.unshift({ name: SCOPE_ATTR, kind: "attr", literal: t4.scope });
3328
+ }
3329
+ };
3330
+ walk8(ir);
3331
+ }
3332
+ function staticMember(cls, name) {
3333
+ return cls.body.body.find(
3334
+ (m) => m.type === "ClassProperty" && m.static && (m.key.type === "Identifier" && m.key.name === name || m.key.type === "StringLiteral" && m.key.value === name)
3335
+ );
3336
+ }
3337
+ function finishShadowStyles(plan, code, moduleId) {
3338
+ const edits = [];
3339
+ const imports = [];
3340
+ let next = 0;
3341
+ const importOf = (specifier) => {
3342
+ const local = `__fxShadow${next++}`;
3343
+ imports.push(`import ${local} from ${JSON.stringify(specifier)};
3344
+ `);
3345
+ return local;
3346
+ };
3347
+ for (const shadow of plan.shadowClasses.values()) {
3348
+ const blocks = shadow.indexes.map((i) => importOf(`${moduleId}?fx-style&index=${i}&inline&lang.css`));
3349
+ let value;
3350
+ const prop = shadow.styles;
3351
+ if (prop?.value) {
3352
+ const start = prop.value.start;
3353
+ let text = code.slice(start, prop.value.end);
3354
+ const replacements = [];
3355
+ walkIdentifiers(prop.value, (id) => {
3356
+ const local = plan.names?.literals.has(id.name) ? importOf(`${moduleId}?fx-literal=${id.name}&inline&lang.css`) : plan.names?.imports.has(id.name) && !/\.css(\?|$)/.test(plan.names.imports.get(id.name).source) ? importOf(`${plan.names.imports.get(id.name).source}?fx-literal=${plan.names.imports.get(id.name).name}&inline&lang.css`) : void 0;
3357
+ if (local) replacements.push({ at: id.start - start, length: id.name.length, with: local });
3358
+ });
3359
+ for (const r of replacements.sort((a, b) => b.at - a.at)) text = text.slice(0, r.at) + r.with + text.slice(r.at + r.length);
3360
+ value = text;
3361
+ }
3362
+ if (!blocks.length && value === void 0) continue;
3363
+ if (prop?.value) {
3364
+ const merged = blocks.length ? `[${value}, ${blocks.join(", ")}]` : value;
3365
+ edits.push({ start: prop.value.start, end: prop.value.end, code: merged });
3366
+ } else if (blocks.length) {
3367
+ const open = shadow.node.body.start + 1;
3368
+ edits.push({ start: open, end: open, code: ` static styles = [${blocks.join(", ")}];` });
3369
+ }
3370
+ }
3371
+ if (imports.length) edits.push({ start: 0, end: 0, code: imports.join("") });
3372
+ return edits;
3373
+ }
3374
+ function walkIdentifiers(node, visit) {
3375
+ if (!node || typeof node !== "object") return;
3376
+ if (node.type === "Identifier") return visit(node);
3377
+ if (node.type === "MemberExpression") return walkIdentifiers(node.object, visit);
3378
+ if (node.type === "ObjectProperty") return walkIdentifiers(node.value, visit);
3379
+ for (const key of Object.keys(node)) {
3380
+ if (key === "loc") continue;
3381
+ const value = node[key];
3382
+ if (Array.isArray(value)) for (const v of value) walkIdentifiers(v, visit);
3383
+ else if (value && typeof value === "object" && "type" in value) walkIdentifiers(value, visit);
3384
+ }
3385
+ }
3386
+ function isStyle(node) {
3387
+ const name = node.openingElement.name;
3388
+ return name.type === "JSXIdentifier" && name.name === "style";
3389
+ }
3390
+ function isScoped(node) {
3391
+ return node.openingElement.attributes.some((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "scoped");
3392
+ }
3393
+ function templateBlocks(quasi) {
3394
+ const blocks = [];
3395
+ let open = null;
3396
+ quasi.quasis.forEach((q, i) => {
3397
+ const raw = q.value.raw;
3398
+ let from = 0;
3399
+ while (from <= raw.length) {
3400
+ if (!open) {
3401
+ const tag = /<style\b([^>]*)>/g;
3402
+ tag.lastIndex = from;
3403
+ const m = tag.exec(raw);
3404
+ if (!m) break;
3405
+ open = { quasis: [], expressions: [], scoped: /\bscoped\b/.test(m[1]) };
3406
+ from = m.index + m[0].length;
3407
+ }
3408
+ const close = raw.indexOf("</style", from);
3409
+ open.quasis.push(slicePiece(q, raw, from, close === -1 ? raw.length : close));
3410
+ if (close === -1) break;
3411
+ blocks.push(open);
3412
+ open = null;
3413
+ from = close;
3414
+ }
3415
+ const expr = quasi.expressions[i];
3416
+ if (open && expr) open.expressions.push(expr);
3417
+ });
3418
+ return blocks;
3419
+ }
3420
+ function slicePiece(q, raw, from, to) {
3421
+ const text = raw.slice(from, to);
3422
+ const start = q.loc?.start;
3423
+ if (!start) return { type: "TemplateElement", value: { raw: text, cooked: text } };
3424
+ const before = raw.slice(0, from).split("\n");
3425
+ const line = start.line + before.length - 1;
3426
+ const column = before.length > 1 ? before[before.length - 1].length : start.column + from;
3427
+ return { type: "TemplateElement", value: { raw: text, cooked: text }, loc: { start: { line, column } } };
3428
+ }
3429
+ function blockPieces(node) {
3430
+ const meaningful = node.children.filter((c) => !(c.type === "JSXText" && !c.value.trim()));
3431
+ if (meaningful.length === 0) return { quasis: [], expressions: [] };
3432
+ if (meaningful.length !== 1) return null;
3433
+ const only = meaningful[0];
3434
+ if (only.type !== "JSXExpressionContainer") return null;
3435
+ const expr = only.expression;
3436
+ if (expr.type === "StringLiteral") return { quasis: [{ type: "TemplateElement", value: { raw: expr.value, cooked: expr.value } }], expressions: [] };
3437
+ if (expr.type === "TemplateLiteral") return { quasis: expr.quasis, expressions: expr.expressions };
3438
+ return null;
3439
+ }
3440
+ function findLiterals(program, modules, plan, intrinsic) {
3441
+ const literals = /* @__PURE__ */ new Map();
3442
+ const imports = /* @__PURE__ */ new Map();
3443
+ const tags = new Set(intrinsic);
3444
+ for (const statement of program.body ?? []) {
3445
+ if (statement.type !== "ImportDeclaration") continue;
3446
+ const source = statement.source.value;
3447
+ const specifiers = statement.specifiers;
3448
+ for (const sp of specifiers) {
3449
+ if (sp.type === "ImportSpecifier") imports.set(sp.local.name, { source, name: sp.imported.name ?? sp.imported.value });
3450
+ }
3451
+ if (!modules.includes(source)) continue;
3452
+ const css = specifiers.find((sp) => sp.type === "ImportSpecifier" && (sp.imported.name ?? sp.imported.value) === "css");
3453
+ if (!css) continue;
3454
+ const local = css.local.name;
3455
+ tags.add(local);
3456
+ imports.delete(local);
3457
+ plan.edits.push({ start: statement.start, end: statement.end, code: importWithout(statement, css) });
3458
+ }
3459
+ if (tags.size === 0) return { literals, imports };
3460
+ const visit = (node) => {
3461
+ if (!node || typeof node !== "object") return;
3462
+ if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.init?.type === "TaggedTemplateExpression" && node.init.tag.type === "Identifier" && tags.has(node.init.tag.name)) {
3463
+ literals.set(node.id.name, node.init);
3464
+ }
3465
+ for (const key of Object.keys(node)) {
3466
+ if (key === "loc") continue;
3467
+ const value = node[key];
3468
+ if (Array.isArray(value)) for (const v of value) visit(v);
3469
+ else if (value && typeof value === "object" && "type" in value) visit(value);
3470
+ }
3471
+ };
3472
+ visit(program);
3473
+ return { literals, imports };
3474
+ }
3475
+ function importWithout(statement, removed) {
3476
+ const rest = statement.specifiers.filter((sp) => sp !== removed);
3477
+ const source = JSON.stringify(statement.source.value);
3478
+ if (rest.length === 0) return "";
3479
+ const def = rest.find((sp) => sp.type === "ImportDefaultSpecifier");
3480
+ const ns = rest.find((sp) => sp.type === "ImportNamespaceSpecifier");
3481
+ const named = rest.filter((sp) => sp.type === "ImportSpecifier").map((sp) => {
3482
+ const imported = sp.imported.name ?? JSON.stringify(sp.imported.value);
3483
+ return imported === sp.local.name ? imported : `${imported} as ${sp.local.name}`;
3484
+ });
3485
+ const parts = [def?.local.name, ns ? `* as ${ns.local.name}` : void 0, named.length ? `{ ${named.join(", ")} }` : void 0].filter(Boolean);
3486
+ return `import ${parts.join(", ")} from ${source};`;
3487
+ }
3488
+ function functionName(fn, named) {
3489
+ if ((fn.type === "FunctionDeclaration" || fn.type === "FunctionExpression") && fn.id) return fn.id.name;
3490
+ return named.get(fn) ?? "anonymous";
3491
+ }
3492
+ function hash(text) {
3493
+ let h = 2166136261;
3494
+ for (let i = 0; i < text.length; i++) {
3495
+ h ^= text.charCodeAt(i);
3496
+ h = Math.imul(h, 16777619);
3497
+ }
3498
+ return (h >>> 0).toString(36);
3499
+ }
3500
+ function scopeNodes(nodes, id, warn = () => {
3501
+ }) {
3502
+ return nodes.map((node) => {
3503
+ if (node.kind === "rule") return { ...node, selector: scopeSelector(node.selector, id, warn) };
3504
+ if (node.kind === "at-rule" && !OPAQUE.test(node.name)) return { ...node, nodes: scopeNodes(node.nodes, id, warn) };
3505
+ return node;
3506
+ });
3507
+ }
3508
+ function scopeSelector(selector, id, warn = () => {
3509
+ }) {
3510
+ return splitTop(selector, ",").map((complex) => scopeComplex(complex.trim(), `[${SCOPE_ATTR}="${id}"]`, warn)).join(", ");
3511
+ }
3512
+ function scopeComplex(selector, attr, warn) {
3513
+ if (/:host\b|::slotted\(/.test(selector)) {
3514
+ warn(`\`${selector}\` only means something in a shadow root; in a light-DOM component it is left as written.`);
3515
+ return selector;
3516
+ }
3517
+ const parts = compounds(selector);
3518
+ const deep = parts.findIndex((p) => p.text.startsWith(":deep("));
3519
+ if (deep !== -1) {
3520
+ const inner = unwrap(parts[deep].text, ":deep(");
3521
+ const before = parts.slice(0, deep);
3522
+ if (before.length === 0) return `${attr} ${inner}`;
3523
+ const scopedBefore = scopeAt(before, before.length - 1, attr);
3524
+ return `${join2(scopedBefore)} ${inner}`;
3525
+ }
3526
+ let target = -1;
3527
+ for (let i = parts.length - 1; i >= 0; i--) {
3528
+ if (!parts[i].text.startsWith(":global(")) {
3529
+ target = i;
3530
+ break;
3531
+ }
3532
+ }
3533
+ const out = target === -1 ? parts : scopeAt(parts, target, attr);
3534
+ return join2(out.map((p) => p.text.startsWith(":global(") ? { ...p, text: unwrap(p.text, ":global(") } : p));
3535
+ }
3536
+ function scopeAt(parts, index, attr) {
3537
+ return parts.map((p, i) => i === index ? { ...p, text: insertAttr(p.text, attr) } : p);
3538
+ }
3539
+ function insertAttr(compound, attr) {
3540
+ let depth = 0;
3541
+ let quote = "";
3542
+ for (let i = 0; i < compound.length; i++) {
3543
+ const c = compound[i];
3544
+ if (quote) {
3545
+ if (c === "\\") i++;
3546
+ else if (c === quote) quote = "";
3547
+ continue;
3548
+ }
3549
+ if (c === "\\") {
3550
+ i++;
3551
+ continue;
3552
+ }
3553
+ if (c === '"' || c === "'") quote = c;
3554
+ else if (c === "(" || c === "[") depth++;
3555
+ else if (c === ")" || c === "]") depth--;
3556
+ else if (c === ":" && depth === 0 && i > 0) return compound.slice(0, i) + attr + compound.slice(i);
3557
+ }
3558
+ return compound + attr;
3559
+ }
3560
+ function compounds(selector) {
3561
+ const out = [];
3562
+ let depth = 0;
3563
+ let quote = "";
3564
+ let current = "";
3565
+ let combinator = "";
3566
+ const push = () => {
3567
+ if (current.trim()) out.push({ combinator: out.length ? combinator || " " : "", text: current.trim() });
3568
+ current = "";
3569
+ combinator = "";
3570
+ };
3571
+ for (let i = 0; i < selector.length; i++) {
3572
+ const c = selector[i];
3573
+ if (quote) {
3574
+ current += c;
3575
+ if (c === "\\") current += selector[++i] ?? "";
3576
+ else if (c === quote) quote = "";
3577
+ continue;
3578
+ }
3579
+ if (c === "\\") {
3580
+ current += c + (selector[++i] ?? "");
3581
+ continue;
3582
+ }
3583
+ if (c === '"' || c === "'") quote = c;
3584
+ if (c === "(" || c === "[") depth++;
3585
+ if (c === ")" || c === "]") depth--;
3586
+ if (depth === 0 && (c === ">" || c === "+" || c === "~")) {
3587
+ push();
3588
+ combinator = c;
3589
+ continue;
3590
+ }
3591
+ if (depth === 0 && /\s/.test(c)) {
3592
+ if (current.trim()) {
3593
+ const keep = combinator;
3594
+ push();
3595
+ combinator = keep;
3596
+ }
3597
+ continue;
3598
+ }
3599
+ current += c;
3600
+ }
3601
+ push();
3602
+ return out;
3603
+ }
3604
+ function join2(parts) {
3605
+ return parts.map((p, i) => i === 0 ? p.text : p.combinator === " " ? ` ${p.text}` : ` ${p.combinator} ${p.text}`).join("");
3606
+ }
3607
+ function unwrap(text, prefix) {
3608
+ return text.slice(prefix.length, text.lastIndexOf(")")).trim() + text.slice(text.lastIndexOf(")") + 1);
3609
+ }
3610
+ function splitTop(selector, separator) {
3611
+ const parts = [];
3612
+ let depth = 0;
3613
+ let quote = "";
3614
+ let start = 0;
3615
+ for (let i = 0; i < selector.length; i++) {
3616
+ const c = selector[i];
3617
+ if (quote) {
3618
+ if (c === "\\") i++;
3619
+ else if (c === quote) quote = "";
3620
+ } else if (c === '"' || c === "'") quote = c;
3621
+ else if (c === "\\") i++;
3622
+ else if (c === "(" || c === "[") depth++;
3623
+ else if (c === ")" || c === "]") depth--;
3624
+ else if (c === separator && depth === 0) {
3625
+ parts.push(selector.slice(start, i));
3626
+ start = i + 1;
3627
+ }
3628
+ }
3629
+ parts.push(selector.slice(start));
3630
+ return parts.filter((p) => p.trim());
3631
+ }
3632
+ function shiftLoc(node, source, start) {
3633
+ const loc = node.loc && node.loc.source === source ? { source, line: start.line + node.loc.line - 1, column: node.loc.line === 1 ? start.column + node.loc.column : node.loc.column } : node.loc;
3634
+ if (node.kind === "rule" || node.kind === "at-rule") return { ...node, loc, nodes: node.nodes.map((n) => shiftLoc(n, source, start)) };
3635
+ return { ...node, loc };
3636
+ }
3637
+ function dropLoc(node) {
3638
+ const { loc: _loc, ...rest } = node;
3639
+ if (rest.kind === "rule" || rest.kind === "at-rule") return { ...rest, nodes: rest.nodes.map(dropLoc) };
3640
+ return rest;
3641
+ }
3642
+ function printStyleWithMap(nodes, minify, sourceName, module2) {
3643
+ const lines = [[]];
3644
+ let css = "";
3645
+ let column = 0;
3646
+ const emit = (text) => {
3647
+ for (const ch of text) {
3648
+ css += ch;
3649
+ if (ch === "\n") {
3650
+ lines.push([]);
3651
+ column = 0;
3652
+ } else column++;
3653
+ }
3654
+ };
3655
+ const mark = (loc) => {
3656
+ if (loc && loc.source === sourceName) lines[lines.length - 1].push([column, 0, loc.line - 1, loc.column]);
3657
+ };
3658
+ const write = (node, depth) => {
3659
+ const pad = minify ? "" : " ".repeat(depth);
3660
+ const nl = minify ? "" : "\n";
3661
+ const sp = minify ? "" : " ";
3662
+ emit(pad);
3663
+ mark(node.loc);
3664
+ if (node.kind === "declaration") {
3665
+ emit(`${node.property}:${sp}${node.value}${node.important ? `${sp}!important` : ""};${nl}`);
3666
+ } else if (node.kind === "raw") {
3667
+ emit(`${node.text}${nl}`);
3668
+ } else {
3669
+ emit(`${node.kind === "rule" ? node.selector : `@${node.name}${node.params ? " " + node.params : ""}`}${sp}{${nl}`);
3670
+ for (const child of node.nodes) write(child, depth + 1);
3671
+ emit(`${pad}}${nl}`);
3672
+ }
3673
+ };
3674
+ for (const node of nodes) write(node, 0);
3675
+ return { css, map: { version: 3, sources: [module2.id], sourcesContent: [module2.code], names: [], mappings: (0, import_sourcemap_codec.encode)(lines) } };
3676
+ }
3677
+ function printStyle(nodes, minify = false) {
3678
+ const out = [];
3679
+ const write = (node, depth) => {
3680
+ const pad = minify ? "" : " ".repeat(depth);
3681
+ const nl = minify ? "" : "\n";
3682
+ const sp = minify ? "" : " ";
3683
+ if (node.kind === "declaration") {
3684
+ out.push(`${pad}${node.property}:${sp}${node.value}${node.important ? `${sp}!important` : ""};${nl}`);
3685
+ } else if (node.kind === "raw") {
3686
+ out.push(`${pad}${node.text}${nl}`);
3687
+ } else {
3688
+ const header = node.kind === "rule" ? node.selector : `@${node.name}${node.params ? " " + node.params : ""}`;
3689
+ out.push(`${pad}${header}${sp}{${nl}`);
3690
+ for (const child of node.nodes) write(child, depth + 1);
3691
+ out.push(`${pad}}${nl}`);
3692
+ }
3693
+ };
3694
+ for (const node of nodes) write(node, 0);
3695
+ return out.join("");
3696
+ }
3697
+ var import_sourcemap_codec, SCOPE_ATTR, FUNCTIONS, OPAQUE;
3698
+ var init_styles = __esm({
3699
+ "src/transform/styles.ts"() {
3700
+ "use strict";
3701
+ import_sourcemap_codec = require("@jridgewell/sourcemap-codec");
3702
+ SCOPE_ATTR = "data-fx-s";
3703
+ FUNCTIONS = /* @__PURE__ */ new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "ObjectMethod", "ClassMethod"]);
3704
+ OPAQUE = /(^|-)keyframes$|^font-face$|^property$|^counter-style$|^page$|^font-feature-values$/;
3705
+ }
3706
+ });
3707
+
2796
3708
  // src/lower/compile-template.ts
2797
3709
  function compilePieces(pieces, holes, options = {}) {
2798
3710
  const used = /* @__PURE__ */ new Set();
@@ -2801,6 +3713,7 @@ function compilePieces(pieces, holes, options = {}) {
2801
3713
  ...options.sourceFile ? { sourceFile: options.sourceFile } : {},
2802
3714
  ...options.positionAt ? { positionAt: options.positionAt } : {}
2803
3715
  });
3716
+ if (options.templateStyles) applyTemplateStyles(ir, options.templateStyles);
2804
3717
  analyzeStatic(ir);
2805
3718
  resolveComponentSources(ir, {
2806
3719
  resolver: createComponentResolver(options.resolve ?? []),
@@ -2823,6 +3736,7 @@ function compilePieces(pieces, holes, options = {}) {
2823
3736
  var init_compile_template = __esm({
2824
3737
  "src/lower/compile-template.ts"() {
2825
3738
  "use strict";
3739
+ init_styles();
2826
3740
  init_analyze_static();
2827
3741
  init_collapse_strategies();
2828
3742
  init_imperative();
@@ -3355,8 +4269,8 @@ function isJsxRoot(node, parent, key) {
3355
4269
  if (!isJsx(node)) return false;
3356
4270
  return !(parent && isJsx(parent) && key === "children");
3357
4271
  }
3358
- function isTemplateTag(node, litTag) {
3359
- return node.type === "TaggedTemplateExpression" && node.tag?.type === "Identifier" && (node.tag.name === litTag || node.tag.name === "svg");
4272
+ function isTemplateTag(node, tags) {
4273
+ return node.type === "TaggedTemplateExpression" && node.tag?.type === "Identifier" && tags.has(node.tag.name);
3360
4274
  }
3361
4275
  function outermost(list) {
3362
4276
  return list.filter(
@@ -3504,22 +4418,28 @@ function transformTemplates(code, id, options = {}) {
3504
4418
  const wantsIntrinsics = options.intrinsics !== false && mayHaveIntrinsic(code);
3505
4419
  const wantsSources = options.sourceLocations === true && mayHaveSourceMarks(code);
3506
4420
  const sourceName = relativeToRoot(id, options.sourceRoot);
3507
- if (!mayHaveTemplate && !(wantsJsx && code.includes("<")) && !wantsIntrinsics && !wantsSources) return null;
4421
+ const wantsStyles = options.styles !== void 0 && (code.includes("<style") || code.includes("css`"));
4422
+ if (!mayHaveTemplate && !(wantsJsx && code.includes("<")) && !wantsIntrinsics && !wantsSources && !wantsStyles) return null;
3508
4423
  const ast = (0, import_parser2.parse)(code, {
3509
4424
  sourceType: "module",
3510
4425
  plugins: ["jsx", "typescript"],
3511
4426
  errorRecovery: true
3512
4427
  });
4428
+ const intrinsicTags = options.intrinsics === false ? /* @__PURE__ */ new Map() : tagIntrinsics(moduleBindings(ast.program));
4429
+ const templateTags = /* @__PURE__ */ new Map([[litTag, "html"], ["svg", "svg"]]);
4430
+ for (const [name, tag] of intrinsicTags) if (tag !== "css") templateTags.set(name, tag);
4431
+ const cssTags = [...intrinsicTags].filter(([, tag]) => tag === "css").map(([name]) => name);
3513
4432
  const found = [];
3514
4433
  walk7(ast.program, (n, parent, key) => {
3515
- if (wantsLit && isTemplateTag(n, litTag)) found.push(n);
4434
+ if (wantsLit && isTemplateTag(n, templateTags)) found.push(n);
3516
4435
  else if (wantsJsx && isJsxRoot(n, parent, key)) found.push(n);
3517
4436
  });
3518
4437
  const props = options.propsDestructure === false ? { edits: [], diagnostics: [], used: /* @__PURE__ */ new Set() } : collectPropsEdits(ast.program, code, { runtimeModule: options.runtimeModule });
3519
4438
  const intrinsics = options.intrinsics === false ? { edits: [], diagnostics: [], imports: /* @__PURE__ */ new Map() } : collectIntrinsicEdits(ast.program);
3520
4439
  if (intrinsics.edits.length) avoidBindingClashes(ast.program, intrinsics, moduleBindings(ast.program));
3521
4440
  const sources = options.sourceLocations === true ? collectSourceEdits(ast.program, sourceName) : { edits: [], marked: 0 };
3522
- const silent = found.length === 0 && props.edits.length === 0 && sources.edits.length === 0 && intrinsics.edits.length === 0 && intrinsics.diagnostics.length === 0 && props.diagnostics.length === 0;
4441
+ const styles = wantsStyles ? collectStyles(ast.program, sourceName, options.styles, { html: templateTags, css: cssTags }, { id, code }) : void 0;
4442
+ const silent = found.length === 0 && props.edits.length === 0 && sources.edits.length === 0 && intrinsics.edits.length === 0 && intrinsics.diagnostics.length === 0 && props.diagnostics.length === 0 && !styles?.edits.length && !styles?.diagnostics.length;
3523
4443
  if (silent) return null;
3524
4444
  found.sort((a, b) => b.start - a.start || a.end - b.end);
3525
4445
  const used = new Set(props.used);
@@ -3533,20 +4453,32 @@ function transformTemplates(code, id, options = {}) {
3533
4453
  declared: declaredNames(ast.program)
3534
4454
  };
3535
4455
  const hoisted = /* @__PURE__ */ new Map();
3536
- const edits = [...props.edits, ...intrinsics.edits, ...sources.edits];
4456
+ const edits = [...props.edits, ...intrinsics.edits, ...sources.edits, ...styles?.edits ?? []];
3537
4457
  for (const node of found) {
3538
4458
  const nested = found.some(
3539
4459
  (o) => o !== node && o.start <= node.start && o.end >= node.end && o.end - o.start > node.end - node.start
3540
4460
  );
3541
4461
  if (isJsx(node)) {
3542
- edits.push({ start: node.start, end: node.end, code: compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceName) });
4462
+ edits.push({ start: node.start, end: node.end, code: compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceName, styles) });
3543
4463
  continue;
3544
4464
  }
3545
4465
  const holes = node.quasi.expressions.map(
3546
4466
  (e) => bindHole2(code, e, edits, options.sourceLocations === true)
3547
4467
  );
4468
+ const scope = styles?.scopes.find((s) => node.start >= s.start && node.end <= s.end)?.value;
4469
+ const owner = styles?.templateOwners.get(node.start);
4470
+ const templateStyles = options.styles && styles && (scope || owner) ? {
4471
+ options: options.styles,
4472
+ ...scope ? { scope } : {},
4473
+ ...owner ? { owner } : {},
4474
+ line: node.loc?.start.line ?? 0,
4475
+ column: node.loc?.start.column ?? 0,
4476
+ styles: styles.styles,
4477
+ diagnostics: styles.diagnostics
4478
+ } : void 0;
3548
4479
  const result = compilePieces(toPieces(node), holes, {
3549
4480
  ...options,
4481
+ ...templateStyles ? { templateStyles } : {},
3550
4482
  hoistTemplates: true,
3551
4483
  // A tag inside a template gets the same mark a JSX tag does. Without one, the nodes it
3552
4484
  // makes are the only ones in the module carrying no mark, and they take whichever was
@@ -3555,12 +4487,13 @@ function transformTemplates(code, id, options = {}) {
3555
4487
  // Same defaults as the plugin, so moving a build over is not a rendering change.
3556
4488
  templateClone: options.templateClone ?? true,
3557
4489
  partialTemplates: nested ? false : options.partialTemplates ?? true,
3558
- svg: node.tag.name === "svg"
4490
+ svg: templateTags.get(node.tag.name) === "svg"
3559
4491
  });
3560
4492
  for (const symbol of result.imports) used.add(symbol);
3561
4493
  collectNeeds(result.ir, needs);
3562
4494
  edits.push({ start: node.start, end: node.end, code: adoptTemplates2(result.code, result.templates, hoisted) });
3563
4495
  }
4496
+ if (styles && options.styles) edits.push(...finishShadowStyles(styles, code, id));
3564
4497
  const out = new import_magic_string2.default(code);
3565
4498
  for (const e of outermost(edits)) {
3566
4499
  if (e.start === e.end) out.appendLeft(e.start, e.code);
@@ -3618,13 +4551,19 @@ function transformTemplates(code, id, options = {}) {
3618
4551
  ...loadDiagnostics.length ? { loadDiagnostics } : {},
3619
4552
  ...unresolvedDiagnostics.length ? { unresolvedDiagnostics } : {},
3620
4553
  ...props.diagnostics.length ? { propsDiagnostics: props.diagnostics } : {},
3621
- ...intrinsics.diagnostics.length ? { intrinsicDiagnostics: intrinsics.diagnostics } : {}
4554
+ ...intrinsics.diagnostics.length ? { intrinsicDiagnostics: intrinsics.diagnostics } : {},
4555
+ ...styles?.styles.length ? { styles: styles.styles } : {},
4556
+ ...styles?.diagnostics.length ? { styleDiagnostics: styles.diagnostics } : {},
4557
+ ...styles?.literals.length ? { styleLiterals: styles.literals } : {},
4558
+ ...styles?.references.length ? { styleReferences: styles.references } : {}
3622
4559
  };
3623
4560
  }
3624
- function compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceFile) {
4561
+ function compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceFile, styles) {
3625
4562
  const ir = buildJsxIR(node, {
3626
4563
  code: (n) => slice(code, n.start, n.end, edits),
3627
4564
  used,
4565
+ ...styles?.scopes.length ? { scopes: styles.scopes } : {},
4566
+ ...styles?.drop.size ? { dropStyles: styles.drop } : {},
3628
4567
  ...options.sourceLocations === true && sourceFile ? { sourceFile } : {}
3629
4568
  });
3630
4569
  analyzeStatic(ir);
@@ -3710,6 +4649,7 @@ var init_templates = __esm({
3710
4649
  init_props();
3711
4650
  init_intrinsics2();
3712
4651
  init_source_locations();
4652
+ init_styles();
3713
4653
  init_intrinsics();
3714
4654
  init_reactive_bindings();
3715
4655
  init_builtins();
@@ -3752,14 +4692,14 @@ var init_transform = __esm({
3752
4692
  });
3753
4693
 
3754
4694
  // src/transform/server-fn-id.ts
3755
- function hash(s) {
4695
+ function hash2(s) {
3756
4696
  let h = 5381;
3757
4697
  for (let i = 0; i < s.length; i++) h = h * 33 ^ s.charCodeAt(i);
3758
4698
  return (h >>> 0).toString(16);
3759
4699
  }
3760
4700
  function serverFnId(filename, root, index) {
3761
4701
  const rel = (filename || "unknown").replace(root ? root + "/" : "", "");
3762
- return `${hash(rel)}:${index}`;
4702
+ return `${hash2(rel)}:${index}`;
3763
4703
  }
3764
4704
  var init_server_fn_id = __esm({
3765
4705
  "src/transform/server-fn-id.ts"() {
@@ -4015,7 +4955,7 @@ function componentGlobals(rules, options = {}) {
4015
4955
  resolved.set(name, target);
4016
4956
  }
4017
4957
  }
4018
- const usableIntrinsics = intrinsics ? Object.entries(INTRINSICS).filter(([, i]) => {
4958
+ const usableIntrinsics = intrinsics ? Object.entries({ ...INTRINSICS, ...TAG_INTRINSICS }).filter(([, i]) => {
4019
4959
  const pkg = packageOf(i.module);
4020
4960
  return !available || !pkg || available.has(pkg);
4021
4961
  }) : [];
@@ -4078,11 +5018,11 @@ function coveredByInclude(root, target) {
4078
5018
  }
4079
5019
  if (config.extends !== void 0) return void 0;
4080
5020
  if (!Array.isArray(config.include)) return true;
4081
- const relative2 = relativePosix(root, target);
5021
+ const relative3 = relativePosix(root, target);
4082
5022
  return config.include.some((entry) => {
4083
5023
  if (typeof entry !== "string") return false;
4084
5024
  const pattern = entry.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*\//g, "(?:.*/)?").replace(/\*/g, "[^/]*");
4085
- return new RegExp(`^${pattern}(?:/.*)?$`).test(relative2);
5025
+ return new RegExp(`^${pattern}(?:/.*)?$`).test(relative3);
4086
5026
  });
4087
5027
  }
4088
5028
  function relativePosix(from, to) {
@@ -4168,13 +5108,169 @@ function writeComponentGlobals(options, root) {
4168
5108
  }
4169
5109
  function warnIfUnreachable(root, target) {
4170
5110
  if (coveredByInclude(root, target) !== false) return void 0;
4171
- const relative2 = relativePosix(root, target);
4172
- return `[fluixi] ${relative2} is not covered by "include" in tsconfig.json, so TypeScript will ignore it and every resolved component stays "Cannot find name". Add "${relative2}" to "include".`;
5111
+ const relative3 = relativePosix(root, target);
5112
+ return `[fluixi] ${relative3} is not covered by "include" in tsconfig.json, so TypeScript will ignore it and every resolved component stays "Cannot find name". Add "${relative3}" to "include".`;
4173
5113
  }
4174
5114
 
4175
- // src/integrations.ts
5115
+ // src/vite-styles.ts
4176
5116
  var import_node_fs2 = require("node:fs");
4177
5117
  var import_node_path2 = require("node:path");
5118
+ var STYLE_ID = /\?fx-style&index=(\d+)(?:&inline)?&lang\.css$/;
5119
+ var LITERAL_ID = /\?fx-literal=([\w$]+)&inline&lang\.css$/;
5120
+ var styleId = (file, index) => `${file}?fx-style&index=${index}&lang.css`;
5121
+ var loader = (name) => `const href = new URL(${JSON.stringify(`./${name}`)}, import.meta.url).href;
5122
+ if (typeof document !== 'undefined' && !document.querySelector(\`link[data-fx-css="\${href}"]\`)) {
5123
+ const link = document.createElement('link');
5124
+ link.rel = 'stylesheet';
5125
+ link.href = href;
5126
+ link.dataset.fxCss = href;
5127
+ document.head.appendChild(link);
5128
+ }
5129
+ `;
5130
+ function createComponentStyles(compile) {
5131
+ let api;
5132
+ let server;
5133
+ let root = process.cwd();
5134
+ let library;
5135
+ const emitted = /* @__PURE__ */ new Map();
5136
+ const fileOf = (id) => (0, import_node_fs2.existsSync)(id) ? id : (0, import_node_path2.join)(root, id);
5137
+ const cache = /* @__PURE__ */ new Map();
5138
+ const resolveReference = async (ref, importer, ctx, seen) => {
5139
+ const resolved = await ctx.resolve?.(ref.source, importer);
5140
+ if (!resolved) return null;
5141
+ const file = resolved.id.split("?")[0];
5142
+ if (seen.has(file)) return null;
5143
+ let code;
5144
+ try {
5145
+ code = (0, import_node_fs2.readFileSync)(fileOf(file), "utf8");
5146
+ } catch {
5147
+ return null;
5148
+ }
5149
+ const styles = api.styles(file);
5150
+ let result = compile(code, file, styles);
5151
+ if (result?.styleReferences?.length) {
5152
+ const imported = await resolveAll(result.styleReferences, file, ctx, /* @__PURE__ */ new Set([...seen, file]));
5153
+ result = compile(code, file, { ...styles, imported });
5154
+ }
5155
+ const literal = result?.styleLiterals?.find((l) => l.name === ref.name);
5156
+ return literal ? styles.process(literal.css, file) : null;
5157
+ };
5158
+ const resolveAll = async (refs, importer, ctx, seen) => {
5159
+ const imported = /* @__PURE__ */ new Map();
5160
+ for (const ref of refs) {
5161
+ const nodes = await resolveReference(ref, importer, ctx, seen);
5162
+ if (nodes) imported.set(`${ref.source}#${ref.name}`, nodes);
5163
+ }
5164
+ return imported;
5165
+ };
5166
+ const run = async (code, id, ctx) => {
5167
+ const file = fileOf(id);
5168
+ const styles = api.styles(file);
5169
+ let result = compile(code, file, styles);
5170
+ if (result?.styleReferences?.length) {
5171
+ const imported = await resolveAll(result.styleReferences, id, ctx, /* @__PURE__ */ new Set([file]));
5172
+ result = compile(code, file, { ...styles, imported });
5173
+ }
5174
+ const css = (result?.styles ?? []).map((s) => s.css);
5175
+ const maps = (result?.styles ?? []).map((s) => s.map ?? null);
5176
+ const literals = new Map((result?.styleLiterals ?? []).map((l) => [l.name, l.css]));
5177
+ const before = cache.get(file);
5178
+ cache.set(file, { source: code, css, maps, literals });
5179
+ if (server && before) {
5180
+ for (let i = 0; i < css.length; i++) {
5181
+ if (before.css[i] === css[i]) continue;
5182
+ const mod = server.moduleGraph.getModuleById(styleId(id, i));
5183
+ if (mod) void server.reloadModule(mod);
5184
+ }
5185
+ }
5186
+ const page = (result?.styles ?? []).flatMap((s, i) => s.shadow ? [] : [i]);
5187
+ if (!result || page.length === 0) return result;
5188
+ if (library) {
5189
+ const name = `${(0, import_node_path2.basename)(file, (0, import_node_path2.extname)(file))}.css`;
5190
+ if ((0, import_node_fs2.existsSync)((0, import_node_path2.join)((0, import_node_path2.dirname)(file), name))) {
5191
+ throw new Error(`[fluixi] ${file} has <style> blocks, and ${name} next to it would be overwritten by their CSS. Rename one of them.`);
5192
+ }
5193
+ const out = (0, import_node_path2.join)((0, import_node_path2.relative)(library, (0, import_node_path2.dirname)(file)), name);
5194
+ emitted.set(out, page.map((i) => css[i]).join("\n"));
5195
+ emitted.set(`${out}.js`, loader(name));
5196
+ return { ...result, code: `${result.code}
5197
+ import ${JSON.stringify(`./${name}`)};` };
5198
+ }
5199
+ const imports = page.map((i) => `
5200
+ import ${JSON.stringify(styleId(id, i))};`).join("");
5201
+ return { ...result, code: result.code + imports };
5202
+ };
5203
+ return {
5204
+ /** Looks for @fluixi-css/vite among the plugins; without it component styles stay off. */
5205
+ connect(plugins, projectRoot) {
5206
+ if (projectRoot) root = projectRoot;
5207
+ api = (plugins ?? []).map((p) => p?.api?.fluixiCss).find((a) => Boolean(a));
5208
+ api?.useComponentStyles();
5209
+ },
5210
+ /** Turns on library output, from the directory of a `build.lib` entry. */
5211
+ setLibrary(entryDir) {
5212
+ library = entryDir;
5213
+ },
5214
+ /** A library build's style import: left for the package's consumer to bundle. */
5215
+ isLibraryImport(id, importer) {
5216
+ if (library === void 0 || !importer || !id.startsWith("./") || !id.endsWith(".css")) return false;
5217
+ return emitted.has((0, import_node_path2.join)((0, import_node_path2.relative)(library, (0, import_node_path2.dirname)(fileOf(importer))), id));
5218
+ },
5219
+ /** The style files a library build writes, by path under the output directory. */
5220
+ libraryFiles() {
5221
+ return emitted;
5222
+ },
5223
+ get active() {
5224
+ return api !== void 0;
5225
+ },
5226
+ setServer(dev) {
5227
+ server = dev;
5228
+ },
5229
+ isStyle(id) {
5230
+ return STYLE_ID.test(id) || LITERAL_ID.test(id);
5231
+ },
5232
+ /**
5233
+ * A style request as the module id Vite should keep: the file's absolute path and the
5234
+ * query. Vite invalidates a module when the file its id names changes, so a root-relative id
5235
+ * (`/src/card.tsx?fx-style…`, which is how a request arrives before the component's imports
5236
+ * were analysed) was never invalidated, and an edited block kept its old CSS until restart.
5237
+ */
5238
+ resolveId(id) {
5239
+ const at = id.indexOf("?");
5240
+ return fileOf(id.slice(0, at)) + id.slice(at);
5241
+ },
5242
+ compile: run,
5243
+ /**
5244
+ * A block's CSS and its map back to the module, compiling the module again when the cache
5245
+ * does not match the file.
5246
+ */
5247
+ async load(id, ctx) {
5248
+ const match = STYLE_ID.exec(id);
5249
+ const literal = LITERAL_ID.exec(id);
5250
+ if (!match && !literal || !api) return null;
5251
+ const file = id.slice(0, id.indexOf("?"));
5252
+ let source;
5253
+ try {
5254
+ source = (0, import_node_fs2.readFileSync)(fileOf(file), "utf8");
5255
+ } catch {
5256
+ return null;
5257
+ }
5258
+ if (cache.get(fileOf(file))?.source !== source) await run(source, file, ctx);
5259
+ const entry = cache.get(fileOf(file));
5260
+ if (literal) {
5261
+ const css = entry?.literals.get(literal[1]);
5262
+ if (css === void 0) throw new Error(`[fluixi] ${literal[1]} in ${file} is not a css\`\` value, so it cannot be a shadow root's styles.`);
5263
+ return { code: css, map: null };
5264
+ }
5265
+ const index = Number(match[1]);
5266
+ return { code: entry?.css[index] ?? "", map: entry?.maps[index] ?? null };
5267
+ }
5268
+ };
5269
+ }
5270
+
5271
+ // src/integrations.ts
5272
+ var import_node_fs3 = require("node:fs");
5273
+ var import_node_path3 = require("node:path");
4178
5274
  async function createBabelPlugin(options = {}) {
4179
5275
  let pluginModule = null;
4180
5276
  try {
@@ -4239,6 +5335,11 @@ var RETIRED_SUBPATHS = {
4239
5335
  "@fluixi/core/rx": "is gone — import signals and effects from @fluixi/reactive, or the server primitives it resolved to under node from @fluixi/core/server"
4240
5336
  };
4241
5337
  function createVitePlugin(options = {}) {
5338
+ let transformTemplates2;
5339
+ let projectRoot = process.cwd();
5340
+ const componentStyles = createComponentStyles((code, id, styles) => {
5341
+ return transformTemplates2(code, id, { ...transformOptions(options, id), sourceRoot: projectRoot, styles });
5342
+ });
4242
5343
  return {
4243
5344
  name: "vite-fluixi-jsx",
4244
5345
  enforce: "pre",
@@ -4252,14 +5353,32 @@ function createVitePlugin(options = {}) {
4252
5353
  },
4253
5354
  // Written once per build, and on dev-server start, rather than per transform: the
4254
5355
  // set of resolvable names comes from config, so nothing a file does can change it.
4255
- configResolved(config) {
5356
+ async configResolved(config) {
4256
5357
  const outcome = writeComponentGlobals(options, config?.root ?? process.cwd());
4257
5358
  if (outcome.warning) console.warn(outcome.warning);
5359
+ if (options.babel) return;
5360
+ projectRoot = config?.root ?? projectRoot;
5361
+ componentStyles.connect(config?.plugins, config?.root);
5362
+ const entry = config?.command === "build" && config.build?.lib ? config.build.lib.entry : void 0;
5363
+ const first = typeof entry === "string" ? entry : Array.isArray(entry) ? entry[0] : entry && typeof entry === "object" ? Object.values(entry)[0] : void 0;
5364
+ if (typeof first === "string") componentStyles.setLibrary((0, import_node_path3.dirname)((0, import_node_path3.resolve)(config.root ?? process.cwd(), first)));
5365
+ if (componentStyles.active) ({ transformTemplates: transformTemplates2 } = await Promise.resolve().then(() => (init_transform(), transform_exports)));
5366
+ },
5367
+ generateBundle() {
5368
+ for (const [fileName, source] of componentStyles.libraryFiles()) this.emitFile({ type: "asset", fileName, source });
5369
+ },
5370
+ configureServer(server) {
5371
+ componentStyles.setServer(server);
5372
+ },
5373
+ load(id) {
5374
+ return componentStyles.isStyle(id) ? componentStyles.load(id, this) : null;
4258
5375
  },
4259
5376
  // A subpath we retired resolves to nothing, and the bundler reports that as
4260
5377
  // `Missing "./x" specifier`, which names neither the replacement nor the file
4261
5378
  // that asked. Answer it here, where the rename is known.
4262
5379
  resolveId(id, importer) {
5380
+ if (componentStyles.isStyle(id)) return componentStyles.resolveId(id);
5381
+ if (componentStyles.isLibraryImport(id, importer)) return { id, external: true };
4263
5382
  const moved = RETIRED_SUBPATHS[id];
4264
5383
  if (!moved) return null;
4265
5384
  throw new Error(
@@ -4271,6 +5390,11 @@ function createVitePlugin(options = {}) {
4271
5390
  if (!/\.(jsx|tsx|ts)$/i.test(id)) {
4272
5391
  return null;
4273
5392
  }
5393
+ if (!options.babel && componentStyles.active) {
5394
+ const result = await componentStyles.compile(code, id, this);
5395
+ reportDiagnostics(result, id);
5396
+ return result;
5397
+ }
4274
5398
  if (!options.babel) return compileModule(code, id, options);
4275
5399
  const babelPlugin = await createBabelPlugin(options);
4276
5400
  try {
@@ -4366,6 +5490,7 @@ function reportDiagnostics(result, id) {
4366
5490
  for (const d of r?.unresolvedDiagnostics ?? []) say(d.message);
4367
5491
  for (const d of r?.loadDiagnostics ?? []) say(d.message);
4368
5492
  for (const d of r?.intrinsicDiagnostics ?? []) say(d.message);
5493
+ for (const d of r?.styleDiagnostics ?? []) say(`${d.line}:${d.column + 1} ${d.message}`);
4369
5494
  for (const d of r?.propsDiagnostics ?? []) {
4370
5495
  say(`\`${d.name}\` was left destructured: ${d.message}. It reads once instead of tracking.`);
4371
5496
  }
@@ -4418,8 +5543,8 @@ function createESBuildPlugin(options = {}) {
4418
5543
  name: "esbuild-fluixi-jsx",
4419
5544
  setup(build) {
4420
5545
  build.onLoad({ filter: /\.[jt]sx?$/ }, async (args) => {
4421
- const { readFileSync: readFileSync3 } = await import("node:fs");
4422
- const source = readFileSync3(args.path, "utf8");
5546
+ const { readFileSync: readFileSync4 } = await import("node:fs");
5547
+ const source = readFileSync4(args.path, "utf8");
4423
5548
  try {
4424
5549
  const result = await compileModule(source, args.path, options);
4425
5550
  return {
@@ -4447,17 +5572,17 @@ function hasServerDirective(source) {
4447
5572
  function findServerFnModules(dir, out = []) {
4448
5573
  let entries;
4449
5574
  try {
4450
- entries = (0, import_node_fs2.readdirSync)(dir, { withFileTypes: true });
5575
+ entries = (0, import_node_fs3.readdirSync)(dir, { withFileTypes: true });
4451
5576
  } catch {
4452
5577
  return out;
4453
5578
  }
4454
5579
  for (const e of entries) {
4455
5580
  if (e.name === "node_modules" || e.name === "dist" || e.name.startsWith(".")) continue;
4456
- const full = (0, import_node_path2.join)(dir, e.name);
5581
+ const full = (0, import_node_path3.join)(dir, e.name);
4457
5582
  if (e.isDirectory()) findServerFnModules(full, out);
4458
5583
  else if (/\.[jt]sx?$/.test(e.name)) {
4459
5584
  try {
4460
- if (hasServerDirective((0, import_node_fs2.readFileSync)(full, "utf8"))) out.push(full);
5585
+ if (hasServerDirective((0, import_node_fs3.readFileSync)(full, "utf8"))) out.push(full);
4461
5586
  } catch {
4462
5587
  }
4463
5588
  }
@@ -4477,7 +5602,7 @@ function serverFunctionsVitePlugin(options = {}) {
4477
5602
  },
4478
5603
  load(id) {
4479
5604
  if (id !== "\0" + SERVER_FNS_VMOD) return null;
4480
- const mods = findServerFnModules((0, import_node_path2.join)(root, "src"));
5605
+ const mods = findServerFnModules((0, import_node_path3.join)(root, "src"));
4481
5606
  const setup = `import '@fluixi/start/server-fn-setup';
4482
5607
  `;
4483
5608
  const imports = mods.map((m) => `import ${JSON.stringify(m)};`).join("\n");