jskelet 0.5.4 → 0.6.0
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 +18 -13
- package/CHANGELOG.md +387 -385
- package/README.md +9 -7
- package/bin/jskelet.mjs +24 -10
- package/docs/01-baslangic.md +4 -3
- package/docs/02-mimari.md +4 -3
- package/docs/03-routing.md +11 -6
- package/docs/04-render-ve-sablonlar.md +35 -43
- package/docs/05-islands.md +12 -8
- package/docs/07-yapilandirma.md +53 -25
- package/docs/08-build.md +40 -18
- package/docs/09-dev-araclari.md +5 -1
- package/docs/10-dagitim.md +6 -1
- package/docs/11-tasima.md +51 -17
- package/docs/12-panel-ve-oturum.md +10 -4
- package/docs/README.md +7 -5
- package/docs/en/01-getting-started.md +4 -3
- package/docs/en/02-architecture.md +5 -5
- package/docs/en/03-routing.md +12 -7
- package/docs/en/04-rendering.md +47 -59
- package/docs/en/05-islands.md +13 -8
- package/docs/en/07-configuration.md +55 -27
- package/docs/en/08-build.md +43 -21
- package/docs/en/09-dev-tools.md +6 -1
- package/docs/en/10-deployment.md +6 -1
- package/docs/en/11-migration.md +51 -16
- package/docs/en/12-dashboards-and-sessions.md +9 -4
- package/docs/en/README.md +7 -5
- package/package.json +49 -14
- package/src/build/tasks/client.mjs +91 -10
- package/src/build/tasks/icons.mjs +152 -18
- package/src/client/index.js +2 -2
- package/src/compile/codegen.js +4 -0
- package/src/compile/compile-all.js +12 -21
- package/src/compile/expr.js +5 -0
- package/src/compile/parse.js +64 -8
- package/src/compile/resolve.js +3 -0
- package/src/config/defaults.js +12 -2
- package/src/config/index.js +31 -3
- package/src/dev-server.mjs +26 -3
- package/src/http/cookies-entry.js +1 -0
- package/src/http/cookies.js +18 -0
- package/src/logo.png +0 -0
- package/src/migrate/apply.mjs +262 -0
- package/src/migrate/babel.mjs +79 -0
- package/src/migrate/classify.mjs +155 -0
- package/src/migrate/config.mjs +126 -0
- package/src/migrate/fs-walk.mjs +191 -0
- package/src/migrate/parse.mjs +26 -0
- package/src/migrate/scan.mjs +177 -0
- package/src/migrate/transform/expr-source.mjs +168 -0
- package/src/migrate/transform/island.mjs +67 -0
- package/src/migrate/transform/jsx-to-component.mjs +302 -0
- package/src/migrate/transform/jsx-to-jsk.mjs +330 -0
- package/src/migrate/transform/page-split.mjs +435 -0
- package/src/migrate/write.mjs +81 -0
- package/src/migrate.mjs +171 -0
- package/src/server/auth/handoff.js +94 -11
- package/src/server/create-app.js +28 -10
- package/src/server/ejs-adapter.js +59 -0
- package/src/server/image-optimizer.js +94 -26
- package/src/server/port-guard.js +255 -0
- package/src/server/render.js +27 -9
- package/src/server/status-page.js +105 -4
- package/src/start.mjs +18 -3
- package/src/templates/layout.ejs +8 -28
- package/src/templates/layout.jsk +30 -0
- package/src/templates/layout.render.js +41 -0
- package/src/views/helpers/tags.js +86 -3
- package/types/build/resolve-peer.d.mts +13 -0
- package/types/client/dom.d.ts +55 -0
- package/types/client/form.d.ts +19 -0
- package/types/client/index.d.ts +20 -0
- package/types/client/registry.d.ts +53 -0
- package/types/client/safe-image.d.ts +19 -0
- package/types/client/shared-cookie.d.ts +82 -0
- package/types/client/store.d.ts +18 -0
- package/types/client/swap.d.ts +46 -0
- package/types/compile/codegen.d.ts +32 -0
- package/types/compile/compile-all.d.ts +42 -0
- package/types/compile/errors.d.ts +30 -0
- package/types/compile/expr.d.ts +67 -0
- package/types/compile/index.d.ts +10 -0
- package/types/compile/parse.d.ts +82 -0
- package/types/compile/resolve.d.ts +46 -0
- package/types/compile/scan-exports.d.ts +9 -0
- package/types/config/defaults.d.ts +449 -0
- package/types/config/index.d.ts +299 -0
- package/types/config/pattern.d.ts +38 -0
- package/types/http/control-flow.d.ts +45 -0
- package/types/http/cookies-entry.d.ts +5 -0
- package/types/http/cookies.d.ts +113 -0
- package/types/http/request-cache.d.ts +13 -0
- package/types/http/request-context.d.ts +67 -0
- package/types/http/shared-cookie.d.ts +73 -0
- package/types/index.d.ts +30 -0
- package/types/log.d.mts +153 -0
- package/types/server/admin/actions.d.ts +16 -0
- package/types/server/admin/auth.d.ts +52 -0
- package/types/server/admin/event-log.d.ts +38 -0
- package/types/server/admin/gate.d.ts +43 -0
- package/types/server/admin/inventory.d.ts +40 -0
- package/types/server/admin/mount.d.ts +6 -0
- package/types/server/admin/router.d.ts +6 -0
- package/types/server/admin/snapshot.d.ts +6 -0
- package/types/server/assets.d.ts +47 -0
- package/types/server/auth/handoff.d.ts +12 -0
- package/types/server/cache-deps.d.ts +16 -0
- package/types/server/cache-vary.d.ts +30 -0
- package/types/server/cloudflare.d.ts +163 -0
- package/types/server/create-app.d.ts +25 -0
- package/types/server/data-cache.d.ts +116 -0
- package/types/server/dev/devtools.d.ts +44 -0
- package/types/server/dev/report.d.ts +229 -0
- package/types/server/dev/socket.d.ts +17 -0
- package/types/server/dev/version-check.d.mts +15 -0
- package/types/server/ejs-adapter.d.ts +11 -0
- package/types/server/head-hints.d.ts +40 -0
- package/types/server/html-cache.d.ts +173 -0
- package/types/server/image-optimizer.d.ts +68 -0
- package/types/server/logs/access-middleware.d.ts +7 -0
- package/types/server/logs/file-sink.d.ts +17 -0
- package/types/server/logs/pipeline.d.ts +37 -0
- package/types/server/logs/s3-put.d.ts +85 -0
- package/types/server/logs/s3-sink.d.ts +26 -0
- package/types/server/metadata.d.ts +38 -0
- package/types/server/middleware/compression.d.ts +17 -0
- package/types/server/middleware/csrf.d.ts +4 -0
- package/types/server/middleware/dev-gate.d.ts +2 -0
- package/types/server/middleware/headers.d.ts +2 -0
- package/types/server/middleware/redirects.d.ts +2 -0
- package/types/server/middleware/static-precompressed.d.ts +5 -0
- package/types/server/middleware/trailing-slash.d.ts +11 -0
- package/types/server/middleware/upstream-proxy.d.ts +21 -0
- package/types/server/og-image.d.ts +149 -0
- package/types/server/port-guard.d.ts +50 -0
- package/types/server/prewarm.d.ts +128 -0
- package/types/server/redis.d.ts +163 -0
- package/types/server/render.d.ts +101 -0
- package/types/server/router.d.ts +5 -0
- package/types/server/status-page.d.ts +24 -0
- package/types/server/upstream-limiter.d.ts +123 -0
- package/types/server/upstream-tracking.d.ts +42 -0
- package/types/shared/cookie-domain.d.ts +29 -0
- package/types/templates/layout.render.d.ts +7 -0
- package/types/version.d.mts +10 -0
- package/types/views/components/loader.d.ts +5 -0
- package/types/views/helpers/html.d.ts +39 -0
- package/types/views/helpers/tags.d.ts +127 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Babel ifade → kaynak metin + JSK parseExpr doğrulaması.
|
|
3
|
+
* `@babel/generator` yok; JSX attr / {{ }} için yeterli alt küme basılır.
|
|
4
|
+
*/
|
|
5
|
+
import { t } from "../babel.mjs";
|
|
6
|
+
import { parseExpr } from "../../compile/expr.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {import('@babel/types').Node} node
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
12
|
+
export function exprToSource(node) {
|
|
13
|
+
if (t.isTSAsExpression(node) || t.isTSTypeAssertion(node) || t.isTSNonNullExpression(node)) {
|
|
14
|
+
return exprToSource(/** @type {{ expression: import('@babel/types').Expression }} */ (node).expression);
|
|
15
|
+
}
|
|
16
|
+
if (t.isParenthesizedExpression(node)) {
|
|
17
|
+
return `(${exprToSource(node.expression)})`;
|
|
18
|
+
}
|
|
19
|
+
if (t.isIdentifier(node)) return node.name;
|
|
20
|
+
if (t.isStringLiteral(node)) return JSON.stringify(node.value);
|
|
21
|
+
if (t.isNumericLiteral(node)) return String(node.value);
|
|
22
|
+
if (t.isBooleanLiteral(node)) return node.value ? "true" : "false";
|
|
23
|
+
if (t.isNullLiteral(node)) return "null";
|
|
24
|
+
if (t.isBigIntLiteral(node)) return node.value + "n";
|
|
25
|
+
|
|
26
|
+
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
|
27
|
+
const obj = exprToSource(node.object);
|
|
28
|
+
const opt = "optional" in node && node.optional ? "?." : ".";
|
|
29
|
+
if (node.computed) {
|
|
30
|
+
return `${obj}${opt === "?." ? "?." : ""}[${exprToSource(/** @type {import('@babel/types').Expression} */ (node.property))}]`;
|
|
31
|
+
}
|
|
32
|
+
if (t.isIdentifier(node.property)) {
|
|
33
|
+
return `${obj}${opt}${node.property.name}`;
|
|
34
|
+
}
|
|
35
|
+
return `${obj}.${exprToSource(node.property)}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (t.isUnaryExpression(node)) {
|
|
39
|
+
return `${node.operator}${exprToSource(node.argument)}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (t.isBinaryExpression(node) || t.isLogicalExpression(node)) {
|
|
43
|
+
return `${exprToSource(/** @type {import('@babel/types').Expression} */ (node.left))} ${node.operator} ${exprToSource(node.right)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (t.isConditionalExpression(node)) {
|
|
47
|
+
return `${exprToSource(node.test)} ? ${exprToSource(node.consequent)} : ${exprToSource(node.alternate)}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (t.isTemplateLiteral(node)) {
|
|
51
|
+
let out = "`";
|
|
52
|
+
for (let i = 0; i < node.quasis.length; i++) {
|
|
53
|
+
out += node.quasis[i].value.cooked ?? node.quasis[i].value.raw;
|
|
54
|
+
if (i < node.expressions.length) {
|
|
55
|
+
out += "${" + exprToSource(node.expressions[i]) + "}";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return out + "`";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (t.isArrayExpression(node)) {
|
|
62
|
+
return `[${node.elements.map((el) => (el ? exprToSource(el) : "")).join(", ")}]`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (t.isObjectExpression(node)) {
|
|
66
|
+
const props = node.properties.map((p) => {
|
|
67
|
+
if (t.isSpreadElement(p)) return `...${exprToSource(p.argument)}`;
|
|
68
|
+
if (t.isObjectMethod(p)) return `/* method */`;
|
|
69
|
+
if (t.isObjectProperty(p)) {
|
|
70
|
+
const key = t.isIdentifier(p.key) && !p.computed ? p.key.name : exprToSource(p.key);
|
|
71
|
+
return `${key}: ${exprToSource(/** @type {import('@babel/types').Expression} */ (p.value))}`;
|
|
72
|
+
}
|
|
73
|
+
return "";
|
|
74
|
+
});
|
|
75
|
+
return `{ ${props.join(", ")} }`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (t.isCallExpression(node)) {
|
|
79
|
+
const callee = exprToSource(node.callee);
|
|
80
|
+
const args = node.arguments.map((a) => exprToSource(/** @type {import('@babel/types').Node} */ (a))).join(", ");
|
|
81
|
+
return `${callee}(${args})`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (t.isAwaitExpression(node)) {
|
|
85
|
+
return `await ${exprToSource(node.argument)}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (t.isArrowFunctionExpression(node) || t.isFunctionExpression(node)) {
|
|
89
|
+
return `/* function */`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (t.isThisExpression(node)) return "this";
|
|
93
|
+
|
|
94
|
+
return `/* unsupported:${node.type} */`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {string} source
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
export function isJskExpr(source) {
|
|
102
|
+
if (!source || source.includes("/* unsupported")) return false;
|
|
103
|
+
try {
|
|
104
|
+
parseExpr(source);
|
|
105
|
+
return true;
|
|
106
|
+
} catch {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {import('@babel/types').Expression} expr
|
|
113
|
+
* @returns {{ ok: true, source: string } | { ok: false, reason: string }}
|
|
114
|
+
*/
|
|
115
|
+
export function tryJskExpr(expr) {
|
|
116
|
+
if (t.isStringLiteral(expr)) {
|
|
117
|
+
return { ok: true, source: JSON.stringify(expr.value) };
|
|
118
|
+
}
|
|
119
|
+
if (t.isNumericLiteral(expr)) {
|
|
120
|
+
return { ok: true, source: String(expr.value) };
|
|
121
|
+
}
|
|
122
|
+
if (t.isBooleanLiteral(expr)) {
|
|
123
|
+
return { ok: true, source: expr.value ? "true" : "false" };
|
|
124
|
+
}
|
|
125
|
+
if (t.isNullLiteral(expr)) {
|
|
126
|
+
return { ok: true, source: "null" };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const source = exprToSource(expr);
|
|
130
|
+
if (isJskExpr(source)) return { ok: true, source };
|
|
131
|
+
return { ok: false, reason: `expression not valid in .jsk: ${source}` };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @param {import('@babel/types').Expression} expr
|
|
136
|
+
* @param {Set<string>} out
|
|
137
|
+
*/
|
|
138
|
+
export function collectRootIdents(expr, out) {
|
|
139
|
+
if (t.isIdentifier(expr)) {
|
|
140
|
+
out.add(expr.name);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (t.isMemberExpression(expr) || t.isOptionalMemberExpression(expr)) {
|
|
144
|
+
collectRootIdents(/** @type {import('@babel/types').Expression} */ (expr.object), out);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (t.isConditionalExpression(expr)) {
|
|
148
|
+
collectRootIdents(expr.test, out);
|
|
149
|
+
collectRootIdents(expr.consequent, out);
|
|
150
|
+
collectRootIdents(expr.alternate, out);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (t.isLogicalExpression(expr) || t.isBinaryExpression(expr)) {
|
|
154
|
+
collectRootIdents(/** @type {import('@babel/types').Expression} */ (expr.left), out);
|
|
155
|
+
collectRootIdents(expr.right, out);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (t.isUnaryExpression(expr)) {
|
|
159
|
+
collectRootIdents(expr.argument, out);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (t.isCallExpression(expr)) {
|
|
163
|
+
collectRootIdents(/** @type {import('@babel/types').Expression} */ (expr.callee), out);
|
|
164
|
+
for (const arg of expr.arguments) {
|
|
165
|
+
if (t.isExpression(arg)) collectRootIdents(arg, out);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `"use client"` → island `mount()` iskeleti.
|
|
3
|
+
*/
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {{ status: 'ok' | 'partial' | 'skipped', notes: string[], code: string | null, islandName: string }} IslandResult
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} code
|
|
12
|
+
* @param {string} filename
|
|
13
|
+
* @param {{ name?: string }} [options]
|
|
14
|
+
* @returns {IslandResult}
|
|
15
|
+
*/
|
|
16
|
+
export function transformIsland(code, filename, options = {}) {
|
|
17
|
+
const base = path.basename(filename).replace(/\.(jsx?|tsx?|mjs|cjs)$/, "");
|
|
18
|
+
const islandName = options.name ?? camel(base);
|
|
19
|
+
/** @type {IslandResult} */
|
|
20
|
+
const result = {
|
|
21
|
+
status: "partial",
|
|
22
|
+
notes: [],
|
|
23
|
+
code: null,
|
|
24
|
+
islandName,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const hasHooks = /\buse(State|Effect|Memo|Callback|Ref|Context|Reducer)\s*\(/.test(code);
|
|
28
|
+
if (hasHooks) {
|
|
29
|
+
result.notes.push("hooks require a manual port into mount()");
|
|
30
|
+
}
|
|
31
|
+
if (!/["']use client["']/.test(code) && !hasHooks) {
|
|
32
|
+
result.notes.push("no \"use client\" directive — treating as island candidate anyway");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
result.code =
|
|
36
|
+
`/**\n` +
|
|
37
|
+
` * Island migrated from ${filename.replace(/\\/g, "/")}\n` +
|
|
38
|
+
` * Register: registerAll({ ${islandName}: () => import("./${islandName}.js") })\n` +
|
|
39
|
+
` * Markup: <div data-island="${islandName}" data-island-props='{}'></div>\n` +
|
|
40
|
+
` */\n` +
|
|
41
|
+
`/**\n` +
|
|
42
|
+
` * @param {HTMLElement} el\n` +
|
|
43
|
+
` * @param {Record<string, unknown>} [props]\n` +
|
|
44
|
+
` */\n` +
|
|
45
|
+
`export function mount(el, props = {}) {\n` +
|
|
46
|
+
` // TODO[jskelet-migrate]: port client logic from ${path.basename(filename)}\n` +
|
|
47
|
+
(hasHooks
|
|
48
|
+
? ` // Original file used React hooks — rewrite with plain DOM + events.\n`
|
|
49
|
+
: "") +
|
|
50
|
+
` el.textContent = "";\n` +
|
|
51
|
+
` void props;\n` +
|
|
52
|
+
`}\n`;
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {string} name
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
function camel(name) {
|
|
62
|
+
return name
|
|
63
|
+
.split(/[-_/]+/)
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.map((p, i) => (i === 0 ? p.toLowerCase() : p.charAt(0).toUpperCase() + p.slice(1)))
|
|
66
|
+
.join("");
|
|
67
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presentational React bileşeni → `views/components/*.js` HTML-string fonksiyon.
|
|
3
|
+
*/
|
|
4
|
+
import { t } from "../babel.mjs";
|
|
5
|
+
import { parseSource } from "../parse.mjs";
|
|
6
|
+
import { exprToSource } from "./expr-source.mjs";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {{ status: 'ok' | 'partial' | 'skipped', notes: string[], name: string | null, code: string | null }} ComponentResult
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} code
|
|
14
|
+
* @param {string} filename
|
|
15
|
+
* @returns {ComponentResult}
|
|
16
|
+
*/
|
|
17
|
+
export function transformComponent(code, filename) {
|
|
18
|
+
/** @type {ComponentResult} */
|
|
19
|
+
const result = { status: "ok", notes: [], name: null, code: null };
|
|
20
|
+
|
|
21
|
+
if (/["']use client["']/.test(code) || /\buse(State|Effect|Memo|Callback|Ref|Context)\s*\(/.test(code)) {
|
|
22
|
+
result.status = "skipped";
|
|
23
|
+
result.notes.push("client / hooks — use island transform");
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let ast;
|
|
28
|
+
try {
|
|
29
|
+
ast = parseSource(code, filename);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
result.status = "skipped";
|
|
32
|
+
result.notes.push(error instanceof Error ? error.message : String(error));
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const fn = findComponentFunction(ast);
|
|
37
|
+
if (!fn) {
|
|
38
|
+
result.status = "skipped";
|
|
39
|
+
result.notes.push("no exported function component with JSX return found");
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
result.name = fn.name;
|
|
44
|
+
const props = fn.props;
|
|
45
|
+
const jsx = fn.jsx;
|
|
46
|
+
const body = renderJsxToTemplate(jsx, result);
|
|
47
|
+
if (result.status === "skipped") return result;
|
|
48
|
+
|
|
49
|
+
const propsParam = props.length
|
|
50
|
+
? `{ ${props.map((p) => (p === "className" ? "class: className" : p)).join(", ")} }`
|
|
51
|
+
: "props = {}";
|
|
52
|
+
|
|
53
|
+
const needsCn = body.includes("cn(");
|
|
54
|
+
const imports = ["esc", "attrs", ...(needsCn ? ["cn"] : [])];
|
|
55
|
+
const preamble = extractPreamble(ast);
|
|
56
|
+
|
|
57
|
+
result.code =
|
|
58
|
+
`import { ${imports.join(", ")} } from "jskelet/html";\n` +
|
|
59
|
+
(preamble ? `\n${preamble}\n` : "\n") +
|
|
60
|
+
`export function ${camelName(fn.name)}(${propsParam}) {\n` +
|
|
61
|
+
` return ${body};\n` +
|
|
62
|
+
`}\n`;
|
|
63
|
+
|
|
64
|
+
if (result.notes.length && result.status === "ok") result.status = "partial";
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Import dışı üst seviye const'ları (TONES vb.) kopyala.
|
|
70
|
+
* @param {import('@babel/types').File} ast
|
|
71
|
+
* @returns {string}
|
|
72
|
+
*/
|
|
73
|
+
function extractPreamble(ast) {
|
|
74
|
+
/** @type {string[]} */
|
|
75
|
+
const lines = [];
|
|
76
|
+
for (const node of ast.program.body) {
|
|
77
|
+
if (t.isImportDeclaration(node)) continue;
|
|
78
|
+
if (t.isExportNamedDeclaration(node) || t.isExportDefaultDeclaration(node)) continue;
|
|
79
|
+
if (t.isVariableDeclaration(node)) {
|
|
80
|
+
lines.push(printVar(node));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return lines.join("\n");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {import('@babel/types').VariableDeclaration} stmt
|
|
88
|
+
* @returns {string}
|
|
89
|
+
*/
|
|
90
|
+
function printVar(stmt) {
|
|
91
|
+
const decls = stmt.declarations
|
|
92
|
+
.map((d) => {
|
|
93
|
+
const id = t.isIdentifier(d.id) ? d.id.name : "/* pat */";
|
|
94
|
+
const init = d.init ? ` = ${exprToSource(d.init)}` : "";
|
|
95
|
+
return id + init;
|
|
96
|
+
})
|
|
97
|
+
.join(", ");
|
|
98
|
+
return `${stmt.kind} ${decls};`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {import('@babel/types').File} ast
|
|
103
|
+
* @returns {{ name: string, props: string[], jsx: import('@babel/types').JSXElement | import('@babel/types').JSXFragment } | null}
|
|
104
|
+
*/
|
|
105
|
+
function findComponentFunction(ast) {
|
|
106
|
+
for (const node of ast.program.body) {
|
|
107
|
+
// export function Badge(...) { return <...> }
|
|
108
|
+
if (t.isExportNamedDeclaration(node) && t.isFunctionDeclaration(node.declaration)) {
|
|
109
|
+
const decl = node.declaration;
|
|
110
|
+
if (!decl.id) continue;
|
|
111
|
+
const jsx = findReturnJsx(decl.body);
|
|
112
|
+
if (!jsx) continue;
|
|
113
|
+
return { name: decl.id.name, props: propsFromParams(decl.params), jsx };
|
|
114
|
+
}
|
|
115
|
+
// export const Badge = (...) => <...>
|
|
116
|
+
if (
|
|
117
|
+
t.isExportNamedDeclaration(node) &&
|
|
118
|
+
node.declaration &&
|
|
119
|
+
t.isVariableDeclaration(node.declaration)
|
|
120
|
+
) {
|
|
121
|
+
for (const d of node.declaration.declarations) {
|
|
122
|
+
if (!t.isIdentifier(d.id)) continue;
|
|
123
|
+
if (!d.init) continue;
|
|
124
|
+
if (t.isArrowFunctionExpression(d.init) || t.isFunctionExpression(d.init)) {
|
|
125
|
+
let jsx = null;
|
|
126
|
+
if (t.isJSXElement(d.init.body) || t.isJSXFragment(d.init.body)) {
|
|
127
|
+
jsx = d.init.body;
|
|
128
|
+
} else if (t.isBlockStatement(d.init.body)) {
|
|
129
|
+
jsx = findReturnJsx(d.init.body);
|
|
130
|
+
}
|
|
131
|
+
if (!jsx) continue;
|
|
132
|
+
return { name: d.id.name, props: propsFromParams(d.init.params), jsx };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// export default function
|
|
137
|
+
if (t.isExportDefaultDeclaration(node) && t.isFunctionDeclaration(node.declaration)) {
|
|
138
|
+
const decl = node.declaration;
|
|
139
|
+
const name = decl.id?.name ?? "Component";
|
|
140
|
+
const jsx = findReturnJsx(decl.body);
|
|
141
|
+
if (!jsx) continue;
|
|
142
|
+
return { name, props: propsFromParams(decl.params), jsx };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @param {import('@babel/types').BlockStatement} body
|
|
150
|
+
* @returns {import('@babel/types').JSXElement | import('@babel/types').JSXFragment | null}
|
|
151
|
+
*/
|
|
152
|
+
function findReturnJsx(body) {
|
|
153
|
+
for (const stmt of body.body) {
|
|
154
|
+
if (t.isReturnStatement(stmt) && stmt.argument) {
|
|
155
|
+
if (t.isJSXElement(stmt.argument) || t.isJSXFragment(stmt.argument)) {
|
|
156
|
+
return stmt.argument;
|
|
157
|
+
}
|
|
158
|
+
if (t.isParenthesizedExpression(stmt.argument)) {
|
|
159
|
+
const inner = stmt.argument.expression;
|
|
160
|
+
if (t.isJSXElement(inner) || t.isJSXFragment(inner)) return inner;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {import('@babel/types').FunctionDeclaration['params']} params
|
|
169
|
+
* @returns {string[]}
|
|
170
|
+
*/
|
|
171
|
+
function propsFromParams(params) {
|
|
172
|
+
if (params.length === 0) return [];
|
|
173
|
+
const first = params[0];
|
|
174
|
+
if (t.isObjectPattern(first)) {
|
|
175
|
+
return first.properties
|
|
176
|
+
.map((p) => {
|
|
177
|
+
if (t.isObjectProperty(p) && t.isIdentifier(p.key)) return p.key.name;
|
|
178
|
+
if (t.isRestElement(p) && t.isIdentifier(p.argument)) return p.argument.name;
|
|
179
|
+
return null;
|
|
180
|
+
})
|
|
181
|
+
.filter(Boolean);
|
|
182
|
+
}
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {import('@babel/types').JSXElement | import('@babel/types').JSXFragment} node
|
|
188
|
+
* @param {ComponentResult} result
|
|
189
|
+
* @returns {string} template literal expression (with backticks)
|
|
190
|
+
*/
|
|
191
|
+
function renderJsxToTemplate(node, result) {
|
|
192
|
+
const inner = renderNode(node, result);
|
|
193
|
+
return `\`${inner}\``;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {import('@babel/types').Node} node
|
|
198
|
+
* @param {ComponentResult} result
|
|
199
|
+
* @returns {string}
|
|
200
|
+
*/
|
|
201
|
+
function renderNode(node, result) {
|
|
202
|
+
if (t.isJSXFragment(node)) {
|
|
203
|
+
return node.children.map((c) => renderNode(c, result)).join("");
|
|
204
|
+
}
|
|
205
|
+
if (t.isJSXText(node)) {
|
|
206
|
+
return escapeTemplate(node.value);
|
|
207
|
+
}
|
|
208
|
+
if (t.isJSXExpressionContainer(node)) {
|
|
209
|
+
if (t.isJSXEmptyExpression(node.expression)) return "";
|
|
210
|
+
return `\${esc(${exprToSource(node.expression)})}`;
|
|
211
|
+
}
|
|
212
|
+
if (t.isJSXElement(node)) {
|
|
213
|
+
return renderElement(node, result);
|
|
214
|
+
}
|
|
215
|
+
result.notes.push(`unsupported node ${node.type}`);
|
|
216
|
+
result.status = "partial";
|
|
217
|
+
return "";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @param {import('@babel/types').JSXElement} el
|
|
222
|
+
* @param {ComponentResult} result
|
|
223
|
+
* @returns {string}
|
|
224
|
+
*/
|
|
225
|
+
function renderElement(el, result) {
|
|
226
|
+
const nameNode = el.openingElement.name;
|
|
227
|
+
if (!t.isJSXIdentifier(nameNode)) {
|
|
228
|
+
result.notes.push("namespaced JSX not supported");
|
|
229
|
+
result.status = "partial";
|
|
230
|
+
return "";
|
|
231
|
+
}
|
|
232
|
+
const tag = nameNode.name;
|
|
233
|
+
/** @type {Record<string, string>} */
|
|
234
|
+
const staticAttrs = {};
|
|
235
|
+
/** @type {string[]} */
|
|
236
|
+
const dynamicParts = [];
|
|
237
|
+
|
|
238
|
+
for (const attr of el.openingElement.attributes) {
|
|
239
|
+
if (t.isJSXSpreadAttribute(attr)) {
|
|
240
|
+
result.notes.push("spread attributes");
|
|
241
|
+
result.status = "partial";
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (!t.isJSXAttribute(attr) || !t.isJSXIdentifier(attr.name)) continue;
|
|
245
|
+
let key = attr.name.name;
|
|
246
|
+
if (key === "className") key = "class";
|
|
247
|
+
if (key === "htmlFor") key = "for";
|
|
248
|
+
if (key === "dangerouslySetInnerHTML") {
|
|
249
|
+
result.notes.push("dangerouslySetInnerHTML in component — use raw html prop");
|
|
250
|
+
result.status = "partial";
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (attr.value === null) {
|
|
255
|
+
staticAttrs[key] = "true";
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (t.isStringLiteral(attr.value)) {
|
|
259
|
+
staticAttrs[key] = attr.value.value;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (t.isJSXExpressionContainer(attr.value) && t.isExpression(attr.value.expression)) {
|
|
263
|
+
const src = exprToSource(attr.value.expression);
|
|
264
|
+
if (key === "class" && src.startsWith("cn(")) {
|
|
265
|
+
dynamicParts.push(`class: ${src}`);
|
|
266
|
+
} else {
|
|
267
|
+
dynamicParts.push(`${JSON.stringify(key)}: ${src}`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const attrsCall =
|
|
273
|
+
Object.keys(staticAttrs).length || dynamicParts.length
|
|
274
|
+
? `\${attrs({ ${[
|
|
275
|
+
...Object.entries(staticAttrs).map(([k, v]) => `${JSON.stringify(k)}: ${JSON.stringify(v)}`),
|
|
276
|
+
...dynamicParts,
|
|
277
|
+
].join(", ")} })}`
|
|
278
|
+
: "";
|
|
279
|
+
|
|
280
|
+
const children = el.children.map((c) => renderNode(c, result)).join("");
|
|
281
|
+
if (el.openingElement.selfClosing && !children) {
|
|
282
|
+
return `<${tag}${attrsCall} />`;
|
|
283
|
+
}
|
|
284
|
+
return `<${tag}${attrsCall}>${children}</${tag}>`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* @param {string} text
|
|
289
|
+
* @returns {string}
|
|
290
|
+
*/
|
|
291
|
+
function escapeTemplate(text) {
|
|
292
|
+
return text.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @param {string} name
|
|
297
|
+
* @returns {string}
|
|
298
|
+
*/
|
|
299
|
+
function camelName(name) {
|
|
300
|
+
if (!name) return "component";
|
|
301
|
+
return name.charAt(0).toLowerCase() + name.slice(1);
|
|
302
|
+
}
|