@fluixi/template-runtime 0.1.0-alpha.3 → 0.1.0-alpha.5

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * CDN entry: the full build the browser runtime with the template compiler in
2
+ * CDN entry: the full build: the browser runtime with the template compiler in
3
3
  * front of it, so `` html`...` `` works on a page that has no build step.
4
4
  *
5
5
  * <script src="https://.../@fluixi/template-runtime/fluixi.global.js"></script>
package/dist/classify.cjs CHANGED
@@ -1 +1,99 @@
1
- "use strict";var s=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(n,e)=>{for(var o in e)s(n,o,{get:e[o],enumerable:!0})},f=(n,e,o,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of c(e))!d.call(n,i)&&i!==o&&s(n,i,{get:()=>e[i],enumerable:!(t=a(e,i))||t.enumerable});return n};var h=n=>f(s({},"__esModule",{value:!0}),n);var u={};p(u,{classify:()=>m});module.exports=h(u);function k(n,e){switch(n.kind){case"Attribute":if(n.value&&n.value.kind==="hole")e.set(n.value.hole,"attr");else if(n.value&&n.value.kind==="mixed")for(let o of n.value.parts)"hole"in o&&e.set(o.hole,"attr");return;case"PropertyBinding":e.set(n.hole,"attr");return;case"BindDirective":e.set(n.hole,"bind");return;case"EventBinding":e.set(n.hole,"event");return;case"RefBinding":e.set(n.hole,"ref");return;case"Spread":e.set(n.hole,"spread");return;case"IfDirective":e.set(n.hole,"control");return;case"EachDirective":e.set(n.hole,"control"),n.key&&"hole"in n.key&&e.set(n.key.hole,"attr");return;default:return}}function l(n,e,o){for(let t of n)switch(t.kind){case"Expression":e.has(t.hole)||e.set(t.hole,"text");break;case"Element":case"Component":{t.kind==="Component"&&(t.tagHole!==null?e.set(t.tagHole,"component"):t.tag&&o.add(t.tag.split(".")[0]));let i=t.attributes.some(r=>r.kind==="EachDirective");for(let r of t.attributes)k(r,e);if(i)for(let r of t.children)r.kind==="Expression"&&e.set(r.hole,"render");l(t.children,e,o);break}case"Root":case"Fragment":l(t.children,e,o);break;default:break}}function m(n){let e=new Map,o=new Set;return l(n.root.children,e,o),{holes:e,components:o}}
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/classify.ts
21
+ var classify_exports = {};
22
+ __export(classify_exports, {
23
+ classify: () => classify
24
+ });
25
+ module.exports = __toCommonJS(classify_exports);
26
+ function attrHoles(attr, out) {
27
+ switch (attr.kind) {
28
+ case "Attribute":
29
+ if (attr.value && attr.value.kind === "hole") out.set(attr.value.hole, "attr");
30
+ else if (attr.value && attr.value.kind === "mixed") {
31
+ for (const part of attr.value.parts) {
32
+ if ("hole" in part) out.set(part.hole, "attr");
33
+ }
34
+ }
35
+ return;
36
+ case "PropertyBinding":
37
+ out.set(attr.hole, "attr");
38
+ return;
39
+ // bindPair() wants the signal itself, so this one is never called.
40
+ case "BindDirective":
41
+ out.set(attr.hole, "bind");
42
+ return;
43
+ case "EventBinding":
44
+ out.set(attr.hole, "event");
45
+ return;
46
+ case "RefBinding":
47
+ out.set(attr.hole, "ref");
48
+ return;
49
+ case "Spread":
50
+ out.set(attr.hole, "spread");
51
+ return;
52
+ case "IfDirective":
53
+ out.set(attr.hole, "control");
54
+ return;
55
+ case "EachDirective":
56
+ out.set(attr.hole, "control");
57
+ if (attr.key && "hole" in attr.key) out.set(attr.key.hole, "attr");
58
+ return;
59
+ default:
60
+ return;
61
+ }
62
+ }
63
+ function walk(nodes, out, tags) {
64
+ for (const node of nodes) {
65
+ switch (node.kind) {
66
+ case "Expression":
67
+ if (!out.has(node.hole)) out.set(node.hole, "text");
68
+ break;
69
+ case "Element":
70
+ case "Component": {
71
+ if (node.kind === "Component") {
72
+ if (node.tagHole !== null) out.set(node.tagHole, "component");
73
+ else if (node.tag) tags.add(node.tag.split(".")[0]);
74
+ }
75
+ const repeats = node.attributes.some((a) => a.kind === "EachDirective");
76
+ for (const attr of node.attributes) attrHoles(attr, out);
77
+ if (repeats) {
78
+ for (const child of node.children) {
79
+ if (child.kind === "Expression") out.set(child.hole, "render");
80
+ }
81
+ }
82
+ walk(node.children, out, tags);
83
+ break;
84
+ }
85
+ case "Root":
86
+ case "Fragment":
87
+ walk(node.children, out, tags);
88
+ break;
89
+ default:
90
+ break;
91
+ }
92
+ }
93
+ }
94
+ function classify(parsed) {
95
+ const holes = /* @__PURE__ */ new Map();
96
+ const components = /* @__PURE__ */ new Set();
97
+ walk(parsed.root.children, holes, components);
98
+ return { holes, components };
99
+ }
package/dist/classify.mjs CHANGED
@@ -1 +1,78 @@
1
- function l(e,n){switch(e.kind){case"Attribute":if(e.value&&e.value.kind==="hole")n.set(e.value.hole,"attr");else if(e.value&&e.value.kind==="mixed")for(let o of e.value.parts)"hole"in o&&n.set(o.hole,"attr");return;case"PropertyBinding":n.set(e.hole,"attr");return;case"BindDirective":n.set(e.hole,"bind");return;case"EventBinding":n.set(e.hole,"event");return;case"RefBinding":n.set(e.hole,"ref");return;case"Spread":n.set(e.hole,"spread");return;case"IfDirective":n.set(e.hole,"control");return;case"EachDirective":n.set(e.hole,"control"),e.key&&"hole"in e.key&&n.set(e.key.hole,"attr");return;default:return}}function r(e,n,o){for(let t of e)switch(t.kind){case"Expression":n.has(t.hole)||n.set(t.hole,"text");break;case"Element":case"Component":{t.kind==="Component"&&(t.tagHole!==null?n.set(t.tagHole,"component"):t.tag&&o.add(t.tag.split(".")[0]));let s=t.attributes.some(i=>i.kind==="EachDirective");for(let i of t.attributes)l(i,n);if(s)for(let i of t.children)i.kind==="Expression"&&n.set(i.hole,"render");r(t.children,n,o);break}case"Root":case"Fragment":r(t.children,n,o);break;default:break}}function a(e){let n=new Map,o=new Set;return r(e.root.children,n,o),{holes:n,components:o}}export{a as classify};
1
+ // src/classify.ts
2
+ function attrHoles(attr, out) {
3
+ switch (attr.kind) {
4
+ case "Attribute":
5
+ if (attr.value && attr.value.kind === "hole") out.set(attr.value.hole, "attr");
6
+ else if (attr.value && attr.value.kind === "mixed") {
7
+ for (const part of attr.value.parts) {
8
+ if ("hole" in part) out.set(part.hole, "attr");
9
+ }
10
+ }
11
+ return;
12
+ case "PropertyBinding":
13
+ out.set(attr.hole, "attr");
14
+ return;
15
+ // bindPair() wants the signal itself, so this one is never called.
16
+ case "BindDirective":
17
+ out.set(attr.hole, "bind");
18
+ return;
19
+ case "EventBinding":
20
+ out.set(attr.hole, "event");
21
+ return;
22
+ case "RefBinding":
23
+ out.set(attr.hole, "ref");
24
+ return;
25
+ case "Spread":
26
+ out.set(attr.hole, "spread");
27
+ return;
28
+ case "IfDirective":
29
+ out.set(attr.hole, "control");
30
+ return;
31
+ case "EachDirective":
32
+ out.set(attr.hole, "control");
33
+ if (attr.key && "hole" in attr.key) out.set(attr.key.hole, "attr");
34
+ return;
35
+ default:
36
+ return;
37
+ }
38
+ }
39
+ function walk(nodes, out, tags) {
40
+ for (const node of nodes) {
41
+ switch (node.kind) {
42
+ case "Expression":
43
+ if (!out.has(node.hole)) out.set(node.hole, "text");
44
+ break;
45
+ case "Element":
46
+ case "Component": {
47
+ if (node.kind === "Component") {
48
+ if (node.tagHole !== null) out.set(node.tagHole, "component");
49
+ else if (node.tag) tags.add(node.tag.split(".")[0]);
50
+ }
51
+ const repeats = node.attributes.some((a) => a.kind === "EachDirective");
52
+ for (const attr of node.attributes) attrHoles(attr, out);
53
+ if (repeats) {
54
+ for (const child of node.children) {
55
+ if (child.kind === "Expression") out.set(child.hole, "render");
56
+ }
57
+ }
58
+ walk(node.children, out, tags);
59
+ break;
60
+ }
61
+ case "Root":
62
+ case "Fragment":
63
+ walk(node.children, out, tags);
64
+ break;
65
+ default:
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ function classify(parsed) {
71
+ const holes = /* @__PURE__ */ new Map();
72
+ const components = /* @__PURE__ */ new Set();
73
+ walk(parsed.root.children, holes, components);
74
+ return { holes, components };
75
+ }
76
+ export {
77
+ classify
78
+ };
package/dist/index.cjs CHANGED
@@ -1 +1,219 @@
1
- "use strict";var T=Object.create;var a=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,N=Object.prototype.hasOwnProperty;var D=(e,n)=>{for(var t in n)a(e,t,{get:n[t],enumerable:!0})},h=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of S(n))!N.call(e,r)&&r!==t&&a(e,r,{get:()=>n[r],enumerable:!(o=E(n,r))||o.enumerable});return e};var O=(e,n,t)=>(t=e!=null?T(A(e)):{},h(n||!e||!e.__esModule?a(t,"default",{value:e,enumerable:!0}):t,e)),B=e=>h(a({},"__esModule",{value:!0}),e);var J={};D(J,{define:()=>L,html:()=>z,svg:()=>G});module.exports=B(J);var j=O(require("@fluixi/dom"),1),y=require("@fluixi/reactive"),k=require("@fluixi/compiler/compile"),v=require("@fluixi/template-parser");function P(e,n){switch(e.kind){case"Attribute":if(e.value&&e.value.kind==="hole")n.set(e.value.hole,"attr");else if(e.value&&e.value.kind==="mixed")for(let t of e.value.parts)"hole"in t&&n.set(t.hole,"attr");return;case"PropertyBinding":n.set(e.hole,"attr");return;case"BindDirective":n.set(e.hole,"bind");return;case"EventBinding":n.set(e.hole,"event");return;case"RefBinding":n.set(e.hole,"ref");return;case"Spread":n.set(e.hole,"spread");return;case"IfDirective":n.set(e.hole,"control");return;case"EachDirective":n.set(e.hole,"control"),e.key&&"hole"in e.key&&n.set(e.key.hole,"attr");return;default:return}}function c(e,n,t){for(let o of e)switch(o.kind){case"Expression":n.has(o.hole)||n.set(o.hole,"text");break;case"Element":case"Component":{o.kind==="Component"&&(o.tagHole!==null?n.set(o.tagHole,"component"):o.tag&&t.add(o.tag.split(".")[0]));let r=o.attributes.some(s=>s.kind==="EachDirective");for(let s of o.attributes)P(s,n);if(r)for(let s of o.children)s.kind==="Expression"&&n.set(s.hole,"render");c(o.children,n,t);break}case"Root":case"Fragment":c(o.children,n,t);break;default:break}}function w(e){let n=new Map,t=new Set;return c(e.root.children,n,t),{holes:n,components:t}}var p={...j,createMemo:y.createMemo},F=["For","Index","Show","Switch","Match","Portal","Dynamic","ErrorBoundary","Suspense"],f=new Map;function L(e){for(let[n,t]of Object.entries(e))f.set(n,t)}function I(e){return e==="text"||e==="attr"||e==="control"}function W(e,n){return e.map((t,o)=>{let r=n.get(o)??"text";return r==="render"?{code:`$$v[${o}]`,reactive:!1,arrow:{params:["$$item"],body:`$$v[${o}]($$item)`,bodyReactive:!1}}:typeof t=="function"&&I(r)?{code:`$$v[${o}]()`,reactive:!0}:{code:`$$v[${o}]`,reactive:!1}})}function U(e){if(f.has(e))return f.get(e);let n=globalThis[e];if(n!==void 0)return n;throw new Error(`[fluixi] <${e}> is not defined. Pass it as a hole — <\${${e}} /> — or register it with define({ ${e} }).`)}function _(e,n,t){let o=e,r=(0,v.parse)(o,{svg:t}),s=r.diagnostics.filter(i=>i.severity==="error");if(s.length>0)throw new Error(`[fluixi] ${t?"svg":"html"}\`\`: ${s.map(i=>i.message).join("; ")}`);let{holes:$,components:b}=w(r),{code:H,imports:d}=(0,k.compileTemplate)({statics:o,holes:W(n,$)},{svg:t}),u=d.filter(i=>!(i in p));if(u.length>0)throw new Error(`[fluixi] no runtime binding for: ${u.join(", ")}`);let l=Array.from(new Set([...d,...F])).filter(i=>i in p),m=Array.from(b).filter(i=>!l.includes(i)),M=[...l,...m],C=new Function(...M,"$$v",`return (${H});`);return{run:i=>C(...l.map(R=>p[R]),...m.map(U),i)}}var g=new WeakMap;function q(e){let n="";for(let t of e)n+=typeof t=="function"?"f":"v";return n}function x(e){return(n,...t)=>{let o=g.get(n);o||(o=new Map,g.set(n,o));let r=q(t),s=o.get(r);return s||(s=_(n,t,e),o.set(r,s)),s.run(t)}}var z=x(!1),G=x(!0);
1
+ /*! @fluixi/template-runtime v0.1.0-alpha.5 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/index.ts
32
+ var index_exports = {};
33
+ __export(index_exports, {
34
+ define: () => define,
35
+ html: () => html,
36
+ svg: () => svg
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+ var DOM = __toESM(require("@fluixi/dom"), 1);
40
+ var import_reactive = require("@fluixi/reactive");
41
+ var import_compile = require("@fluixi/compiler/compile");
42
+ var import_template_parser = require("@fluixi/template-parser");
43
+
44
+ // src/classify.ts
45
+ function attrHoles(attr, out) {
46
+ switch (attr.kind) {
47
+ case "Attribute":
48
+ if (attr.value && attr.value.kind === "hole") out.set(attr.value.hole, "attr");
49
+ else if (attr.value && attr.value.kind === "mixed") {
50
+ for (const part of attr.value.parts) {
51
+ if ("hole" in part) out.set(part.hole, "attr");
52
+ }
53
+ }
54
+ return;
55
+ case "PropertyBinding":
56
+ out.set(attr.hole, "attr");
57
+ return;
58
+ // bindPair() wants the signal itself, so this one is never called.
59
+ case "BindDirective":
60
+ out.set(attr.hole, "bind");
61
+ return;
62
+ case "EventBinding":
63
+ out.set(attr.hole, "event");
64
+ return;
65
+ case "RefBinding":
66
+ out.set(attr.hole, "ref");
67
+ return;
68
+ case "Spread":
69
+ out.set(attr.hole, "spread");
70
+ return;
71
+ case "IfDirective":
72
+ out.set(attr.hole, "control");
73
+ return;
74
+ case "EachDirective":
75
+ out.set(attr.hole, "control");
76
+ if (attr.key && "hole" in attr.key) out.set(attr.key.hole, "attr");
77
+ return;
78
+ default:
79
+ return;
80
+ }
81
+ }
82
+ function walk(nodes, out, tags) {
83
+ for (const node of nodes) {
84
+ switch (node.kind) {
85
+ case "Expression":
86
+ if (!out.has(node.hole)) out.set(node.hole, "text");
87
+ break;
88
+ case "Element":
89
+ case "Component": {
90
+ if (node.kind === "Component") {
91
+ if (node.tagHole !== null) out.set(node.tagHole, "component");
92
+ else if (node.tag) tags.add(node.tag.split(".")[0]);
93
+ }
94
+ const repeats = node.attributes.some((a) => a.kind === "EachDirective");
95
+ for (const attr of node.attributes) attrHoles(attr, out);
96
+ if (repeats) {
97
+ for (const child of node.children) {
98
+ if (child.kind === "Expression") out.set(child.hole, "render");
99
+ }
100
+ }
101
+ walk(node.children, out, tags);
102
+ break;
103
+ }
104
+ case "Root":
105
+ case "Fragment":
106
+ walk(node.children, out, tags);
107
+ break;
108
+ default:
109
+ break;
110
+ }
111
+ }
112
+ }
113
+ function classify(parsed) {
114
+ const holes = /* @__PURE__ */ new Map();
115
+ const components = /* @__PURE__ */ new Set();
116
+ walk(parsed.root.children, holes, components);
117
+ return { holes, components };
118
+ }
119
+
120
+ // src/index.ts
121
+ var RUNTIME = {
122
+ ...DOM,
123
+ createMemo: import_reactive.createMemo
124
+ };
125
+ var CONTROL_FLOW = [
126
+ "For",
127
+ "Index",
128
+ "Show",
129
+ "Switch",
130
+ "Match",
131
+ "Portal",
132
+ "Dynamic",
133
+ "ErrorBoundary",
134
+ "Suspense"
135
+ ];
136
+ var registry = /* @__PURE__ */ new Map();
137
+ function define(components) {
138
+ for (const [name, value] of Object.entries(components)) registry.set(name, value);
139
+ }
140
+ function reads(context) {
141
+ return context === "text" || context === "attr" || context === "control";
142
+ }
143
+ function describe(values, holes) {
144
+ return values.map((value, i) => {
145
+ const context = holes.get(i) ?? "text";
146
+ if (context === "render") {
147
+ return {
148
+ code: `$$v[${i}]`,
149
+ reactive: false,
150
+ arrow: { params: ["$$item"], body: `$$v[${i}]($$item)`, bodyReactive: false }
151
+ };
152
+ }
153
+ if (typeof value === "function" && reads(context)) {
154
+ return { code: `$$v[${i}]()`, reactive: true };
155
+ }
156
+ return { code: `$$v[${i}]`, reactive: false };
157
+ });
158
+ }
159
+ function resolveComponent(name) {
160
+ if (registry.has(name)) return registry.get(name);
161
+ const global = globalThis[name];
162
+ if (global !== void 0) return global;
163
+ throw new Error(
164
+ `[fluixi] <${name}> is not defined. Pass it as a hole — <\${${name}} /> — or register it with define({ ${name} }).`
165
+ );
166
+ }
167
+ function build(strings, values, svg2) {
168
+ const statics = strings;
169
+ const parsed = (0, import_template_parser.parse)(statics, { svg: svg2 });
170
+ const fatal = parsed.diagnostics.filter((d) => d.severity === "error");
171
+ if (fatal.length > 0) {
172
+ throw new Error(
173
+ `[fluixi] ${svg2 ? "svg" : "html"}\`\`: ${fatal.map((d) => d.message).join("; ")}`
174
+ );
175
+ }
176
+ const { holes, components } = classify(parsed);
177
+ const { code, imports } = (0, import_compile.compileTemplate)({ statics, holes: describe(values, holes) }, { svg: svg2 });
178
+ const missing = imports.filter((n) => !(n in RUNTIME));
179
+ if (missing.length > 0) {
180
+ throw new Error(`[fluixi] no runtime binding for: ${missing.join(", ")}`);
181
+ }
182
+ const helpers = Array.from(/* @__PURE__ */ new Set([...imports, ...CONTROL_FLOW])).filter(
183
+ (n) => n in RUNTIME
184
+ );
185
+ const tags = Array.from(components).filter((n) => !helpers.includes(n));
186
+ const names = [...helpers, ...tags];
187
+ const factory = new Function(...names, "$$v", `return (${code});`);
188
+ return {
189
+ run: (vals) => factory(
190
+ ...helpers.map((n) => RUNTIME[n]),
191
+ ...tags.map(resolveComponent),
192
+ vals
193
+ )
194
+ };
195
+ }
196
+ var cache = /* @__PURE__ */ new WeakMap();
197
+ function shapeOf(values) {
198
+ let shape = "";
199
+ for (const value of values) shape += typeof value === "function" ? "f" : "v";
200
+ return shape;
201
+ }
202
+ function tag(svg2) {
203
+ return (strings, ...values) => {
204
+ let shapes = cache.get(strings);
205
+ if (!shapes) {
206
+ shapes = /* @__PURE__ */ new Map();
207
+ cache.set(strings, shapes);
208
+ }
209
+ const shape = shapeOf(values);
210
+ let compiled = shapes.get(shape);
211
+ if (!compiled) {
212
+ compiled = build(strings, values, svg2);
213
+ shapes.set(shape, compiled);
214
+ }
215
+ return compiled.run(values);
216
+ };
217
+ }
218
+ var html = tag(false);
219
+ var svg = tag(true);
package/dist/index.js CHANGED
@@ -79,7 +79,7 @@ function build(strings, values, svg) {
79
79
  }
