@forgewp/compiler 0.3.0 → 0.4.2
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 +24 -5
- package/bin/analyze.js +158 -0
- package/bin/cli.js +33 -4
- package/bin/doctor.js +125 -11
- package/bin/export.js +20 -0
- package/bin/fresh.js +32 -343
- package/bin/i18n-extract.js +121 -0
- package/bin/i18n-translate.js +112 -0
- package/bin/make-block.js +27 -60
- package/bin/make-island.js +72 -0
- package/bin/make-post-type.js +6 -6
- package/bin/make-template.js +145 -0
- package/bin/repair.js +2 -2
- package/bin/sync-hooks.js +221 -0
- package/bin/sync-routes.js +28 -158
- package/define-config.d.ts +1 -0
- package/define-config.js +1 -0
- package/lib/adapters/html-adapter.js +65 -0
- package/lib/adapters/stub-adapter.js +49 -0
- package/lib/add-component.js +282 -183
- package/lib/asset-loader.js +27 -0
- package/lib/block-compiler.js +552 -0
- package/lib/blueprints.js +15 -501
- package/lib/define-config.d.ts +3 -0
- package/lib/define-config.js +8 -0
- package/lib/diagnostics.js +798 -0
- package/lib/export-theme.js +81 -18
- package/lib/framework-adapter.js +34 -0
- package/lib/functions-builder.js +2044 -0
- package/lib/generate-theme.js +1140 -1352
- package/lib/hydration-scanner.js +663 -21
- package/lib/i18n/extractor.js +60 -0
- package/lib/i18n/providers.js +165 -0
- package/lib/index.d.ts +115 -3
- package/lib/index.js +19 -5
- package/lib/load-config.js +122 -6
- package/lib/markup-processor.js +589 -0
- package/lib/php-builders.js +381 -0
- package/lib/react-adapter.js +1336 -0
- package/lib/render-static.js +9 -82
- package/lib/render-theme.mts +543 -232
- package/lib/screenshot-fallback.png +0 -0
- package/lib/types.js +1 -0
- package/lib/validate-export.js +183 -0
- package/package.json +15 -3
- package/templates/PresetsStyle.tsx +74 -0
- package/templates/SEO.tsx +109 -0
- package/templates/forgewp-config.ts +43 -0
- package/templates/menus.json +11 -0
- package/templates/mock-data.json +434 -0
- package/templates/wordpress.tsx +1297 -0
- package/bin/make-component.js +0 -200
package/bin/sync-routes.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import pc from "picocolors";
|
|
6
|
+
import { loadConfig } from "../lib/load-config.js";
|
|
7
|
+
import { loadFrameworkAdapter } from "../lib/framework-adapter.js";
|
|
6
8
|
|
|
7
9
|
console.log(`\n⚡ ${pc.bold(pc.bgCyan(pc.black(" FORGEWP SITEMAP COMPILER (SYNC:ROUTES) ")))}\n`);
|
|
8
10
|
|
|
@@ -11,18 +13,18 @@ const args = process.argv.slice(2);
|
|
|
11
13
|
const isForce = args.includes("--force") || args.includes("--override");
|
|
12
14
|
|
|
13
15
|
// 1. Locate menus configuration file
|
|
14
|
-
let menusPath = path.join(projectRoot, "
|
|
16
|
+
let menusPath = path.join(projectRoot, "cms", "menus.json");
|
|
15
17
|
let menus = null;
|
|
16
18
|
|
|
17
19
|
if (!existsSync(menusPath)) {
|
|
18
20
|
// Try fallback in mock-data.json
|
|
19
|
-
const mockDataPath = path.join(projectRoot, "
|
|
21
|
+
const mockDataPath = path.join(projectRoot, "cms", "mock-data.json");
|
|
20
22
|
if (existsSync(mockDataPath)) {
|
|
21
23
|
try {
|
|
22
24
|
const mockData = JSON.parse(readFileSync(mockDataPath, "utf8"));
|
|
23
25
|
if (mockData.menu) {
|
|
24
26
|
menus = mockData.menu;
|
|
25
|
-
console.log(` ℹ️ Using legacy menus block inside:
|
|
27
|
+
console.log(` ℹ️ Using legacy menus block inside: cms/mock-data.json`);
|
|
26
28
|
}
|
|
27
29
|
} catch (e) {
|
|
28
30
|
// Ignored
|
|
@@ -48,14 +50,14 @@ if (!existsSync(menusPath)) {
|
|
|
48
50
|
mkdirSync(dir, { recursive: true });
|
|
49
51
|
}
|
|
50
52
|
writeFileSync(menusPath, JSON.stringify(menus, null, 2), "utf8");
|
|
51
|
-
console.log(` 🎉 ${pc.green("Created dedicated sitemap source")}:
|
|
53
|
+
console.log(` 🎉 ${pc.green("Created dedicated sitemap source")}: cms/menus.json`);
|
|
52
54
|
}
|
|
53
55
|
} else {
|
|
54
56
|
try {
|
|
55
57
|
menus = JSON.parse(readFileSync(menusPath, "utf8"));
|
|
56
|
-
console.log(` 📖 ${pc.green("Loaded sitemap source")}:
|
|
58
|
+
console.log(` 📖 ${pc.green("Loaded sitemap source")}: cms/menus.json`);
|
|
57
59
|
} catch (err) {
|
|
58
|
-
console.error(pc.red(`\n❌ Error: Failed to parse
|
|
60
|
+
console.error(pc.red(`\n❌ Error: Failed to parse cms/menus.json.\nReason: ${err.message}`));
|
|
59
61
|
process.exit(1);
|
|
60
62
|
}
|
|
61
63
|
}
|
|
@@ -101,160 +103,28 @@ if (routesToScaffold.length === 0) {
|
|
|
101
103
|
process.exit(0);
|
|
102
104
|
}
|
|
103
105
|
|
|
104
|
-
//
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.join("") + "Page";
|
|
106
|
+
// Load configuration and invoke adapter
|
|
107
|
+
let frameworkAdapter = "react";
|
|
108
|
+
try {
|
|
109
|
+
const config = await loadConfig(projectRoot);
|
|
110
|
+
frameworkAdapter = config.frameworkAdapter || "react";
|
|
111
|
+
} catch (err) {
|
|
112
|
+
// Use default
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
for (const route of routesToScaffold) {
|
|
125
|
-
const componentName = toPascalCase(route.title);
|
|
126
|
-
const componentPath = path.join(pagesDir, `${componentName}.tsx`);
|
|
127
|
-
|
|
128
|
-
const componentContent = `/**
|
|
129
|
-
* ⚡ Auto-Generated Page Component by ForgeWP
|
|
130
|
-
*
|
|
131
|
-
* This file was generated automatically from your sitemap configuration (wordpress/menus.json).
|
|
132
|
-
* You can safely edit this file to customize the visual layout, styles, and logic.
|
|
133
|
-
* Subsequent runs of 'pnpm forgewp sync:routes' will NOT overwrite your changes.
|
|
134
|
-
*
|
|
135
|
-
* To force reset this page back to boilerplate defaults, run:
|
|
136
|
-
* 'pnpm forgewp sync:routes --force'
|
|
137
|
-
*/
|
|
138
|
-
import { SEO } from "../../.forgewp/SEO";
|
|
139
|
-
import { WpQueryLoop, useWpTitle, useWpExcerpt, useWpFeaturedImage } from "../../.forgewp/wordpress";
|
|
140
|
-
|
|
141
|
-
export function ${componentName}() {
|
|
142
|
-
return (
|
|
143
|
-
<main className="container mx-auto px-6 py-12">
|
|
144
|
-
{/* Dynamic SEO Injector */}
|
|
145
|
-
<SEO
|
|
146
|
-
title="${route.title}"
|
|
147
|
-
description="Explore our exclusive ${route.title} section, dynamically loaded in Headless React."
|
|
148
|
-
/>
|
|
149
|
-
|
|
150
|
-
{/* Hero Header Area */}
|
|
151
|
-
<header className="border-b-4 border-black pb-6 mb-12">
|
|
152
|
-
<h1 className="text-5xl font-black tracking-tight uppercase">${route.title}</h1>
|
|
153
|
-
<p className="text-zinc-500 mt-2 text-lg">
|
|
154
|
-
Auto-generated template. Edit <code className="bg-zinc-100 px-1 py-0.5 rounded text-sm text-red-600 font-mono">src/app/pages/${componentName}.tsx</code> to customize this page.
|
|
155
|
-
</p>
|
|
156
|
-
</header>
|
|
157
|
-
|
|
158
|
-
{/* Grid Starter - WordPress Mock Loop */}
|
|
159
|
-
<section className="grid grid-cols-1 md:grid-cols-3 gap-8">
|
|
160
|
-
<WpQueryLoop postType="post" postsPerPage={3}>
|
|
161
|
-
<article className="border-2 border-black p-6 bg-white shadow-[4px_4px_0px_0px_rgba(0,0,0,1)] hover:translate-y-[-2px] hover:shadow-[6px_6px_0px_0px_rgba(0,0,0,1)] transition-all">
|
|
162
|
-
<div className="w-full h-48 bg-zinc-100 mb-4 border border-zinc-200 overflow-hidden">
|
|
163
|
-
<img
|
|
164
|
-
src={useWpFeaturedImage()}
|
|
165
|
-
alt={useWpTitle()}
|
|
166
|
-
className="w-full h-full object-cover"
|
|
167
|
-
/>
|
|
168
|
-
</div>
|
|
169
|
-
<h2 className="text-xl font-bold uppercase tracking-tight mb-2">
|
|
170
|
-
{useWpTitle()}
|
|
171
|
-
</h2>
|
|
172
|
-
<p className="text-zinc-600 text-sm mb-4">
|
|
173
|
-
{useWpExcerpt()}
|
|
174
|
-
</p>
|
|
175
|
-
<a
|
|
176
|
-
href="#"
|
|
177
|
-
className="inline-block px-4 py-2 border-2 border-black bg-zinc-100 font-bold uppercase text-xs hover:bg-black hover:text-white transition-colors"
|
|
178
|
-
>
|
|
179
|
-
Read More
|
|
180
|
-
</a>
|
|
181
|
-
</article>
|
|
182
|
-
</WpQueryLoop>
|
|
183
|
-
</section>
|
|
184
|
-
</main>
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
`;
|
|
188
|
-
|
|
189
|
-
if (existsSync(componentPath) && !isForce) {
|
|
190
|
-
console.log(` ${pc.gray("ℹ️ [Skip] Component already exists:")} src/app/pages/${componentName}.tsx`);
|
|
191
|
-
skippedCount++;
|
|
192
|
-
} else {
|
|
193
|
-
writeFileSync(componentPath, componentContent, "utf8");
|
|
194
|
-
console.log(` ${pc.green("✅ Scaffolded component")}: src/app/pages/${componentName}.tsx`);
|
|
195
|
-
scaffoldedCount++;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// 5. Inject pages into src/app/routes.tsx
|
|
200
|
-
const routesFilePath = path.join(projectRoot, "src/app/routes.tsx");
|
|
201
|
-
if (existsSync(routesFilePath)) {
|
|
202
|
-
let routesContent = readFileSync(routesFilePath, "utf8");
|
|
203
|
-
let modified = false;
|
|
204
|
-
|
|
205
|
-
for (const route of routesToScaffold) {
|
|
206
|
-
const componentName = toPascalCase(route.title);
|
|
207
|
-
|
|
208
|
-
// Check if the component is imported
|
|
209
|
-
const importRegex = new RegExp(`import\\s+\\{\\s*${componentName}\\s*\\}\\s+from\\s+["']\\./pages/${componentName}["']`);
|
|
210
|
-
if (!importRegex.test(routesContent)) {
|
|
211
|
-
// Find a clean place to inject imports
|
|
212
|
-
// Right above "export default function" or under other imports
|
|
213
|
-
const defaultExportIndex = routesContent.indexOf("export default function");
|
|
214
|
-
if (defaultExportIndex !== -1) {
|
|
215
|
-
routesContent =
|
|
216
|
-
routesContent.slice(0, defaultExportIndex) +
|
|
217
|
-
`import { ${componentName} } from "./pages/${componentName}";\n` +
|
|
218
|
-
routesContent.slice(defaultExportIndex);
|
|
219
|
-
modified = true;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// Check if the Route path is registered
|
|
224
|
-
const routeRegex = new RegExp(`path\\s*=\\s*["']${route.path}["']`);
|
|
225
|
-
if (!routeRegex.test(routesContent)) {
|
|
226
|
-
// Find the fallback route or closing Switch tag to inject
|
|
227
|
-
const fallbackMarker = "{/* Fallback route */}";
|
|
228
|
-
const fallbackIndex = routesContent.indexOf(fallbackMarker);
|
|
229
|
-
if (fallbackIndex !== -1) {
|
|
230
|
-
routesContent =
|
|
231
|
-
routesContent.slice(0, fallbackIndex) +
|
|
232
|
-
`<Route path="${route.path}" component={${componentName}} />\n\n ` +
|
|
233
|
-
routesContent.slice(fallbackIndex);
|
|
234
|
-
modified = true;
|
|
235
|
-
} else {
|
|
236
|
-
// Fallback to closing Switch tag
|
|
237
|
-
const switchCloseIndex = routesContent.indexOf("</Switch>");
|
|
238
|
-
if (switchCloseIndex !== -1) {
|
|
239
|
-
routesContent =
|
|
240
|
-
routesContent.slice(0, switchCloseIndex) +
|
|
241
|
-
` <Route path="${route.path}" component={${componentName}} />\n ` +
|
|
242
|
-
routesContent.slice(switchCloseIndex);
|
|
243
|
-
modified = true;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (modified) {
|
|
250
|
-
writeFileSync(routesFilePath, routesContent, "utf8");
|
|
251
|
-
console.log(` ${pc.green("✅ Synced routing paths in")}: src/app/routes.tsx`);
|
|
115
|
+
try {
|
|
116
|
+
const adapterModule = await loadFrameworkAdapter(frameworkAdapter);
|
|
117
|
+
const adapter = adapterModule.default || adapterModule;
|
|
118
|
+
if (typeof adapter.onSyncRoutes === "function") {
|
|
119
|
+
await adapter.onSyncRoutes(projectRoot, {
|
|
120
|
+
routesToScaffold,
|
|
121
|
+
isForce,
|
|
122
|
+
pc
|
|
123
|
+
});
|
|
252
124
|
} else {
|
|
253
|
-
console.log(
|
|
125
|
+
console.log(pc.yellow(` ⚠️ No sync:routes hook implemented for framework adapter: ${frameworkAdapter}`));
|
|
254
126
|
}
|
|
255
|
-
}
|
|
256
|
-
console.
|
|
127
|
+
} catch (err) {
|
|
128
|
+
console.error(pc.red(`\n❌ Failed to execute sync:routes: ${err.message}`));
|
|
129
|
+
process.exit(1);
|
|
257
130
|
}
|
|
258
|
-
|
|
259
|
-
console.log("\n" + "─".repeat(60));
|
|
260
|
-
console.log(pc.green(`\n🎉 ${pc.bold("SITEMAP SYNC COMPLETE:")} Generated ${scaffoldedCount} new pages (skipped ${skippedCount}).\n`));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { defineConfig } from './lib/define-config.js';
|
package/define-config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { defineConfig } from './lib/define-config.js';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Static HTML adapter proof-of-concept.
|
|
6
|
+
* This adapter reads a prebuilt static HTML entrypoint and returns it as app markup.
|
|
7
|
+
*/
|
|
8
|
+
export async function renderStaticMarkup(themeRoot) {
|
|
9
|
+
const builtHtml = path.join(themeRoot, 'dist', 'index.html');
|
|
10
|
+
if (!existsSync(builtHtml)) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
`Static HTML adapter requires a built HTML entrypoint at ${builtHtml}. ` +
|
|
13
|
+
'Run your static theme build first.',
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const appHtml = readFileSync(builtHtml, 'utf8');
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
appHtml,
|
|
21
|
+
headerHtml: '',
|
|
22
|
+
footerHtml: '',
|
|
23
|
+
headHtml: '',
|
|
24
|
+
singleHtml: '',
|
|
25
|
+
singleHeadHtml: '',
|
|
26
|
+
notFoundHtml: '',
|
|
27
|
+
archiveHtml: '',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function scanForHydrationIslands(themeRoot) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function findComponentPath(themeRoot, kebabName) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getHydrationRollupInputs(themeRoot) {
|
|
40
|
+
return {
|
|
41
|
+
index: path.resolve(themeRoot, 'index.html'),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getCriticalFiles(themeRoot) {
|
|
46
|
+
return ['src/main.tsx'];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function onFresh(themeRoot) {
|
|
50
|
+
// Static HTML adapter uses a single index.html and does not require routes.tsx or page.tsx setups.
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function onMakeBlock(themeRoot, { pascalCase, pc }) {
|
|
54
|
+
console.log(pc.yellow(`\n⚠️ The Static HTML framework adapter does not support custom Gutenberg blocks.`));
|
|
55
|
+
console.log(` Block "${pascalCase}" was not created.\n`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function onMakeComponent(themeRoot, { pascalCase, pc }) {
|
|
59
|
+
console.log(pc.yellow(`\n⚠️ The Static HTML framework adapter does not support custom loop components.`));
|
|
60
|
+
console.log(` Component "${pascalCase}" was not created.\n`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function onSyncRoutes(themeRoot, { pc }) {
|
|
64
|
+
console.log(pc.gray(`\nℹ️ The Static HTML framework adapter uses static HTML. Route synchronization skipped.\n`));
|
|
65
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Example adapter stub for a non-React framework.
|
|
5
|
+
* Replace these methods with framework-specific rendering and hydration logic.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export async function renderStaticMarkup(themeRoot) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
'Stub adapter cannot render static markup. Implement renderStaticMarkup() for your framework.',
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function scanForHydrationIslands(themeRoot) {
|
|
15
|
+
// Return a list of kebab-case hydration island identifiers.
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function findComponentPath(themeRoot, kebabName) {
|
|
20
|
+
// Resolve a component source path for hydration input generation.
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getHydrationRollupInputs(themeRoot) {
|
|
25
|
+
// Provide default Vite entrypoints for the theme build.
|
|
26
|
+
return {
|
|
27
|
+
index: path.resolve(themeRoot, 'index.html'),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getCriticalFiles(themeRoot) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function onFresh(themeRoot) {
|
|
36
|
+
// Stub adapter onFresh no-op.
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function onMakeBlock(themeRoot, { pascalCase, pc }) {
|
|
40
|
+
console.log(pc.yellow(`\n⚠️ The Stub framework adapter does not support custom Gutenberg blocks.`));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function onMakeComponent(themeRoot, { pascalCase, pc }) {
|
|
44
|
+
console.log(pc.yellow(`\n⚠️ The Stub framework adapter does not support custom loop components.`));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function onSyncRoutes(themeRoot, { pc }) {
|
|
48
|
+
console.log(pc.gray(`\nℹ️ The Stub framework adapter route synchronization skipped.\n`));
|
|
49
|
+
}
|