@forgewp/compiler 0.4.2 → 0.4.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/README.md +70 -3
- package/bin/add.js +30 -15
- package/bin/analyze.js +66 -1
- package/bin/cli.js +149 -70
- package/bin/doctor.js +21 -12
- package/bin/export.js +69 -20
- package/bin/fresh.js +6 -1
- package/bin/i18n-extract.js +3 -10
- package/bin/i18n-translate.js +15 -24
- package/bin/make-block.js +24 -0
- package/bin/make-editable.js +166 -0
- package/bin/make-form.js +131 -0
- package/bin/make-island.js +11 -0
- package/bin/make-loop.js +147 -0
- package/bin/make-mail.js +27 -0
- package/bin/make-page.js +83 -0
- package/bin/make-post-type.js +68 -29
- package/bin/make-sandbox.js +609 -0
- package/bin/make-section.js +221 -0
- package/bin/make-shell.js +375 -0
- package/bin/repair.js +14 -26
- package/bin/seed.js +119 -0
- package/bin/sync-routes.js +107 -85
- package/lib/adapters/html-adapter.js +13 -3
- package/lib/adapters/stub-adapter.js +10 -2
- package/lib/add-component.js +153 -92
- package/lib/asset-loader.js +333 -1
- package/lib/assets/asset-graph.js +432 -0
- package/lib/assets/asset-pipeline.js +125 -0
- package/lib/assets/diagnostics-report.js +345 -0
- package/lib/assets/external-fetcher.js +173 -0
- package/lib/assets/font-orchestrator.js +348 -0
- package/lib/assets/font-usage-scanner.js +242 -0
- package/lib/assets/image-optimizer.js +229 -0
- package/lib/assets/index.js +51 -0
- package/lib/assets/preload-generator.js +70 -0
- package/lib/assets/public-url.js +153 -0
- package/lib/block-authoring-help.js +11 -0
- package/lib/blocks/ast-parser.js +67 -0
- package/lib/blocks/discover-blocks.js +146 -0
- package/lib/blocks/editor-scope.js +733 -0
- package/lib/blocks/editor-transpiler.js +1229 -0
- package/lib/blocks/index.js +2838 -0
- package/lib/blocks/php-transpiler.js +4928 -0
- package/lib/blocks/scanner.js +1487 -0
- package/lib/blocks/shared-utils.js +290 -0
- package/lib/blocks/source-sanitize.js +1449 -0
- package/lib/blueprints.js +77 -5
- package/lib/build-assets.js +38 -33
- package/lib/collect-page-template-routes.js +102 -0
- package/lib/collect-static-routes.js +175 -0
- package/lib/diagnostics.js +58 -33
- package/lib/export-theme.js +84 -5
- package/lib/extract-template-content.js +90 -0
- package/lib/forms-preview.js +42 -0
- package/lib/functions/cpt-routing.js +69 -0
- package/lib/functions/forms.js +2461 -0
- package/lib/functions/hydration-enqueuer.js +400 -0
- package/lib/functions/index.js +1623 -0
- package/lib/functions/load-menus.js +112 -0
- package/lib/functions/load-translations.js +148 -0
- package/lib/functions/rest-endpoints.js +2923 -0
- package/lib/functions/routing.js +765 -0
- package/lib/functions/seed-mock-data.js +392 -0
- package/lib/functions/seed-products.js +481 -0
- package/lib/functions/settings-page.js +273 -0
- package/lib/functions/theme-setup.js +494 -0
- package/lib/generate-theme.js +2076 -303
- package/lib/hydration/editable-schemas.js +332 -0
- package/lib/hydration/enter-hidden-initial.js +65 -0
- package/lib/hydration/form-schemas.js +227 -0
- package/lib/hydration/index.js +12 -0
- package/lib/hydration/is-interactive.js +951 -0
- package/lib/hydration/island-split.js +1231 -0
- package/lib/hydration/islands-scanner.js +599 -0
- package/lib/hydration/resolve-island-chunk.js +103 -0
- package/lib/hydration/resolve-island-component.js +52 -0
- package/lib/hydration/rollup-inputs.js +81 -0
- package/lib/hydration/root-class-extractor.js +220 -0
- package/lib/hydration/scan-app-providers.js +154 -0
- package/lib/hydration/static-lint.js +382 -0
- package/lib/hydration/transform.js +257 -0
- package/lib/hydration/uses-block-area.js +49 -0
- package/lib/hydration/utils.js +119 -0
- package/lib/i18n/translation-cache.js +141 -0
- package/lib/icon-registry.js +72 -0
- package/lib/index.d.ts +324 -8
- package/lib/index.js +3 -2
- package/lib/load-config.js +338 -144
- package/lib/mail-templates.js +37 -0
- package/lib/markup-processor.js +483 -92
- package/lib/node-runtime.js +7 -0
- package/lib/page-config-plugin.js +84 -0
- package/lib/php-builders.js +383 -42
- package/lib/react-adapter.js +1053 -246
- package/lib/registration-metadata.js +11 -0
- package/lib/render-theme.mts +1410 -543
- package/lib/route-layout.js +76 -0
- package/lib/template-discovery.js +58 -0
- package/lib/theme-compatibility.js +32 -0
- package/lib/validate-export.js +51 -2
- package/lib/validate.js +27 -5
- package/lib/virtual-plugin.js +319 -0
- package/lib/walkthrough.md +26 -0
- package/package.json +17 -5
- package/templates/PresetsStyle.tsx +1 -1
- package/templates/auth/cms/email-logs.json +9 -0
- package/templates/auth/cms/menus.json +11 -0
- package/templates/auth/cms/mock-data.json +123 -0
- package/templates/auth/cms/roles.json +7 -0
- package/templates/auth/cms/sessions.json +1 -0
- package/templates/auth/cms/site-settings.json +28 -0
- package/templates/auth/cms/translations.json +4 -0
- package/templates/auth/cms/users.json +46 -0
- package/templates/auth/components/dashboard-view-wrapper.tsx +168 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +141 -0
- package/templates/auth/components/login-form-wrapper.tsx +300 -0
- package/templates/auth/components/reset-password-form-wrapper.tsx +190 -0
- package/templates/auth/components/signup-form-wrapper.tsx +257 -0
- package/templates/auth/components/verify-email-view-wrapper.tsx +265 -0
- package/templates/auth/pages/DashboardPage.tsx +12 -0
- package/templates/auth/pages/ForgotPasswordPage.tsx +11 -0
- package/templates/auth/pages/LoginPage.tsx +11 -0
- package/templates/auth/pages/ResetPasswordPage.tsx +11 -0
- package/templates/auth/pages/SignUpPage.tsx +11 -0
- package/templates/auth/pages/VerifyEmailPage.tsx +11 -0
- package/templates/auth/ui/button.tsx +57 -0
- package/templates/auth/ui/card.tsx +76 -0
- package/templates/auth/ui/field.tsx +242 -0
- package/templates/auth/ui/input.tsx +22 -0
- package/templates/auth/ui/label.tsx +24 -0
- package/templates/auth/ui/separator.tsx +31 -0
- package/templates/forgewp-config.ts +70 -3
- package/templates/menus.json +12 -10
- package/templates/mock-data.json +122 -433
- package/templates/products.json +5 -0
- package/templates/roles.json +7 -0
- package/templates/sessions.json +1 -0
- package/templates/users.json +38 -0
- package/templates/wordpress.tsx +1835 -1297
- package/bin/make-template.js +0 -145
- package/lib/block-compiler.js +0 -552
- package/lib/functions-builder.js +0 -2044
- package/lib/hydration-scanner.js +0 -821
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Scans for `defineEditable` schemas used as page/template ACF field groups.
|
|
6
|
+
*
|
|
7
|
+
* Sources (later wins on same slug):
|
|
8
|
+
* 1. Colocated `export const editable` under `src/app/**` (legacy)
|
|
9
|
+
* 2. `src/editables/**` (legacy short path)
|
|
10
|
+
* 3. `src/cms/editables/**`
|
|
11
|
+
* 4. `cms/editables/**` (preferred — sits with mock-data / theme CMS)
|
|
12
|
+
*
|
|
13
|
+
* Section files under editables/sections/ are block schemas only
|
|
14
|
+
* (not registered as page ACF groups). Page files compose them via
|
|
15
|
+
* buildPageEditable / pageEditableSections or a normal defineEditable.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} themeRoot
|
|
18
|
+
* @returns {Record<string, Record<string, any>>} template/page slug → parsed schema
|
|
19
|
+
*/
|
|
20
|
+
export function scanForEditableSchemas(themeRoot) {
|
|
21
|
+
const schemas = {};
|
|
22
|
+
const filesToScan = [];
|
|
23
|
+
|
|
24
|
+
const dirsToScan = [
|
|
25
|
+
path.join(themeRoot, "src", "app"),
|
|
26
|
+
path.join(themeRoot, "src", "editables"),
|
|
27
|
+
path.join(themeRoot, "src", "cms", "editables"),
|
|
28
|
+
path.join(themeRoot, "cms", "editables"),
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
function collectFilesRecursive(dir) {
|
|
32
|
+
if (!existsSync(dir)) return;
|
|
33
|
+
try {
|
|
34
|
+
const items = readdirSync(dir, { withFileTypes: true });
|
|
35
|
+
for (const item of items) {
|
|
36
|
+
const fullPath = path.join(dir, item.name);
|
|
37
|
+
if (item.isDirectory()) {
|
|
38
|
+
// sections/ hold block-level schemas; not page ACF groups
|
|
39
|
+
if (item.name === "sections") continue;
|
|
40
|
+
collectFilesRecursive(fullPath);
|
|
41
|
+
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
42
|
+
filesToScan.push(fullPath);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} catch {}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (const dir of dirsToScan) {
|
|
49
|
+
collectFilesRecursive(dir);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Prefer cms/editables over src on slug conflict
|
|
53
|
+
filesToScan.sort((a, b) => priority(a) - priority(b));
|
|
54
|
+
|
|
55
|
+
for (const filePath of filesToScan) {
|
|
56
|
+
try {
|
|
57
|
+
const content = readFileSync(filePath, "utf8");
|
|
58
|
+
if (!content.includes("defineEditable") && !content.includes("buildPageEditable") && !content.includes("pageEditableSections")) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let parsed = null;
|
|
63
|
+
|
|
64
|
+
// Composed page: pageEditableSections + section files
|
|
65
|
+
if (content.includes("pageEditableSections")) {
|
|
66
|
+
parsed = parseComposedPageEditable(filePath, content);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!parsed) {
|
|
70
|
+
const schemaStr = extractDefineEditableContent(content);
|
|
71
|
+
if (schemaStr) {
|
|
72
|
+
parsed = parseEditableSchema(schemaStr, filePath);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (parsed && Object.keys(parsed).length > 0) {
|
|
77
|
+
const baseName = path.basename(filePath, path.extname(filePath));
|
|
78
|
+
let slug = baseName
|
|
79
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
80
|
+
.toLowerCase();
|
|
81
|
+
|
|
82
|
+
if (slug === "page") {
|
|
83
|
+
slug = "front-page";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
schemas[slug] = parsed;
|
|
87
|
+
}
|
|
88
|
+
} catch (err) {
|
|
89
|
+
console.warn(`[Schema Scanner] Failed to scan file ${filePath}:`, err.message);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return schemas;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function priority(filePath) {
|
|
97
|
+
const n = filePath.replace(/\\/g, "/");
|
|
98
|
+
if (n.includes("/cms/editables/")) return 3;
|
|
99
|
+
if (n.includes("/src/cms/editables/")) return 2;
|
|
100
|
+
if (n.includes("/src/editables/")) return 1;
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Parse:
|
|
106
|
+
* export const pageEditableSections = [
|
|
107
|
+
* { section: 'about-hero', prefix: 'hero_', rename: { image1: 'image_1' } },
|
|
108
|
+
* ...
|
|
109
|
+
* ];
|
|
110
|
+
* and merge schemas from cms/editables/sections/<section>.ts
|
|
111
|
+
*/
|
|
112
|
+
function parseComposedPageEditable(pageFilePath, content) {
|
|
113
|
+
const match = content.match(
|
|
114
|
+
/export\s+const\s+pageEditableSections\s*=\s*(\[[\s\S]*?\]);/,
|
|
115
|
+
);
|
|
116
|
+
if (!match) return null;
|
|
117
|
+
|
|
118
|
+
let sections;
|
|
119
|
+
try {
|
|
120
|
+
sections = new Function(`return (${match[1]});`)();
|
|
121
|
+
} catch (e) {
|
|
122
|
+
console.warn(`[Schema Scanner] Could not eval pageEditableSections in ${pageFilePath}:`, e.message);
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!Array.isArray(sections)) return null;
|
|
127
|
+
|
|
128
|
+
const sectionsDir = path.join(path.dirname(pageFilePath), "sections");
|
|
129
|
+
const merged = {};
|
|
130
|
+
|
|
131
|
+
for (const entry of sections) {
|
|
132
|
+
if (!entry || typeof entry.section !== "string") continue;
|
|
133
|
+
const sectionPath = path.join(sectionsDir, `${entry.section}.ts`);
|
|
134
|
+
const sectionPathTsx = path.join(sectionsDir, `${entry.section}.tsx`);
|
|
135
|
+
const resolved = existsSync(sectionPath)
|
|
136
|
+
? sectionPath
|
|
137
|
+
: existsSync(sectionPathTsx)
|
|
138
|
+
? sectionPathTsx
|
|
139
|
+
: null;
|
|
140
|
+
if (!resolved) {
|
|
141
|
+
console.warn(`[Schema Scanner] Missing section file for "${entry.section}" (expected under ${sectionsDir})`);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const sectionCode = readFileSync(resolved, "utf8");
|
|
146
|
+
const schemaStr = extractDefineEditableContent(sectionCode);
|
|
147
|
+
if (!schemaStr) continue;
|
|
148
|
+
const schema = parseEditableSchema(schemaStr, resolved);
|
|
149
|
+
if (!schema) continue;
|
|
150
|
+
|
|
151
|
+
const prefix = entry.prefix || "";
|
|
152
|
+
const rename = entry.rename || {};
|
|
153
|
+
for (const [key, field] of Object.entries(schema)) {
|
|
154
|
+
const suffix = rename[key] ?? key;
|
|
155
|
+
merged[`${prefix}${suffix}`] = field;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return merged;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Collect `const NAME = <literal>;` at module scope (column 0) before `endIndex`.
|
|
164
|
+
* Uses brace/bracket balancing so mid-function `const x = [a].filter(...)` is ignored.
|
|
165
|
+
* @param {string} fileContent
|
|
166
|
+
* @param {number} endIndex
|
|
167
|
+
* @returns {string} JS prelude statements
|
|
168
|
+
*/
|
|
169
|
+
function extractModuleLevelConstPrelude(fileContent, endIndex) {
|
|
170
|
+
const head = fileContent.slice(0, Math.max(0, endIndex));
|
|
171
|
+
let prelude = "";
|
|
172
|
+
// Line-start only (module scope). Indentation ⇒ inside a function/block.
|
|
173
|
+
const re = /^(?:export\s+)?const\s+([A-Za-z_$][\w$]*)\s*=\s*/gm;
|
|
174
|
+
let m;
|
|
175
|
+
while ((m = re.exec(head)) !== null) {
|
|
176
|
+
const name = m[1];
|
|
177
|
+
let i = m.index + m[0].length;
|
|
178
|
+
while (i < head.length && /\s/.test(head[i])) i++;
|
|
179
|
+
if (i >= head.length) continue;
|
|
180
|
+
const startChar = head[i];
|
|
181
|
+
let value = null;
|
|
182
|
+
|
|
183
|
+
if (startChar === "'" || startChar === '"' || startChar === "`") {
|
|
184
|
+
const quote = startChar;
|
|
185
|
+
let j = i + 1;
|
|
186
|
+
let esc = false;
|
|
187
|
+
for (; j < head.length; j++) {
|
|
188
|
+
if (esc) {
|
|
189
|
+
esc = false;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (head[j] === "\\") {
|
|
193
|
+
esc = true;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (head[j] === quote) {
|
|
197
|
+
j++;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
while (j < head.length && /\s/.test(head[j])) j++;
|
|
202
|
+
if (head[j] === ";") {
|
|
203
|
+
value = head.slice(i, j);
|
|
204
|
+
}
|
|
205
|
+
} else if (startChar === "[" || startChar === "{") {
|
|
206
|
+
const open = startChar;
|
|
207
|
+
const close = open === "[" ? "]" : "}";
|
|
208
|
+
let depth = 0;
|
|
209
|
+
let j = i;
|
|
210
|
+
let inStr = null;
|
|
211
|
+
let esc = false;
|
|
212
|
+
for (; j < head.length; j++) {
|
|
213
|
+
const c = head[j];
|
|
214
|
+
if (inStr) {
|
|
215
|
+
if (esc) esc = false;
|
|
216
|
+
else if (c === "\\") esc = true;
|
|
217
|
+
else if (c === inStr) inStr = null;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (c === "'" || c === '"' || c === "`") {
|
|
221
|
+
inStr = c;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (c === open) depth++;
|
|
225
|
+
else if (c === close) {
|
|
226
|
+
depth--;
|
|
227
|
+
if (depth === 0) {
|
|
228
|
+
j++;
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
while (j < head.length && /\s/.test(head[j])) j++;
|
|
234
|
+
// Only pure literals ending with `;` — reject `.filter(...)` chains etc.
|
|
235
|
+
if (head[j] === ";") {
|
|
236
|
+
value = head.slice(i, j);
|
|
237
|
+
}
|
|
238
|
+
} else if (/^-?\d/.test(head.slice(i)) || head.startsWith("true", i) || head.startsWith("false", i) || head.startsWith("null", i)) {
|
|
239
|
+
let j = i;
|
|
240
|
+
while (j < head.length && head[j] !== ";" && head[j] !== "\n") j++;
|
|
241
|
+
if (head[j] === ";") {
|
|
242
|
+
const raw = head.slice(i, j).trim();
|
|
243
|
+
if (/^(-?\d+(\.\d+)?|true|false|null)$/.test(raw)) {
|
|
244
|
+
value = raw;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (value != null) {
|
|
250
|
+
prelude += `const ${name} = ${value};\n`;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return prelude;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function extractDefineEditableContent(fileContent) {
|
|
257
|
+
const match = fileContent.match(/export\s+const\s+editable\s*=\s*defineEditable\s*\(/);
|
|
258
|
+
if (!match) return null;
|
|
259
|
+
|
|
260
|
+
const startIndex = match.index + match[0].length;
|
|
261
|
+
let parenCount = 1;
|
|
262
|
+
let currentIndex = startIndex;
|
|
263
|
+
|
|
264
|
+
while (parenCount > 0 && currentIndex < fileContent.length) {
|
|
265
|
+
const char = fileContent[currentIndex];
|
|
266
|
+
if (char === "(") {
|
|
267
|
+
parenCount++;
|
|
268
|
+
} else if (char === ")") {
|
|
269
|
+
parenCount--;
|
|
270
|
+
}
|
|
271
|
+
currentIndex++;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (parenCount === 0) {
|
|
275
|
+
return fileContent.substring(startIndex, currentIndex - 1);
|
|
276
|
+
}
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Eval defineEditable object literal. Injects same-file const declarations
|
|
282
|
+
* (ICON_ALLOWLIST, etc.) when present — mirrors block scanner behaviour.
|
|
283
|
+
*/
|
|
284
|
+
function parseEditableSchema(schemaStr, filePathForConsts) {
|
|
285
|
+
const text = (opts) => ({ type: "text", ...opts });
|
|
286
|
+
const richText = (opts) => ({ type: "richText", ...opts });
|
|
287
|
+
const image = (opts) => ({ type: "image", ...opts });
|
|
288
|
+
const boolean = (opts) => ({ type: "boolean", ...opts });
|
|
289
|
+
const repeater = (opts) => ({ type: "repeater", ...opts });
|
|
290
|
+
const color = (opts) => ({ type: "color", ...opts });
|
|
291
|
+
const url = (opts) => ({ type: "url", ...opts });
|
|
292
|
+
const select = (opts) => ({ type: "select", ...opts });
|
|
293
|
+
const number = (opts) => ({ type: "number", ...opts });
|
|
294
|
+
const icon = (opts) => ({ type: "icon", provider: "lucide", ...opts });
|
|
295
|
+
|
|
296
|
+
let prelude = "";
|
|
297
|
+
if (filePathForConsts && existsSync(filePathForConsts)) {
|
|
298
|
+
try {
|
|
299
|
+
const fileContent = readFileSync(filePathForConsts, "utf8");
|
|
300
|
+
const editableIdx = fileContent.indexOf("defineEditable(");
|
|
301
|
+
// Only module-level consts (start of line). A loose `const x = [a, b]`
|
|
302
|
+
// match used to swallow mid-function bodies (e.g. `const customImages =
|
|
303
|
+
// [galleryImage1, …].filter…`) and then `eval` failed with
|
|
304
|
+
// "galleryImage1 is not defined" — dropping the whole hotel schema so
|
|
305
|
+
// CPT fields never registered for REST / ACF.
|
|
306
|
+
prelude = extractModuleLevelConstPrelude(
|
|
307
|
+
fileContent,
|
|
308
|
+
editableIdx === -1 ? fileContent.length : editableIdx,
|
|
309
|
+
);
|
|
310
|
+
} catch {}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
const fn = new Function(
|
|
315
|
+
"text",
|
|
316
|
+
"richText",
|
|
317
|
+
"image",
|
|
318
|
+
"boolean",
|
|
319
|
+
"repeater",
|
|
320
|
+
"color",
|
|
321
|
+
"url",
|
|
322
|
+
"select",
|
|
323
|
+
"number",
|
|
324
|
+
"icon",
|
|
325
|
+
`${prelude}return (${schemaStr});`,
|
|
326
|
+
);
|
|
327
|
+
return fn(text, richText, image, boolean, repeater, color, url, select, number, icon);
|
|
328
|
+
} catch (e) {
|
|
329
|
+
console.error("[Schema Parser] Failed to evaluate schema literal:", e.message);
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect enter-hidden animation initials used by motion libraries.
|
|
3
|
+
* Covers both `initial={{ opacity: 0 }}` and `initial="hidden"` +
|
|
4
|
+
* `variants.hidden = { opacity: 0 }`. Theme-agnostic: any object with
|
|
5
|
+
* a hidden enter style counts, regardless of component name.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function isEnterHiddenStyle(style) {
|
|
9
|
+
if (!style || typeof style !== "object") return false;
|
|
10
|
+
if (style.opacity === 0) return true;
|
|
11
|
+
if (typeof style.scale === "number" && style.scale !== 1) return true;
|
|
12
|
+
if (typeof style.x === "number" && style.x !== 0) return true;
|
|
13
|
+
if (typeof style.y === "number" && style.y !== 0) return true;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function resolveInitialStyle(props) {
|
|
18
|
+
if (!props || props.initial == null) return props && props.initial;
|
|
19
|
+
if (typeof props.initial === "string" && props.variants && typeof props.variants === "object") {
|
|
20
|
+
return props.variants[props.initial];
|
|
21
|
+
}
|
|
22
|
+
return props.initial;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const ENTER_VARIANT_NAME = /^(hidden|initial|start|inactive)$/i;
|
|
26
|
+
|
|
27
|
+
export function shouldSkipEnterInitial(props) {
|
|
28
|
+
if (!props || props.initial === false) return false;
|
|
29
|
+
|
|
30
|
+
if (props.initial != null && isEnterHiddenStyle(resolveInitialStyle(props))) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Stagger parents: initial="hidden" + animate="visible" with an empty
|
|
35
|
+
// variants.hidden. Children inherit the named variant and apply their
|
|
36
|
+
// own { opacity: 0 } — those children often never hydrate.
|
|
37
|
+
if (typeof props.initial === "string" && ENTER_VARIANT_NAME.test(props.initial)) {
|
|
38
|
+
if (props.animate === "visible" || props.whileInView === "visible") return true;
|
|
39
|
+
if (props.variants && props.variants.visible) return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Child that only declares variants (initial inherited from parent).
|
|
43
|
+
if (
|
|
44
|
+
props.initial == null &&
|
|
45
|
+
props.variants &&
|
|
46
|
+
isEnterHiddenStyle(props.variants.hidden) &&
|
|
47
|
+
props.variants.visible
|
|
48
|
+
) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Props to use during compile-time SSR so hidden enter states paint visible.
|
|
57
|
+
*/
|
|
58
|
+
export function visibleFirstPaintProps(props) {
|
|
59
|
+
if (!props || !shouldSkipEnterInitial(props)) return props;
|
|
60
|
+
const next = { ...props, initial: false };
|
|
61
|
+
if (props.variants && props.variants.visible && (props.animate == null || props.animate === "visible" || props.whileInView)) {
|
|
62
|
+
next.animate = "visible";
|
|
63
|
+
}
|
|
64
|
+
return next;
|
|
65
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Scans `cms/forms/**` for `defineWpForm(...)` schemas — the same
|
|
6
|
+
* text-extraction + `new Function` eval technique scanForEditableSchemas
|
|
7
|
+
* uses for `cms/editables/**`, simplified since a form config is a plain
|
|
8
|
+
* nested object literal (no field-builder helper functions to inject).
|
|
9
|
+
*
|
|
10
|
+
* The filename becomes the form's key, matching `cms/editables/{slug}.ts`'s
|
|
11
|
+
* filename → slug convention (e.g. `contact.ts` → `contact`).
|
|
12
|
+
*
|
|
13
|
+
* @param {string} themeRoot
|
|
14
|
+
* @returns {Record<string, import('../index.d.ts').ForgeWPFormConfig>} form key → parsed config
|
|
15
|
+
*/
|
|
16
|
+
export function scanForFormSchemas(themeRoot) {
|
|
17
|
+
const forms = {};
|
|
18
|
+
const formsDir = path.join(themeRoot, "cms", "forms");
|
|
19
|
+
if (!existsSync(formsDir)) return forms;
|
|
20
|
+
|
|
21
|
+
const filesToScan = [];
|
|
22
|
+
function collectFilesRecursive(dir) {
|
|
23
|
+
if (!existsSync(dir)) return;
|
|
24
|
+
try {
|
|
25
|
+
const items = readdirSync(dir, { withFileTypes: true });
|
|
26
|
+
for (const item of items) {
|
|
27
|
+
const fullPath = path.join(dir, item.name);
|
|
28
|
+
if (item.isDirectory()) {
|
|
29
|
+
collectFilesRecursive(fullPath);
|
|
30
|
+
} else if (item.isFile() && (item.name.endsWith(".ts") || item.name.endsWith(".tsx"))) {
|
|
31
|
+
filesToScan.push(fullPath);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} catch {}
|
|
35
|
+
}
|
|
36
|
+
collectFilesRecursive(formsDir);
|
|
37
|
+
|
|
38
|
+
for (const filePath of filesToScan) {
|
|
39
|
+
try {
|
|
40
|
+
const content = readFileSync(filePath, "utf8");
|
|
41
|
+
if (!content.includes("defineWpForm")) continue;
|
|
42
|
+
|
|
43
|
+
const schemaStr = extractDefineWpFormContent(content);
|
|
44
|
+
if (!schemaStr) continue;
|
|
45
|
+
|
|
46
|
+
const parsed = parseFormSchema(schemaStr, content);
|
|
47
|
+
if (!parsed) continue;
|
|
48
|
+
|
|
49
|
+
const baseName = path.basename(filePath, path.extname(filePath));
|
|
50
|
+
const slug = baseName
|
|
51
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
52
|
+
.toLowerCase();
|
|
53
|
+
|
|
54
|
+
forms[slug] = parsed;
|
|
55
|
+
} catch (err) {
|
|
56
|
+
console.warn(`[Form Schema Scanner] Failed to scan file ${filePath}:`, err.message);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return forms;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function extractDefineWpFormContent(fileContent) {
|
|
64
|
+
const match = fileContent.match(/export\s+const\s+form\s*=\s*defineWpForm\s*\(/);
|
|
65
|
+
if (!match) return null;
|
|
66
|
+
|
|
67
|
+
const startIndex = match.index + match[0].length;
|
|
68
|
+
let parenCount = 1;
|
|
69
|
+
let currentIndex = startIndex;
|
|
70
|
+
let inStr = null;
|
|
71
|
+
let esc = false;
|
|
72
|
+
let inLineComment = false;
|
|
73
|
+
let inBlockComment = false;
|
|
74
|
+
|
|
75
|
+
while (parenCount > 0 && currentIndex < fileContent.length) {
|
|
76
|
+
const char = fileContent[currentIndex];
|
|
77
|
+
const next = fileContent[currentIndex + 1];
|
|
78
|
+
|
|
79
|
+
if (inLineComment) {
|
|
80
|
+
if (char === "\n") inLineComment = false;
|
|
81
|
+
} else if (inBlockComment) {
|
|
82
|
+
if (char === "*" && next === "/") {
|
|
83
|
+
inBlockComment = false;
|
|
84
|
+
currentIndex++;
|
|
85
|
+
}
|
|
86
|
+
} else if (inStr) {
|
|
87
|
+
if (esc) esc = false;
|
|
88
|
+
else if (char === "\\") esc = true;
|
|
89
|
+
else if (char === inStr) inStr = null;
|
|
90
|
+
} else if (char === "/" && next === "/") {
|
|
91
|
+
inLineComment = true;
|
|
92
|
+
currentIndex++;
|
|
93
|
+
} else if (char === "/" && next === "*") {
|
|
94
|
+
inBlockComment = true;
|
|
95
|
+
currentIndex++;
|
|
96
|
+
} else if (char === "'" || char === '"' || char === "`") {
|
|
97
|
+
inStr = char;
|
|
98
|
+
} else if (char === "(") {
|
|
99
|
+
parenCount++;
|
|
100
|
+
} else if (char === ")") {
|
|
101
|
+
parenCount--;
|
|
102
|
+
}
|
|
103
|
+
currentIndex++;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (parenCount === 0) {
|
|
107
|
+
return fileContent.substring(startIndex, currentIndex - 1);
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Eval a defineWpForm object literal. Same module-level const prelude
|
|
114
|
+
* support as editable schemas (e.g. a shared `const SUBJECT_OPTIONS = [...]`
|
|
115
|
+
* referenced from inside the config), for parity — not required for the
|
|
116
|
+
* common case, which is a fully self-contained object literal.
|
|
117
|
+
*/
|
|
118
|
+
function parseFormSchema(schemaStr, fileContent) {
|
|
119
|
+
let prelude = "";
|
|
120
|
+
try {
|
|
121
|
+
const formIdx = fileContent.indexOf("defineWpForm(");
|
|
122
|
+
prelude = extractModuleLevelConstPrelude(
|
|
123
|
+
fileContent,
|
|
124
|
+
formIdx === -1 ? fileContent.length : formIdx,
|
|
125
|
+
);
|
|
126
|
+
} catch {}
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const fn = new Function(`${prelude}return (${schemaStr});`);
|
|
130
|
+
return fn();
|
|
131
|
+
} catch (e) {
|
|
132
|
+
console.error("[Form Schema Scanner] Failed to evaluate form config literal:", e.message);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Collect `const NAME = <literal>;` at module scope (column 0) before `endIndex`.
|
|
139
|
+
* Uses brace/bracket balancing so mid-function `const x = [a].filter(...)` is ignored.
|
|
140
|
+
* @param {string} fileContent
|
|
141
|
+
* @param {number} endIndex
|
|
142
|
+
* @returns {string} JS prelude statements
|
|
143
|
+
*/
|
|
144
|
+
function extractModuleLevelConstPrelude(fileContent, endIndex) {
|
|
145
|
+
const head = fileContent.slice(0, Math.max(0, endIndex));
|
|
146
|
+
let prelude = "";
|
|
147
|
+
const re = /^(?:export\s+)?const\s+([A-Za-z_$][\w$]*)\s*=\s*/gm;
|
|
148
|
+
let m;
|
|
149
|
+
while ((m = re.exec(head)) !== null) {
|
|
150
|
+
const name = m[1];
|
|
151
|
+
let i = m.index + m[0].length;
|
|
152
|
+
while (i < head.length && /\s/.test(head[i])) i++;
|
|
153
|
+
if (i >= head.length) continue;
|
|
154
|
+
const startChar = head[i];
|
|
155
|
+
let value = null;
|
|
156
|
+
|
|
157
|
+
if (startChar === "'" || startChar === '"' || startChar === "`") {
|
|
158
|
+
const quote = startChar;
|
|
159
|
+
let j = i + 1;
|
|
160
|
+
let esc = false;
|
|
161
|
+
for (; j < head.length; j++) {
|
|
162
|
+
if (esc) {
|
|
163
|
+
esc = false;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (head[j] === "\\") {
|
|
167
|
+
esc = true;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (head[j] === quote) {
|
|
171
|
+
j++;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
while (j < head.length && /\s/.test(head[j])) j++;
|
|
176
|
+
if (head[j] === ";") {
|
|
177
|
+
value = head.slice(i, j);
|
|
178
|
+
}
|
|
179
|
+
} else if (startChar === "[" || startChar === "{") {
|
|
180
|
+
const open = startChar;
|
|
181
|
+
const close = open === "[" ? "]" : "}";
|
|
182
|
+
let depth = 0;
|
|
183
|
+
let j = i;
|
|
184
|
+
let inStr = null;
|
|
185
|
+
let esc = false;
|
|
186
|
+
for (; j < head.length; j++) {
|
|
187
|
+
const c = head[j];
|
|
188
|
+
if (inStr) {
|
|
189
|
+
if (esc) esc = false;
|
|
190
|
+
else if (c === "\\") esc = true;
|
|
191
|
+
else if (c === inStr) inStr = null;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (c === "'" || c === '"' || c === "`") {
|
|
195
|
+
inStr = c;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (c === open) depth++;
|
|
199
|
+
else if (c === close) {
|
|
200
|
+
depth--;
|
|
201
|
+
if (depth === 0) {
|
|
202
|
+
j++;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
while (j < head.length && /\s/.test(head[j])) j++;
|
|
208
|
+
if (head[j] === ";") {
|
|
209
|
+
value = head.slice(i, j);
|
|
210
|
+
}
|
|
211
|
+
} else if (/^-?\d/.test(head.slice(i)) || head.startsWith("true", i) || head.startsWith("false", i) || head.startsWith("null", i)) {
|
|
212
|
+
let j = i;
|
|
213
|
+
while (j < head.length && head[j] !== ";" && head[j] !== "\n") j++;
|
|
214
|
+
if (head[j] === ";") {
|
|
215
|
+
const raw = head.slice(i, j).trim();
|
|
216
|
+
if (/^(-?\d+(\.\d+)?|true|false|null)$/.test(raw)) {
|
|
217
|
+
value = raw;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (value != null) {
|
|
223
|
+
prelude += `const ${name} = ${value};\n`;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return prelude;
|
|
227
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { isComponentInteractive, isExportInteractive, getInteractiveExportNames } from "./is-interactive.js";
|
|
2
|
+
export { getComponentRootClassName } from "./root-class-extractor.js";
|
|
3
|
+
export { scanForHydrationIslands, scanForHydrationIslandsWithProps, findComponentPath } from "./islands-scanner.js";
|
|
4
|
+
export { getHydrationRollupInputs } from "./rollup-inputs.js";
|
|
5
|
+
export { scanForEditableSchemas } from "./editable-schemas.js";
|
|
6
|
+
export { scanForFormSchemas } from "./form-schemas.js";
|
|
7
|
+
export { runStaticLintChecks } from "./static-lint.js";
|
|
8
|
+
export { resolveImportPath, doesComponentUseAuthHooks } from "./utils.js";
|
|
9
|
+
export { transformThemeFile } from "./transform.js";
|
|
10
|
+
export { resolveIslandComponent } from "./resolve-island-component.js";
|
|
11
|
+
export { resolveIslandChunk } from "./resolve-island-chunk.js";
|
|
12
|
+
export { scanAppProviders, fileImportsProvider } from "./scan-app-providers.js";
|