@forgewp/compiler 0.1.7 → 0.1.9
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 +29 -2
- package/bin/add.js +33 -3
- package/bin/clean.js +37 -0
- package/bin/cli.js +97 -0
- package/bin/doctor.js +99 -0
- package/bin/export.js +0 -0
- package/bin/fresh.js +421 -0
- package/bin/make-block.js +130 -0
- package/bin/make-component.js +200 -0
- package/bin/make-post-type.js +129 -0
- package/bin/repair.js +69 -0
- package/bin/sync-routes.js +260 -0
- package/lib/add-component.js +19 -4
- package/lib/blueprints.js +501 -316
- package/lib/generate-theme.js +341 -11
- package/lib/index.d.ts +1 -0
- package/lib/render-static.js +4 -0
- package/lib/render-theme.mts +32 -0
- package/package.json +3 -4
package/lib/generate-theme.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { copyFileSync, cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { copyFileSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -37,14 +37,12 @@ export function generateTheme({
|
|
|
37
37
|
cpSync(distAssets, assetsOut, { recursive: true });
|
|
38
38
|
|
|
39
39
|
// Drop unused JS from the theme ZIP
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
unlinkSync(path.join(assetsOut, file));
|
|
45
|
-
}
|
|
40
|
+
const files = readdirSync(assetsOut);
|
|
41
|
+
for (const file of files) {
|
|
42
|
+
if ((file.endsWith(".js") || file.endsWith(".js.map")) && file !== "forgewp-editor.js") {
|
|
43
|
+
rmSync(path.join(assetsOut, file), { force: true });
|
|
46
44
|
}
|
|
47
|
-
}
|
|
45
|
+
}
|
|
48
46
|
|
|
49
47
|
// Fix nav links and split markup
|
|
50
48
|
const processedApp = processMarkup(appHtml);
|
|
@@ -111,10 +109,59 @@ export function generateTheme({
|
|
|
111
109
|
writeFileSync(path.join(staticDir, "archive.html"), processedArchive, "utf8");
|
|
112
110
|
}
|
|
113
111
|
|
|
112
|
+
// Dynamic Gutenberg blocks compilation (Phase 5)
|
|
113
|
+
const compiledBlocks = compileBlocks(themeRoot, outDir);
|
|
114
|
+
|
|
115
|
+
const editorScriptContent = `
|
|
116
|
+
const { registerBlockType } = wp.blocks;
|
|
117
|
+
const { createElement } = wp.element;
|
|
118
|
+
const { InspectorControls, useBlockProps } = wp.blockEditor;
|
|
119
|
+
const { PanelBody, TextControl } = wp.components;
|
|
120
|
+
const ServerSideRender = wp.serverSideRender;
|
|
121
|
+
|
|
122
|
+
if (window.forgeWpBlocks) {
|
|
123
|
+
window.forgeWpBlocks.forEach(block => {
|
|
124
|
+
registerBlockType(block.name, {
|
|
125
|
+
title: block.title,
|
|
126
|
+
icon: block.icon,
|
|
127
|
+
category: block.category,
|
|
128
|
+
attributes: block.attributes,
|
|
129
|
+
edit: function(props) {
|
|
130
|
+
const { attributes, setAttributes } = props;
|
|
131
|
+
const blockProps = useBlockProps();
|
|
132
|
+
|
|
133
|
+
// Build inspector controls dynamically from attributes
|
|
134
|
+
const controls = Object.keys(block.attributes).map(key => {
|
|
135
|
+
return createElement(TextControl, {
|
|
136
|
+
label: key.charAt(0).toUpperCase() + key.slice(1),
|
|
137
|
+
value: attributes[key],
|
|
138
|
+
onChange: (val) => setAttributes({ [key]: val })
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return createElement('div', blockProps,
|
|
143
|
+
createElement(InspectorControls, null,
|
|
144
|
+
createElement(PanelBody, { title: 'Block Settings', initialOpen: true }, controls)
|
|
145
|
+
),
|
|
146
|
+
createElement(ServerSideRender, {
|
|
147
|
+
block: block.name,
|
|
148
|
+
attributes: attributes
|
|
149
|
+
})
|
|
150
|
+
);
|
|
151
|
+
},
|
|
152
|
+
save: function() {
|
|
153
|
+
return null; // Dynamic blocks return null in save
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
`;
|
|
159
|
+
writeFileSync(path.join(outDir, "assets", "forgewp-editor.js"), editorScriptContent, "utf8");
|
|
160
|
+
|
|
114
161
|
writeFileSync(path.join(outDir, "style.css"), buildStyleCss(config), "utf8");
|
|
115
162
|
writeFileSync(
|
|
116
163
|
path.join(outDir, "functions.php"),
|
|
117
|
-
buildFunctionsPhp(config, assets),
|
|
164
|
+
buildFunctionsPhp(config, assets, compiledBlocks, themeRoot),
|
|
118
165
|
"utf8",
|
|
119
166
|
);
|
|
120
167
|
writeFileSync(path.join(outDir, "header.php"), buildHeaderPhp(config), "utf8");
|
|
@@ -126,6 +173,49 @@ export function generateTheme({
|
|
|
126
173
|
writeFileSync(path.join(outDir, "archive.php"), buildArchivePhp(), "utf8");
|
|
127
174
|
writeFileSync(path.join(outDir, "page.php"), buildPagePhp(), "utf8");
|
|
128
175
|
|
|
176
|
+
// Dynamic Custom Page Templates Compiler
|
|
177
|
+
const forgewpDir = path.join(themeRoot, ".forgewp");
|
|
178
|
+
if (existsSync(forgewpDir)) {
|
|
179
|
+
const templateFiles = readdirSync(forgewpDir);
|
|
180
|
+
for (const file of templateFiles) {
|
|
181
|
+
if (file.startsWith("template-") && file.endsWith(".html") && !file.includes("-head")) {
|
|
182
|
+
const slug = file.replace(".html", "");
|
|
183
|
+
const rawHtml = readFileSync(path.join(forgewpDir, file), "utf8");
|
|
184
|
+
|
|
185
|
+
let processedHtml = processMarkup(rawHtml);
|
|
186
|
+
if (processedHeader) processedHtml = processedHtml.replace(processedHeader, "");
|
|
187
|
+
if (processedFooter) processedHtml = processedHtml.replace(processedFooter, "");
|
|
188
|
+
|
|
189
|
+
writeFileSync(path.join(staticDir, file), processedHtml, "utf8");
|
|
190
|
+
|
|
191
|
+
const headFile = file.replace(".html", "-head.html");
|
|
192
|
+
if (existsSync(path.join(forgewpDir, headFile))) {
|
|
193
|
+
writeFileSync(path.join(staticDir, headFile), readFileSync(path.join(forgewpDir, headFile), "utf8"), "utf8");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Generate the native WordPress PHP Custom Page Template
|
|
197
|
+
const templateName = slug.replace("template-", "").split("-").map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
|
|
198
|
+
const phpContent = `<?php
|
|
199
|
+
/**
|
|
200
|
+
* Template Name: ${templateName}
|
|
201
|
+
*
|
|
202
|
+
* @package ${config.textDomain}
|
|
203
|
+
*/
|
|
204
|
+
|
|
205
|
+
get_header();
|
|
206
|
+
|
|
207
|
+
$markup_file = get_template_directory() . '/forgewp-static/${file}';
|
|
208
|
+
if (file_exists($markup_file)) {
|
|
209
|
+
include $markup_file;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
get_footer();
|
|
213
|
+
`;
|
|
214
|
+
writeFileSync(path.join(outDir, `page-${slug.replace("template-", "")}.php`), phpContent, "utf8");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
129
219
|
// Dynamic theme.json compiler (Phase 5)
|
|
130
220
|
const themeJsonSrc = path.join(themeRoot, "wordpress", "theme.json");
|
|
131
221
|
let themeJson = {
|
|
@@ -263,6 +353,41 @@ function processMarkup(html) {
|
|
|
263
353
|
''
|
|
264
354
|
);
|
|
265
355
|
|
|
356
|
+
// ── Custom Meta Fields (ACF / metadata support) ──
|
|
357
|
+
processed = processed.replace(
|
|
358
|
+
/__FORGEWP_CUSTOM_FIELD__([a-zA-Z0-9_-]+)__/g,
|
|
359
|
+
"<?php echo esc_html( get_post_meta( get_the_ID(), '$1', true ) ); ?>"
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
// ── Dynamic Custom Menus ──
|
|
363
|
+
processed = processed.replace(
|
|
364
|
+
/<forgewp-menu\s+[^>]*location="([^"]+)"\s+[^>]*className="([^"]*)"\s+[^>]*linkClassName="([^"]*)"\s*\/?>/g,
|
|
365
|
+
'<?php\n $locations = get_nav_menu_locations();\n $menu_id = isset($locations[\'$1\']) ? $locations[\'$1\'] : null;\n $menu_items = $menu_id ? wp_get_nav_menu_items($menu_id) : array();\n if (!empty($menu_items)) {\n echo \'<nav class="$2">\';\n foreach ($menu_items as $item) {\n echo \'<a href="\' . esc_url($item->url) . \'" class="$3">\' . esc_html($item->title) . \'</a>\';\n }\n echo \'</nav>\';\n } else {\n echo \'<nav class="$2"><a href="\' . esc_url(home_url(\'/\')) . \'" class="$3">Home</a></nav>\';\n }\n ?>'
|
|
366
|
+
);
|
|
367
|
+
processed = processed.replace(/<\/forgewp-menu>/g, '');
|
|
368
|
+
|
|
369
|
+
// ── WordPress Shortcodes Support ──
|
|
370
|
+
processed = processed.replace(
|
|
371
|
+
/<forgewp-shortcode\s+[^>]*code="([^"]+)"\s*\/?>/g,
|
|
372
|
+
(match, code) => {
|
|
373
|
+
const decodedCode = code.replace(/"/g, '"').replace(/'/g, "'");
|
|
374
|
+
return `<?php echo do_shortcode('${decodedCode}'); ?>`;
|
|
375
|
+
}
|
|
376
|
+
);
|
|
377
|
+
processed = processed.replace(/<\/forgewp-shortcode>/g, '');
|
|
378
|
+
|
|
379
|
+
// ── Custom WP_Query Loop Blocks ──
|
|
380
|
+
processed = processed.replace(
|
|
381
|
+
/<forgewp-query-loop-start\s+[^>]*post[Tt]ype="([^"]+)"\s+[^>]*posts[Pp]er[Pp]age="([^"]+)"\s*(?:[^>]*category[Nn]ame="([^"]*)")?\s*\/?>/g,
|
|
382
|
+
'<?php\n $query_args = array(\n \'post_type\' => \'$1\',\n \'posts_per_page\' => $2,\n );\n if (\'$3\' !== \'\') {\n $query_args[\'category_name\'] = \'$3\';\n }\n $custom_query = new WP_Query($query_args);\n if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();\n ?>'
|
|
383
|
+
);
|
|
384
|
+
processed = processed.replace(/<\/forgewp-query-loop-start>/g, '');
|
|
385
|
+
processed = processed.replace(
|
|
386
|
+
/<forgewp-query-loop-end\s*\/?>/g,
|
|
387
|
+
'<?php\n endwhile;\n wp_reset_postdata();\n endif;\n ?>'
|
|
388
|
+
);
|
|
389
|
+
processed = processed.replace(/<\/forgewp-query-loop-end>/g, '');
|
|
390
|
+
|
|
266
391
|
return processed;
|
|
267
392
|
}
|
|
268
393
|
|
|
@@ -293,11 +418,51 @@ Text Domain: ${config.textDomain}
|
|
|
293
418
|
* @param {import('./types.js').ForgeWPThemeConfig} config
|
|
294
419
|
* @param {import('./types.js').ForgeWPBuildAssets} assets
|
|
295
420
|
*/
|
|
296
|
-
function buildFunctionsPhp(config, assets) {
|
|
421
|
+
function buildFunctionsPhp(config, assets, blockSlugs = [], themeRoot = "") {
|
|
297
422
|
const css = assets.cssFile.replace(/^assets\//, "");
|
|
298
423
|
const version = config.version.replace(/'/g, "\\'");
|
|
299
424
|
const googleFonts = config.settings?.typography?.googleFonts || [];
|
|
300
425
|
|
|
426
|
+
let cptRegistration = "";
|
|
427
|
+
try {
|
|
428
|
+
const mockDataPath = path.join(themeRoot, "wordpress", "mock-data.json");
|
|
429
|
+
if (existsSync(mockDataPath)) {
|
|
430
|
+
const mockData = JSON.parse(readFileSync(mockDataPath, "utf8"));
|
|
431
|
+
const postTypes = Object.keys(mockData).filter(k => k !== "posts" && k !== "pages" && k !== "post" && k !== "page" && k !== "menus");
|
|
432
|
+
if (postTypes.length > 0) {
|
|
433
|
+
cptRegistration = `
|
|
434
|
+
/**
|
|
435
|
+
* Register dynamic Custom Post Types inferred from local mock data.
|
|
436
|
+
*/
|
|
437
|
+
function forgewp_register_custom_post_types() {
|
|
438
|
+
${postTypes.map(pt => ` register_post_type('${pt}', array(
|
|
439
|
+
'labels' => array(
|
|
440
|
+
'name' => '${pt.charAt(0).toUpperCase() + pt.slice(1)}s',
|
|
441
|
+
'singular_name' => '${pt.charAt(0).toUpperCase() + pt.slice(1)}',
|
|
442
|
+
'menu_name' => '${pt.charAt(0).toUpperCase() + pt.slice(1)}s',
|
|
443
|
+
'name_admin_bar' => '${pt.charAt(0).toUpperCase() + pt.slice(1)}',
|
|
444
|
+
'add_new' => 'Add New',
|
|
445
|
+
'add_new_item' => 'Add New ${pt.charAt(0).toUpperCase() + pt.slice(1)}',
|
|
446
|
+
'new_item' => 'New ${pt.charAt(0).toUpperCase() + pt.slice(1)}',
|
|
447
|
+
'edit_item' => 'Edit ${pt.charAt(0).toUpperCase() + pt.slice(1)}',
|
|
448
|
+
'view_item' => 'View ${pt.charAt(0).toUpperCase() + pt.slice(1)}',
|
|
449
|
+
'all_items' => 'All ${pt.charAt(0).toUpperCase() + pt.slice(1)}s',
|
|
450
|
+
'search_items' => 'Search ${pt.charAt(0).toUpperCase() + pt.slice(1)}s',
|
|
451
|
+
'not_found' => 'No ${pt.charAt(0).toUpperCase() + pt.slice(1)}s found.',
|
|
452
|
+
),
|
|
453
|
+
'public' => true,
|
|
454
|
+
'has_archive' => true,
|
|
455
|
+
'show_in_rest'=> true,
|
|
456
|
+
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'excerpt'),
|
|
457
|
+
'menu_icon' => 'dashicons-admin-post',
|
|
458
|
+
));`).join("\n")}
|
|
459
|
+
}
|
|
460
|
+
add_action('init', 'forgewp_register_custom_post_types');
|
|
461
|
+
`;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
} catch (e) {}
|
|
465
|
+
|
|
301
466
|
let fontsEnqueue = "";
|
|
302
467
|
let preconnectFilter = "";
|
|
303
468
|
|
|
@@ -333,6 +498,43 @@ add_filter('wp_resource_hints', 'forgewp_google_fonts_resource_hints', 10, 2);
|
|
|
333
498
|
`;
|
|
334
499
|
}
|
|
335
500
|
|
|
501
|
+
let blocksRegistration = "";
|
|
502
|
+
if (blockSlugs.length > 0) {
|
|
503
|
+
const blocksArray = blockSlugs.map(s => `'${s}'`).join(", ");
|
|
504
|
+
blocksRegistration = `
|
|
505
|
+
/**
|
|
506
|
+
* Register dynamic Gutenberg blocks compiled by ForgeWP.
|
|
507
|
+
*/
|
|
508
|
+
function forgewp_register_dynamic_blocks(): void {
|
|
509
|
+
$blocks = array(${blockSlugs.map(s => `'${s.name.replace("forgewp/", "")}'`).join(", ")});
|
|
510
|
+
foreach ($blocks as $block) {
|
|
511
|
+
register_block_type(__DIR__ . '/blocks/' . $block);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
add_action('init', 'forgewp_register_dynamic_blocks');
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Enqueue Block Editor JavaScript to dynamically render block interfaces.
|
|
518
|
+
*/
|
|
519
|
+
function forgewp_enqueue_block_editor_assets(): void {
|
|
520
|
+
wp_enqueue_script(
|
|
521
|
+
'forgewp-editor-script',
|
|
522
|
+
get_template_directory_uri() . '/assets/forgewp-editor.js',
|
|
523
|
+
array('wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render'),
|
|
524
|
+
FORGEWP_THEME_VERSION,
|
|
525
|
+
true
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
wp_localize_script(
|
|
529
|
+
'forgewp-editor-script',
|
|
530
|
+
'forgeWpBlocks',
|
|
531
|
+
json_decode('${JSON.stringify(blockSlugs)}')
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
add_action('enqueue_block_editor_assets', 'forgewp_enqueue_block_editor_assets');
|
|
535
|
+
`;
|
|
536
|
+
}
|
|
537
|
+
|
|
336
538
|
return `<?php
|
|
337
539
|
/**
|
|
338
540
|
* ${config.name} — generated by ForgeWP
|
|
@@ -374,9 +576,12 @@ function forgewp_theme_setup(): void {
|
|
|
374
576
|
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script'));
|
|
375
577
|
add_theme_support('wp-block-styles');
|
|
376
578
|
add_theme_support('editor-styles');
|
|
579
|
+
|
|
580
|
+
// Load compiled theme stylesheet inside Gutenberg Block Editor
|
|
581
|
+
add_editor_style('assets/${css}');
|
|
377
582
|
}
|
|
378
583
|
add_action('after_setup_theme', 'forgewp_theme_setup');
|
|
379
|
-
${preconnectFilter}`;
|
|
584
|
+
${preconnectFilter}${blocksRegistration}${cptRegistration}`;
|
|
380
585
|
}
|
|
381
586
|
|
|
382
587
|
function buildHeaderPhp(config) {
|
|
@@ -611,3 +816,128 @@ if (file_exists($page_file)) {
|
|
|
611
816
|
get_footer();
|
|
612
817
|
`;
|
|
613
818
|
}
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* Scans the \`src/blocks/\` folder, parses block settings and JSX content,
|
|
822
|
+
* and compiles them into official, dynamic WordPress blocks.
|
|
823
|
+
*
|
|
824
|
+
* @param {string} themeRoot
|
|
825
|
+
* @param {string} outDir
|
|
826
|
+
* @returns {string[]} Compiled block slugs
|
|
827
|
+
*/
|
|
828
|
+
function compileBlocks(themeRoot, outDir) {
|
|
829
|
+
const blocksDir = path.join(themeRoot, "src", "blocks");
|
|
830
|
+
const blockSlugs = [];
|
|
831
|
+
|
|
832
|
+
if (!existsSync(blocksDir)) {
|
|
833
|
+
return blockSlugs;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
const entries = readdirSync(blocksDir);
|
|
837
|
+
for (const entry of entries) {
|
|
838
|
+
const entryPath = path.join(blocksDir, entry);
|
|
839
|
+
let blockFile = "";
|
|
840
|
+
let blockSlug = "";
|
|
841
|
+
|
|
842
|
+
if (statSync(entryPath).isDirectory()) {
|
|
843
|
+
const indexPath = path.join(entryPath, "index.tsx");
|
|
844
|
+
if (existsSync(indexPath)) {
|
|
845
|
+
blockFile = indexPath;
|
|
846
|
+
blockSlug = entry.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase().replace(/[^a-z0-9]+/g, "-");
|
|
847
|
+
}
|
|
848
|
+
} else if (entry.endsWith(".tsx") || entry.endsWith(".jsx")) {
|
|
849
|
+
blockFile = entryPath;
|
|
850
|
+
blockSlug = entry.replace(/\.(tsx|jsx)$/, "").replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase().replace(/[^a-z0-9]+/g, "-");
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
if (blockFile) {
|
|
854
|
+
try {
|
|
855
|
+
const code = readFileSync(blockFile, "utf8");
|
|
856
|
+
|
|
857
|
+
// Parse human-readable title and metadata from setting exports
|
|
858
|
+
const settingsMatch = code.match(/export\s+const\s+settings\s*=\s*(\{[\s\S]*?\});/);
|
|
859
|
+
let settings = {
|
|
860
|
+
apiVersion: 3,
|
|
861
|
+
name: `forgewp/${blockSlug}`,
|
|
862
|
+
title: blockSlug.split("-").map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(" "),
|
|
863
|
+
category: "design",
|
|
864
|
+
icon: "admin-generic",
|
|
865
|
+
attributes: {},
|
|
866
|
+
render: "file:./render.php"
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
if (settingsMatch) {
|
|
870
|
+
try {
|
|
871
|
+
// Evaluates settings safely without external libraries
|
|
872
|
+
const evalFn = new Function(`return ${settingsMatch[1]};`);
|
|
873
|
+
settings = { ...settings, ...evalFn() };
|
|
874
|
+
// Ensure schema, apiVersion, name and render path are strictly aligned
|
|
875
|
+
settings.name = `forgewp/${blockSlug}`;
|
|
876
|
+
settings.apiVersion = 3;
|
|
877
|
+
settings.render = "file:./render.php";
|
|
878
|
+
} catch (e) {
|
|
879
|
+
console.warn(`[Gutenberg Block Compiler] Error parsing settings for ${blockSlug}:`, e.message);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// Parse JSX block content
|
|
884
|
+
let jsx = "";
|
|
885
|
+
const returnMatch = code.match(/return\s*\(\s*(<[\s\S]*?>)\s*\)/);
|
|
886
|
+
if (returnMatch) {
|
|
887
|
+
jsx = returnMatch[1];
|
|
888
|
+
} else {
|
|
889
|
+
const returnMatchSingle = code.match(/return\s+(<[\s\S]*?>);/);
|
|
890
|
+
if (returnMatchSingle) {
|
|
891
|
+
jsx = returnMatchSingle[1];
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
if (!jsx) {
|
|
896
|
+
console.warn(`[Gutenberg Block Compiler] Skipping block ${blockSlug}: No returning JSX element found.`);
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// Translate dynamic attributes inside returning JSX into dynamic PHP echoes
|
|
901
|
+
let phpMarkup = jsx;
|
|
902
|
+
|
|
903
|
+
// className="..." -> class="..."
|
|
904
|
+
phpMarkup = phpMarkup.replace(/className=/g, "class=");
|
|
905
|
+
|
|
906
|
+
// src={image} or src={attributes.image}
|
|
907
|
+
phpMarkup = phpMarkup.replace(/(src|href|alt|title)=\{\s*(?:attributes\.|props\.)?([a-zA-Z0-9_-]+)\s*\}/gi, (match, attr, varName) => {
|
|
908
|
+
const escFunc = attr === "href" || attr === "src" ? "esc_url" : "esc_attr";
|
|
909
|
+
return `${attr}="<?php echo ${escFunc}( $attributes['${varName}'] ?? '' ); ?>"`;
|
|
910
|
+
});
|
|
911
|
+
|
|
912
|
+
// {title} or {props.title}
|
|
913
|
+
phpMarkup = phpMarkup.replace(/\{\s*(?:attributes\.|props\.)?([a-zA-Z0-9_-]+)\s*\}/g, (match, varName) => {
|
|
914
|
+
return `<?php echo esc_html( $attributes['${varName}'] ?? '' ); ?>`;
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
// Wrap the final PHP template markup in a clean block wrapper
|
|
918
|
+
const renderPhpContent = `<?php
|
|
919
|
+
/**
|
|
920
|
+
* Gutenberg dynamic block template — ${settings.title}
|
|
921
|
+
* Autogenerated by ForgeWP Theme Compiler. Do not modify manually.
|
|
922
|
+
*/
|
|
923
|
+
?>
|
|
924
|
+
${phpMarkup}
|
|
925
|
+
`;
|
|
926
|
+
|
|
927
|
+
const blockOutDir = path.join(outDir, "blocks", blockSlug);
|
|
928
|
+
mkdirSync(blockOutDir, { recursive: true });
|
|
929
|
+
|
|
930
|
+
// Write block.json and render.php
|
|
931
|
+
writeFileSync(path.join(blockOutDir, "block.json"), JSON.stringify(settings, null, 2), "utf8");
|
|
932
|
+
writeFileSync(path.join(blockOutDir, "render.php"), renderPhpContent, "utf8");
|
|
933
|
+
|
|
934
|
+
blockSlugs.push(settings);
|
|
935
|
+
} catch (e) {
|
|
936
|
+
console.error(`[Gutenberg Block Compiler] Failed to compile block ${blockSlug}:`, e.message);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
return blockSlugs;
|
|
942
|
+
}
|
|
943
|
+
|
package/lib/index.d.ts
CHANGED
package/lib/render-static.js
CHANGED
|
@@ -43,6 +43,10 @@ export async function renderStaticMarkup(themeRoot) {
|
|
|
43
43
|
const detail = result.stderr?.trim() || result.stdout?.trim() || "Unknown error";
|
|
44
44
|
throw new Error(`Static render failed:\n${detail}`);
|
|
45
45
|
}
|
|
46
|
+
|
|
47
|
+
if (result.stderr) {
|
|
48
|
+
console.warn(result.stderr);
|
|
49
|
+
}
|
|
46
50
|
|
|
47
51
|
const outDir = result.stdout?.trim() || path.join(themeRoot, ".forgewp");
|
|
48
52
|
const appHtmlPath = path.join(outDir, "app.html");
|
package/lib/render-theme.mts
CHANGED
|
@@ -13,6 +13,7 @@ if (!themeRoot) {
|
|
|
13
13
|
// Use the theme's own node_modules for ALL packages (React 19)
|
|
14
14
|
const require = createRequire(path.join(themeRoot, "package.json"));
|
|
15
15
|
const React = require("react");
|
|
16
|
+
globalThis.React = React; // Polyfill for classic JSX transform in Node.js
|
|
16
17
|
const { renderToStaticMarkup } = require("react-dom/server");
|
|
17
18
|
|
|
18
19
|
const appUrl = pathToFileURL(path.join(themeRoot, "src", "app", "page.tsx")).href;
|
|
@@ -197,4 +198,35 @@ if (singleHtml) writeFileSync(path.join(outDir, "single.html"), singleHtml, "utf
|
|
|
197
198
|
if (notFoundHtml) writeFileSync(path.join(outDir, "404.html"), notFoundHtml, "utf8");
|
|
198
199
|
if (archiveHtml) writeFileSync(path.join(outDir, "archive.html"), archiveHtml, "utf8");
|
|
199
200
|
|
|
201
|
+
// ── Compile Custom Page Templates ──────────────────────────────────────────────
|
|
202
|
+
const pagesDir = path.join(themeRoot, "src", "app", "pages");
|
|
203
|
+
console.warn("CHECKING PAGES DIR: " + pagesDir + " EXISTS: " + existsSync(pagesDir));
|
|
204
|
+
if (existsSync(pagesDir)) {
|
|
205
|
+
const { readdirSync } = require("node:fs");
|
|
206
|
+
const pages = readdirSync(pagesDir).filter(f => f.endsWith(".tsx"));
|
|
207
|
+
|
|
208
|
+
for (const pageFile of pages) {
|
|
209
|
+
const pagePath = path.join(pagesDir, pageFile);
|
|
210
|
+
console.warn(`Attempting to compile template: ${pageFile}`);
|
|
211
|
+
try {
|
|
212
|
+
const module = await import(pathToFileURL(pagePath).href);
|
|
213
|
+
const CustomPage = module.default || Object.values(module)[0];
|
|
214
|
+
if (CustomPage) {
|
|
215
|
+
const customHtml = renderPage(CustomPage);
|
|
216
|
+
const pageName = pageFile.replace(".tsx", "");
|
|
217
|
+
const slug = pageName.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
218
|
+
writeFileSync(path.join(outDir, `template-${slug}.html`), customHtml, "utf8");
|
|
219
|
+
console.warn(`WROTE template-${slug}.html to ${outDir}`);
|
|
220
|
+
|
|
221
|
+
const customSeo = extractSeoPropsFromSource(pagePath);
|
|
222
|
+
const customHeadHtml = buildHeadHtml(layoutSeo, customSeo);
|
|
223
|
+
writeFileSync(path.join(outDir, `template-${slug}-head.html`), customHeadHtml, "utf8");
|
|
224
|
+
console.warn(`WROTE template-${slug}-head.html to ${outDir}`);
|
|
225
|
+
}
|
|
226
|
+
} catch (e) {
|
|
227
|
+
console.warn(`Failed to render custom page ${pageFile}:`, e.message);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
200
232
|
process.stdout.write(outDir);
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forgewp/compiler",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Compile ForgeWP React themes into installable WordPress themes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"bin": {
|
|
8
|
-
"forgewp
|
|
9
|
-
"forgewp-add": "./bin/add.js"
|
|
8
|
+
"forgewp": "./bin/cli.js"
|
|
10
9
|
},
|
|
11
10
|
"exports": {
|
|
12
11
|
".": {
|
|
@@ -26,7 +25,7 @@
|
|
|
26
25
|
"jiti": "^2.6.1",
|
|
27
26
|
"picocolors": "^1.1.1",
|
|
28
27
|
"react-helmet-async": "^3.0.0",
|
|
29
|
-
"@forgewp/
|
|
28
|
+
"@forgewp/ui": "0.1.9"
|
|
30
29
|
},
|
|
31
30
|
"devDependencies": {
|
|
32
31
|
"@types/node": "^22.19.19"
|