@fluixi/compiler 1.0.0-alpha.93 → 1.0.0-alpha.95

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 (93) hide show
  1. package/LICENSE +1 -1
  2. package/dist/analyze/intrinsics.cjs +13 -1
  3. package/dist/analyze/intrinsics.d.cts +14 -0
  4. package/dist/analyze/intrinsics.d.ts +14 -0
  5. package/dist/analyze/intrinsics.d.ts.map +1 -1
  6. package/dist/analyze/intrinsics.js +17 -0
  7. package/dist/analyze/intrinsics.mjs +13 -1
  8. package/dist/analyze/static-graph.cjs +1 -1
  9. package/dist/analyze/static-graph.mjs +1 -1
  10. package/dist/{babel-RJHJQVEM.mjs → babel-V33QLJBF.mjs} +4 -4
  11. package/dist/{chunk-56QSOVVL.mjs → chunk-62F2LLBM.mjs} +1 -1
  12. package/dist/{chunk-R7PPFBCL.mjs → chunk-7GNSGUHK.mjs} +4 -83
  13. package/dist/{chunk-U7HCFLZ2.mjs → chunk-B64LVPTU.mjs} +356 -24
  14. package/dist/{chunk-OD53TMER.mjs → chunk-C3AK5PBV.mjs} +13 -1
  15. package/dist/{chunk-TJIH7TLX.mjs → chunk-GLFUPZ27.mjs} +1 -1
  16. package/dist/chunk-HUABRVWB.mjs +92 -0
  17. package/dist/chunk-XCWP7WJG.mjs +584 -0
  18. package/dist/codegen/backends/imperative.cjs +1 -1
  19. package/dist/codegen/backends/imperative.d.ts.map +1 -1
  20. package/dist/codegen/backends/imperative.js +2 -1
  21. package/dist/codegen/backends/imperative.mjs +1 -1
  22. package/dist/frontend/babel/build-ir-lit.cjs +352 -22
  23. package/dist/frontend/babel/build-ir-lit.mjs +352 -22
  24. package/dist/frontend/babel/build-ir.cjs +352 -22
  25. package/dist/frontend/babel/build-ir.mjs +352 -22
  26. package/dist/frontend/babel/index.cjs +353 -23
  27. package/dist/frontend/babel/index.mjs +353 -23
  28. package/dist/frontend/babel/lower-template.cjs +352 -22
  29. package/dist/frontend/babel/lower-template.mjs +352 -22
  30. package/dist/frontend/babel/plugin.cjs +352 -22
  31. package/dist/frontend/babel/plugin.mjs +352 -22
  32. package/dist/index.cjs +321 -22
  33. package/dist/index.mjs +333 -22
  34. package/dist/integrations.cjs +1240 -56
  35. package/dist/integrations.d.ts.map +1 -1
  36. package/dist/integrations.js +62 -9
  37. package/dist/integrations.mjs +257 -26
  38. package/dist/lower/compile-template.cjs +315 -21
  39. package/dist/lower/compile-template.d.cts +13 -0
  40. package/dist/lower/compile-template.d.ts +13 -0
  41. package/dist/lower/compile-template.d.ts.map +1 -1
  42. package/dist/lower/compile-template.js +3 -0
  43. package/dist/lower/compile-template.mjs +327 -21
  44. package/dist/lower/entities.cjs +288 -0
  45. package/dist/lower/entities.d.cts +1 -0
  46. package/dist/lower/entities.d.ts +2 -0
  47. package/dist/lower/entities.d.ts.map +1 -0
  48. package/dist/lower/entities.js +54 -0
  49. package/dist/lower/entities.mjs +267 -0
  50. package/dist/lower/jsx.cjs +104 -47
  51. package/dist/lower/jsx.d.cts +8 -0
  52. package/dist/lower/jsx.d.ts +8 -0
  53. package/dist/lower/jsx.d.ts.map +1 -1
  54. package/dist/lower/jsx.js +92 -2
  55. package/dist/lower/jsx.mjs +104 -47
  56. package/dist/lower/template.cjs +293 -20
  57. package/dist/lower/template.d.ts.map +1 -1
  58. package/dist/lower/template.js +35 -19
  59. package/dist/lower/template.mjs +293 -20
  60. package/dist/resolve/builtins.cjs +1 -1
  61. package/dist/resolve/builtins.mjs +1 -1
  62. package/dist/resolve/component-globals.cjs +6 -1
  63. package/dist/resolve/component-globals.js +2 -2
  64. package/dist/resolve/component-globals.mjs +6 -1
  65. package/dist/resolve/write-globals.cjs +6 -1
  66. package/dist/resolve/write-globals.mjs +6 -1
  67. package/dist/{server-fns-ZFRDIQ2X.mjs → server-fns-N3SDJXA7.mjs} +5 -4
  68. package/dist/{server-functions-CTDXX67X.mjs → server-functions-LVS66Q4R.mjs} +3 -3
  69. package/dist/transform/index.cjs +973 -33
  70. package/dist/transform/index.mjs +991 -47
  71. package/dist/transform/props.mjs +19 -14
  72. package/dist/transform/server-fns.mjs +19 -14
  73. package/dist/transform/styles.cjs +606 -0
  74. package/dist/transform/styles.d.cts +237 -0
  75. package/dist/transform/styles.d.ts +238 -0
  76. package/dist/transform/styles.d.ts.map +1 -0
  77. package/dist/transform/styles.js +743 -0
  78. package/dist/transform/styles.mjs +671 -0
  79. package/dist/transform/templates.cjs +972 -32
  80. package/dist/transform/templates.d.cts +13 -0
  81. package/dist/transform/templates.d.ts +13 -0
  82. package/dist/transform/templates.d.ts.map +1 -1
  83. package/dist/transform/templates.js +49 -12
  84. package/dist/transform/templates.mjs +990 -46
  85. package/dist/{transform-QWWQDCXE.mjs → transform-P4AETYV4.mjs} +49 -16
  86. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  87. package/dist/vite-styles.cjs +223 -0
  88. package/dist/vite-styles.d.cts +68 -0
  89. package/dist/vite-styles.d.ts +69 -0
  90. package/dist/vite-styles.d.ts.map +1 -0
  91. package/dist/vite-styles.js +241 -0
  92. package/dist/vite-styles.mjs +214 -0
  93. package/package.json +5 -4
@@ -36,6 +36,580 @@ module.exports = __toCommonJS(templates_exports);
36
36
  var import_parser2 = require("@babel/parser");
37
37
  var import_magic_string2 = __toESM(require("magic-string"), 1);
38
38
 
