@forgewp/compiler 0.4.3 → 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.
Files changed (133) hide show
  1. package/README.md +59 -0
  2. package/bin/add.js +20 -14
  3. package/bin/cli.js +145 -70
  4. package/bin/doctor.js +21 -12
  5. package/bin/export.js +69 -20
  6. package/bin/fresh.js +5 -0
  7. package/bin/i18n-extract.js +3 -10
  8. package/bin/i18n-translate.js +15 -24
  9. package/bin/make-block.js +13 -0
  10. package/bin/make-editable.js +166 -0
  11. package/bin/make-form.js +131 -0
  12. package/bin/make-loop.js +40 -43
  13. package/bin/make-mail.js +27 -0
  14. package/bin/make-post-type.js +57 -29
  15. package/bin/make-sandbox.js +62 -11
  16. package/bin/make-section.js +221 -0
  17. package/bin/make-shell.js +375 -0
  18. package/bin/repair.js +14 -26
  19. package/bin/seed.js +119 -0
  20. package/bin/sync-routes.js +107 -85
  21. package/lib/adapters/html-adapter.js +5 -0
  22. package/lib/adapters/stub-adapter.js +4 -0
  23. package/lib/add-component.js +153 -92
  24. package/lib/asset-loader.js +313 -3
  25. package/lib/assets/asset-graph.js +432 -0
  26. package/lib/assets/asset-pipeline.js +125 -0
  27. package/lib/assets/diagnostics-report.js +345 -0
  28. package/lib/assets/external-fetcher.js +173 -0
  29. package/lib/assets/font-orchestrator.js +348 -0
  30. package/lib/assets/font-usage-scanner.js +242 -0
  31. package/lib/assets/image-optimizer.js +229 -0
  32. package/lib/assets/index.js +51 -0
  33. package/lib/assets/preload-generator.js +70 -0
  34. package/lib/assets/public-url.js +153 -0
  35. package/lib/block-authoring-help.js +11 -0
  36. package/lib/blocks/ast-parser.js +67 -0
  37. package/lib/blocks/discover-blocks.js +146 -0
  38. package/lib/blocks/editor-scope.js +733 -0
  39. package/lib/blocks/editor-transpiler.js +1229 -0
  40. package/lib/blocks/index.js +2838 -0
  41. package/lib/blocks/php-transpiler.js +4928 -0
  42. package/lib/blocks/scanner.js +1487 -0
  43. package/lib/blocks/shared-utils.js +290 -0
  44. package/lib/blocks/source-sanitize.js +1449 -0
  45. package/lib/blueprints.js +31 -91
  46. package/lib/build-assets.js +38 -33
  47. package/lib/collect-page-template-routes.js +102 -0
  48. package/lib/collect-static-routes.js +175 -0
  49. package/lib/diagnostics.js +58 -33
  50. package/lib/export-theme.js +84 -5
  51. package/lib/extract-template-content.js +90 -0
  52. package/lib/forms-preview.js +42 -0
  53. package/lib/functions/cpt-routing.js +69 -0
  54. package/lib/functions/forms.js +2461 -0
  55. package/lib/functions/hydration-enqueuer.js +400 -0
  56. package/lib/functions/index.js +1623 -0
  57. package/lib/functions/load-menus.js +112 -0
  58. package/lib/functions/load-translations.js +148 -0
  59. package/lib/functions/rest-endpoints.js +2923 -0
  60. package/lib/functions/routing.js +765 -0
  61. package/lib/functions/seed-mock-data.js +392 -0
  62. package/lib/functions/seed-products.js +481 -0
  63. package/lib/functions/settings-page.js +273 -0
  64. package/lib/functions/theme-setup.js +494 -0
  65. package/lib/generate-theme.js +1916 -339
  66. package/lib/hydration/editable-schemas.js +332 -0
  67. package/lib/hydration/enter-hidden-initial.js +65 -0
  68. package/lib/hydration/form-schemas.js +227 -0
  69. package/lib/hydration/index.js +12 -0
  70. package/lib/hydration/is-interactive.js +951 -0
  71. package/lib/hydration/island-split.js +1231 -0
  72. package/lib/hydration/islands-scanner.js +599 -0
  73. package/lib/hydration/resolve-island-chunk.js +103 -0
  74. package/lib/hydration/resolve-island-component.js +52 -0
  75. package/lib/hydration/rollup-inputs.js +81 -0
  76. package/lib/hydration/root-class-extractor.js +220 -0
  77. package/lib/hydration/scan-app-providers.js +154 -0
  78. package/lib/hydration/static-lint.js +382 -0
  79. package/lib/hydration/transform.js +257 -0
  80. package/lib/hydration/uses-block-area.js +49 -0
  81. package/lib/hydration/utils.js +119 -0
  82. package/lib/i18n/translation-cache.js +141 -0
  83. package/lib/icon-registry.js +72 -0
  84. package/lib/index.d.ts +287 -8
  85. package/lib/index.js +2 -1
  86. package/lib/load-config.js +338 -242
  87. package/lib/mail-templates.js +37 -0
  88. package/lib/markup-processor.js +388 -102
  89. package/lib/node-runtime.js +7 -0
  90. package/lib/page-config-plugin.js +64 -20
  91. package/lib/php-builders.js +383 -44
  92. package/lib/react-adapter.js +983 -235
  93. package/lib/registration-metadata.js +11 -0
  94. package/lib/render-theme.mts +1410 -616
  95. package/lib/route-layout.js +76 -0
  96. package/lib/template-discovery.js +58 -0
  97. package/lib/theme-compatibility.js +32 -0
  98. package/lib/validate-export.js +2 -2
  99. package/lib/validate.js +27 -5
  100. package/lib/virtual-plugin.js +319 -0
  101. package/lib/walkthrough.md +26 -0
  102. package/package.json +16 -5
  103. package/templates/auth/cms/email-logs.json +9 -0
  104. package/templates/auth/cms/menus.json +11 -0
  105. package/templates/auth/cms/mock-data.json +123 -0
  106. package/templates/auth/cms/roles.json +7 -0
  107. package/templates/auth/cms/sessions.json +1 -0
  108. package/templates/auth/cms/site-settings.json +28 -0
  109. package/templates/auth/cms/translations.json +4 -0
  110. package/templates/auth/cms/users.json +46 -0
  111. package/templates/auth/components/forgot-password-form-wrapper.tsx +10 -2
  112. package/templates/auth/components/login-form-wrapper.tsx +118 -21
  113. package/templates/auth/components/reset-password-form-wrapper.tsx +10 -2
  114. package/templates/auth/components/signup-form-wrapper.tsx +2 -2
  115. package/templates/auth/components/verify-email-view-wrapper.tsx +5 -5
  116. package/templates/auth/pages/DashboardPage.tsx +1 -4
  117. package/templates/auth/pages/ForgotPasswordPage.tsx +1 -4
  118. package/templates/auth/pages/LoginPage.tsx +1 -4
  119. package/templates/auth/pages/ResetPasswordPage.tsx +1 -4
  120. package/templates/auth/pages/SignUpPage.tsx +1 -4
  121. package/templates/auth/pages/VerifyEmailPage.tsx +1 -4
  122. package/templates/auth/ui/button.tsx +57 -0
  123. package/templates/auth/ui/card.tsx +76 -0
  124. package/templates/auth/ui/field.tsx +242 -0
  125. package/templates/auth/ui/input.tsx +22 -0
  126. package/templates/auth/ui/label.tsx +24 -0
  127. package/templates/auth/ui/separator.tsx +31 -0
  128. package/templates/menus.json +12 -10
  129. package/templates/users.json +8 -4
  130. package/templates/wordpress.tsx +690 -245
  131. package/lib/block-compiler.js +0 -552
  132. package/lib/functions-builder.js +0 -2970
  133. package/lib/hydration-scanner.js +0 -833