80
80
  // The same call site hands back the same TemplateStringsArray on every
81
81
  // evaluation, so it keys the cache. The emitted code differs by whether each hole
82
- // held an accessor, though, and a site can be called both ways so the shape is
82
+ // held an accessor, though, and a site can be called both ways, so the shape is
83
83
  // part of the key rather than something the first call fixes for good.
84
84
  const cache = new WeakMap();
85
85
  function shapeOf(values) {
package/dist/index.mjs CHANGED
@@ -1 +1,189 @@
1
- import*as H from"@fluixi/dom";import{createMemo as M}from"@fluixi/reactive";import{compileTemplate as C}from"@fluixi/compiler/compile";import{parse as R}from"@fluixi/template-parser";function b(e,n){switch(e.kind){case"Attribute":if(e.value&&e.value.kind==="hole")n.set(e.value.hole,"attr");else if(e.value&&e.value.kind==="mixed")for(let o of e.value.parts)"hole"in o&&n.set(o.hole,"attr");return;case"PropertyBinding":n.set(e.hole,"attr");return;case"BindDirective":n.set(e.hole,"bind");return;case"EventBinding":n.set(e.hole,"event");return;case"RefBinding":n.set(e.hole,"ref");return;case"Spread":n.set(e.hole,"spread");return;case"IfDirective":n.set(e.hole,"control");return;case"EachDirective":n.set(e.hole,"control"),e.key&&"hole"in e.key&&n.set(e.key.hole,"attr");return;default:return}}function l(e,n,o){for(let t of e)switch(t.kind){case"Expression":n.has(t.hole)||n.set(t.hole,"text");break;case"Element":case"Component":{t.kind==="Component"&&(t.tagHole!==null?n.set(t.tagHole,"component"):t.tag&&o.add(t.tag.split(".")[0]));let i=t.attributes.some(r=>r.kind==="EachDirective");for(let r of t.attributes)b(r,n);if(i)for(let r of t.children)r.kind==="Expression"&&n.set(r.hole,"render");l(t.children,n,o);break}case"Root":case"Fragment":l(t.children,n,o);break;default:break}}function m(e){let n=new Map,o=new Set;return l(e.root.children,n,o),{holes:n,components:o}}var c={...H,createMemo:M},T=["For","Index","Show","Switch","Match","Portal","Dynamic","ErrorBoundary","Suspense"],p=new Map;function L(e){for(let[n,o]of Object.entries(e))p.set(n,o)}function E(e){return e==="text"||e==="attr"||e==="control"}function S(e,n){return e.map((o,t)=>{let i=n.get(t)??"text";return i==="render"?{code:`$$v[${t}]`,reactive:!1,arrow:{params:["$$item"],body:`$$v[${t}]($$item)`,bodyReactive:!1}}:typeof o=="function"&&E(i)?{code:`$$v[${t}]()`,reactive:!0}:{code:`$$v[${t}]`,reactive:!1}})}function A(e){if(p.has(e))return p.get(e);let n=globalThis[e];if(n!==void 0)return n;throw new Error(`[fluixi] <${e}> is not defined. Pass it as a hole — <\${${e}} /> — or register it with define({ ${e} }).`)}function N(e,n,o){let t=e,i=R(t,{svg:o}),r=i.diagnostics.filter(s=>s.severity==="error");if(r.length>0)throw new Error(`[fluixi] ${o?"svg":"html"}\`\`: ${r.map(s=>s.message).join("; ")}`);let{holes:g,components:y}=m(i),{code:k,imports:f}=C({statics:t,holes:S(n,g)},{svg:o}),d=f.filter(s=>!(s in c));if(d.length>0)throw new Error(`[fluixi] no runtime binding for: ${d.join(", ")}`);let a=Array.from(new Set([...f,...T])).filter(s=>s in c),u=Array.from(y).filter(s=>!a.includes(s)),v=[...a,...u],x=new Function(...v,"$$v",`return (${k});`);return{run:s=>x(...a.map($=>c[$]),...u.map(A),s)}}var h=new WeakMap;function D(e){let n="";for(let o of e)n+=typeof o=="function"?"f":"v";return n}function w(e){return(n,...o)=>{let t=h.get(n);t||(t=new Map,h.set(n,t));let i=D(o),r=t.get(i);return r||(r=N(n,o,e),t.set(i,r)),r.run(o)}}var I=w(!1),W=w(!0);export{L as define,I as html,W as svg};
1
+ /*! @fluixi/template-runtime v0.1.0-alpha.5 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+
3
+ // src/index.ts
4
+ import * as DOM from "@fluixi/dom";
5
+ import { createMemo } from "@fluixi/reactive";
6
+ import { compileTemplate } from "@fluixi/compiler/compile";
7
+ import { parse } from "@fluixi/template-parser";
8
+
9
+ // src/classify.ts
10
+ function attrHoles(attr, out) {
11
+ switch (attr.kind) {
12
+ case "Attribute":
13
+ if (attr.value && attr.value.kind === "hole") out.set(attr.value.hole, "attr");
14
+ else if (attr.value && attr.value.kind === "mixed") {
15
+ for (const part of attr.value.parts) {
16
+ if ("hole" in part) out.set(part.hole, "attr");
17
+ }
18
+ }
19
+ return;
20
+ case "PropertyBinding":
21
+ out.set(attr.hole, "attr");
22
+ return;
23
+ // bindPair() wants the signal itself, so this one is never called.
24
+ case "BindDirective":
25
+ out.set(attr.hole, "bind");
26
+ return;
27
+ case "EventBinding":
28
+ out.set(attr.hole, "event");
29
+ return;
30
+ case "RefBinding":
31
+ out.set(attr.hole, "ref");
32
+ return;
33
+ case "Spread":
34
+ out.set(attr.hole, "spread");
35
+ return;
36
+ case "IfDirective":
37
+ out.set(attr.hole, "control");
38
+ return;
39
+ case "EachDirective":
40
+ out.set(attr.hole, "control");
41
+ if (attr.key && "hole" in attr.key) out.set(attr.key.hole, "attr");
42
+ return;
43
+ default:
44
+ return;
45
+ }
46
+ }
47
+ function walk(nodes, out, tags) {
48
+ for (const node of nodes) {
49
+ switch (node.kind) {
50
+ case "Expression":
51
+ if (!out.has(node.hole)) out.set(node.hole, "text");
52
+ break;
53
+ case "Element":
54
+ case "Component": {
55
+ if (node.kind === "Component") {
56
+ if (node.tagHole !== null) out.set(node.tagHole, "component");
57
+ else if (node.tag) tags.add(node.tag.split(".")[0]);
58
+ }
59
+ const repeats = node.attributes.some((a) => a.kind === "EachDirective");
60
+ for (const attr of node.attributes) attrHoles(attr, out);
61
+ if (repeats) {
62
+ for (const child of node.children) {
63
+ if (child.kind === "Expression") out.set(child.hole, "render");
64
+ }
65
+ }
66
+ walk(node.children, out, tags);
67
+ break;
68
+ }
69
+ case "Root":
70
+ case "Fragment":
71
+ walk(node.children, out, tags);
72
+ break;
73
+ default:
74
+ break;
75
+ }
76
+ }
77
+ }
78
+ function classify(parsed) {
79
+ const holes = /* @__PURE__ */ new Map();
80
+ const components = /* @__PURE__ */ new Set();
81
+ walk(parsed.root.children, holes, components);
82
+ return { holes, components };
83
+ }
84
+
85
+ // src/index.ts
86
+ var RUNTIME = {
87
+ ...DOM,
88
+ createMemo
89
+ };
90
+ var CONTROL_FLOW = [
91
+ "For",
92
+ "Index",
93
+ "Show",
94
+ "Switch",
95
+ "Match",
96
+ "Portal",
97
+ "Dynamic",
98
+ "ErrorBoundary",
99
+ "Suspense"
100
+ ];
101
+ var registry = /* @__PURE__ */ new Map();
102
+ function define(components) {
103
+ for (const [name, value] of Object.entries(components)) registry.set(name, value);
104
+ }
105
+ function reads(context) {
106
+ return context === "text" || context === "attr" || context === "control";
107
+ }
108
+ function describe(values, holes) {
109
+ return values.map((value, i) => {
110
+ const context = holes.get(i) ?? "text";
111
+ if (context === "render") {
112
+ return {
113
+ code: `$$v[${i}]`,
114
+ reactive: false,
115
+ arrow: { params: ["$$item"], body: `$$v[${i}]($$item)`, bodyReactive: false }
116
+ };
117
+ }
118
+ if (typeof value === "function" && reads(context)) {
119
+ return { code: `$$v[${i}]()`, reactive: true };
120
+ }
121
+ return { code: `$$v[${i}]`, reactive: false };
122
+ });
123
+ }
124
+ function resolveComponent(name) {
125
+ if (registry.has(name)) return registry.get(name);
126
+ const global = globalThis[name];
127
+ if (global !== void 0) return global;
128
+ throw new Error(
129
+ `[fluixi] <${name}> is not defined. Pass it as a hole — <\${${name}} /> — or register it with define({ ${name} }).`
130
+ );
131
+ }
132
+ function build(strings, values, svg2) {
133
+ const statics = strings;
134
+ const parsed = parse(statics, { svg: svg2 });
135
+ const fatal = parsed.diagnostics.filter((d) => d.severity === "error");
136
+ if (fatal.length > 0) {
137
+ throw new Error(
138
+ `[fluixi] ${svg2 ? "svg" : "html"}\`\`: ${fatal.map((d) => d.message).join("; ")}`
139
+ );
140
+ }
141
+ const { holes, components } = classify(parsed);
142
+ const { code, imports } = compileTemplate({ statics, holes: describe(values, holes) }, { svg: svg2 });
143
+ const missing = imports.filter((n) => !(n in RUNTIME));
144
+ if (missing.length > 0) {
145
+ throw new Error(`[fluixi] no runtime binding for: ${missing.join(", ")}`);
146
+ }
147
+ const helpers = Array.from(/* @__PURE__ */ new Set([...imports, ...CONTROL_FLOW])).filter(
148
+ (n) => n in RUNTIME
149
+ );
150
+ const tags = Array.from(components).filter((n) => !helpers.includes(n));
151
+ const names = [...helpers, ...tags];
152
+ const factory = new Function(...names, "$$v", `return (${code});`);
153
+ return {
154
+ run: (vals) => factory(
155
+ ...helpers.map((n) => RUNTIME[n]),
156
+ ...tags.map(resolveComponent),
157
+ vals
158
+ )
159
+ };
160
+ }
161
+ var cache = /* @__PURE__ */ new WeakMap();
162
+ function shapeOf(values) {
163
+ let shape = "";
164
+ for (const value of values) shape += typeof value === "function" ? "f" : "v";
165
+ return shape;
166
+ }
167
+ function tag(svg2) {
168
+ return (strings, ...values) => {
169
+ let shapes = cache.get(strings);
170
+ if (!shapes) {
171
+ shapes = /* @__PURE__ */ new Map();
172
+ cache.set(strings, shapes);
173
+ }
174
+ const shape = shapeOf(values);
175
+ let compiled = shapes.get(shape);
176
+ if (!compiled) {
177
+ compiled = build(strings, values, svg2);
178
+ shapes.set(shape, compiled);
179
+ }
180
+ return compiled.run(values);
181
+ };
182
+ }
183
+ var html = tag(false);
184
+ var svg = tag(true);
185
+ export {
186
+ define,
187
+ html,
188
+ svg
189
+ };