39
+ // src/transform/styles.ts
40
+ var import_sourcemap_codec = require("@jridgewell/sourcemap-codec");
41
+ var SCOPE_ATTR = "data-fx-s";
42
+ var FUNCTIONS = /* @__PURE__ */ new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "ObjectMethod", "ClassMethod"]);
43
+ function collectStyles(program, sourceName, options, tags = { html: /* @__PURE__ */ new Map([["html", "html"]]) }, module2) {
44
+ const plan = {
45
+ scopes: [],
46
+ templateOwners: /* @__PURE__ */ new Map(),
47
+ drop: /* @__PURE__ */ new Set(),
48
+ styles: [],
49
+ diagnostics: [],
50
+ edits: [],
51
+ literals: [],
52
+ references: [],
53
+ shadowClasses: /* @__PURE__ */ new Map()
54
+ };
55
+ if (!options) return plan;
56
+ const literalsOf = findLiterals(program, options.cssModules ?? ["@fluixi/core", "@fluixi/dom"], plan, tags.css ?? []);
57
+ plan.names = literalsOf;
58
+ const classOf = /* @__PURE__ */ new Map();
59
+ const shadowOf = (owner) => {
60
+ const cls = classOf.get(owner);
61
+ return cls ? plan.shadowClasses.get(cls) : void 0;
62
+ };
63
+ const resolved = /* @__PURE__ */ new Map();
64
+ const piecesToNodes = (quasis, expressions, at, stack) => {
65
+ const out = [];
66
+ for (let i = 0; i < quasis.length; i++) {
67
+ const text = quasis[i].value.cooked ?? quasis[i].value.raw;
68
+ if (text.trim()) {
69
+ try {
70
+ const start = quasis[i].loc?.start;
71
+ out.push(...options.process(text, sourceName).map((n) => start ? shiftLoc(n, sourceName, start) : n));
72
+ } catch (error) {
73
+ plan.diagnostics.push({ ...at, message: `CSS could not be processed: ${error.message}` });
74
+ return null;
75
+ }
76
+ }
77
+ const expr = expressions[i];
78
+ if (!expr) continue;
79
+ const value = expr.type === "Identifier" ? resolveLiteral(expr.name, at, stack) : void 0;
80
+ if (value === void 0) {
81
+ plan.diagnostics.push({
82
+ ...at,
83
+ 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))."
84
+ });
85
+ return null;
86
+ }
87
+ if (value === null) return null;
88
+ out.push(...value);
89
+ }
90
+ return out;
91
+ };
92
+ function resolveLiteral(name, at, stack) {
93
+ const imported = literalsOf.imports.get(name);
94
+ if (imported) {
95
+ const nodes = options?.imported?.get(`${imported.source}#${imported.name}`);
96
+ if (nodes) return nodes.map((n) => keepLocIn(n, options?.importedModules));
97
+ plan.references.push({ name: imported.name, source: imported.source, ...at });
98
+ plan.diagnostics.push({
99
+ ...at,
100
+ 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.`
101
+ });
102
+ return null;
103
+ }
104
+ const literal = literalsOf.literals.get(name);
105
+ if (!literal) return void 0;
106
+ if (stack.includes(name)) {
107
+ plan.diagnostics.push({ ...at, message: `css\`\` values include each other: ${[...stack, name].join(" → ")}.` });
108
+ return null;
109
+ }
110
+ if (!resolved.has(name)) {
111
+ resolved.set(name, piecesToNodes(literal.quasi.quasis, literal.quasi.expressions, at, [...stack, name]));
112
+ }
113
+ return resolved.get(name);
114
+ }
115
+ for (const [name, literal] of literalsOf.literals) {
116
+ const at = { line: literal.loc?.start.line ?? 0, column: literal.loc?.start.column ?? 0 };
117
+ const nodes = resolveLiteral(name, at, []);
118
+ const id = hash(`${sourceName}:css:${name}`);
119
+ plan.edits.push({ start: literal.start, end: literal.end, code: `({ $fxStyle: ${JSON.stringify(id)} })` });
120
+ if (nodes) plan.literals.push({ id, name, css: printStyle(nodes, options.minify), nodes });
121
+ }
122
+ const scopeOf = /* @__PURE__ */ new Map();
123
+ const names = /* @__PURE__ */ new Map();
124
+ const named = /* @__PURE__ */ new Map();
125
+ const visit = (node, owners) => {
126
+ if (!node || typeof node !== "object") return;
127
+ if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.init && FUNCTIONS.has(node.init.type)) {
128
+ named.set(node.init, node.id.name);
129
+ }
130
+ if ((node.type === "ClassDeclaration" || node.type === "ClassExpression") && node.id) {
131
+ for (const member of node.body.body) if (member.type === "ClassMethod") named.set(member, node.id.name);
132
+ }
133
+ if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
134
+ for (const member of node.body.body) if (member.type === "ClassMethod") classOf.set(member, node);
135
+ const shadow = staticMember(node, "shadow");
136
+ if (shadow?.value && !(shadow.value.type === "BooleanLiteral" && shadow.value.value === false)) {
137
+ plan.shadowClasses.set(node, { node, styles: staticMember(node, "styles"), indexes: [] });
138
+ }
139
+ }
140
+ if (node.type === "TaggedTemplateExpression" && node.tag.type === "Identifier" && tags.html.get(node.tag.name) === "html") {
141
+ const text = node.quasi.quasis.map((q) => q.value.raw).join("");
142
+ if (/<style[\s>]/.test(text)) {
143
+ const at = { line: node.loc?.start.line ?? 0, column: node.loc?.start.column ?? 0 };
144
+ const owner = owners[0];
145
+ if (!owner) plan.diagnostics.push({ ...at, message: "<style> has to be written inside a component." });
146
+ else {
147
+ const id = idFor(owner);
148
+ const shadow = shadowOf(owner);
149
+ plan.templateOwners.set(node.start, { id, component: functionName(owner, named) });
150
+ for (const block of templateBlocks(node.quasi)) compileBlock(block, block.scoped, owner, at);
151
+ }
152
+ }
153
+ }
154
+ const inner = FUNCTIONS.has(node.type) ? [...owners, node] : owners;
155
+ if (node.type === "JSXElement" && isStyle(node)) {
156
+ handle(node, owners);
157
+ return;
158
+ }
159
+ for (const key of Object.keys(node)) {
160
+ if (key === "loc" || key === "leadingComments" || key === "trailingComments" || key === "innerComments") continue;
161
+ const value = node[key];
162
+ if (Array.isArray(value)) for (const v of value) visit(v, inner);
163
+ else if (value && typeof value === "object" && "type" in value) visit(value, inner);
164
+ }
165
+ };
166
+ const handle = (node, owners) => {
167
+ const at = { line: node.loc?.start.line ?? 0, column: node.loc?.start.column ?? 0 };
168
+ const pieces = blockPieces(node);
169
+ if (pieces === null) {
170
+ plan.diagnostics.push({
171
+ ...at,
172
+ message: "<style> needs its CSS as one template literal, <style>{`...`}</style>: JSX reads a bare `{` as an expression."
173
+ });
174
+ return;
175
+ }
176
+ const owner = owners[0];
177
+ if (!owner) {
178
+ plan.diagnostics.push({ ...at, message: "<style> has to be written inside a component." });
179
+ return;
180
+ }
181
+ compileBlock(pieces, isScoped(node), owner, at);
182
+ plan.drop.add(node.start);
183
+ };
184
+ const compileBlock = (pieces, scoped, owner, at) => {
185
+ const component = functionName(owner, named);
186
+ const id = idFor(owner);
187
+ const processed = piecesToNodes(pieces.quasis, pieces.expressions, at, []);
188
+ if (!processed) return;
189
+ let nodes = processed;
190
+ const shadow = shadowOf(owner);
191
+ if (scoped && !shadow) {
192
+ if (!plan.scopes.some((s) => s.value === id)) plan.scopes.push({ start: owner.start, end: owner.end, value: id });
193
+ nodes = scopeNodes(nodes, id, (message) => plan.diagnostics.push({ ...at, message }));
194
+ }
195
+ const printed = module2 ? printStyleWithMap(nodes, options.minify, sourceName, module2, options.importedModules) : { css: printStyle(nodes, options.minify) };
196
+ plan.styles.push({ id, scoped: scoped && !shadow, component, ...printed, ...shadow ? { shadow: true } : {}, ...at });
197
+ if (shadow) shadow.indexes.push(plan.styles.length - 1);
198
+ };
199
+ function idFor(owner) {
200
+ let id = scopeOf.get(owner);
201
+ if (!id) {
202
+ const component = functionName(owner, named);
203
+ const seen = names.get(component) ?? 0;
204
+ names.set(component, seen + 1);
205
+ id = hash(`${sourceName}:${component}${seen ? ":" + seen : ""}`);
206
+ scopeOf.set(owner, id);
207
+ }
208
+ return id;
209
+ }
210
+ visit(program, []);
211
+ return plan;
212
+ }
213
+ function applyTemplateStyles(ir, t) {
214
+ const walk8 = (node) => {
215
+ const children = node.children;
216
+ if (children) {
217
+ for (let i = children.length - 1; i >= 0; i--) {
218
+ const child = children[i];
219
+ if (child.kind === "element" && child.tag === "style" && t.owner) children.splice(i, 1);
220
+ else walk8(child);
221
+ }
222
+ }
223
+ if (t.scope && node.kind === "element" && node.tag !== "style" && node.props && !node.props.some((p) => p.name === SCOPE_ATTR)) {
224
+ node.props.unshift({ name: SCOPE_ATTR, kind: "attr", literal: t.scope });
225
+ }
226
+ };
227
+ walk8(ir);
228
+ }
229
+ function staticMember(cls, name) {
230
+ return cls.body.body.find(
231
+ (m) => m.type === "ClassProperty" && m.static && (m.key.type === "Identifier" && m.key.name === name || m.key.type === "StringLiteral" && m.key.value === name)
232
+ );
233
+ }
234
+ var styleIds = {
235
+ block: (owner, index) => `${owner}.fx-${index}.css`,
236
+ inline: (owner, index) => `${owner}.fx-${index}.css?inline`,
237
+ literal: (owner, name) => `${owner}.fx-literal-${name}.css?inline`
238
+ };
239
+ function finishShadowStyles(plan, code, moduleId) {
240
+ const edits = [];
241
+ const imports = [];
242
+ let next = 0;
243
+ const importOf = (specifier) => {
244
+ const local = `__fxShadow${next++}`;
245
+ imports.push(`import ${local} from ${JSON.stringify(specifier)};
246
+ `);
247
+ return local;
248
+ };
249
+ for (const shadow of plan.shadowClasses.values()) {
250
+ const blocks = shadow.indexes.map((i) => importOf(styleIds.inline(moduleId, i)));
251
+ let value;
252
+ const prop = shadow.styles;
253
+ if (prop?.value) {
254
+ const start = prop.value.start;
255
+ let text = code.slice(start, prop.value.end);
256
+ const replacements = [];
257
+ walkIdentifiers(prop.value, (id) => {
258
+ const local = plan.names?.literals.has(id.name) ? importOf(styleIds.literal(moduleId, id.name)) : plan.names?.imports.has(id.name) && !/\.css(\?|$)/.test(plan.names.imports.get(id.name).source) ? importOf(styleIds.literal(plan.names.imports.get(id.name).source, plan.names.imports.get(id.name).name)) : void 0;
259
+ if (local) replacements.push({ at: id.start - start, length: id.name.length, with: local });
260
+ });
261
+ 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);
262
+ value = text;
263
+ }
264
+ if (!blocks.length && value === void 0) continue;
265
+ if (prop?.value) {
266
+ const merged = blocks.length ? `[${value}, ${blocks.join(", ")}]` : value;
267
+ edits.push({ start: prop.value.start, end: prop.value.end, code: merged });
268
+ } else if (blocks.length) {
269
+ const open = shadow.node.body.start + 1;
270
+ edits.push({ start: open, end: open, code: ` static styles = [${blocks.join(", ")}];` });
271
+ }
272
+ }
273
+ if (imports.length) edits.push({ start: 0, end: 0, code: imports.join("") });
274
+ return edits;
275
+ }
276
+ function walkIdentifiers(node, visit) {
277
+ if (!node || typeof node !== "object") return;
278
+ if (node.type === "Identifier") return visit(node);
279
+ if (node.type === "MemberExpression") return walkIdentifiers(node.object, visit);
280
+ if (node.type === "ObjectProperty") return walkIdentifiers(node.value, visit);
281
+ for (const key of Object.keys(node)) {
282
+ if (key === "loc") continue;
283
+ const value = node[key];
284
+ if (Array.isArray(value)) for (const v of value) walkIdentifiers(v, visit);
285
+ else if (value && typeof value === "object" && "type" in value) walkIdentifiers(value, visit);
286
+ }
287
+ }
288
+ function isStyle(node) {
289
+ const name = node.openingElement.name;
290
+ return name.type === "JSXIdentifier" && name.name === "style";
291
+ }
292
+ function isScoped(node) {
293
+ return node.openingElement.attributes.some((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "scoped");
294
+ }
295
+ function templateBlocks(quasi) {
296
+ const blocks = [];
297
+ let open = null;
298
+ quasi.quasis.forEach((q, i) => {
299
+ const raw = q.value.raw;
300
+ let from = 0;
301
+ while (from <= raw.length) {
302
+ if (!open) {
303
+ const tag = /<style\b([^>]*)>/g;
304
+ tag.lastIndex = from;
305
+ const m = tag.exec(raw);
306
+ if (!m) break;
307
+ open = { quasis: [], expressions: [], scoped: /\bscoped\b/.test(m[1]) };
308
+ from = m.index + m[0].length;
309
+ }
310
+ const close = raw.indexOf("</style", from);
311
+ open.quasis.push(slicePiece(q, raw, from, close === -1 ? raw.length : close));
312
+ if (close === -1) break;
313
+ blocks.push(open);
314
+ open = null;
315
+ from = close;
316
+ }
317
+ const expr = quasi.expressions[i];
318
+ if (open && expr) open.expressions.push(expr);
319
+ });
320
+ return blocks;
321
+ }
322
+ function slicePiece(q, raw, from, to) {
323
+ const text = raw.slice(from, to);
324
+ const start = q.loc?.start;
325
+ if (!start) return { type: "TemplateElement", value: { raw: text, cooked: text } };
326
+ const before = raw.slice(0, from).split("\n");
327
+ const line = start.line + before.length - 1;
328
+ const column = before.length > 1 ? before[before.length - 1].length : start.column + from;
329
+ return { type: "TemplateElement", value: { raw: text, cooked: text }, loc: { start: { line, column } } };
330
+ }
331
+ function blockPieces(node) {
332
+ const meaningful = node.children.filter((c) => !(c.type === "JSXText" && !c.value.trim()));
333
+ if (meaningful.length === 0) return { quasis: [], expressions: [] };
334
+ if (meaningful.length !== 1) return null;
335
+ const only = meaningful[0];
336
+ if (only.type !== "JSXExpressionContainer") return null;
337
+ const expr = only.expression;
338
+ if (expr.type === "StringLiteral") return { quasis: [{ type: "TemplateElement", value: { raw: expr.value, cooked: expr.value } }], expressions: [] };
339
+ if (expr.type === "TemplateLiteral") return { quasis: expr.quasis, expressions: expr.expressions };
340
+ return null;
341
+ }
342
+ function findLiterals(program, modules, plan, intrinsic) {
343
+ const literals = /* @__PURE__ */ new Map();
344
+ const imports = /* @__PURE__ */ new Map();
345
+ const tags = new Set(intrinsic);
346
+ for (const statement of program.body ?? []) {
347
+ if (statement.type !== "ImportDeclaration") continue;
348
+ const source = statement.source.value;
349
+ const specifiers = statement.specifiers;
350
+ for (const sp of specifiers) {
351
+ if (sp.type === "ImportSpecifier") imports.set(sp.local.name, { source, name: sp.imported.name ?? sp.imported.value });
352
+ }
353
+ if (!modules.includes(source)) continue;
354
+ const css = specifiers.find((sp) => sp.type === "ImportSpecifier" && (sp.imported.name ?? sp.imported.value) === "css");
355
+ if (!css) continue;
356
+ const local = css.local.name;
357
+ tags.add(local);
358
+ imports.delete(local);
359
+ plan.edits.push({ start: statement.start, end: statement.end, code: importWithout(statement, css) });
360
+ }
361
+ if (tags.size === 0) return { literals, imports };
362
+ const visit = (node) => {
363
+ if (!node || typeof node !== "object") return;
364
+ if (node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.init?.type === "TaggedTemplateExpression" && node.init.tag.type === "Identifier" && tags.has(node.init.tag.name)) {
365
+ literals.set(node.id.name, node.init);
366
+ }
367
+ for (const key of Object.keys(node)) {
368
+ if (key === "loc") continue;
369
+ const value = node[key];
370
+ if (Array.isArray(value)) for (const v of value) visit(v);
371
+ else if (value && typeof value === "object" && "type" in value) visit(value);
372
+ }
373
+ };
374
+ visit(program);
375
+ return { literals, imports };
376
+ }
377
+ function importWithout(statement, removed) {
378
+ const rest = statement.specifiers.filter((sp) => sp !== removed);
379
+ const source = JSON.stringify(statement.source.value);
380
+ if (rest.length === 0) return "";
381
+ const def = rest.find((sp) => sp.type === "ImportDefaultSpecifier");
382
+ const ns = rest.find((sp) => sp.type === "ImportNamespaceSpecifier");
383
+ const named = rest.filter((sp) => sp.type === "ImportSpecifier").map((sp) => {
384
+ const imported = sp.imported.name ?? JSON.stringify(sp.imported.value);
385
+ return imported === sp.local.name ? imported : `${imported} as ${sp.local.name}`;
386
+ });
387
+ const parts = [def?.local.name, ns ? `* as ${ns.local.name}` : void 0, named.length ? `{ ${named.join(", ")} }` : void 0].filter(Boolean);
388
+ return `import ${parts.join(", ")} from ${source};`;
389
+ }
390
+ function functionName(fn, named) {
391
+ if ((fn.type === "FunctionDeclaration" || fn.type === "FunctionExpression") && fn.id) return fn.id.name;
392
+ return named.get(fn) ?? "anonymous";
393
+ }
394
+ function hash(text) {
395
+ let h = 2166136261;
396
+ for (let i = 0; i < text.length; i++) {
397
+ h ^= text.charCodeAt(i);
398
+ h = Math.imul(h, 16777619);
399
+ }
400
+ return (h >>> 0).toString(36);
401
+ }
402
+ var OPAQUE = /(^|-)keyframes$|^font-face$|^property$|^counter-style$|^page$|^font-feature-values$/;
403
+ function scopeNodes(nodes, id, warn = () => {
404
+ }) {
405
+ return nodes.map((node) => {
406
+ if (node.kind === "rule") return { ...node, selector: scopeSelector(node.selector, id, warn) };
407
+ if (node.kind === "at-rule" && !OPAQUE.test(node.name)) return { ...node, nodes: scopeNodes(node.nodes, id, warn) };
408
+ return node;
409
+ });
410
+ }
411
+ function scopeSelector(selector, id, warn = () => {
412
+ }) {
413
+ return splitTop(selector, ",").map((complex) => scopeComplex(complex.trim(), `[${SCOPE_ATTR}="${id}"]`, warn)).join(", ");
414
+ }
415
+ function scopeComplex(selector, attr, warn) {
416
+ if (/:host\b|::slotted\(/.test(selector)) {
417
+ warn(`\`${selector}\` only means something in a shadow root; in a light-DOM component it is left as written.`);
418
+ return selector;
419
+ }
420
+ const parts = compounds(selector);
421
+ const deep = parts.findIndex((p) => p.text.startsWith(":deep("));
422
+ if (deep !== -1) {
423
+ const inner = unwrap(parts[deep].text, ":deep(");
424
+ const before = parts.slice(0, deep);
425
+ if (before.length === 0) return `${attr} ${inner}`;
426
+ const scopedBefore = scopeAt(before, before.length - 1, attr);
427
+ return `${join(scopedBefore)} ${inner}`;
428
+ }
429
+ let target = -1;
430
+ for (let i = parts.length - 1; i >= 0; i--) {
431
+ if (!parts[i].text.startsWith(":global(")) {
432
+ target = i;
433
+ break;
434
+ }
435
+ }
436
+ const out = target === -1 ? parts : scopeAt(parts, target, attr);
437
+ return join(out.map((p) => p.text.startsWith(":global(") ? { ...p, text: unwrap(p.text, ":global(") } : p));
438
+ }
439
+ function scopeAt(parts, index, attr) {
440
+ return parts.map((p, i) => i === index ? { ...p, text: insertAttr(p.text, attr) } : p);
441
+ }
442
+ function insertAttr(compound, attr) {
443
+ let depth = 0;
444
+ let quote = "";
445
+ for (let i = 0; i < compound.length; i++) {
446
+ const c = compound[i];
447
+ if (quote) {
448
+ if (c === "\\") i++;
449
+ else if (c === quote) quote = "";
450
+ continue;
451
+ }
452
+ if (c === "\\") {
453
+ i++;
454
+ continue;
455
+ }
456
+ if (c === '"' || c === "'") quote = c;
457
+ else if (c === "(" || c === "[") depth++;
458
+ else if (c === ")" || c === "]") depth--;
459
+ else if (c === ":" && depth === 0 && i > 0) return compound.slice(0, i) + attr + compound.slice(i);
460
+ }
461
+ return compound + attr;
462
+ }
463
+ function compounds(selector) {
464
+ const out = [];
465
+ let depth = 0;
466
+ let quote = "";
467
+ let current = "";
468
+ let combinator = "";
469
+ const push = () => {
470
+ if (current.trim()) out.push({ combinator: out.length ? combinator || " " : "", text: current.trim() });
471
+ current = "";
472
+ combinator = "";
473
+ };
474
+ for (let i = 0; i < selector.length; i++) {
475
+ const c = selector[i];
476
+ if (quote) {
477
+ current += c;
478
+ if (c === "\\") current += selector[++i] ?? "";
479
+ else if (c === quote) quote = "";
480
+ continue;
481
+ }
482
+ if (c === "\\") {
483
+ current += c + (selector[++i] ?? "");
484
+ continue;
485
+ }
486
+ if (c === '"' || c === "'") quote = c;
487
+ if (c === "(" || c === "[") depth++;
488
+ if (c === ")" || c === "]") depth--;
489
+ if (depth === 0 && (c === ">" || c === "+" || c === "~")) {
490
+ push();
491
+ combinator = c;
492
+ continue;
493
+ }
494
+ if (depth === 0 && /\s/.test(c)) {
495
+ if (current.trim()) {
496
+ const keep = combinator;
497
+ push();
498
+ combinator = keep;
499
+ }
500
+ continue;
501
+ }
502
+ current += c;
503
+ }
504
+ push();
505
+ return out;
506
+ }
507
+ function join(parts) {
508
+ return parts.map((p, i) => i === 0 ? p.text : p.combinator === " " ? ` ${p.text}` : ` ${p.combinator} ${p.text}`).join("");
509
+ }
510
+ function unwrap(text, prefix) {
511
+ return text.slice(prefix.length, text.lastIndexOf(")")).trim() + text.slice(text.lastIndexOf(")") + 1);
512
+ }
513
+ function splitTop(selector, separator) {
514
+ const parts = [];
515
+ let depth = 0;
516
+ let quote = "";
517
+ let start = 0;
518
+ for (let i = 0; i < selector.length; i++) {
519
+ const c = selector[i];
520
+ if (quote) {
521
+ if (c === "\\") i++;
522
+ else if (c === quote) quote = "";
523
+ } else if (c === '"' || c === "'") quote = c;
524
+ else if (c === "\\") i++;
525
+ else if (c === "(" || c === "[") depth++;
526
+ else if (c === ")" || c === "]") depth--;
527
+ else if (c === separator && depth === 0) {
528
+ parts.push(selector.slice(start, i));
529
+ start = i + 1;
530
+ }
531
+ }
532
+ parts.push(selector.slice(start));
533
+ return parts.filter((p) => p.trim());
534
+ }
535
+ function shiftLoc(node, source, start) {
536
+ 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;
537
+ if (node.kind === "rule" || node.kind === "at-rule") return { ...node, loc, nodes: node.nodes.map((n) => shiftLoc(n, source, start)) };
538
+ return { ...node, loc };
539
+ }
540
+ function keepLocIn(node, modules) {
541
+ const keep = node.loc !== void 0 && modules?.has(node.loc.source) === true;
542
+ const { loc, ...rest } = node;
543
+ const kept = keep ? { ...rest, loc } : rest;
544
+ if (kept.kind === "rule" || kept.kind === "at-rule") return { ...kept, nodes: kept.nodes.map((n) => keepLocIn(n, modules)) };
545
+ return kept;
546
+ }
547
+ function printStyleWithMap(nodes, minify, sourceName, module2, others) {
548
+ const sources = [module2];
549
+ const index = /* @__PURE__ */ new Map([[sourceName, 0]]);
550
+ const sourceOf = (name) => {
551
+ if (index.has(name)) return index.get(name);
552
+ const other = others?.get(name);
553
+ if (!other) return void 0;
554
+ index.set(name, sources.push(other) - 1);
555
+ return index.get(name);
556
+ };
557
+ const lines = [[]];
558
+ let css = "";
559
+ let column = 0;
560
+ const emit = (text) => {
561
+ for (const ch of text) {
562
+ css += ch;
563
+ if (ch === "\n") {
564
+ lines.push([]);
565
+ column = 0;
566
+ } else column++;
567
+ }
568
+ };
569
+ const mark = (loc) => {
570
+ const at = loc ? sourceOf(loc.source) : void 0;
571
+ if (loc && at !== void 0) lines[lines.length - 1].push([column, at, loc.line - 1, loc.column]);
572
+ };
573
+ const write = (node, depth) => {
574
+ const pad = minify ? "" : " ".repeat(depth);
575
+ const nl = minify ? "" : "\n";
576
+ const sp = minify ? "" : " ";
577
+ emit(pad);
578
+ mark(node.loc);
579
+ if (node.kind === "declaration") {
580
+ emit(`${node.property}:${sp}${node.value}${node.important ? `${sp}!important` : ""};${nl}`);
581
+ } else if (node.kind === "raw") {
582
+ emit(`${node.text}${nl}`);
583
+ } else {
584
+ emit(`${node.kind === "rule" ? node.selector : `@${node.name}${node.params ? " " + node.params : ""}`}${sp}{${nl}`);
585
+ for (const child of node.nodes) write(child, depth + 1);
586
+ emit(`${pad}}${nl}`);
587
+ }
588
+ };
589
+ for (const node of nodes) write(node, 0);
590
+ return { css, map: { version: 3, sources: sources.map((s) => s.id), sourcesContent: sources.map((s) => s.code), names: [], mappings: (0, import_sourcemap_codec.encode)(lines) } };
591
+ }
592
+ function printStyle(nodes, minify = false) {
593
+ const out = [];
594
+ const write = (node, depth) => {
595
+ const pad = minify ? "" : " ".repeat(depth);
596
+ const nl = minify ? "" : "\n";
597
+ const sp = minify ? "" : " ";
598
+ if (node.kind === "declaration") {
599
+ out.push(`${pad}${node.property}:${sp}${node.value}${node.important ? `${sp}!important` : ""};${nl}`);
600
+ } else if (node.kind === "raw") {
601
+ out.push(`${pad}${node.text}${nl}`);
602
+ } else {
603
+ const header = node.kind === "rule" ? node.selector : `@${node.name}${node.params ? " " + node.params : ""}`;
604
+ out.push(`${pad}${header}${sp}{${nl}`);
605
+ for (const child of node.nodes) write(child, depth + 1);
606
+ out.push(`${pad}}${nl}`);
607
+ }
608
+ };
609
+ for (const node of nodes) write(node, 0);
610
+ return out.join("");
611
+ }
612
+
39
613
  // src/optimize/analyze-static.ts
40
614
  function propReason(prop) {
41
615
  switch (prop.kind) {
@@ -533,6 +1107,16 @@ var PRIMITIVE_MODULES = [
533
1107
  "@fluixi/reactive/cached-resource",
534
1108
  "@fluixi/core"
535
1109
  ];
1110
+ var TAG_INTRINSICS = {
1111
+ $html: { export: "html", module: "@fluixi/core" },
1112
+ $svg: { export: "svg", module: "@fluixi/core" },
1113
+ $css: { export: "css", module: "@fluixi/core" }
1114
+ };
1115
+ function tagIntrinsics(bound) {
1116
+ const tags = /* @__PURE__ */ new Map();
1117
+ for (const [name, tag] of Object.entries(TAG_INTRINSICS)) if (!bound.has(name)) tags.set(name, tag.export);
1118
+ return tags;
1119
+ }
536
1120
  var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
537
1121
  function mayHaveIntrinsic(code) {
538
1122
  return INTRINSIC_CALL.test(code);
@@ -1036,7 +1620,7 @@ function emitComponent(name, props, children, used, templates, at) {
1036
1620
  }
1037
1621
  let obj = `{ ${bodies.map(([name2, body]) => `get ${propKey(name2)}() { return ${body}; }`).join(", ")} }`;
1038
1622
  if (bodies.some(([, body]) => /\bthis\b/.test(body))) {
1039
- const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => ${body}`);
1623
+ const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => (${body})`);
1040
1624
  const getters = bodies.map(([name2]) => `get ${propKey(name2)}() { return _$t[${JSON.stringify(name2)}](); }`);
1041
1625
  obj = `((_$t) => ({ ${getters.join(", ")} }))({ ${thunks.join(", ")} })`;
1042
1626
  }
@@ -1235,6 +1819,271 @@ function cleanTemplateText(value) {
1235
1819
  return out;
1236
1820
  }
1237
1821
 
1822
+ // src/lower/entities.ts
1823
+ var NAMED = {
1824
+ quot: 34,
1825
+ amp: 38,
1826
+ apos: 39,
1827
+ lt: 60,
1828
+ gt: 62,
1829
+ nbsp: 160,
1830
+ iexcl: 161,
1831
+ cent: 162,
1832
+ pound: 163,
1833
+ curren: 164,
1834
+ yen: 165,
1835
+ brvbar: 166,
1836
+ sect: 167,
1837
+ uml: 168,
1838
+ copy: 169,
1839
+ ordf: 170,
1840
+ laquo: 171,
1841
+ not: 172,
1842
+ shy: 173,
1843
+ reg: 174,
1844
+ macr: 175,
1845
+ deg: 176,
1846
+ plusmn: 177,
1847
+ sup2: 178,
1848
+ sup3: 179,
1849
+ acute: 180,
1850
+ micro: 181,
1851
+ para: 182,
1852
+ middot: 183,
1853
+ cedil: 184,
1854
+ sup1: 185,
1855
+ ordm: 186,
1856
+ raquo: 187,
1857
+ frac14: 188,
1858
+ frac12: 189,
1859
+ frac34: 190,
1860
+ iquest: 191,
1861
+ Agrave: 192,
1862
+ Aacute: 193,
1863
+ Acirc: 194,
1864
+ Atilde: 195,
1865
+ Auml: 196,
1866
+ Aring: 197,
1867
+ AElig: 198,
1868
+ Ccedil: 199,
1869
+ Egrave: 200,
1870
+ Eacute: 201,
1871
+ Ecirc: 202,
1872
+ Euml: 203,
1873
+ Igrave: 204,
1874
+ Iacute: 205,
1875
+ Icirc: 206,
1876
+ Iuml: 207,
1877
+ ETH: 208,
1878
+ Ntilde: 209,
1879
+ Ograve: 210,
1880
+ Oacute: 211,
1881
+ Ocirc: 212,
1882
+ Otilde: 213,
1883
+ Ouml: 214,
1884
+ times: 215,
1885
+ Oslash: 216,
1886
+ Ugrave: 217,
1887
+ Uacute: 218,
1888
+ Ucirc: 219,
1889
+ Uuml: 220,
1890
+ Yacute: 221,
1891
+ THORN: 222,
1892
+ szlig: 223,
1893
+ agrave: 224,
1894
+ aacute: 225,
1895
+ acirc: 226,
1896
+ atilde: 227,
1897
+ auml: 228,
1898
+ aring: 229,
1899
+ aelig: 230,
1900
+ ccedil: 231,
1901
+ egrave: 232,
1902
+ eacute: 233,
1903
+ ecirc: 234,
1904
+ euml: 235,
1905
+ igrave: 236,
1906
+ iacute: 237,
1907
+ icirc: 238,
1908
+ iuml: 239,
1909
+ eth: 240,
1910
+ ntilde: 241,
1911
+ ograve: 242,
1912
+ oacute: 243,
1913
+ ocirc: 244,
1914
+ otilde: 245,
1915
+ ouml: 246,
1916
+ divide: 247,
1917
+ oslash: 248,
1918
+ ugrave: 249,
1919
+ uacute: 250,
1920
+ ucirc: 251,
1921
+ uuml: 252,
1922
+ yacute: 253,
1923
+ thorn: 254,
1924
+ yuml: 255,
1925
+ OElig: 338,
1926
+ oelig: 339,
1927
+ Scaron: 352,
1928
+ scaron: 353,
1929
+ Yuml: 376,
1930
+ fnof: 402,
1931
+ circ: 710,
1932
+ tilde: 732,
1933
+ Alpha: 913,
1934
+ Beta: 914,
1935
+ Gamma: 915,
1936
+ Delta: 916,
1937
+ Epsilon: 917,
1938
+ Zeta: 918,
1939
+ Eta: 919,
1940
+ Theta: 920,
1941
+ Iota: 921,
1942
+ Kappa: 922,
1943
+ Lambda: 923,
1944
+ Mu: 924,
1945
+ Nu: 925,
1946
+ Xi: 926,
1947
+ Omicron: 927,
1948
+ Pi: 928,
1949
+ Rho: 929,
1950
+ Sigma: 931,
1951
+ Tau: 932,
1952
+ Upsilon: 933,
1953
+ Phi: 934,
1954
+ Chi: 935,
1955
+ Psi: 936,
1956
+ Omega: 937,
1957
+ alpha: 945,
1958
+ beta: 946,
1959
+ gamma: 947,
1960
+ delta: 948,
1961
+ epsilon: 949,
1962
+ zeta: 950,
1963
+ eta: 951,
1964
+ theta: 952,
1965
+ iota: 953,
1966
+ kappa: 954,
1967
+ lambda: 955,
1968
+ mu: 956,
1969
+ nu: 957,
1970
+ xi: 958,
1971
+ omicron: 959,
1972
+ pi: 960,
1973
+ rho: 961,
1974
+ sigmaf: 962,
1975
+ sigma: 963,
1976
+ tau: 964,
1977
+ upsilon: 965,
1978
+ phi: 966,
1979
+ chi: 967,
1980
+ psi: 968,
1981
+ omega: 969,
1982
+ thetasym: 977,
1983
+ upsih: 978,
1984
+ piv: 982,
1985
+ ensp: 8194,
1986
+ emsp: 8195,
1987
+ thinsp: 8201,
1988
+ zwnj: 8204,
1989
+ zwj: 8205,
1990
+ lrm: 8206,
1991
+ rlm: 8207,
1992
+ ndash: 8211,
1993
+ mdash: 8212,
1994
+ lsquo: 8216,
1995
+ rsquo: 8217,
1996
+ sbquo: 8218,
1997
+ ldquo: 8220,
1998
+ rdquo: 8221,
1999
+ bdquo: 8222,
2000
+ dagger: 8224,
2001
+ Dagger: 8225,
2002
+ bull: 8226,
2003
+ hellip: 8230,
2004
+ permil: 8240,
2005
+ prime: 8242,
2006
+ Prime: 8243,
2007
+ lsaquo: 8249,
2008
+ rsaquo: 8250,
2009
+ oline: 8254,
2010
+ frasl: 8260,
2011
+ euro: 8364,
2012
+ image: 8465,
2013
+ weierp: 8472,
2014
+ real: 8476,
2015
+ trade: 8482,
2016
+ alefsym: 8501,
2017
+ larr: 8592,
2018
+ uarr: 8593,
2019
+ rarr: 8594,
2020
+ darr: 8595,
2021
+ harr: 8596,
2022
+ crarr: 8629,
2023
+ lArr: 8656,
2024
+ uArr: 8657,
2025
+ rArr: 8658,
2026
+ dArr: 8659,
2027
+ hArr: 8660,
2028
+ forall: 8704,
2029
+ part: 8706,
2030
+ exist: 8707,
2031
+ empty: 8709,
2032
+ nabla: 8711,
2033
+ isin: 8712,
2034
+ notin: 8713,
2035
+ ni: 8715,
2036
+ prod: 8719,
2037
+ sum: 8721,
2038
+ minus: 8722,
2039
+ lowast: 8727,
2040
+ radic: 8730,
2041
+ prop: 8733,
2042
+ infin: 8734,
2043
+ ang: 8736,
2044
+ and: 8743,
2045
+ or: 8744,
2046
+ cap: 8745,
2047
+ cup: 8746,
2048
+ int: 8747,
2049
+ there4: 8756,
2050
+ sim: 8764,
2051
+ cong: 8773,
2052
+ asymp: 8776,
2053
+ ne: 8800,
2054
+ equiv: 8801,
2055
+ le: 8804,
2056
+ ge: 8805,
2057
+ sub: 8834,
2058
+ sup: 8835,
2059
+ nsub: 8836,
2060
+ sube: 8838,
2061
+ supe: 8839,
2062
+ oplus: 8853,
2063
+ otimes: 8855,
2064
+ perp: 8869,
2065
+ sdot: 8901,
2066
+ lceil: 8968,
2067
+ rceil: 8969,
2068
+ lfloor: 8970,
2069
+ rfloor: 8971,
2070
+ lang: 9001,
2071
+ rang: 9002,
2072
+ loz: 9674,
2073
+ spades: 9824,
2074
+ clubs: 9827,
2075
+ hearts: 9829,
2076
+ diams: 9830
2077
+ };
2078
+ var REFERENCE = /&(?:#[xX]([0-9a-fA-F]{1,6})|#([0-9]{1,7})|([a-zA-Z][a-zA-Z0-9]{1,31}));/g;
2079
+ function decodeEntities(value) {
2080
+ if (!value.includes("&")) return value;
2081
+ return value.replace(REFERENCE, (match, hex, dec, name) => {
2082
+ const code = hex ? parseInt(hex, 16) : dec ? parseInt(dec, 10) : Object.prototype.hasOwnProperty.call(NAMED, name) ? NAMED[name] : void 0;
2083
+ return code !== void 0 && code <= 1114111 ? String.fromCodePoint(code) : match;
2084
+ });
2085
+ }
2086
+
1238
2087
  // src/lower/template.ts
1239
2088
  var DELEGATED = /* @__PURE__ */ new Set([
1240
2089
  "click",
@@ -1321,7 +2170,7 @@ var Lowerer = class {
1321
2170
  switch (node.kind) {
1322
2171
  case "Text": {
1323
2172
  if (node.raw) return { kind: "text", value: node.value };
1324
- const cleaned = cleanTemplateText(node.value);
2173
+ const cleaned = decodeEntities(cleanTemplateText(node.value));
1325
2174
  return cleaned ? { kind: "text", value: cleaned } : null;
1326
2175
  }
1327
2176
  case "Comment":
@@ -1357,7 +2206,7 @@ var Lowerer = class {
1357
2206
  kind: "element",
1358
2207
  tag: el.tag,
1359
2208
  svg: el.namespace === "svg",
1360
- props: this.lowerAttributes(el.attributes),
2209
+ props: this.lowerAttributes(el.attributes, true),
1361
2210
  children,
1362
2211
  static: false,
1363
2212
  ...at ? { at } : {}
@@ -1470,13 +2319,22 @@ var Lowerer = class {
1470
2319
  return clone.kind === "Element" ? this.lowerElement(clone) : this.lowerComponent(clone);
1471
2320
  }
1472
2321
  // --- attributes ---------------------------------------------------------
1473
- lowerAttributes(attrs) {
2322
+ /**
2323
+ * `element`: a DOM element, where each class:/style: value that moves is its own accessor.
2324
+ * A component gets the object as before, rebuilt whole when any value moves.
2325
+ */
2326
+ lowerAttributes(attrs, element = false) {
2327
+ let anyReactive = false;
2328
+ const entry = (name, h) => {
2329
+ anyReactive ||= h.reactive;
2330
+ if (!element || !h.reactive) return `${JSON.stringify(name)}: ${h.code}`;
2331
+ const accessor = `() => ${paren(h.code, "arrowBody")}`;
2332
+ return `${JSON.stringify(name)}: ${h.at ? markHole(accessor, h.at.line, h.at.column) : accessor}`;
2333
+ };
1474
2334
  const props = [];
1475
2335
  const classEntries = [];
1476
- let classReactive = false;
1477
2336
  let classAt;
1478
2337
  const styleEntries = [];
1479
- let styleReactive = false;
1480
2338
  let styleAt;
1481
2339
  const usePairs = [];
1482
2340
  for (const a of attrs) {
@@ -1516,15 +2374,13 @@ var Lowerer = class {
1516
2374
  break;
1517
2375
  case "ClassDirective": {
1518
2376
  const h = this.hole(a.hole);
1519
- classEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
1520
- classReactive = classReactive || h.reactive;
2377
+ classEntries.push(entry(a.name, h));
1521
2378
  classAt ??= h.at;
1522
2379
  break;
1523
2380
  }
1524
2381
  case "StyleDirective": {
1525
2382
  const h = this.hole(a.hole);
1526
- styleEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
1527
- styleReactive = styleReactive || h.reactive;
2383
+ styleEntries.push(entry(a.name, h));
1528
2384
  styleAt ??= h.at;
1529
2385
  break;
1530
2386
  }
@@ -1544,18 +2400,19 @@ var Lowerer = class {
1544
2400
  name: "classList",
1545
2401
  kind: "attr",
1546
2402
  expr: `{ ${classEntries.join(", ")} }`,
1547
- reactive: classReactive,
2403
+ reactive: !element && anyReactive,
1548
2404
  ...classAt ? { at: classAt } : {}
1549
2405
  });
1550
2406
  }
1551
2407
  if (styleEntries.length > 0) {
1552
- props.push({
1553
- name: "style",
1554
- kind: "attr",
1555
- expr: `{ ${styleEntries.join(", ")} }`,
1556
- reactive: styleReactive,
1557
- ...styleAt ? { at: styleAt } : {}
1558
- });
2408
+ const entries = `{ ${styleEntries.join(", ")} }`;
2409
+ const base = props.findIndex((p) => p.name === "style");
2410
+ if (base === -1) props.push({ name: "style", kind: "attr", expr: entries, reactive: !element && anyReactive, ...styleAt ? { at: styleAt } : {} });
2411
+ else {
2412
+ const prop = props[base];
2413
+ const value = prop.expr === void 0 ? JSON.stringify(prop.literal) : prop.reactive ? `() => ${paren(prop.expr, "arrowBody")}` : prop.expr;
2414
+ props[base] = { name: "style", kind: "attr", expr: `[${value}, ${entries}]`, reactive: false, ...prop.at ? { at: prop.at } : {} };
2415
+ }
1559
2416
  }
1560
2417
  if (usePairs.length > 0) {
1561
2418
  props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
@@ -1644,7 +2501,7 @@ var Lowerer = class {
1644
2501
  return base;
1645
2502
  }
1646
2503
  if (v.kind === "static") {
1647
- base.literal = v.value;
2504
+ base.literal = decodeEntities(v.value);
1648
2505
  return base;
1649
2506
  }
1650
2507
  if (v.kind === "hole") {
@@ -1657,7 +2514,7 @@ var Lowerer = class {
1657
2514
  let reactive = false;
1658
2515
  let at;
1659
2516
  const body = v.parts.map((p) => {
1660
- if ("text" in p) return escapeTemplate(p.text);
2517
+ if ("text" in p) return escapeTemplate(decodeEntities(p.text));
1661
2518
  const h = this.hole(p.hole);
1662
2519
  reactive = reactive || h.reactive;
1663
2520
  at ??= h.at;
@@ -1682,6 +2539,7 @@ function compilePieces(pieces, holes, options = {}) {
1682
2539
  ...options.sourceFile ? { sourceFile: options.sourceFile } : {},
1683
2540
  ...options.positionAt ? { positionAt: options.positionAt } : {}
1684
2541
  });
2542
+ if (options.templateStyles) applyTemplateStyles(ir, options.templateStyles);
1685
2543
  analyzeStatic(ir);
1686
2544
  resolveComponentSources(ir, {
1687
2545
  resolver: createComponentResolver(options.resolve ?? []),
@@ -1880,9 +2738,12 @@ function attrExprCode(attr, ctx) {
1880
2738
  }
1881
2739
  return null;
1882
2740
  }
1883
- function buildProps(attrs, ctx) {
2741
+ function buildProps(attrs, ctx, element = false) {
1884
2742
  const props = [];
1885
2743
  const usePairs = [];
2744
+ const classEntries = [];
2745
+ const styleEntries = [];
2746
+ let styleAt;
1886
2747
  for (const a of attrs) {
1887
2748
  if (a.type === "JSXSpreadAttribute") {
1888
2749
  props.push({ name: "", kind: "spread", expr: ctx.code(a.argument) });
@@ -1898,6 +2759,27 @@ function buildProps(attrs, ctx) {
1898
2759
  usePairs.push(opts != null ? `[${dir}, () => ${paren(opts, "arrowBody")}]` : `[${dir}]`);
1899
2760
  continue;
1900
2761
  }
2762
+ if (element && (ns === "class" || ns === "style")) {
2763
+ const key = JSON.stringify(a.name.name.name);
2764
+ const v = a.value;
2765
+ let value;
2766
+ if (v == null) value = "true";
2767
+ else if (isString(v)) value = JSON.stringify(literalOf(v));
2768
+ else if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
2769
+ value = keyValue(v.expression, ctx);
2770
+ if (ns === "style") styleAt ??= posOf(v.expression, ctx);
2771
+ } else continue;
2772
+ (ns === "class" ? classEntries : styleEntries).push(`${key}: ${value}`);
2773
+ continue;
2774
+ }
2775
+ if (element && a.name.type === "JSXIdentifier" && a.name.name === "style" && a.value?.type === "JSXExpressionContainer") {
2776
+ const entries = objectEntries(a.value.expression, ctx);
2777
+ if (entries) {
2778
+ styleEntries.unshift(...entries);
2779
+ styleAt = posOf(a.value.expression, ctx);
2780
+ continue;
2781
+ }
2782
+ }
1901
2783
  if (ns === "oncapture") {
1902
2784
  const evt = a.name.name.name.toLowerCase();
1903
2785
  const h = attrExprCode(a, ctx) ?? "undefined";
@@ -1906,9 +2788,37 @@ function buildProps(attrs, ctx) {
1906
2788
  }
1907
2789
  props.push(buildAttr(a, ctx));
1908
2790
  }
2791
+ if (classEntries.length > 0) props.push({ name: "classList", kind: "attr", expr: `{ ${classEntries.join(", ")} }`, reactive: false });
2792
+ if (styleEntries.length > 0) {
2793
+ const entries = `{ ${styleEntries.join(", ")} }`;
2794
+ const base = props.findIndex((p) => p.name === "style" && p.kind === "attr");
2795
+ if (base === -1) props.push({ name: "style", kind: "attr", expr: entries, reactive: false, ...styleAt ? { at: styleAt } : {} });
2796
+ else {
2797
+ const prop = props[base];
2798
+ const value = prop.literal !== void 0 ? JSON.stringify(prop.literal) : prop.reactive ? `() => ${paren(prop.expr, "arrowBody")}` : prop.expr;
2799
+ props[base] = { name: "style", kind: "attr", expr: `[${value}, ${entries}]`, reactive: false, ...prop.at ? { at: prop.at } : {} };
2800
+ }
2801
+ }
1909
2802
  if (usePairs.length > 0) props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
1910
2803
  return props;
1911
2804
  }
2805
+ function keyValue(expr, ctx) {
2806
+ const code = ctx.code(expr);
2807
+ if (!isReactive(expr)) return code;
2808
+ const accessor = `() => ${paren(code, "arrowBody")}`;
2809
+ const at = posOf(expr, ctx);
2810
+ return at ? markHole(accessor, at.line, at.column) : accessor;
2811
+ }
2812
+ function objectEntries(expr, ctx) {
2813
+ if (expr?.type !== "ObjectExpression") return null;
2814
+ const entries = [];
2815
+ for (const p of expr.properties) {
2816
+ if (p.type !== "ObjectProperty") return null;
2817
+ const key = p.computed ? `[${ctx.code(p.key)}]` : p.key.type === "Identifier" ? JSON.stringify(p.key.name) : JSON.stringify(String(p.key.value));
2818
+ entries.push(`${key}: ${keyValue(p.value, ctx)}`);
2819
+ }
2820
+ return entries;
2821
+ }
1912
2822
  function buildChildren(children, ctx) {
1913
2823
  const out = [];
1914
2824
  for (let i = 0; i < children.length; i++) {
@@ -1936,6 +2846,7 @@ function buildChildren(children, ctx) {
1936
2846
  });
1937
2847
  }
1938
2848
  } else if (child.type === "JSXElement" || child.type === "JSXFragment") {
2849
+ if (ctx.dropStyles?.has(child.start)) continue;
1939
2850
  out.push(buildNode(child, ctx));
1940
2851
  } else if (child.type === "JSXSpreadChild") {
1941
2852
  out.push({ kind: "expr", code: ctx.code(child.expression), reactive: false });
@@ -2005,13 +2916,17 @@ function buildElement(node, ctx, handled) {
2005
2916
  const children = each ? [each] : buildChildren(node.children, ctx);
2006
2917
  if (tag === CONTAINER2) return children.length === 1 ? children[0] : { kind: "fragment", children };
2007
2918
  const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
2008
- return { kind: "element", tag, svg: SVG_TAGS.has(tag), props: buildProps(attributes, ctx), children, static: false, ...at ? { at } : {} };
2919
+ const props = buildProps(attributes, ctx, true);
2920
+ const scope = ctx.scopes?.find((s) => node.start >= s.start && node.end <= s.end);
2921
+ if (scope) props.unshift({ name: "data-fx-s", kind: "attr", literal: scope.value });
2922
+ return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
2009
2923
  }
2010
2924
  function buildNode(node, ctx) {
2011
2925
  if (node.type === "JSXFragment") {
2012
2926
  return { kind: "fragment", children: buildChildren(node.children, ctx) };
2013
2927
  }
2014
2928
  if (directive(node, "if")) return buildIf(node, null, ctx);
2929
+ if (ctx.dropStyles?.has(node.start)) return { kind: "fragment", children: [] };
2015
2930
  const { tag, component } = tagInfo(node.openingElement.name, ctx);
2016
2931
  if (!component) return buildElement(node, ctx, []);
2017
2932
  const props = buildProps(node.openingElement.attributes, ctx);
@@ -2517,8 +3432,8 @@ function isJsxRoot(node, parent, key) {
2517
3432
  if (!isJsx(node)) return false;
2518
3433
  return !(parent && isJsx(parent) && key === "children");
2519
3434
  }
2520
- function isTemplateTag(node, litTag) {
2521
- return node.type === "TaggedTemplateExpression" && node.tag?.type === "Identifier" && (node.tag.name === litTag || node.tag.name === "svg");
3435
+ function isTemplateTag(node, tags) {
3436
+ return node.type === "TaggedTemplateExpression" && node.tag?.type === "Identifier" && tags.has(node.tag.name);
2522
3437
  }
2523
3438
  function outermost(list) {
2524
3439
  return list.filter(
@@ -2666,22 +3581,28 @@ function transformTemplates(code, id, options = {}) {
2666
3581
  const wantsIntrinsics = options.intrinsics !== false && mayHaveIntrinsic(code);
2667
3582
  const wantsSources = options.sourceLocations === true && mayHaveSourceMarks(code);
2668
3583
  const sourceName = relativeToRoot(id, options.sourceRoot);
2669
- if (!mayHaveTemplate && !(wantsJsx && code.includes("<")) && !wantsIntrinsics && !wantsSources) return null;
3584
+ const wantsStyles = options.styles !== void 0 && (code.includes("<style") || code.includes("css`"));
3585
+ if (!mayHaveTemplate && !(wantsJsx && code.includes("<")) && !wantsIntrinsics && !wantsSources && !wantsStyles) return null;
2670
3586
  const ast = (0, import_parser2.parse)(code, {
2671
3587
  sourceType: "module",
2672
3588
  plugins: ["jsx", "typescript"],
2673
3589
  errorRecovery: true
2674
3590
  });
3591
+ const intrinsicTags = options.intrinsics === false ? /* @__PURE__ */ new Map() : tagIntrinsics(moduleBindings(ast.program));
3592
+ const templateTags = /* @__PURE__ */ new Map([[litTag, "html"], ["svg", "svg"]]);
3593
+ for (const [name, tag] of intrinsicTags) if (tag !== "css") templateTags.set(name, tag);
3594
+ const cssTags = [...intrinsicTags].filter(([, tag]) => tag === "css").map(([name]) => name);
2675
3595
  const found = [];
2676
3596
  walk7(ast.program, (n, parent, key) => {
2677
- if (wantsLit && isTemplateTag(n, litTag)) found.push(n);
3597
+ if (wantsLit && isTemplateTag(n, templateTags)) found.push(n);
2678
3598
  else if (wantsJsx && isJsxRoot(n, parent, key)) found.push(n);
2679
3599
  });
2680
3600
  const props = options.propsDestructure === false ? { edits: [], diagnostics: [], used: /* @__PURE__ */ new Set() } : collectPropsEdits(ast.program, code, { runtimeModule: options.runtimeModule });
2681
3601
  const intrinsics = options.intrinsics === false ? { edits: [], diagnostics: [], imports: /* @__PURE__ */ new Map() } : collectIntrinsicEdits(ast.program);
2682
3602
  if (intrinsics.edits.length) avoidBindingClashes(ast.program, intrinsics, moduleBindings(ast.program));
2683
3603
  const sources = options.sourceLocations === true ? collectSourceEdits(ast.program, sourceName) : { edits: [], marked: 0 };
2684
- 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;
3604
+ const styles = wantsStyles ? collectStyles(ast.program, sourceName, options.styles, { html: templateTags, css: cssTags }, { id, code }) : void 0;
3605
+ 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;
2685
3606
  if (silent) return null;
2686
3607
  found.sort((a, b) => b.start - a.start || a.end - b.end);
2687
3608
  const used = new Set(props.used);
@@ -2695,20 +3616,32 @@ function transformTemplates(code, id, options = {}) {
2695
3616
  declared: declaredNames(ast.program)
2696
3617
  };
2697
3618
  const hoisted = /* @__PURE__ */ new Map();
2698
- const edits = [...props.edits, ...intrinsics.edits, ...sources.edits];
3619
+ const edits = [...props.edits, ...intrinsics.edits, ...sources.edits, ...styles?.edits ?? []];
2699
3620
  for (const node of found) {
2700
3621
  const nested = found.some(
2701
3622
  (o) => o !== node && o.start <= node.start && o.end >= node.end && o.end - o.start > node.end - node.start
2702
3623
  );
2703
3624
  if (isJsx(node)) {
2704
- edits.push({ start: node.start, end: node.end, code: compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceName) });
3625
+ edits.push({ start: node.start, end: node.end, code: compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceName, styles) });
2705
3626
  continue;
2706
3627
  }
2707
3628
  const holes = node.quasi.expressions.map(
2708
3629
  (e) => bindHole(code, e, edits, options.sourceLocations === true)
2709
3630
  );
3631
+ const scope = styles?.scopes.find((s) => node.start >= s.start && node.end <= s.end)?.value;
3632
+ const owner = styles?.templateOwners.get(node.start);
3633
+ const templateStyles = options.styles && styles && (scope || owner) ? {
3634
+ options: options.styles,
3635
+ ...scope ? { scope } : {},
3636
+ ...owner ? { owner } : {},
3637
+ line: node.loc?.start.line ?? 0,
3638
+ column: node.loc?.start.column ?? 0,
3639
+ styles: styles.styles,
3640
+ diagnostics: styles.diagnostics
3641
+ } : void 0;
2710
3642
  const result = compilePieces(toPieces(node), holes, {
2711
3643
  ...options,
3644
+ ...templateStyles ? { templateStyles } : {},
2712
3645
  hoistTemplates: true,
2713
3646
  // A tag inside a template gets the same mark a JSX tag does. Without one, the nodes it
2714
3647
  // makes are the only ones in the module carrying no mark, and they take whichever was
@@ -2717,12 +3650,13 @@ function transformTemplates(code, id, options = {}) {
2717
3650
  // Same defaults as the plugin, so moving a build over is not a rendering change.
2718
3651
  templateClone: options.templateClone ?? true,
2719
3652
  partialTemplates: nested ? false : options.partialTemplates ?? true,
2720
- svg: node.tag.name === "svg"
3653
+ svg: templateTags.get(node.tag.name) === "svg"
2721
3654
  });
2722
3655
  for (const symbol of result.imports) used.add(symbol);
2723
3656
  collectNeeds(result.ir, needs);
2724
3657
  edits.push({ start: node.start, end: node.end, code: adoptTemplates(result.code, result.templates, hoisted) });
2725
3658
  }
3659
+ if (styles && options.styles) edits.push(...finishShadowStyles(styles, code, id));
2726
3660
  const out = new import_magic_string2.default(code);
2727
3661
  for (const e of outermost(edits)) {
2728
3662
  if (e.start === e.end) out.appendLeft(e.start, e.code);
@@ -2780,13 +3714,19 @@ function transformTemplates(code, id, options = {}) {
2780
3714
  ...loadDiagnostics.length ? { loadDiagnostics } : {},
2781
3715
  ...unresolvedDiagnostics.length ? { unresolvedDiagnostics } : {},
2782
3716
  ...props.diagnostics.length ? { propsDiagnostics: props.diagnostics } : {},
2783
- ...intrinsics.diagnostics.length ? { intrinsicDiagnostics: intrinsics.diagnostics } : {}
3717
+ ...intrinsics.diagnostics.length ? { intrinsicDiagnostics: intrinsics.diagnostics } : {},
3718
+ ...styles?.styles.length ? { styles: styles.styles } : {},
3719
+ ...styles?.diagnostics.length ? { styleDiagnostics: styles.diagnostics } : {},
3720
+ ...styles?.literals.length ? { styleLiterals: styles.literals } : {},
3721
+ ...styles?.references.length ? { styleReferences: styles.references } : {}
2784
3722
  };
2785
3723
  }
2786
- function compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceFile) {
3724
+ function compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceFile, styles) {
2787
3725
  const ir = buildJsxIR(node, {
2788
3726
  code: (n) => slice(code, n.start, n.end, edits),
2789
3727
  used,
3728
+ ...styles?.scopes.length ? { scopes: styles.scopes } : {},
3729
+ ...styles?.drop.size ? { dropStyles: styles.drop } : {},
2790
3730
  ...options.sourceLocations === true && sourceFile ? { sourceFile } : {}
2791
3731
  });
2792
3732
  analyzeStatic(ir);