jskelet 0.6.1 → 0.6.3

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 (155) hide show
  1. package/AGENTS.md +136 -136
  2. package/CHANGELOG.md +620 -596
  3. package/LICENSE +21 -21
  4. package/bin/jskelet.mjs +130 -130
  5. package/docs/01-baslangic.md +291 -291
  6. package/docs/02-mimari.md +310 -309
  7. package/docs/03-routing.md +515 -515
  8. package/docs/04-render-ve-sablonlar.md +661 -661
  9. package/docs/05-islands.md +486 -486
  10. package/docs/06-cache.md +1443 -1423
  11. package/docs/07-yapilandirma.md +12 -6
  12. package/docs/08-build.md +429 -428
  13. package/docs/09-dev-araclari.md +364 -364
  14. package/docs/10-dagitim.md +338 -338
  15. package/docs/12-panel-ve-oturum.md +478 -478
  16. package/docs/README.md +83 -83
  17. package/docs/en/01-getting-started.md +298 -298
  18. package/docs/en/02-architecture.md +329 -328
  19. package/docs/en/03-routing.md +531 -531
  20. package/docs/en/04-rendering.md +669 -669
  21. package/docs/en/05-islands.md +497 -497
  22. package/docs/en/06-caching.md +1453 -1431
  23. package/docs/en/07-configuration.md +1219 -1214
  24. package/docs/en/08-build.md +447 -446
  25. package/docs/en/09-dev-tools.md +373 -373
  26. package/docs/en/10-deployment.md +340 -340
  27. package/docs/en/11-migration.md +398 -398
  28. package/docs/en/12-dashboards-and-sessions.md +488 -488
  29. package/docs/en/README.md +87 -87
  30. package/package.json +137 -137
  31. package/src/build/ensure-build.mjs +19 -19
  32. package/src/build/paths.mjs +153 -153
  33. package/src/build/resolve-peer.mjs +36 -36
  34. package/src/build/tasks/client.mjs +349 -349
  35. package/src/build/tasks/css.mjs +235 -235
  36. package/src/build/tasks/fonts.mjs +146 -146
  37. package/src/build/tasks/icons.mjs +357 -357
  38. package/src/build/tasks/images.mjs +244 -244
  39. package/src/build/tasks/precompress.mjs +78 -78
  40. package/src/build/tasks/templates.mjs +20 -20
  41. package/src/client/admin/i18n.js +764 -764
  42. package/src/client/admin/login.html +74 -74
  43. package/src/client/admin/panel.css +809 -809
  44. package/src/client/admin/panel.html +495 -495
  45. package/src/client/admin/panel.js +1251 -1251
  46. package/src/client/devtools/report.html +185 -185
  47. package/src/client/devtools/report.js +745 -745
  48. package/src/client/devtools/seo.js +628 -628
  49. package/src/client/dom.js +95 -95
  50. package/src/client/form.js +192 -192
  51. package/src/client/index.js +45 -45
  52. package/src/client/registry.js +305 -305
  53. package/src/client/safe-image.js +91 -91
  54. package/src/client/shared-cookie.js +225 -225
  55. package/src/client/store.js +36 -36
  56. package/src/client/swap.js +188 -188
  57. package/src/compile/codegen.js +336 -336
  58. package/src/compile/compile-all.js +149 -149
  59. package/src/compile/errors.js +66 -66
  60. package/src/compile/expr.js +409 -409
  61. package/src/compile/index.js +17 -17
  62. package/src/compile/parse.js +541 -541
  63. package/src/compile/resolve.js +211 -211
  64. package/src/compile/scan-exports.js +51 -51
  65. package/src/config/defaults.js +17 -1
  66. package/src/config/index.js +13 -0
  67. package/src/config/pattern.js +107 -107
  68. package/src/generate.mjs +163 -163
  69. package/src/http/control-flow.js +71 -71
  70. package/src/http/cookies-entry.js +21 -21
  71. package/src/http/cookies.js +277 -277
  72. package/src/http/request-cache.js +46 -46
  73. package/src/http/request-context.js +165 -165
  74. package/src/http/shared-cookie.js +178 -178
  75. package/src/index.js +101 -101
  76. package/src/init.mjs +230 -230
  77. package/src/migrate/apply.mjs +262 -262
  78. package/src/migrate/babel.mjs +79 -79
  79. package/src/migrate/classify.mjs +155 -155
  80. package/src/migrate/config.mjs +126 -126
  81. package/src/migrate/fs-walk.mjs +191 -191
  82. package/src/migrate/parse.mjs +26 -26
  83. package/src/migrate/scan.mjs +177 -177
  84. package/src/migrate/transform/expr-source.mjs +168 -168
  85. package/src/migrate/transform/island.mjs +67 -67
  86. package/src/migrate/transform/jsx-to-component.mjs +302 -302
  87. package/src/migrate/transform/jsx-to-jsk.mjs +330 -330
  88. package/src/migrate/transform/page-split.mjs +435 -435
  89. package/src/migrate/write.mjs +81 -81
  90. package/src/migrate.mjs +171 -171
  91. package/src/runtime/alias-hooks.mjs +119 -119
  92. package/src/runtime/register.mjs +4 -4
  93. package/src/server/admin/actions.js +229 -229
  94. package/src/server/admin/auth.js +125 -125
  95. package/src/server/admin/event-log.js +151 -151
  96. package/src/server/admin/gate.js +209 -209
  97. package/src/server/admin/inventory.js +188 -188
  98. package/src/server/admin/mount.js +56 -56
  99. package/src/server/admin/router.js +216 -216
  100. package/src/server/admin/snapshot.js +241 -241
  101. package/src/server/assets.js +147 -147
  102. package/src/server/auth/handoff.js +309 -309
  103. package/src/server/cache-blob.js +70 -0
  104. package/src/server/cache-deps.js +42 -42
  105. package/src/server/cache-vary.js +113 -113
  106. package/src/server/cloudflare.js +607 -607
  107. package/src/server/create-app.js +366 -366
  108. package/src/server/data-cache.js +553 -462
  109. package/src/server/dev/report.js +485 -485
  110. package/src/server/dev/socket.js +170 -170
  111. package/src/server/dev/version-check.mjs +139 -139
  112. package/src/server/disk-cache.js +233 -0
  113. package/src/server/ejs-adapter.js +59 -59
  114. package/src/server/html-cache.js +1196 -1122
  115. package/src/server/image-optimizer.js +500 -407
  116. package/src/server/logs/access-middleware.js +66 -66
  117. package/src/server/logs/file-sink.js +193 -66
  118. package/src/server/logs/pipeline.js +165 -158
  119. package/src/server/logs/s3-put.js +214 -214
  120. package/src/server/logs/s3-sink.js +112 -112
  121. package/src/server/metadata.js +102 -102
  122. package/src/server/middleware/compression.js +205 -205
  123. package/src/server/middleware/csrf.js +134 -134
  124. package/src/server/middleware/dev-gate.js +75 -75
  125. package/src/server/middleware/headers.js +37 -37
  126. package/src/server/middleware/redirects.js +32 -32
  127. package/src/server/middleware/robots-txt.js +341 -341
  128. package/src/server/middleware/static-precompressed.js +121 -100
  129. package/src/server/middleware/trailing-slash.js +53 -53
  130. package/src/server/middleware/upstream-proxy.js +141 -141
  131. package/src/server/og-image.js +356 -356
  132. package/src/server/port-guard.js +255 -255
  133. package/src/server/prewarm.js +1082 -1058
  134. package/src/server/redis.js +588 -569
  135. package/src/server/render.js +4 -4
  136. package/src/server/router.js +157 -157
  137. package/src/server/status-page.js +265 -265
  138. package/src/server/upstream-limiter.js +376 -376
  139. package/src/server/upstream-tracking.js +166 -166
  140. package/src/shared/cookie-domain.js +66 -66
  141. package/src/start.mjs +22 -22
  142. package/src/templates/layout.ejs +30 -30
  143. package/src/templates/layout.jsk +30 -30
  144. package/src/version.mjs +31 -31
  145. package/src/views/components/loader.js +101 -101
  146. package/src/views/helpers/html.js +102 -102
  147. package/src/views/helpers/tags.js +375 -375
  148. package/types/config/defaults.d.ts +15 -1
  149. package/types/config/index.d.ts +8 -0
  150. package/types/server/cache-blob.d.ts +13 -0
  151. package/types/server/data-cache.d.ts +9 -0
  152. package/types/server/disk-cache.d.ts +36 -0
  153. package/types/server/html-cache.d.ts +26 -3
  154. package/types/server/logs/file-sink.d.ts +16 -5
  155. package/types/server/redis.d.ts +2 -1
