@forgewp/compiler 0.1.1
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/bin/add.js +16 -0
- package/bin/export.js +90 -0
- package/lib/add-component.js +98 -0
- package/lib/build-assets.js +100 -0
- package/lib/export-theme.js +50 -0
- package/lib/generate-theme.js +282 -0
- package/lib/index.js +2 -0
- package/lib/load-config.js +28 -0
- package/lib/render-static.js +66 -0
- package/lib/render-theme.mts +48 -0
- package/lib/types.js +16 -0
- package/lib/zip-theme.js +34 -0
- package/package.json +27 -0
package/bin/add.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { addComponent } from "../lib/add-component.js";
|
|
3
|
+
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
const component = args[0];
|
|
6
|
+
const style = args.find(a => a.startsWith("--style="))?.split("=")[1] || "forgewp";
|
|
7
|
+
|
|
8
|
+
if (!component) {
|
|
9
|
+
console.error("Usage: forgewp:add <component> [--style=forgewp|shadcn]");
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
addComponent(component, { style }).catch((err) => {
|
|
14
|
+
console.error(err.message);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
});
|
package/bin/export.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { cwd } from "node:process";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
import { exportTheme } from "../lib/export-theme.js";
|
|
7
|
+
|
|
8
|
+
function printHelp() {
|
|
9
|
+
console.log(`
|
|
10
|
+
Usage: forgewp-export [options] [theme-directory]
|
|
11
|
+
|
|
12
|
+
Options:
|
|
13
|
+
--skip-build Use existing dist/ (skip Vite build)
|
|
14
|
+
--no-zip Output folder only, no ZIP file
|
|
15
|
+
-h, --help Show help
|
|
16
|
+
|
|
17
|
+
Examples:
|
|
18
|
+
forgewp-export
|
|
19
|
+
forgewp-export ./my-theme
|
|
20
|
+
forgewp-export --skip-build
|
|
21
|
+
`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parseArgs(argv) {
|
|
25
|
+
const options = {
|
|
26
|
+
themeRoot: cwd(),
|
|
27
|
+
skipBuild: false,
|
|
28
|
+
zip: true,
|
|
29
|
+
help: false,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
33
|
+
const arg = argv[i];
|
|
34
|
+
|
|
35
|
+
if (arg === "-h" || arg === "--help") {
|
|
36
|
+
options.help = true;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (arg === "--skip-build") {
|
|
41
|
+
options.skipBuild = true;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (arg === "--no-zip") {
|
|
46
|
+
options.zip = false;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (arg.startsWith("-")) {
|
|
51
|
+
console.error(pc.red(`Unknown option: ${arg}`));
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
options.themeRoot = path.resolve(arg);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return options;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function main() {
|
|
62
|
+
const options = parseArgs(process.argv.slice(2));
|
|
63
|
+
|
|
64
|
+
if (options.help) {
|
|
65
|
+
printHelp();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const result = await exportTheme({
|
|
71
|
+
themeRoot: options.themeRoot,
|
|
72
|
+
skipBuild: options.skipBuild,
|
|
73
|
+
zip: options.zip,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log(pc.green("\n Export complete\n"));
|
|
77
|
+
console.log(` ${pc.dim("Theme folder")} ${result.outDir}`);
|
|
78
|
+
if (result.zipPath) {
|
|
79
|
+
console.log(` ${pc.dim("ZIP")} ${result.zipPath}`);
|
|
80
|
+
}
|
|
81
|
+
console.log(
|
|
82
|
+
`\n ${pc.dim("Install:")} Upload the ZIP to Appearance → Themes → Add New\n`,
|
|
83
|
+
);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error(pc.red(`\n Export failed: ${error instanceof Error ? error.message : error}\n`));
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
main();
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
import { readFileSync, writeFileSync, readdirSync, existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
|
|
6
|
+
export async function addComponent(component, options) {
|
|
7
|
+
const { style = "forgewp" } = options;
|
|
8
|
+
const projectRoot = process.cwd();
|
|
9
|
+
|
|
10
|
+
console.log(pc.cyan(`\n ForgeWP add — ${component} (${style})\n`));
|
|
11
|
+
|
|
12
|
+
// 1. Check local registry first
|
|
13
|
+
let registryPath;
|
|
14
|
+
try {
|
|
15
|
+
const { createRequire } = await import("node:module");
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
registryPath = path.join(path.dirname(require.resolve("@forgewp/registry/package.json")), "components");
|
|
18
|
+
} catch (e) {
|
|
19
|
+
// Fallback for development/local execution
|
|
20
|
+
registryPath = path.resolve(path.dirname(new URL(import.meta.url).pathname), "../../registry/components");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const localComponentFile = path.join(registryPath, component, `${style}.tsx`);
|
|
24
|
+
|
|
25
|
+
if (existsSync(localComponentFile)) {
|
|
26
|
+
console.log(pc.dim(` Found ${component} in ForgeWP local registry...`));
|
|
27
|
+
const targetDir = path.join(projectRoot, "src", "components", "ui");
|
|
28
|
+
if (!existsSync(targetDir)) mkdirSync(targetDir, { recursive: true });
|
|
29
|
+
|
|
30
|
+
const targetFile = path.join(targetDir, `${component}.tsx`);
|
|
31
|
+
const content = readFileSync(localComponentFile, "utf8");
|
|
32
|
+
writeFileSync(targetFile, content, "utf8");
|
|
33
|
+
|
|
34
|
+
console.log(pc.green(`\n Success! ${component} added to src/components/ui/\n`));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 2. Run shadcn add (fallback)
|
|
39
|
+
console.log(pc.dim(` Fetching ${component} from shadcn/ui...`));
|
|
40
|
+
try {
|
|
41
|
+
// We use --yes to skip prompts
|
|
42
|
+
execSync(`npx shadcn@latest add ${component} --yes`, {
|
|
43
|
+
stdio: "inherit",
|
|
44
|
+
cwd: projectRoot,
|
|
45
|
+
shell: process.platform === "win32",
|
|
46
|
+
});
|
|
47
|
+
} catch (err) {
|
|
48
|
+
throw new Error(`Failed to add component via shadcn: ${err.message}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 2. If style is forgewp, sharpen the components
|
|
52
|
+
if (style === "forgewp") {
|
|
53
|
+
console.log(pc.dim(" Applying ForgeWP Sharp aesthetics..."));
|
|
54
|
+
const uiDir = path.join(projectRoot, "src", "components", "ui");
|
|
55
|
+
|
|
56
|
+
if (!existsSync(uiDir)) return;
|
|
57
|
+
|
|
58
|
+
const files = readdirSync(uiDir);
|
|
59
|
+
// Find files related to the component
|
|
60
|
+
const relevantFiles = files.filter(f =>
|
|
61
|
+
f.toLowerCase().includes(component.toLowerCase().replace(/-/g, "")) ||
|
|
62
|
+
f.toLowerCase().includes(component.toLowerCase())
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (relevantFiles.length > 0) {
|
|
66
|
+
relevantFiles.forEach(f => {
|
|
67
|
+
if (f.endsWith(".tsx")) sharpenFile(path.join(uiDir, f));
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
// Fallback: sharpen everything in the UI dir to be safe
|
|
71
|
+
files.forEach(f => {
|
|
72
|
+
if (f.endsWith(".tsx")) sharpenFile(path.join(uiDir, f));
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
console.log(pc.green(`\n Success! ${component} added to src/components/ui/\n`));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function sharpenFile(filePath) {
|
|
81
|
+
let content = readFileSync(filePath, "utf8");
|
|
82
|
+
|
|
83
|
+
// Regex to find and replace rounding classes, including variants (e.g., focus:rounded-md)
|
|
84
|
+
const sharpened = content.replace(/(?:\b|['"\s])([a-z0-9-:]+)?rounded(-[a-z0-9\[\]]+)?(?=["'\s])/g, (match, prefix, suffix) => {
|
|
85
|
+
// If it's just 'rounded' or 'rounded-*', replace it
|
|
86
|
+
if (match.includes("rounded")) {
|
|
87
|
+
// Preserve prefixes like 'focus:'
|
|
88
|
+
const cleanPrefix = match.split("rounded")[0];
|
|
89
|
+
return `${cleanPrefix}rounded-none`;
|
|
90
|
+
}
|
|
91
|
+
return match;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (content !== sharpened) {
|
|
95
|
+
writeFileSync(filePath, sharpened, "utf8");
|
|
96
|
+
console.log(pc.dim(` Sharpened ${path.basename(filePath)}`));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} themeRoot
|
|
7
|
+
* @param {{ packageManager?: string }} [options]
|
|
8
|
+
* @returns {import('./types.js').ForgeWPBuildAssets}
|
|
9
|
+
*/
|
|
10
|
+
export function buildAssets(themeRoot, options = {}) {
|
|
11
|
+
const pm = options.packageManager ?? detectPackageManager();
|
|
12
|
+
const build = runPackageScript(themeRoot, pm, "build");
|
|
13
|
+
|
|
14
|
+
if (build.status !== 0) {
|
|
15
|
+
throw new Error("Vite build failed. Fix build errors and run export again.");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return readBuildManifest(themeRoot);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function detectPackageManager() {
|
|
22
|
+
const ua = process.env.npm_config_user_agent ?? "";
|
|
23
|
+
if (ua.startsWith("pnpm/")) return "pnpm";
|
|
24
|
+
if (ua.startsWith("yarn/")) return "yarn";
|
|
25
|
+
if (ua.startsWith("bun/")) return "bun";
|
|
26
|
+
return "npm";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function runPackageScript(cwd, pm, script) {
|
|
30
|
+
const env = { ...process.env, INIT_CWD: cwd };
|
|
31
|
+
|
|
32
|
+
switch (pm) {
|
|
33
|
+
case "pnpm":
|
|
34
|
+
return spawnSync("pnpm", ["run", script], {
|
|
35
|
+
cwd,
|
|
36
|
+
stdio: "inherit",
|
|
37
|
+
shell: process.platform === "win32",
|
|
38
|
+
env,
|
|
39
|
+
});
|
|
40
|
+
case "yarn":
|
|
41
|
+
return spawnSync("yarn", ["run", script], {
|
|
42
|
+
cwd,
|
|
43
|
+
stdio: "inherit",
|
|
44
|
+
shell: process.platform === "win32",
|
|
45
|
+
env,
|
|
46
|
+
});
|
|
47
|
+
case "bun":
|
|
48
|
+
return spawnSync("bun", ["run", script], {
|
|
49
|
+
cwd,
|
|
50
|
+
stdio: "inherit",
|
|
51
|
+
shell: process.platform === "win32",
|
|
52
|
+
env,
|
|
53
|
+
});
|
|
54
|
+
default:
|
|
55
|
+
return spawnSync("npm", ["run", script], {
|
|
56
|
+
cwd,
|
|
57
|
+
stdio: "inherit",
|
|
58
|
+
shell: process.platform === "win32",
|
|
59
|
+
env,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {string} themeRoot
|
|
66
|
+
* @returns {import('./types.js').ForgeWPBuildAssets}
|
|
67
|
+
*/
|
|
68
|
+
export function readBuildManifest(themeRoot) {
|
|
69
|
+
const candidates = [
|
|
70
|
+
path.join(themeRoot, "dist", ".vite", "manifest.json"),
|
|
71
|
+
path.join(themeRoot, "dist", "manifest.json"),
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const manifestPath = candidates.find((candidate) => existsSync(candidate));
|
|
75
|
+
|
|
76
|
+
if (!manifestPath) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"Build manifest not found. Enable build.manifest in vite.config.ts and run build first.",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
83
|
+
const entry =
|
|
84
|
+
manifest["index.html"] ??
|
|
85
|
+
Object.values(manifest).find((chunk) => chunk?.isEntry);
|
|
86
|
+
|
|
87
|
+
if (!entry?.file) {
|
|
88
|
+
throw new Error("Could not find entry chunk in Vite manifest");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const cssFile = entry.css?.[0];
|
|
92
|
+
if (!cssFile) {
|
|
93
|
+
throw new Error("No CSS file in Vite build output");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
cssFile,
|
|
98
|
+
jsFile: entry.file,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import pc from "picocolors";
|
|
3
|
+
import { buildAssets } from "./build-assets.js";
|
|
4
|
+
import { generateTheme } from "./generate-theme.js";
|
|
5
|
+
import { loadConfig } from "./load-config.js";
|
|
6
|
+
import { renderStaticMarkup } from "./render-static.js";
|
|
7
|
+
import { assertZipCreated, zipTheme } from "./zip-theme.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {Object} options
|
|
11
|
+
* @param {string} options.themeRoot
|
|
12
|
+
* @param {boolean} [options.skipBuild]
|
|
13
|
+
* @param {boolean} [options.zip]
|
|
14
|
+
* @param {string} [options.packageManager]
|
|
15
|
+
*/
|
|
16
|
+
export async function exportTheme(options) {
|
|
17
|
+
const themeRoot = path.resolve(options.themeRoot);
|
|
18
|
+
const config = await loadConfig(themeRoot);
|
|
19
|
+
|
|
20
|
+
console.log(pc.cyan(`\n ForgeWP export — ${config.name}\n`));
|
|
21
|
+
|
|
22
|
+
const assets = options.skipBuild
|
|
23
|
+
? (await import("./build-assets.js")).readBuildManifest(themeRoot)
|
|
24
|
+
: buildAssets(themeRoot, { packageManager: options.packageManager });
|
|
25
|
+
|
|
26
|
+
console.log(pc.dim(" Rendering React app to static HTML…"));
|
|
27
|
+
const markup = await renderStaticMarkup(themeRoot);
|
|
28
|
+
|
|
29
|
+
const outDir = path.join(themeRoot, ".forgewp", "out", config.slug);
|
|
30
|
+
const zipPath = path.join(themeRoot, ".forgewp", `${config.slug}.zip`);
|
|
31
|
+
|
|
32
|
+
console.log(pc.dim(" Generating WordPress theme files…"));
|
|
33
|
+
generateTheme({
|
|
34
|
+
themeRoot,
|
|
35
|
+
outDir,
|
|
36
|
+
config,
|
|
37
|
+
appHtml: markup.appHtml,
|
|
38
|
+
headerHtml: markup.headerHtml,
|
|
39
|
+
footerHtml: markup.footerHtml,
|
|
40
|
+
assets,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (options.zip !== false) {
|
|
44
|
+
console.log(pc.dim(" Creating ZIP…"));
|
|
45
|
+
await zipTheme(outDir, zipPath);
|
|
46
|
+
assertZipCreated(zipPath);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { config, outDir, zipPath: options.zip !== false ? zipPath : null, assets };
|
|
50
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { copyFileSync, cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Object} options
|
|
6
|
+
* @param {string} options.themeRoot
|
|
7
|
+
* @param {string} options.outDir
|
|
8
|
+
* @param {import('./types.js').ForgeWPThemeConfig} options.config
|
|
9
|
+
* @param {string} options.appHtml
|
|
10
|
+
* @param {string} [options.headerHtml]
|
|
11
|
+
* @param {string} [options.footerHtml]
|
|
12
|
+
* @param {import('./types.js').ForgeWPBuildAssets} options.assets
|
|
13
|
+
*/
|
|
14
|
+
export function generateTheme({
|
|
15
|
+
themeRoot,
|
|
16
|
+
outDir,
|
|
17
|
+
config,
|
|
18
|
+
appHtml,
|
|
19
|
+
headerHtml = "",
|
|
20
|
+
footerHtml = "",
|
|
21
|
+
assets,
|
|
22
|
+
}) {
|
|
23
|
+
if (existsSync(outDir)) {
|
|
24
|
+
rmSync(outDir, { recursive: true, force: true });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mkdirSync(outDir, { recursive: true });
|
|
28
|
+
|
|
29
|
+
const assetsOut = path.join(outDir, "assets");
|
|
30
|
+
const distAssets = path.join(themeRoot, "dist", "assets");
|
|
31
|
+
mkdirSync(assetsOut, { recursive: true });
|
|
32
|
+
cpSync(distAssets, assetsOut, { recursive: true });
|
|
33
|
+
|
|
34
|
+
// Drop unused JS from the theme ZIP
|
|
35
|
+
import("node:fs").then(({ readdirSync, unlinkSync }) => {
|
|
36
|
+
const files = readdirSync(assetsOut);
|
|
37
|
+
for (const file of files) {
|
|
38
|
+
if (file.endsWith(".js") || file.endsWith(".js.map")) {
|
|
39
|
+
unlinkSync(path.join(assetsOut, file));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Fix nav links and split markup
|
|
45
|
+
const processedApp = processMarkup(appHtml);
|
|
46
|
+
const processedHeader = processMarkup(headerHtml);
|
|
47
|
+
const processedFooter = processMarkup(footerHtml);
|
|
48
|
+
|
|
49
|
+
// Extract content (App markup minus Header/Footer)
|
|
50
|
+
let contentHtml = processedApp;
|
|
51
|
+
if (processedHeader) {
|
|
52
|
+
contentHtml = contentHtml.replace(processedHeader, "");
|
|
53
|
+
}
|
|
54
|
+
if (processedFooter) {
|
|
55
|
+
contentHtml = contentHtml.replace(processedFooter, "");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const staticDir = path.join(outDir, "forgewp-static");
|
|
59
|
+
mkdirSync(staticDir, { recursive: true });
|
|
60
|
+
writeFileSync(path.join(staticDir, "content.html"), contentHtml, "utf8");
|
|
61
|
+
writeFileSync(path.join(staticDir, "header.html"), processedHeader, "utf8");
|
|
62
|
+
writeFileSync(path.join(staticDir, "footer.html"), processedFooter, "utf8");
|
|
63
|
+
|
|
64
|
+
writeFileSync(path.join(outDir, "style.css"), buildStyleCss(config), "utf8");
|
|
65
|
+
writeFileSync(
|
|
66
|
+
path.join(outDir, "functions.php"),
|
|
67
|
+
buildFunctionsPhp(config, assets),
|
|
68
|
+
"utf8",
|
|
69
|
+
);
|
|
70
|
+
writeFileSync(path.join(outDir, "header.php"), buildHeaderPhp(config), "utf8");
|
|
71
|
+
writeFileSync(path.join(outDir, "footer.php"), buildFooterPhp(config), "utf8");
|
|
72
|
+
writeFileSync(path.join(outDir, "index.php"), buildIndexPhp(), "utf8");
|
|
73
|
+
writeFileSync(path.join(outDir, "single.php"), buildSinglePhp(), "utf8");
|
|
74
|
+
writeFileSync(path.join(outDir, "front-page.php"), buildIndexPhp(), "utf8");
|
|
75
|
+
|
|
76
|
+
const themeJsonSrc = path.join(themeRoot, "wordpress", "theme.json");
|
|
77
|
+
if (existsSync(themeJsonSrc)) {
|
|
78
|
+
copyFileSync(themeJsonSrc, path.join(outDir, "theme.json"));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const screenshot = path.join(themeRoot, "wordpress", "screenshot.png");
|
|
82
|
+
if (existsSync(screenshot)) {
|
|
83
|
+
copyFileSync(screenshot, path.join(outDir, "screenshot.png"));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Process markup: fix nav links etc.
|
|
89
|
+
*/
|
|
90
|
+
function processMarkup(html) {
|
|
91
|
+
if (!html) return "";
|
|
92
|
+
// Fix nav links — # → <?php echo esc_url( home_url( '/' ) ); ?>
|
|
93
|
+
// We use a placeholder and replace it in the PHP file generation if needed,
|
|
94
|
+
// but for now we'll do it via string replacement in the template files.
|
|
95
|
+
return html.replace(
|
|
96
|
+
/href="#"/g,
|
|
97
|
+
'href="<?php echo esc_url( home_url( \'/\' ) ); ?>"',
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {import('./types.js').ForgeWPThemeConfig} config
|
|
103
|
+
*/
|
|
104
|
+
function buildStyleCss(config) {
|
|
105
|
+
return `/*
|
|
106
|
+
Theme Name: ${config.name}
|
|
107
|
+
Theme URI: https://forgewp.dev
|
|
108
|
+
Author: ForgeWP
|
|
109
|
+
Description: ${config.description}
|
|
110
|
+
Version: ${config.version}
|
|
111
|
+
Requires at least: 6.0
|
|
112
|
+
Tested up to: 6.7
|
|
113
|
+
Requires PHP: 7.4
|
|
114
|
+
License: GNU General Public License v2 or later
|
|
115
|
+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
|
116
|
+
Text Domain: ${config.textDomain}
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
/* Compiled styles are enqueued from assets/ via functions.php */
|
|
120
|
+
`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {import('./types.js').ForgeWPThemeConfig} config
|
|
125
|
+
* @param {import('./types.js').ForgeWPBuildAssets} assets
|
|
126
|
+
*/
|
|
127
|
+
function buildFunctionsPhp(config, assets) {
|
|
128
|
+
const css = assets.cssFile.replace(/^assets\//, "");
|
|
129
|
+
const version = config.version.replace(/'/g, "\\'");
|
|
130
|
+
|
|
131
|
+
return `<?php
|
|
132
|
+
/**
|
|
133
|
+
* ${config.name} — generated by ForgeWP
|
|
134
|
+
*
|
|
135
|
+
* @package ${config.textDomain}
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
if (! defined('ABSPATH')) {
|
|
139
|
+
exit;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
define('FORGEWP_THEME_VERSION', '${version}');
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Enqueue compiled theme assets (CSS only in v1 — static HTML, no hydration).
|
|
146
|
+
*/
|
|
147
|
+
function forgewp_enqueue_assets(): void {
|
|
148
|
+
$theme_uri = get_template_directory_uri();
|
|
149
|
+
$css_path = get_template_directory() . '/assets/${css}';
|
|
150
|
+
|
|
151
|
+
if (file_exists($css_path)) {
|
|
152
|
+
wp_enqueue_style(
|
|
153
|
+
'${config.textDomain}-app',
|
|
154
|
+
$theme_uri . '/assets/${css}',
|
|
155
|
+
array(),
|
|
156
|
+
FORGEWP_THEME_VERSION
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
add_action('wp_enqueue_scripts', 'forgewp_enqueue_assets');
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Theme supports.
|
|
164
|
+
*/
|
|
165
|
+
function forgewp_theme_setup(): void {
|
|
166
|
+
add_theme_support('title-tag');
|
|
167
|
+
add_theme_support('post-thumbnails');
|
|
168
|
+
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script'));
|
|
169
|
+
add_theme_support('wp-block-styles');
|
|
170
|
+
add_theme_support('editor-styles');
|
|
171
|
+
}
|
|
172
|
+
add_action('after_setup_theme', 'forgewp_theme_setup');
|
|
173
|
+
`;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function buildHeaderPhp(config) {
|
|
177
|
+
return `<?php
|
|
178
|
+
/**
|
|
179
|
+
* Theme header
|
|
180
|
+
*
|
|
181
|
+
* @package ${config.textDomain}
|
|
182
|
+
*/
|
|
183
|
+
?><!DOCTYPE html>
|
|
184
|
+
<html <?php language_attributes(); ?>>
|
|
185
|
+
<head>
|
|
186
|
+
<meta charset="<?php bloginfo('charset'); ?>">
|
|
187
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
188
|
+
<?php wp_head(); ?>
|
|
189
|
+
<style>
|
|
190
|
+
/* Reset WP link underlines to match React/Tailwind expected styling */
|
|
191
|
+
a { text-decoration: none !important; }
|
|
192
|
+
a:hover { text-decoration: none !important; }
|
|
193
|
+
</style>
|
|
194
|
+
</head>
|
|
195
|
+
<body <?php body_class(); ?>>
|
|
196
|
+
<?php wp_body_open(); ?>
|
|
197
|
+
<?php
|
|
198
|
+
$header_file = get_template_directory() . '/forgewp-static/header.html';
|
|
199
|
+
if (file_exists($header_file)) {
|
|
200
|
+
include $header_file;
|
|
201
|
+
}
|
|
202
|
+
?>
|
|
203
|
+
`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function buildFooterPhp(config) {
|
|
207
|
+
return `<?php
|
|
208
|
+
/**
|
|
209
|
+
* Theme footer
|
|
210
|
+
*
|
|
211
|
+
* @package ${config.textDomain}
|
|
212
|
+
*/
|
|
213
|
+
$footer_file = get_template_directory() . '/forgewp-static/footer.html';
|
|
214
|
+
if (file_exists($footer_file)) {
|
|
215
|
+
include $footer_file;
|
|
216
|
+
}
|
|
217
|
+
wp_footer();
|
|
218
|
+
?>
|
|
219
|
+
</body>
|
|
220
|
+
</html>
|
|
221
|
+
`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function buildIndexPhp() {
|
|
225
|
+
return `<?php
|
|
226
|
+
/**
|
|
227
|
+
* Main template — displays ForgeWP compiled static markup
|
|
228
|
+
*
|
|
229
|
+
* @package forgewp
|
|
230
|
+
*/
|
|
231
|
+
|
|
232
|
+
get_header();
|
|
233
|
+
|
|
234
|
+
$markup_file = get_template_directory() . '/forgewp-static/content.html';
|
|
235
|
+
|
|
236
|
+
if (is_front_page() && file_exists($markup_file)) {
|
|
237
|
+
include $markup_file;
|
|
238
|
+
} elseif (have_posts()) {
|
|
239
|
+
echo '<main class="mx-auto max-w-3xl px-6 py-12">';
|
|
240
|
+
while (have_posts()) {
|
|
241
|
+
the_post();
|
|
242
|
+
echo '<article class="mb-12">';
|
|
243
|
+
echo '<h1 class="text-3xl font-bold mb-4">' . get_the_title() . '</h1>';
|
|
244
|
+
echo '<div class="prose prose-zinc max-w-none">';
|
|
245
|
+
the_content();
|
|
246
|
+
echo '</div>';
|
|
247
|
+
echo '</article>';
|
|
248
|
+
}
|
|
249
|
+
echo '</main>';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
get_footer();
|
|
253
|
+
`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function buildSinglePhp() {
|
|
257
|
+
return `<?php
|
|
258
|
+
/**
|
|
259
|
+
* Single post template
|
|
260
|
+
*
|
|
261
|
+
* @package forgewp
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
get_header();
|
|
265
|
+
|
|
266
|
+
if (have_posts()) {
|
|
267
|
+
while (have_posts()) {
|
|
268
|
+
the_post();
|
|
269
|
+
echo '<main class="mx-auto max-w-3xl px-6 py-12">';
|
|
270
|
+
echo '<article>';
|
|
271
|
+
echo '<h1 class="text-4xl font-bold mb-6">' . get_the_title() . '</h1>';
|
|
272
|
+
echo '<div class="prose prose-zinc max-w-none">';
|
|
273
|
+
the_content();
|
|
274
|
+
echo '</div>';
|
|
275
|
+
echo '</article>';
|
|
276
|
+
echo '</main>';
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
get_footer();
|
|
281
|
+
`;
|
|
282
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { createJiti } from "jiti";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} themeRoot
|
|
8
|
+
* @returns {Promise<import('./types.js').ForgeWPThemeConfig>}
|
|
9
|
+
*/
|
|
10
|
+
export async function loadConfig(themeRoot) {
|
|
11
|
+
const configPath = path.join(themeRoot, "wp.config.ts");
|
|
12
|
+
|
|
13
|
+
if (!existsSync(configPath)) {
|
|
14
|
+
throw new Error(`Missing wp.config.ts in ${themeRoot}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const jiti = createJiti(pathToFileURL(configPath).href, {
|
|
18
|
+
interopDefault: true,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const config = await jiti.import(configPath);
|
|
22
|
+
|
|
23
|
+
if (!config?.slug || !config?.name) {
|
|
24
|
+
throw new Error("wp.config.ts must export name, slug, version, description, textDomain");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
|
|
7
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Server-render the theme App to static HTML (no client JS in WordPress for v1).
|
|
12
|
+
* @param {string} themeRoot
|
|
13
|
+
* @returns {Promise<string>}
|
|
14
|
+
*/
|
|
15
|
+
export async function renderStaticMarkup(themeRoot) {
|
|
16
|
+
const renderScript = path.join(__dirname, "render-theme.mts");
|
|
17
|
+
const tsconfig = path.join(themeRoot, "tsconfig.json");
|
|
18
|
+
|
|
19
|
+
let tsxCli = "tsx";
|
|
20
|
+
try {
|
|
21
|
+
tsxCli = require.resolve("tsx/cli");
|
|
22
|
+
} catch {
|
|
23
|
+
// fallback to PATH
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const args = [tsxCli];
|
|
27
|
+
if (existsSync(tsconfig)) {
|
|
28
|
+
args.push("--tsconfig", tsconfig);
|
|
29
|
+
}
|
|
30
|
+
args.push(renderScript, themeRoot);
|
|
31
|
+
|
|
32
|
+
const result = spawnSync(process.execPath, args, {
|
|
33
|
+
cwd: themeRoot,
|
|
34
|
+
env: {
|
|
35
|
+
...process.env,
|
|
36
|
+
INIT_CWD: themeRoot,
|
|
37
|
+
},
|
|
38
|
+
encoding: "utf8",
|
|
39
|
+
shell: false,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (result.status !== 0) {
|
|
43
|
+
const detail = result.stderr?.trim() || result.stdout?.trim() || "Unknown error";
|
|
44
|
+
throw new Error(`Static render failed:\n${detail}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const outDir = result.stdout?.trim() || path.join(themeRoot, ".forgewp");
|
|
48
|
+
const appHtmlPath = path.join(outDir, "app.html");
|
|
49
|
+
|
|
50
|
+
if (!existsSync(appHtmlPath)) {
|
|
51
|
+
throw new Error(`Render output not found: ${appHtmlPath}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const headerHtmlPath = path.join(outDir, "header.html");
|
|
55
|
+
const footerHtmlPath = path.join(outDir, "footer.html");
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
appHtml: readFileSync(appHtmlPath, "utf8"),
|
|
59
|
+
headerHtml: existsSync(headerHtmlPath)
|
|
60
|
+
? readFileSync(headerHtmlPath, "utf8")
|
|
61
|
+
: "",
|
|
62
|
+
footerHtml: existsSync(footerHtmlPath)
|
|
63
|
+
? readFileSync(footerHtmlPath, "utf8")
|
|
64
|
+
: "",
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
|
|
6
|
+
const themeRoot = process.argv[2];
|
|
7
|
+
|
|
8
|
+
if (!themeRoot) {
|
|
9
|
+
console.error("Usage: render-theme.mts <theme-root>");
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const require = createRequire(path.join(themeRoot, "package.json"));
|
|
14
|
+
const React = require("react");
|
|
15
|
+
const { renderToStaticMarkup } = require("react-dom/server");
|
|
16
|
+
|
|
17
|
+
const appUrl = pathToFileURL(path.join(themeRoot, "src", "App.tsx")).href;
|
|
18
|
+
const headerUrl = pathToFileURL(path.join(themeRoot, "src", "components", "SiteHeader.tsx")).href;
|
|
19
|
+
const footerUrl = pathToFileURL(path.join(themeRoot, "src", "components", "SiteFooter.tsx")).href;
|
|
20
|
+
|
|
21
|
+
const { default: App } = await import(appUrl);
|
|
22
|
+
|
|
23
|
+
let headerHtml = "";
|
|
24
|
+
try {
|
|
25
|
+
const { SiteHeader } = await import(headerUrl);
|
|
26
|
+
headerHtml = renderToStaticMarkup(React.createElement(SiteHeader));
|
|
27
|
+
} catch (e) {
|
|
28
|
+
console.warn("SiteHeader not found, skipping separate render");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let footerHtml = "";
|
|
32
|
+
try {
|
|
33
|
+
const { SiteFooter } = await import(footerUrl);
|
|
34
|
+
footerHtml = renderToStaticMarkup(React.createElement(SiteFooter));
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.warn("SiteFooter not found, skipping separate render");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const appHtml = renderToStaticMarkup(React.createElement(App));
|
|
40
|
+
|
|
41
|
+
const outDir = path.join(themeRoot, ".forgewp");
|
|
42
|
+
mkdirSync(outDir, { recursive: true });
|
|
43
|
+
|
|
44
|
+
writeFileSync(path.join(outDir, "header.html"), headerHtml, "utf8");
|
|
45
|
+
writeFileSync(path.join(outDir, "footer.html"), footerHtml, "utf8");
|
|
46
|
+
writeFileSync(path.join(outDir, "app.html"), appHtml, "utf8");
|
|
47
|
+
|
|
48
|
+
process.stdout.write(outDir);
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} ForgeWPThemeConfig
|
|
3
|
+
* @property {string} name
|
|
4
|
+
* @property {string} slug
|
|
5
|
+
* @property {string} version
|
|
6
|
+
* @property {string} description
|
|
7
|
+
* @property {string} textDomain
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {Object} ForgeWPBuildAssets
|
|
12
|
+
* @property {string} cssFile
|
|
13
|
+
* @property {string} jsFile
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export {};
|
package/lib/zip-theme.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createWriteStream, existsSync, mkdirSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import archiver from "archiver";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} themeDir
|
|
7
|
+
* @param {string} zipPath
|
|
8
|
+
* @returns {Promise<string>}
|
|
9
|
+
*/
|
|
10
|
+
export function zipTheme(themeDir, zipPath) {
|
|
11
|
+
mkdirSync(path.dirname(zipPath), { recursive: true });
|
|
12
|
+
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const output = createWriteStream(zipPath);
|
|
15
|
+
const archive = archiver("zip", { zlib: { level: 9 } });
|
|
16
|
+
|
|
17
|
+
output.on("close", () => resolve(zipPath));
|
|
18
|
+
archive.on("error", reject);
|
|
19
|
+
output.on("error", reject);
|
|
20
|
+
|
|
21
|
+
archive.pipe(output);
|
|
22
|
+
archive.directory(themeDir, path.basename(themeDir));
|
|
23
|
+
archive.finalize();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} zipPath
|
|
29
|
+
*/
|
|
30
|
+
export function assertZipCreated(zipPath) {
|
|
31
|
+
if (!existsSync(zipPath)) {
|
|
32
|
+
throw new Error(`ZIP was not created: ${zipPath}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@forgewp/compiler",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Compile ForgeWP React themes into installable WordPress themes",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"bin": {
|
|
8
|
+
"forgewp-export": "./bin/export.js",
|
|
9
|
+
"forgewp-add": "./bin/add.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./lib/index.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"bin",
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"archiver": "^7.0.1",
|
|
23
|
+
"jiti": "^2.6.1",
|
|
24
|
+
"picocolors": "^1.1.1",
|
|
25
|
+
"@forgewp/registry": "0.1.1"
|
|
26
|
+
}
|
|
27
|
+
}
|