@@ -0,0 +1,290 @@
1
+ import { readFileSync, existsSync, writeFileSync, rmSync, readdirSync, statSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { createRequire } from "node:module";
4
+ import { execSync } from "node:child_process";
5
+ import { tmpdir } from "node:os";
6
+ import { pathToFileURL } from "node:url";
7
+
8
+ export const _svgHtmlCache = new Map();
9
+ const fallbackRequire = createRequire(import.meta.url);
10
+
11
+ function loadIconPackageSync(packageName, themeRoot) {
12
+ globalThis._forgewp_icon_packages = globalThis._forgewp_icon_packages || {};
13
+ if (globalThis._forgewp_icon_packages[packageName]) {
14
+ return globalThis._forgewp_icon_packages[packageName];
15
+ }
16
+ try {
17
+ const pkgJsonFile = themeRoot ? path.join(themeRoot, 'package.json') : null;
18
+ const projectRequire = pkgJsonFile && existsSync(pkgJsonFile) ? createRequire(pkgJsonFile) : fallbackRequire;
19
+ const resolveMod = (id) => {
20
+ try { return projectRequire(id); } catch (_) { return fallbackRequire(id); }
21
+ };
22
+ if (!globalThis._forgewp_icon_packages['react']) {
23
+ globalThis._forgewp_icon_packages['react'] = resolveMod('react');
24
+ }
25
+ if (!globalThis._forgewp_icon_packages['react-dom/server']) {
26
+ const rdMod = resolveMod('react-dom/server');
27
+ const renderToStaticMarkup = rdMod.renderToStaticMarkup || rdMod.default?.renderToStaticMarkup;
28
+ globalThis._forgewp_icon_packages['react-dom/server'] = { renderToStaticMarkup };
29
+ }
30
+ let pkgJsonPath;
31
+ try { pkgJsonPath = projectRequire.resolve(`${packageName}/package.json`); }
32
+ catch (_) { pkgJsonPath = fallbackRequire.resolve(`${packageName}/package.json`); }
33
+ const pkgDir = path.dirname(pkgJsonPath);
34
+ const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
35
+ const exp = pkgJson.exports?.['.'];
36
+ const cjsRel =
37
+ (typeof exp?.require === 'string' ? exp.require : exp?.require?.default) ||
38
+ (typeof exp?.node === 'string' ? exp.node : exp?.node?.default) ||
39
+ (!pkgJson.type || pkgJson.type === 'commonjs' ? pkgJson.main : null);
40
+ if (cjsRel) {
41
+ const cjsPath = path.resolve(pkgDir, cjsRel);
42
+ if (existsSync(cjsPath)) {
43
+ const mod = { exports: {} };
44
+ const fn = new Function('exports', 'require', 'module', '__filename', '__dirname', readFileSync(cjsPath, 'utf8'));
45
+ const localReq = createRequire(cjsPath);
46
+ fn(mod.exports, (id) => { try { return localReq(id); } catch (_) { return resolveMod(id); } }, mod, cjsPath, path.dirname(cjsPath));
47
+ globalThis._forgewp_icon_packages[packageName] = mod.exports;
48
+ return mod.exports;
49
+ }
50
+ }
51
+ } catch (_) {}
52
+ return null;
53
+ }
54
+
55
+ export async function warmupIconCache(themeRoot) {
56
+ try {
57
+ const pkgJsonPath = path.join(themeRoot, 'package.json');
58
+ if (!existsSync(pkgJsonPath)) return;
59
+ const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
60
+ const deps = { ...pkgJson.dependencies, ...pkgJson.devDependencies };
61
+
62
+ const srcDir = path.join(themeRoot, 'src');
63
+ const discoveredPackages = new Set();
64
+
65
+ // Recursively scan src directory to find external package imports with PascalCase elements
66
+ function scanDir(dir) {
67
+ if (!existsSync(dir)) return;
68
+ const entries = readdirSync(dir);
69
+ for (const entry of entries) {
70
+ const fullPath = path.join(dir, entry);
71
+ const stat = statSync(fullPath);
72
+ if (stat.isDirectory()) {
73
+ scanDir(fullPath);
74
+ } else if (stat.isFile() && /\.(tsx|jsx|ts|js)$/.test(entry)) {
75
+ try {
76
+ const content = readFileSync(fullPath, 'utf8');
77
+ const importRx = /import\s+(?:type\s+)?(?:\{([^}]+)\}|([A-Za-z0-9_$]+))\s+from\s+['"]([^'"]+)['"]/g;
78
+ let match;
79
+ while ((match = importRx.exec(content)) !== null) {
80
+ const namedImports = match[1];
81
+ const defaultImport = match[2];
82
+ const fromPkg = match[3];
83
+
84
+ if (fromPkg.startsWith('.') || fromPkg.startsWith('@/') || fromPkg.startsWith('react') || fromPkg.startsWith('next')) {
85
+ continue;
86
+ }
87
+
88
+ let hasPascalCase = false;
89
+ if (namedImports) {
90
+ for (const name of namedImports.split(',')) {
91
+ const n = name.replace(/\s+as\s+\S+/, '').trim();
92
+ if (n && /^[A-Z]/.test(n)) {
93
+ hasPascalCase = true;
94
+ break;
95
+ }
96
+ }
97
+ } else if (defaultImport && /^[A-Z]/.test(defaultImport)) {
98
+ hasPascalCase = true;
99
+ }
100
+
101
+ if (hasPascalCase && deps[fromPkg]) {
102
+ discoveredPackages.add(fromPkg);
103
+ }
104
+ }
105
+ } catch (_) {}
106
+ }
107
+ }
108
+ }
109
+ scanDir(srcDir);
110
+
111
+ const iconPackages = Array.from(discoveredPackages);
112
+
113
+ if (iconPackages.length === 0) return;
114
+
115
+ const projectRequire = createRequire(pkgJsonPath);
116
+
117
+ // Pre-import React and React DOM Server
118
+ const reactEntry = projectRequire.resolve('react');
119
+ const rdEntry = projectRequire.resolve('react-dom/server');
120
+ const React = (await import(pathToFileURL(reactEntry).href)).default || (await import(pathToFileURL(reactEntry).href));
121
+ const rdMod = await import(pathToFileURL(rdEntry).href);
122
+ const renderToStaticMarkup = rdMod.renderToStaticMarkup || rdMod.default?.renderToStaticMarkup;
123
+
124
+ globalThis._forgewp_icon_packages = globalThis._forgewp_icon_packages || {};
125
+ globalThis._forgewp_icon_packages['react'] = React;
126
+ globalThis._forgewp_icon_packages['react-dom/server'] = { renderToStaticMarkup };
127
+
128
+ for (const pkgName of iconPackages) {
129
+ if (loadIconPackageSync(pkgName, themeRoot)) continue;
130
+ try {
131
+ const pkgJsonPath = projectRequire.resolve(`${pkgName}/package.json`);
132
+ const pkgDir = path.dirname(pkgJsonPath);
133
+ const resolvedPkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
134
+ const esmRelEntry = resolvedPkgJson.module || resolvedPkgJson.exports?.['.']?.import || resolvedPkgJson.main;
135
+ const pkgEntry = path.resolve(pkgDir, esmRelEntry);
136
+
137
+ const pkg = await import(pathToFileURL(pkgEntry).href);
138
+ globalThis._forgewp_icon_packages[pkgName] = pkg;
139
+ } catch (e) {
140
+ // Quietly fail for non-esm/non-icon libraries to avoid console noise
141
+ }
142
+ }
143
+ } catch (e) {
144
+ console.warn('[ForgeWP Compiler] Failed to warmup icon cache:', e.message);
145
+ }
146
+ }
147
+
148
+
149
+
150
+ export function resolvePackageEsmEntry(packageName, themeRoot) {
151
+ const pr = createRequire(path.join(themeRoot, 'package.json'));
152
+ const pkgJsonPath = pr.resolve(`${packageName}/package.json`);
153
+ const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
154
+ const entry = pkgJson.module || pkgJson.exports?.['.']?.import || pkgJson.main;
155
+ return pr.resolve(`${packageName}/${entry}`);
156
+ }
157
+
158
+ /**
159
+ * Dynamically resolves an icon component to an SVG string by spawning a tiny
160
+ * ESM child process that uses pathToFileURL + dynamic import + renderToStaticMarkup.
161
+ */
162
+ export function resolveIconToSvgHtml(componentName, packageName, themeRoot, classNameStr) {
163
+ const cacheKey = `${packageName}::${componentName}::${classNameStr}`;
164
+ if (_svgHtmlCache.has(cacheKey)) return _svgHtmlCache.get(cacheKey);
165
+
166
+ // 1. Try to render from in-process cache (warming synchronously via CJS entry when available)
167
+ loadIconPackageSync(packageName, themeRoot);
168
+ if (globalThis._forgewp_icon_packages && globalThis._forgewp_icon_packages[packageName]) {
169
+ const pkg = globalThis._forgewp_icon_packages[packageName];
170
+ const React = globalThis._forgewp_icon_packages['react'];
171
+ const rd = globalThis._forgewp_icon_packages['react-dom/server'];
172
+
173
+ if (pkg && React && rd) {
174
+ const Icon = pkg[componentName];
175
+ if (Icon) {
176
+ const FinalIcon = (typeof Icon === 'function') ? Icon : (Icon && (Icon.$$typeof || Icon.render) ? Icon : (Icon && Icon.default ? Icon.default : null));
177
+ if (FinalIcon) {
178
+ try {
179
+ const html = rd.renderToStaticMarkup(React.createElement(FinalIcon, { className: classNameStr || '' }));
180
+ if (html && html.startsWith('<svg')) {
181
+ _svgHtmlCache.set(cacheKey, html);
182
+ return html;
183
+ }
184
+ } catch (e) {
185
+ console.warn(`[ForgeWP Compiler] Cache render failed for ${componentName}:`, e.message);
186
+ }
187
+ }
188
+ }
189
+ }
190
+ }
191
+
192
+
193
+ try {
194
+ const projectRequire = createRequire(path.join(themeRoot, 'package.json'));
195
+
196
+ // Resolve absolute ESM entry for the icon package
197
+ const pkgJsonPath = projectRequire.resolve(`${packageName}/package.json`);
198
+ const pkgDir = path.dirname(pkgJsonPath);
199
+ const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
200
+ const esmRelEntry = pkgJson.module || pkgJson.exports?.['.']?.import || pkgJson.main;
201
+ const pkgEntry = path.resolve(pkgDir, esmRelEntry);
202
+
203
+ // React and react-dom/server can use their default resolution
204
+ const reactEntry = projectRequire.resolve('react');
205
+ const rdEntry = projectRequire.resolve('react-dom/server');
206
+
207
+ const lines = [
208
+ `import { pathToFileURL } from 'url';`,
209
+ `const React = (await import(pathToFileURL(${JSON.stringify(reactEntry)}).href)).default;`,
210
+ `const rdMod = await import(pathToFileURL(${JSON.stringify(rdEntry)}).href);`,
211
+ `const renderToStaticMarkup = rdMod.renderToStaticMarkup || rdMod.default?.renderToStaticMarkup;`,
212
+ `const pkg = await import(pathToFileURL(${JSON.stringify(pkgEntry)}).href);`,
213
+ `const Icon = pkg[${JSON.stringify(componentName)}];`,
214
+ `const FinalIcon = (typeof Icon === 'function') ? Icon : (Icon && (Icon.$$typeof || Icon.render) ? Icon : (Icon && Icon.default ? Icon.default : null));`,
215
+ `if (!FinalIcon) { process.stdout.write(''); process.exit(0); }`,
216
+ `const html = renderToStaticMarkup(React.createElement(FinalIcon, { className: ${JSON.stringify(classNameStr || '')} }));`,
217
+ `process.stdout.write(html);`,
218
+ ];
219
+
220
+ const tmpFile = path.join(tmpdir(), `_forgewp_icon_${Date.now()}.mjs`);
221
+ writeFileSync(tmpFile, lines.join('\n'), 'utf8');
222
+
223
+ let svgHtml = '';
224
+ try {
225
+ svgHtml = execSync(`node ${JSON.stringify(tmpFile)}`, {
226
+ encoding: 'utf8',
227
+ timeout: 10000,
228
+ }).trim();
229
+ } finally {
230
+ try { rmSync(tmpFile); } catch (_) {}
231
+ }
232
+
233
+ if (!svgHtml || !svgHtml.startsWith('<svg')) {
234
+ _svgHtmlCache.set(cacheKey, null);
235
+ return null;
236
+ }
237
+
238
+ _svgHtmlCache.set(cacheKey, svgHtml);
239
+ return svgHtml;
240
+ } catch (_) {
241
+ _svgHtmlCache.set(cacheKey, null);
242
+ return null;
243
+ }
244
+ }
245
+
246
+ export function findHtmlTagEnd(code, startPos) {
247
+ let depth = 0;
248
+ let braceDepth = 0;
249
+ let inQuote = false;
250
+ let quoteChar = '';
251
+ let insidePhp = false;
252
+ let pos = startPos;
253
+ while (pos < code.length) {
254
+ const c = code[pos];
255
+
256
+ if (insidePhp) {
257
+ if (code.substring(pos, pos + 2) === '?>') {
258
+ insidePhp = false;
259
+ pos += 2;
260
+ continue;
261
+ }
262
+ } else if (code.substring(pos, pos + 5) === '<?php') {
263
+ insidePhp = true;
264
+ pos += 5;
265
+ continue;
266
+ } else if (inQuote) {
267
+ if (c === quoteChar) {
268
+ inQuote = false;
269
+ }
270
+ } else if (c === '"' || c === "'" || c === '`') {
271
+ inQuote = true;
272
+ quoteChar = c;
273
+ } else if (c === '{') {
274
+ braceDepth++;
275
+ } else if (c === '}') {
276
+ if (braceDepth > 0) braceDepth--;
277
+ } else if (braceDepth === 0) {
278
+ if (c === '<') {
279
+ depth++;
280
+ } else if (c === '>') {
281
+ depth--;
282
+ if (depth === 0) {
283
+ return pos;
284
+ }
285
+ }
286
+ }
287
+ pos++;
288
+ }
289
+ return -1;
290
+ }