@@ -1,336 +1,336 @@
1
- /**
2
- * AST → ESM kaynak metni. `eval` / `new Function` yok; çıktı diskteki
3
- * `.mjs` dosyasına yazılır ve runtime'da normal `import` ile yüklenir.
4
- */
5
- import { codegenExpr } from "./expr.js";
6
- import { CompileError } from "./errors.js";
7
-
8
- /**
9
- * @typedef {import('./parse.js').AstNode} AstNode
10
- * @typedef {import('./parse.js').AttrNode} AttrNode
11
- */
12
-
13
- /**
14
- * @param {AstNode[]} ast
15
- * @param {{ viewId: string, file?: string, knownComponents?: Set<string> }} options
16
- * @returns {{ code: string, includes: string[], components: string[] }}
17
- */
18
- export function codegen(ast, options) {
19
- const ctx = new CodegenContext(options);
20
- ctx.emitNodes(ast);
21
- return {
22
- code: ctx.finish(),
23
- includes: [...ctx.includes],
24
- includeEntries: ctx.includeEntries,
25
- components: [...ctx.components],
26
- };
27
- }
28
-
29
- /**
30
- * Include yolunu view id'ye çevir (uzantısız, `/` ayırıcılı).
31
- * @param {string} includePath
32
- * @returns {string}
33
- */
34
- export function normalizeIncludeId(includePath) {
35
- return includePath.replace(/\\/g, "/").replace(/\.jsk$/i, "").replace(/^\.\//, "");
36
- }
37
-
38
- /**
39
- * `market-hero` / `MarketHero` → `MarketHero`
40
- * @param {string} name
41
- * @returns {string}
42
- */
43
- export function toPascalCase(name) {
44
- if (/^[A-Z]/.test(name) && !name.includes("-")) return name;
45
- return name
46
- .split(/[-_/]+/)
47
- .filter(Boolean)
48
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
49
- .join("");
50
- }
51
-
52
- class CodegenContext {
53
- /**
54
- * @param {{ viewId: string, file?: string, knownComponents?: Set<string> }} options
55
- */
56
- constructor(options) {
57
- this.viewId = options.viewId;
58
- this.file = options.file;
59
- this.knownComponents = options.knownComponents ?? null;
60
- /** @type {Set<string>} */
61
- this.includes = new Set();
62
- /** @type {{ id: string, index: number }[]} */
63
- this.includeEntries = [];
64
- /** @type {Set<string>} */
65
- this.components = new Set();
66
- /** @type {string[]} */
67
- this.body = [];
68
- this.indent = 1;
69
- this.temp = 0;
70
- /** @type {Set<string>} */
71
- this.locals = new Set();
72
- /** Çocuk gövdesinde `s` yerine kullanılacak değişken. */
73
- this.outVar = "s";
74
- }
75
-
76
- /**
77
- * @param {string} line
78
- */
79
- line(line) {
80
- this.body.push(`${" ".repeat(this.indent)}${line}`);
81
- }
82
-
83
- /**
84
- * @param {string} exprJs
85
- */
86
- append(exprJs) {
87
- this.line(`${this.outVar} += ${exprJs};`);
88
- }
89
-
90
- /** @returns {string} */
91
- nextTemp() {
92
- this.temp += 1;
93
- return `__c${this.temp}`;
94
- }
95
-
96
- /**
97
- * @param {AstNode[]} nodes
98
- */
99
- emitNodes(nodes) {
100
- for (const node of nodes) this.emit(node);
101
- }
102
-
103
- /**
104
- * @param {AstNode} node
105
- */
106
- emit(node) {
107
- switch (node.type) {
108
- case "Text":
109
- if (node.value) this.append(JSON.stringify(node.value));
110
- break;
111
- case "Interp": {
112
- const expr = codegenExpr(node.expr, this.locals);
113
- if (node.raw) this.append(`(${expr} ?? "")`);
114
- else this.append(`helpers.esc(${expr})`);
115
- break;
116
- }
117
- case "Element":
118
- this.emitElement(node);
119
- break;
120
- case "Component":
121
- this.emitComponent(node);
122
- break;
123
- case "If":
124
- this.emitIf(node);
125
- break;
126
- case "Each":
127
- this.emitEach(node);
128
- break;
129
- case "Include":
130
- this.emitInclude(node);
131
- break;
132
- default:
133
- break;
134
- }
135
- }
136
-
137
- /**
138
- * @param {import('./parse.js').ElementNode} node
139
- */
140
- emitElement(node) {
141
- const bound = node.attrs.some((a) => a.bound);
142
- if (bound) {
143
- this.append(
144
- `"<${node.name}" + helpers.attrs({${this.attrsObject(node.attrs)}}) + ">"`,
145
- );
146
- } else {
147
- this.append(JSON.stringify(`<${node.name}${staticAttrString(node.attrs)}>`));
148
- }
149
-
150
- if (!node.selfClosing) {
151
- this.emitNodes(node.children);
152
- this.append(JSON.stringify(`</${node.name}>`));
153
- }
154
- }
155
-
156
- /**
157
- * @param {AttrNode[]} attrs
158
- * @returns {string}
159
- */
160
- attrsObject(attrs) {
161
- return attrs
162
- .map((attr) => {
163
- const key = JSON.stringify(attr.name);
164
- if (attr.boolean && !attr.bound) return `${key}: true`;
165
- if (attr.bound && attr.expr) {
166
- return `${key}: ${codegenExpr(attr.expr, this.locals)}`;
167
- }
168
- return `${key}: ${JSON.stringify(attr.value ?? true)}`;
169
- })
170
- .join(", ");
171
- }
172
-
173
- /**
174
- * @param {import('./parse.js').ComponentNode} node
175
- */
176
- emitComponent(node) {
177
- if (this.knownComponents && !this.knownComponents.has(node.name)) {
178
- throw new CompileError(`Unknown component "${node.name}"`, {
179
- file: this.file,
180
- index: node.index,
181
- });
182
- }
183
- this.components.add(node.name);
184
-
185
- const props = this.attrsObject(node.attrs);
186
-
187
- if (node.children.length) {
188
- const tmp = this.nextTemp();
189
- this.line(`{`);
190
- this.indent += 1;
191
- this.line(`let ${tmp} = "";`);
192
- const prevOut = this.outVar;
193
- this.outVar = tmp;
194
- this.emitNodes(node.children);
195
- this.outVar = prevOut;
196
- const propsWithChildren = props
197
- ? `${props}, children: ${tmp}`
198
- : `children: ${tmp}`;
199
- this.append(`(helpers.${node.name}({ ${propsWithChildren} }) ?? "")`);
200
- this.indent -= 1;
201
- this.line(`}`);
202
- } else {
203
- this.append(`(helpers.${node.name}({ ${props} }) ?? "")`);
204
- }
205
- }
206
-
207
- /**
208
- * @param {import('./parse.js').IfNode} node
209
- */
210
- emitIf(node) {
211
- const test = codegenExpr(node.test, this.locals);
212
- this.line(`if (${test}) {`);
213
- this.indent += 1;
214
- this.emitNodes(node.consequent);
215
- this.indent -= 1;
216
- if (node.alternate.length) {
217
- this.line(`} else {`);
218
- this.indent += 1;
219
- this.emitNodes(node.alternate);
220
- this.indent -= 1;
221
- }
222
- this.line(`}`);
223
- }
224
-
225
- /**
226
- * @param {import('./parse.js').EachNode} node
227
- */
228
- emitEach(node) {
229
- const list = codegenExpr(node.list, this.locals);
230
- const idx = node.indexName ?? `__i${++this.temp}`;
231
- this.line(`{`);
232
- this.indent += 1;
233
- this.line(`const __list = ${list} ?? [];`);
234
- this.line(`for (let ${idx} = 0; ${idx} < __list.length; ${idx}++) {`);
235
- this.indent += 1;
236
- this.line(`const ${node.item} = __list[${idx}];`);
237
- const prev = new Set(this.locals);
238
- this.locals.add(node.item);
239
- this.locals.add(idx);
240
- if (node.indexName) this.locals.add(node.indexName);
241
- this.emitNodes(node.children);
242
- this.locals = prev;
243
- this.indent -= 1;
244
- this.line(`}`);
245
- this.indent -= 1;
246
- this.line(`}`);
247
- }
248
-
249
- /**
250
- * @param {import('./parse.js').IncludeNode} node
251
- */
252
- emitInclude(node) {
253
- const id = normalizeIncludeId(node.path);
254
- this.includes.add(id);
255
- this.includeEntries.push({ id, index: node.index });
256
- const alias = includeAlias(id);
257
- this.append(`(${alias}(data, helpers) ?? "")`);
258
- }
259
-
260
- /** @returns {string} */
261
- finish() {
262
- const imports = [...this.includes]
263
- .map((id) => {
264
- const alias = includeAlias(id);
265
- const rel = relativeImport(this.viewId, id);
266
- return `import { render as ${alias} } from ${JSON.stringify(rel)};`;
267
- })
268
- .join("\n");
269
-
270
- const parts = [
271
- "/** @generated by jskelet — do not edit */",
272
- imports,
273
- "/**",
274
- " * @param {Record<string, unknown>} data",
275
- " * @param {Record<string, any>} helpers",
276
- " * @returns {string}",
277
- " */",
278
- "export function render(data, helpers) {",
279
- ' let s = "";',
280
- ...this.body,
281
- " return s;",
282
- "}",
283
- "",
284
- ];
285
-
286
- return parts.filter((l, i) => !(l === "" && i === 1 && !imports)).join("\n");
287
- }
288
- }
289
-
290
- /**
291
- * @param {AttrNode[]} attrs
292
- * @returns {string}
293
- */
294
- function staticAttrString(attrs) {
295
- let out = "";
296
- for (const attr of attrs) {
297
- if (attr.boolean) {
298
- out += ` ${attr.name}`;
299
- continue;
300
- }
301
- out += ` ${attr.name}="${escapeAttr(attr.value ?? "")}"`;
302
- }
303
- return out;
304
- }
305
-
306
- /**
307
- * @param {string} value
308
- * @returns {string}
309
- */
310
- function escapeAttr(value) {
311
- return String(value)
312
- .replace(/&/g, "&amp;")
313
- .replace(/"/g, "&quot;")
314
- .replace(/</g, "&lt;");
315
- }
316
-
317
- /**
318
- * @param {string} id
319
- * @returns {string}
320
- */
321
- function includeAlias(id) {
322
- return `__inc_${id.replace(/[^A-Za-z0-9]/g, "_")}`;
323
- }
324
-
325
- /**
326
- * @param {string} fromViewId
327
- * @param {string} toViewId
328
- * @returns {string}
329
- */
330
- function relativeImport(fromViewId, toViewId) {
331
- const fromParts = fromViewId.split("/");
332
- fromParts.pop();
333
- const depth = fromParts.length;
334
- const prefix = depth === 0 ? "./" : "../".repeat(depth);
335
- return `${prefix}${toViewId}.mjs`;
336
- }
1
+ /**
2
+ * AST → ESM kaynak metni. `eval` / `new Function` yok; çıktı diskteki
3
+ * `.mjs` dosyasına yazılır ve runtime'da normal `import` ile yüklenir.
4
+ */
5
+ import { codegenExpr } from "./expr.js";
6
+ import { CompileError } from "./errors.js";
7
+
8
+ /**
9
+ * @typedef {import('./parse.js').AstNode} AstNode
10
+ * @typedef {import('./parse.js').AttrNode} AttrNode
11
+ */
12
+
13
+ /**
14
+ * @param {AstNode[]} ast
15
+ * @param {{ viewId: string, file?: string, knownComponents?: Set<string> }} options
16
+ * @returns {{ code: string, includes: string[], components: string[] }}
17
+ */
18
+ export function codegen(ast, options) {
19
+ const ctx = new CodegenContext(options);
20
+ ctx.emitNodes(ast);
21
+ return {
22
+ code: ctx.finish(),
23
+ includes: [...ctx.includes],
24
+ includeEntries: ctx.includeEntries,
25
+ components: [...ctx.components],
26
+ };
27
+ }
28
+
29
+ /**
30
+ * Include yolunu view id'ye çevir (uzantısız, `/` ayırıcılı).
31
+ * @param {string} includePath
32
+ * @returns {string}
33
+ */
34
+ export function normalizeIncludeId(includePath) {
35
+ return includePath.replace(/\\/g, "/").replace(/\.jsk$/i, "").replace(/^\.\//, "");
36
+ }
37
+
38
+ /**
39
+ * `market-hero` / `MarketHero` → `MarketHero`
40
+ * @param {string} name
41
+ * @returns {string}
42
+ */
43
+ export function toPascalCase(name) {
44
+ if (/^[A-Z]/.test(name) && !name.includes("-")) return name;
45
+ return name
46
+ .split(/[-_/]+/)
47
+ .filter(Boolean)
48
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
49
+ .join("");
50
+ }
51
+
52
+ class CodegenContext {
53
+ /**
54
+ * @param {{ viewId: string, file?: string, knownComponents?: Set<string> }} options
55
+ */
56
+ constructor(options) {
57
+ this.viewId = options.viewId;
58
+ this.file = options.file;
59
+ this.knownComponents = options.knownComponents ?? null;
60
+ /** @type {Set<string>} */
61
+ this.includes = new Set();
62
+ /** @type {{ id: string, index: number }[]} */
63
+ this.includeEntries = [];
64
+ /** @type {Set<string>} */
65
+ this.components = new Set();
66
+ /** @type {string[]} */
67
+ this.body = [];
68
+ this.indent = 1;
69
+ this.temp = 0;
70
+ /** @type {Set<string>} */
71
+ this.locals = new Set();
72
+ /** Çocuk gövdesinde `s` yerine kullanılacak değişken. */
73
+ this.outVar = "s";
74
+ }
75
+
76
+ /**
77
+ * @param {string} line
78
+ */
79
+ line(line) {
80
+ this.body.push(`${" ".repeat(this.indent)}${line}`);
81
+ }
82
+
83
+ /**
84
+ * @param {string} exprJs
85
+ */
86
+ append(exprJs) {
87
+ this.line(`${this.outVar} += ${exprJs};`);
88
+ }
89
+
90
+ /** @returns {string} */
91
+ nextTemp() {
92
+ this.temp += 1;
93
+ return `__c${this.temp}`;
94
+ }
95
+
96
+ /**
97
+ * @param {AstNode[]} nodes
98
+ */
99
+ emitNodes(nodes) {
100
+ for (const node of nodes) this.emit(node);
101
+ }
102
+
103
+ /**
104
+ * @param {AstNode} node
105
+ */
106
+ emit(node) {
107
+ switch (node.type) {
108
+ case "Text":
109
+ if (node.value) this.append(JSON.stringify(node.value));
110
+ break;
111
+ case "Interp": {
112
+ const expr = codegenExpr(node.expr, this.locals);
113
+ if (node.raw) this.append(`(${expr} ?? "")`);
114
+ else this.append(`helpers.esc(${expr})`);
115
+ break;
116
+ }
117
+ case "Element":
118
+ this.emitElement(node);
119
+ break;
120
+ case "Component":
121
+ this.emitComponent(node);
122
+ break;
123
+ case "If":
124
+ this.emitIf(node);
125
+ break;
126
+ case "Each":
127
+ this.emitEach(node);
128
+ break;
129
+ case "Include":
130
+ this.emitInclude(node);
131
+ break;
132
+ default:
133
+ break;
134
+ }
135
+ }
136
+
137
+ /**
138
+ * @param {import('./parse.js').ElementNode} node
139
+ */
140
+ emitElement(node) {
141
+ const bound = node.attrs.some((a) => a.bound);
142
+ if (bound) {
143
+ this.append(
144
+ `"<${node.name}" + helpers.attrs({${this.attrsObject(node.attrs)}}) + ">"`,
145
+ );
146
+ } else {
147
+ this.append(JSON.stringify(`<${node.name}${staticAttrString(node.attrs)}>`));
148
+ }
149
+
150
+ if (!node.selfClosing) {
151
+ this.emitNodes(node.children);
152
+ this.append(JSON.stringify(`</${node.name}>`));
153
+ }
154
+ }
155
+
156
+ /**
157
+ * @param {AttrNode[]} attrs
158
+ * @returns {string}
159
+ */
160
+ attrsObject(attrs) {
161
+ return attrs
162
+ .map((attr) => {
163
+ const key = JSON.stringify(attr.name);
164
+ if (attr.boolean && !attr.bound) return `${key}: true`;
165
+ if (attr.bound && attr.expr) {
166
+ return `${key}: ${codegenExpr(attr.expr, this.locals)}`;
167
+ }
168
+ return `${key}: ${JSON.stringify(attr.value ?? true)}`;
169
+ })
170
+ .join(", ");
171
+ }
172
+
173
+ /**
174
+ * @param {import('./parse.js').ComponentNode} node
175
+ */
176
+ emitComponent(node) {
177
+ if (this.knownComponents && !this.knownComponents.has(node.name)) {
178
+ throw new CompileError(`Unknown component "${node.name}"`, {
179
+ file: this.file,
180
+ index: node.index,
181
+ });
182
+ }
183
+ this.components.add(node.name);
184
+
185
+ const props = this.attrsObject(node.attrs);
186
+
187
+ if (node.children.length) {
188
+ const tmp = this.nextTemp();
189
+ this.line(`{`);
190
+ this.indent += 1;
191
+ this.line(`let ${tmp} = "";`);
192
+ const prevOut = this.outVar;
193
+ this.outVar = tmp;
194
+ this.emitNodes(node.children);
195
+ this.outVar = prevOut;
196
+ const propsWithChildren = props
197
+ ? `${props}, children: ${tmp}`
198
+ : `children: ${tmp}`;
199
+ this.append(`(helpers.${node.name}({ ${propsWithChildren} }) ?? "")`);
200
+ this.indent -= 1;
201
+ this.line(`}`);
202
+ } else {
203
+ this.append(`(helpers.${node.name}({ ${props} }) ?? "")`);
204
+ }
205
+ }
206
+
207
+ /**
208
+ * @param {import('./parse.js').IfNode} node
209
+ */
210
+ emitIf(node) {
211
+ const test = codegenExpr(node.test, this.locals);
212
+ this.line(`if (${test}) {`);
213
+ this.indent += 1;
214
+ this.emitNodes(node.consequent);
215
+ this.indent -= 1;
216
+ if (node.alternate.length) {
217
+ this.line(`} else {`);
218
+ this.indent += 1;
219
+ this.emitNodes(node.alternate);
220
+ this.indent -= 1;
221
+ }
222
+ this.line(`}`);
223
+ }
224
+
225
+ /**
226
+ * @param {import('./parse.js').EachNode} node
227
+ */
228
+ emitEach(node) {
229
+ const list = codegenExpr(node.list, this.locals);
230
+ const idx = node.indexName ?? `__i${++this.temp}`;
231
+ this.line(`{`);
232
+ this.indent += 1;
233
+ this.line(`const __list = ${list} ?? [];`);
234
+ this.line(`for (let ${idx} = 0; ${idx} < __list.length; ${idx}++) {`);
235
+ this.indent += 1;
236
+ this.line(`const ${node.item} = __list[${idx}];`);
237
+ const prev = new Set(this.locals);
238
+ this.locals.add(node.item);
239
+ this.locals.add(idx);
240
+ if (node.indexName) this.locals.add(node.indexName);
241
+ this.emitNodes(node.children);
242
+ this.locals = prev;
243
+ this.indent -= 1;
244
+ this.line(`}`);
245
+ this.indent -= 1;
246
+ this.line(`}`);
247
+ }
248
+
249
+ /**
250
+ * @param {import('./parse.js').IncludeNode} node
251
+ */
252
+ emitInclude(node) {
253
+ const id = normalizeIncludeId(node.path);
254
+ this.includes.add(id);
255
+ this.includeEntries.push({ id, index: node.index });
256
+ const alias = includeAlias(id);
257
+ this.append(`(${alias}(data, helpers) ?? "")`);
258
+ }
259
+
260
+ /** @returns {string} */
261
+ finish() {
262
+ const imports = [...this.includes]
263
+ .map((id) => {
264
+ const alias = includeAlias(id);
265
+ const rel = relativeImport(this.viewId, id);
266
+ return `import { render as ${alias} } from ${JSON.stringify(rel)};`;
267
+ })
268
+ .join("\n");
269
+
270
+ const parts = [
271
+ "/** @generated by jskelet — do not edit */",
272
+ imports,
273
+ "/**",
274
+ " * @param {Record<string, unknown>} data",
275
+ " * @param {Record<string, any>} helpers",
276
+ " * @returns {string}",
277
+ " */",
278
+ "export function render(data, helpers) {",
279
+ ' let s = "";',
280
+ ...this.body,
281
+ " return s;",
282
+ "}",
283
+ "",
284
+ ];
285
+
286
+ return parts.filter((l, i) => !(l === "" && i === 1 && !imports)).join("\n");
287
+ }
288
+ }
289
+
290
+ /**
291
+ * @param {AttrNode[]} attrs
292
+ * @returns {string}
293
+ */
294
+ function staticAttrString(attrs) {
295
+ let out = "";
296
+ for (const attr of attrs) {
297
+ if (attr.boolean) {
298
+ out += ` ${attr.name}`;
299
+ continue;
300
+ }
301
+ out += ` ${attr.name}="${escapeAttr(attr.value ?? "")}"`;
302
+ }
303
+ return out;
304
+ }
305
+
306
+ /**
307
+ * @param {string} value
308
+ * @returns {string}
309
+ */
310
+ function escapeAttr(value) {
311
+ return String(value)
312
+ .replace(/&/g, "&amp;")
313
+ .replace(/"/g, "&quot;")
314
+ .replace(/</g, "&lt;");
315
+ }
316
+
317
+ /**
318
+ * @param {string} id
319
+ * @returns {string}
320
+ */
321
+ function includeAlias(id) {
322
+ return `__inc_${id.replace(/[^A-Za-z0-9]/g, "_")}`;
323
+ }
324
+
325
+ /**
326
+ * @param {string} fromViewId
327
+ * @param {string} toViewId
328
+ * @returns {string}
329
+ */
330
+ function relativeImport(fromViewId, toViewId) {
331
+ const fromParts = fromViewId.split("/");
332
+ fromParts.pop();
333
+ const depth = fromParts.length;
334
+ const prefix = depth === 0 ? "./" : "../".repeat(depth);
335
+ return `${prefix}${toViewId}.mjs`;
336
+ }