jskelet 0.6.3 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +136 -136
- package/CHANGELOG.md +628 -620
- package/LICENSE +21 -21
- package/README.md +2 -0
- package/bin/jskelet.mjs +130 -130
- package/docs/01-baslangic.md +291 -291
- package/docs/02-mimari.md +310 -310
- package/docs/03-routing.md +515 -515
- package/docs/04-render-ve-sablonlar.md +667 -661
- package/docs/05-islands.md +486 -486
- package/docs/06-cache.md +1467 -1443
- package/docs/07-yapilandirma.md +1208 -1197
- package/docs/08-build.md +429 -429
- package/docs/09-dev-araclari.md +364 -364
- package/docs/10-dagitim.md +348 -338
- package/docs/12-panel-ve-oturum.md +479 -478
- package/docs/README.md +83 -83
- package/docs/en/01-getting-started.md +298 -298
- package/docs/en/02-architecture.md +329 -329
- package/docs/en/03-routing.md +531 -531
- package/docs/en/04-rendering.md +675 -669
- package/docs/en/05-islands.md +497 -497
- package/docs/en/06-caching.md +1476 -1453
- package/docs/en/07-configuration.md +1229 -1219
- package/docs/en/08-build.md +447 -447
- package/docs/en/09-dev-tools.md +373 -373
- package/docs/en/10-deployment.md +351 -340
- package/docs/en/11-migration.md +398 -398
- package/docs/en/12-dashboards-and-sessions.md +489 -488
- package/docs/en/README.md +87 -87
- package/package.json +137 -137
- package/src/build/ensure-build.mjs +19 -19
- package/src/build/paths.mjs +153 -153
- package/src/build/resolve-peer.mjs +36 -36
- package/src/build/tasks/client.mjs +349 -349
- package/src/build/tasks/css.mjs +235 -235
- package/src/build/tasks/fonts.mjs +146 -146
- package/src/build/tasks/icons.mjs +357 -357
- package/src/build/tasks/images.mjs +244 -244
- package/src/build/tasks/precompress.mjs +78 -78
- package/src/build/tasks/templates.mjs +20 -20
- package/src/client/admin/i18n.js +764 -764
- package/src/client/admin/login.html +74 -74
- package/src/client/admin/panel.css +809 -809
- package/src/client/admin/panel.html +495 -495
- package/src/client/admin/panel.js +1251 -1251
- package/src/client/devtools/report.html +185 -185
- package/src/client/devtools/report.js +745 -745
- package/src/client/devtools/seo.js +628 -628
- package/src/client/dom.js +95 -95
- package/src/client/form.js +192 -192
- package/src/client/index.js +45 -45
- package/src/client/registry.js +305 -305
- package/src/client/safe-image.js +91 -91
- package/src/client/shared-cookie.js +225 -225
- package/src/client/store.js +36 -36
- package/src/client/swap.js +188 -188
- package/src/compile/codegen.js +336 -336
- package/src/compile/compile-all.js +149 -149
- package/src/compile/errors.js +66 -66
- package/src/compile/expr.js +409 -409
- package/src/compile/index.js +17 -17
- package/src/compile/parse.js +541 -541
- package/src/compile/resolve.js +211 -211
- package/src/compile/scan-exports.js +51 -51
- package/src/config/defaults.js +541 -534
- package/src/config/index.js +1500 -1469
- package/src/config/pattern.js +107 -107
- package/src/generate.mjs +163 -163
- package/src/http/control-flow.js +71 -71
- package/src/http/cookies-entry.js +21 -21
- package/src/http/cookies.js +277 -277
- package/src/http/request-cache.js +46 -46
- package/src/http/request-context.js +165 -165
- package/src/http/shared-cookie.js +178 -178
- package/src/index.js +101 -101
- package/src/init.mjs +232 -230
- package/src/migrate/apply.mjs +262 -262
- package/src/migrate/babel.mjs +79 -79
- package/src/migrate/classify.mjs +155 -155
- package/src/migrate/config.mjs +126 -126
- package/src/migrate/fs-walk.mjs +191 -191
- package/src/migrate/parse.mjs +26 -26
- package/src/migrate/scan.mjs +177 -177
- package/src/migrate/transform/expr-source.mjs +168 -168
- package/src/migrate/transform/island.mjs +67 -67
- package/src/migrate/transform/jsx-to-component.mjs +302 -302
- package/src/migrate/transform/jsx-to-jsk.mjs +330 -330
- package/src/migrate/transform/page-split.mjs +435 -435
- package/src/migrate/write.mjs +81 -81
- package/src/migrate.mjs +171 -171
- package/src/runtime/alias-hooks.mjs +119 -119
- package/src/runtime/register.mjs +4 -4
- package/src/server/admin/actions.js +229 -229
- package/src/server/admin/auth.js +125 -125
- package/src/server/admin/event-log.js +151 -151
- package/src/server/admin/gate.js +209 -209
- package/src/server/admin/inventory.js +188 -188
- package/src/server/admin/mount.js +56 -56
- package/src/server/admin/router.js +216 -216
- package/src/server/admin/snapshot.js +241 -241
- package/src/server/assets.js +147 -147
- package/src/server/auth/handoff.js +309 -309
- package/src/server/cache-blob.js +70 -70
- package/src/server/cache-control.js +45 -0
- package/src/server/cache-deps.js +42 -42
- package/src/server/cache-vary.js +113 -113
- package/src/server/cloudflare.js +607 -607
- package/src/server/create-app.js +366 -366
- package/src/server/data-cache.js +553 -553
- package/src/server/dev/report.js +485 -485
- package/src/server/dev/socket.js +170 -170
- package/src/server/dev/version-check.mjs +139 -139
- package/src/server/disk-cache.js +233 -233
- package/src/server/ejs-adapter.js +59 -59
- package/src/server/html-cache.js +1196 -1196
- package/src/server/image-optimizer.js +500 -500
- package/src/server/logs/access-middleware.js +66 -66
- package/src/server/logs/file-sink.js +193 -193
- package/src/server/logs/pipeline.js +165 -165
- package/src/server/logs/s3-put.js +214 -214
- package/src/server/logs/s3-sink.js +112 -112
- package/src/server/metadata.js +102 -102
- package/src/server/middleware/compression.js +205 -205
- package/src/server/middleware/csrf.js +134 -134
- package/src/server/middleware/dev-gate.js +75 -75
- package/src/server/middleware/headers.js +37 -37
- package/src/server/middleware/redirects.js +32 -32
- package/src/server/middleware/robots-txt.js +341 -341
- package/src/server/middleware/static-precompressed.js +121 -121
- package/src/server/middleware/trailing-slash.js +53 -53
- package/src/server/middleware/upstream-proxy.js +141 -141
- package/src/server/og-image.js +369 -356
- package/src/server/port-guard.js +255 -255
- package/src/server/prewarm.js +1082 -1082
- package/src/server/redis.js +588 -588
- package/src/server/render.js +910 -910
- package/src/server/router.js +157 -157
- package/src/server/status-page.js +265 -265
- package/src/server/upstream-limiter.js +376 -376
- package/src/server/upstream-tracking.js +166 -166
- package/src/shared/cookie-domain.js +66 -66
- package/src/start.mjs +22 -22
- package/src/templates/layout.ejs +30 -30
- package/src/templates/layout.jsk +30 -30
- package/src/version.mjs +31 -31
- package/src/views/components/loader.js +101 -101
- package/src/views/helpers/html.js +102 -102
- package/src/views/helpers/tags.js +375 -375
- package/types/config/defaults.d.ts +6 -0
- package/types/config/index.d.ts +6 -0
- package/types/server/cache-control.d.ts +28 -0
- package/types/server/og-image.d.ts +5 -0
package/src/compile/codegen.js
CHANGED
|
@@ -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, "&")
|
|
313
|
-
.replace(/"/g, """)
|
|
314
|
-
.replace(/</g, "<");
|
|
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, "&")
|
|
313
|
+
.replace(/"/g, """)
|
|
314
|
+
.replace(/</g, "<");
|
|
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
|
+
}
|