@forgewp/compiler 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -3
- package/bin/add.js +30 -15
- package/bin/analyze.js +66 -1
- package/bin/cli.js +149 -70
- package/bin/doctor.js +21 -12
- package/bin/export.js +69 -20
- package/bin/fresh.js +6 -1
- package/bin/i18n-extract.js +3 -10
- package/bin/i18n-translate.js +15 -24
- package/bin/make-block.js +24 -0
- package/bin/make-editable.js +166 -0
- package/bin/make-form.js +131 -0
- package/bin/make-island.js +11 -0
- package/bin/make-loop.js +147 -0
- package/bin/make-mail.js +27 -0
- package/bin/make-page.js +83 -0
- package/bin/make-post-type.js +68 -29
- package/bin/make-sandbox.js +609 -0
- package/bin/make-section.js +221 -0
- package/bin/make-shell.js +375 -0
- package/bin/repair.js +14 -26
- package/bin/seed.js +119 -0
- package/bin/sync-routes.js +107 -85
- package/lib/adapters/html-adapter.js +13 -3
- package/lib/adapters/stub-adapter.js +10 -2
- package/lib/add-component.js +153 -92
- package/lib/asset-loader.js +333 -1
- package/lib/assets/asset-graph.js +432 -0
- package/lib/assets/asset-pipeline.js +125 -0
- package/lib/assets/diagnostics-report.js +345 -0
- package/lib/assets/external-fetcher.js +173 -0
- package/lib/assets/font-orchestrator.js +348 -0
- package/lib/assets/font-usage-scanner.js +242 -0
- package/lib/assets/image-optimizer.js +229 -0
- package/lib/assets/index.js +51 -0
- package/lib/assets/preload-generator.js +70 -0
- package/lib/assets/public-url.js +153 -0
- package/lib/block-authoring-help.js +11 -0
- package/lib/blocks/ast-parser.js +67 -0
- package/lib/blocks/discover-blocks.js +146 -0
- package/lib/blocks/editor-scope.js +733 -0
- package/lib/blocks/editor-transpiler.js +1229 -0
- package/lib/blocks/index.js +2838 -0
- package/lib/blocks/php-transpiler.js +4928 -0
- package/lib/blocks/scanner.js +1487 -0
- package/lib/blocks/shared-utils.js +290 -0
- package/lib/blocks/source-sanitize.js +1449 -0
- package/lib/blueprints.js +77 -5
- package/lib/build-assets.js +38 -33
- package/lib/collect-page-template-routes.js +102 -0
- package/lib/collect-static-routes.js +175 -0
- package/lib/diagnostics.js +58 -33
- package/lib/export-theme.js +84 -5
- package/lib/extract-template-content.js +90 -0
- package/lib/forms-preview.js +42 -0
- package/lib/functions/cpt-routing.js +69 -0
- package/lib/functions/forms.js +2461 -0
- package/lib/functions/hydration-enqueuer.js +400 -0
- package/lib/functions/index.js +1623 -0
- package/lib/functions/load-menus.js +112 -0
- package/lib/functions/load-translations.js +148 -0
- package/lib/functions/rest-endpoints.js +2923 -0
- package/lib/functions/routing.js +765 -0
- package/lib/functions/seed-mock-data.js +392 -0
- package/lib/functions/seed-products.js +481 -0
- package/lib/functions/settings-page.js +273 -0
- package/lib/functions/theme-setup.js +494 -0
- package/lib/generate-theme.js +2076 -303
- package/lib/hydration/editable-schemas.js +332 -0
- package/lib/hydration/enter-hidden-initial.js +65 -0
- package/lib/hydration/form-schemas.js +227 -0
- package/lib/hydration/index.js +12 -0
- package/lib/hydration/is-interactive.js +951 -0
- package/lib/hydration/island-split.js +1231 -0
- package/lib/hydration/islands-scanner.js +599 -0
- package/lib/hydration/resolve-island-chunk.js +103 -0
- package/lib/hydration/resolve-island-component.js +52 -0
- package/lib/hydration/rollup-inputs.js +81 -0
- package/lib/hydration/root-class-extractor.js +220 -0
- package/lib/hydration/scan-app-providers.js +154 -0
- package/lib/hydration/static-lint.js +382 -0
- package/lib/hydration/transform.js +257 -0
- package/lib/hydration/uses-block-area.js +49 -0
- package/lib/hydration/utils.js +119 -0
- package/lib/i18n/translation-cache.js +141 -0
- package/lib/icon-registry.js +72 -0
- package/lib/index.d.ts +324 -8
- package/lib/index.js +3 -2
- package/lib/load-config.js +338 -144
- package/lib/mail-templates.js +37 -0
- package/lib/markup-processor.js +483 -92
- package/lib/node-runtime.js +7 -0
- package/lib/page-config-plugin.js +84 -0
- package/lib/php-builders.js +383 -42
- package/lib/react-adapter.js +1053 -246
- package/lib/registration-metadata.js +11 -0
- package/lib/render-theme.mts +1410 -543
- package/lib/route-layout.js +76 -0
- package/lib/template-discovery.js +58 -0
- package/lib/theme-compatibility.js +32 -0
- package/lib/validate-export.js +51 -2
- package/lib/validate.js +27 -5
- package/lib/virtual-plugin.js +319 -0
- package/lib/walkthrough.md +26 -0
- package/package.json +17 -5
- package/templates/PresetsStyle.tsx +1 -1
- package/templates/auth/cms/email-logs.json +9 -0
- package/templates/auth/cms/menus.json +11 -0
- package/templates/auth/cms/mock-data.json +123 -0
- package/templates/auth/cms/roles.json +7 -0
- package/templates/auth/cms/sessions.json +1 -0
- package/templates/auth/cms/site-settings.json +28 -0
- package/templates/auth/cms/translations.json +4 -0
- package/templates/auth/cms/users.json +46 -0
- package/templates/auth/components/dashboard-view-wrapper.tsx +168 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +141 -0
- package/templates/auth/components/login-form-wrapper.tsx +300 -0
- package/templates/auth/components/reset-password-form-wrapper.tsx +190 -0
- package/templates/auth/components/signup-form-wrapper.tsx +257 -0
- package/templates/auth/components/verify-email-view-wrapper.tsx +265 -0
- package/templates/auth/pages/DashboardPage.tsx +12 -0
- package/templates/auth/pages/ForgotPasswordPage.tsx +11 -0
- package/templates/auth/pages/LoginPage.tsx +11 -0
- package/templates/auth/pages/ResetPasswordPage.tsx +11 -0
- package/templates/auth/pages/SignUpPage.tsx +11 -0
- package/templates/auth/pages/VerifyEmailPage.tsx +11 -0
- package/templates/auth/ui/button.tsx +57 -0
- package/templates/auth/ui/card.tsx +76 -0
- package/templates/auth/ui/field.tsx +242 -0
- package/templates/auth/ui/input.tsx +22 -0
- package/templates/auth/ui/label.tsx +24 -0
- package/templates/auth/ui/separator.tsx +31 -0
- package/templates/forgewp-config.ts +70 -3
- package/templates/menus.json +12 -10
- package/templates/mock-data.json +122 -433
- package/templates/products.json +5 -0
- package/templates/roles.json +7 -0
- package/templates/sessions.json +1 -0
- package/templates/users.json +38 -0
- package/templates/wordpress.tsx +1835 -1297
- package/bin/make-template.js +0 -145
- package/lib/block-compiler.js +0 -552
- package/lib/functions-builder.js +0 -2044
- package/lib/hydration-scanner.js +0 -821
package/lib/blueprints.js
CHANGED
|
@@ -5,11 +5,83 @@ import { fileURLToPath } from 'node:url';
|
|
|
5
5
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
6
|
const readTemplate = (name) => fs.readFileSync(path.join(__dirname, '..', 'templates', name), 'utf8');
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const baseBlueprints = {
|
|
9
9
|
'cms/menus.json': readTemplate('menus.json'),
|
|
10
10
|
'cms/mock-data.json': readTemplate('mock-data.json'),
|
|
11
|
-
'src/.forgewp/forgewp-config.ts': readTemplate('forgewp-config.ts'),
|
|
12
|
-
'src/.forgewp/wordpress.tsx': readTemplate('wordpress.tsx'),
|
|
13
|
-
'src/.forgewp/SEO.tsx': readTemplate('SEO.tsx'),
|
|
14
|
-
'src/.forgewp/PresetsStyle.tsx': readTemplate('PresetsStyle.tsx'),
|
|
15
11
|
};
|
|
12
|
+
|
|
13
|
+
function checkHasWooCommerce(projectRoot) {
|
|
14
|
+
const packageJsonPath = path.join(projectRoot, 'package.json');
|
|
15
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
16
|
+
try {
|
|
17
|
+
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
18
|
+
if (
|
|
19
|
+
(pkg.dependencies && pkg.dependencies['@forgewp/woocommerce']) ||
|
|
20
|
+
(pkg.devDependencies && pkg.devDependencies['@forgewp/woocommerce']) ||
|
|
21
|
+
(pkg.peerDependencies && pkg.peerDependencies['@forgewp/woocommerce'])
|
|
22
|
+
) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
} catch (e) {}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function checkHasAuth(projectRoot) {
|
|
31
|
+
const packageJsonPath = path.join(projectRoot, 'package.json');
|
|
32
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
33
|
+
try {
|
|
34
|
+
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
35
|
+
if (
|
|
36
|
+
(pkg.dependencies && pkg.dependencies['@forgewp/auth']) ||
|
|
37
|
+
(pkg.devDependencies && pkg.devDependencies['@forgewp/auth']) ||
|
|
38
|
+
(pkg.peerDependencies && pkg.peerDependencies['@forgewp/auth'])
|
|
39
|
+
) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
} catch (e) {}
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function getSystemBlueprints(projectRoot = process.cwd()) {
|
|
48
|
+
const hasWooCommerce = checkHasWooCommerce(projectRoot);
|
|
49
|
+
const hasAuth = checkHasAuth(projectRoot);
|
|
50
|
+
|
|
51
|
+
const blueprints = { ...baseBlueprints };
|
|
52
|
+
if (hasWooCommerce) {
|
|
53
|
+
blueprints['cms/products.json'] = readTemplate('products.json');
|
|
54
|
+
}
|
|
55
|
+
if (hasAuth) {
|
|
56
|
+
blueprints['cms/users.json'] = readTemplate('users.json');
|
|
57
|
+
blueprints['cms/roles.json'] = readTemplate('roles.json');
|
|
58
|
+
blueprints['cms/sessions.json'] = readTemplate('sessions.json');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return blueprints;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const SYSTEM_BLUEPRINTS = new Proxy(baseBlueprints, {
|
|
65
|
+
ownKeys() {
|
|
66
|
+
return Reflect.ownKeys(getSystemBlueprints(process.cwd()));
|
|
67
|
+
},
|
|
68
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
69
|
+
const bps = getSystemBlueprints(process.cwd());
|
|
70
|
+
if (prop in bps) {
|
|
71
|
+
return {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: bps[prop]
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
},
|
|
80
|
+
get(target, prop) {
|
|
81
|
+
const bps = getSystemBlueprints(process.cwd());
|
|
82
|
+
if (typeof prop === 'string' && prop in bps) {
|
|
83
|
+
return bps[prop];
|
|
84
|
+
}
|
|
85
|
+
return target[prop];
|
|
86
|
+
}
|
|
87
|
+
});
|
package/lib/build-assets.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from
|
|
2
|
-
import path from
|
|
3
|
-
import { spawnSync } from
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @param {string} themeRoot
|
|
@@ -9,53 +9,58 @@ import { spawnSync } from "node:child_process";
|
|
|
9
9
|
*/
|
|
10
10
|
export function buildAssets(themeRoot, options = {}) {
|
|
11
11
|
const pm = options.packageManager ?? detectPackageManager();
|
|
12
|
-
const build = runPackageScript(themeRoot, pm,
|
|
12
|
+
const build = runPackageScript(themeRoot, pm, 'build', options);
|
|
13
13
|
|
|
14
14
|
if (build.status !== 0) {
|
|
15
|
-
|
|
15
|
+
if (build.stdout) process.stdout.write(build.stdout);
|
|
16
|
+
if (build.stderr) process.stderr.write(build.stderr);
|
|
17
|
+
throw new Error(
|
|
18
|
+
'Vite build failed. Fix build errors and run export again.',
|
|
19
|
+
);
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
return readBuildManifest(themeRoot);
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
function detectPackageManager() {
|
|
22
|
-
const ua = process.env.npm_config_user_agent ??
|
|
23
|
-
if (ua.startsWith(
|
|
24
|
-
if (ua.startsWith(
|
|
25
|
-
if (ua.startsWith(
|
|
26
|
-
return
|
|
26
|
+
const ua = process.env.npm_config_user_agent ?? '';
|
|
27
|
+
if (ua.startsWith('pnpm/')) return 'pnpm';
|
|
28
|
+
if (ua.startsWith('yarn/')) return 'yarn';
|
|
29
|
+
if (ua.startsWith('bun/')) return 'bun';
|
|
30
|
+
return 'npm';
|
|
27
31
|
}
|
|
28
32
|
|
|
29
|
-
function runPackageScript(cwd, pm, script) {
|
|
33
|
+
function runPackageScript(cwd, pm, script, options = {}) {
|
|
30
34
|
const env = { ...process.env, INIT_CWD: cwd };
|
|
35
|
+
const stdio = options.verbose ? 'inherit' : 'pipe';
|
|
31
36
|
|
|
32
37
|
switch (pm) {
|
|
33
|
-
case
|
|
34
|
-
return spawnSync(
|
|
38
|
+
case 'pnpm':
|
|
39
|
+
return spawnSync('pnpm', ['run', script], {
|
|
35
40
|
cwd,
|
|
36
|
-
stdio
|
|
37
|
-
shell: process.platform ===
|
|
41
|
+
stdio,
|
|
42
|
+
shell: process.platform === 'win32',
|
|
38
43
|
env,
|
|
39
44
|
});
|
|
40
|
-
case
|
|
41
|
-
return spawnSync(
|
|
45
|
+
case 'yarn':
|
|
46
|
+
return spawnSync('yarn', ['run', script], {
|
|
42
47
|
cwd,
|
|
43
|
-
stdio
|
|
44
|
-
shell: process.platform ===
|
|
48
|
+
stdio,
|
|
49
|
+
shell: process.platform === 'win32',
|
|
45
50
|
env,
|
|
46
51
|
});
|
|
47
|
-
case
|
|
48
|
-
return spawnSync(
|
|
52
|
+
case 'bun':
|
|
53
|
+
return spawnSync('bun', ['run', script], {
|
|
49
54
|
cwd,
|
|
50
|
-
stdio
|
|
51
|
-
shell: process.platform ===
|
|
55
|
+
stdio,
|
|
56
|
+
shell: process.platform === 'win32',
|
|
52
57
|
env,
|
|
53
58
|
});
|
|
54
59
|
default:
|
|
55
|
-
return spawnSync(
|
|
60
|
+
return spawnSync('npm', ['run', script], {
|
|
56
61
|
cwd,
|
|
57
|
-
stdio
|
|
58
|
-
shell: process.platform ===
|
|
62
|
+
stdio,
|
|
63
|
+
shell: process.platform === 'win32',
|
|
59
64
|
env,
|
|
60
65
|
});
|
|
61
66
|
}
|
|
@@ -67,30 +72,30 @@ function runPackageScript(cwd, pm, script) {
|
|
|
67
72
|
*/
|
|
68
73
|
export function readBuildManifest(themeRoot) {
|
|
69
74
|
const candidates = [
|
|
70
|
-
path.join(themeRoot,
|
|
71
|
-
path.join(themeRoot,
|
|
75
|
+
path.join(themeRoot, 'dist', '.vite', 'manifest.json'),
|
|
76
|
+
path.join(themeRoot, 'dist', 'manifest.json'),
|
|
72
77
|
];
|
|
73
78
|
|
|
74
79
|
const manifestPath = candidates.find((candidate) => existsSync(candidate));
|
|
75
80
|
|
|
76
81
|
if (!manifestPath) {
|
|
77
82
|
throw new Error(
|
|
78
|
-
|
|
83
|
+
'Build manifest not found. Enable build.manifest in vite.config.ts and run build first.',
|
|
79
84
|
);
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
const manifest = JSON.parse(readFileSync(manifestPath,
|
|
87
|
+
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
83
88
|
const entry =
|
|
84
|
-
manifest[
|
|
89
|
+
manifest['index.html'] ??
|
|
85
90
|
Object.values(manifest).find((chunk) => chunk?.isEntry);
|
|
86
91
|
|
|
87
92
|
if (!entry?.file) {
|
|
88
|
-
throw new Error(
|
|
93
|
+
throw new Error('Could not find entry chunk in Vite manifest');
|
|
89
94
|
}
|
|
90
95
|
|
|
91
96
|
const cssFile = entry.css?.[0];
|
|
92
97
|
if (!cssFile) {
|
|
93
|
-
throw new Error(
|
|
98
|
+
throw new Error('No CSS file in Vite build output');
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
return {
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
function normalizePagePath(url) {
|
|
2
|
+
if (typeof url !== 'string' || !url.startsWith('/') || url.startsWith('//')) {
|
|
3
|
+
return '';
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const path = url.split(/[?#]/, 1)[0].replace(/^\/+|\/+$/g, '');
|
|
7
|
+
return path;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function pathToTemplateSlug(path) {
|
|
11
|
+
return path.split('/').filter(Boolean).join('-').toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function titleToTemplateSlug(title) {
|
|
15
|
+
const componentName =
|
|
16
|
+
String(title)
|
|
17
|
+
.replace(/[^a-zA-Z0-9]/g, ' ')
|
|
18
|
+
.trim()
|
|
19
|
+
.split(/\s+/)
|
|
20
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
21
|
+
.join('') + 'Page';
|
|
22
|
+
|
|
23
|
+
return componentName.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function flattenMenuItems(items, output = []) {
|
|
27
|
+
if (!Array.isArray(items)) return output;
|
|
28
|
+
|
|
29
|
+
for (const item of items) {
|
|
30
|
+
if (!item || typeof item !== 'object') continue;
|
|
31
|
+
output.push(item);
|
|
32
|
+
flattenMenuItems(item.children, output);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return output;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Maps compiled Page templates to their matching menu route and removes
|
|
40
|
+
* duplicate templates that describe the same WordPress page path.
|
|
41
|
+
*/
|
|
42
|
+
export function collectPageTemplateRoutes(templateSlugs, menus = {}) {
|
|
43
|
+
const items = Object.entries(menus)
|
|
44
|
+
.filter(([location]) => !location.startsWith('_'))
|
|
45
|
+
.flatMap(([, menuItems]) => flattenMenuItems(menuItems));
|
|
46
|
+
const routes = new Map();
|
|
47
|
+
|
|
48
|
+
for (const templateSlug of templateSlugs) {
|
|
49
|
+
let matchedRoute = null;
|
|
50
|
+
|
|
51
|
+
for (const item of items) {
|
|
52
|
+
const path = normalizePagePath(item.url);
|
|
53
|
+
if (!path) continue;
|
|
54
|
+
|
|
55
|
+
if (titleToTemplateSlug(item.title) === templateSlug) {
|
|
56
|
+
matchedRoute = {
|
|
57
|
+
title: item.title,
|
|
58
|
+
slug: path,
|
|
59
|
+
score: 4,
|
|
60
|
+
};
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const routeSlug = pathToTemplateSlug(path);
|
|
65
|
+
const templateRouteSlug = templateSlug.endsWith('-page')
|
|
66
|
+
? templateSlug.slice(0, -5)
|
|
67
|
+
: templateSlug;
|
|
68
|
+
if (routeSlug === templateRouteSlug) {
|
|
69
|
+
matchedRoute = {
|
|
70
|
+
title: item.title,
|
|
71
|
+
slug: path,
|
|
72
|
+
score: templateSlug.endsWith('-page') ? 3 : 2,
|
|
73
|
+
};
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const fallbackSlug = templateSlug.endsWith('-page')
|
|
79
|
+
? templateSlug.slice(0, -5)
|
|
80
|
+
: templateSlug;
|
|
81
|
+
const route = matchedRoute || {
|
|
82
|
+
title: fallbackSlug
|
|
83
|
+
.split('-')
|
|
84
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
85
|
+
.join(' '),
|
|
86
|
+
slug: fallbackSlug,
|
|
87
|
+
score: 0,
|
|
88
|
+
};
|
|
89
|
+
const existing = routes.get(route.slug);
|
|
90
|
+
|
|
91
|
+
if (!existing || route.score > existing.score) {
|
|
92
|
+
routes.set(route.slug, {
|
|
93
|
+
title: route.title,
|
|
94
|
+
slug: route.slug,
|
|
95
|
+
template: `page-${templateSlug}.php`,
|
|
96
|
+
score: route.score,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return Array.from(routes.values(), ({ score, ...route }) => route);
|
|
102
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collects local page routes that exist in the React app (menus + routes.tsx)
|
|
5
|
+
* but have no compiled custom template yet. These must still become WordPress
|
|
6
|
+
* pages — otherwise redirect_canonical() guesses a similar CPT slug
|
|
7
|
+
* (e.g. /about → /product/about-a-chair-aa51/).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const RESERVED_FIRST_SEGMENTS = new Set([
|
|
11
|
+
'product',
|
|
12
|
+
'products',
|
|
13
|
+
'product-category',
|
|
14
|
+
'product-tag',
|
|
15
|
+
'shop',
|
|
16
|
+
'cart',
|
|
17
|
+
'checkout',
|
|
18
|
+
'my-account',
|
|
19
|
+
'category',
|
|
20
|
+
'categories',
|
|
21
|
+
'tag',
|
|
22
|
+
'author',
|
|
23
|
+
'feed',
|
|
24
|
+
'search',
|
|
25
|
+
'wp-admin',
|
|
26
|
+
'wp-json',
|
|
27
|
+
'wp-content',
|
|
28
|
+
'wp-includes',
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
function titleFromSlug(slug) {
|
|
32
|
+
const leaf = slug.split('/').filter(Boolean).pop() || slug;
|
|
33
|
+
return leaf
|
|
34
|
+
.split('-')
|
|
35
|
+
.filter(Boolean)
|
|
36
|
+
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
|
|
37
|
+
.join(' ');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function consider(url, title, description, seen, out) {
|
|
41
|
+
if (!url || typeof url !== 'string') return;
|
|
42
|
+
const trimmed = url.trim();
|
|
43
|
+
if (
|
|
44
|
+
!trimmed.startsWith('/') ||
|
|
45
|
+
trimmed.startsWith('//') ||
|
|
46
|
+
trimmed.startsWith('/http')
|
|
47
|
+
) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const pathOnly = trimmed.split('?')[0].split('#')[0];
|
|
51
|
+
const slug = pathOnly.replace(/^\//, '').replace(/\/$/, '');
|
|
52
|
+
if (!slug || slug.includes(':') || slug.includes('*')) return;
|
|
53
|
+
|
|
54
|
+
const first = slug.split('/')[0];
|
|
55
|
+
if (RESERVED_FIRST_SEGMENTS.has(first)) return;
|
|
56
|
+
if (seen.has(slug)) return;
|
|
57
|
+
|
|
58
|
+
seen.add(slug);
|
|
59
|
+
const entry = {
|
|
60
|
+
slug,
|
|
61
|
+
title: (title && String(title).trim()) || titleFromSlug(slug),
|
|
62
|
+
template: 'page-placeholder.php',
|
|
63
|
+
};
|
|
64
|
+
if (description && String(description).trim()) {
|
|
65
|
+
entry.description = String(description).trim();
|
|
66
|
+
}
|
|
67
|
+
out.push(entry);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function walkMenuItems(items, seen, out) {
|
|
71
|
+
if (!Array.isArray(items)) return;
|
|
72
|
+
for (const item of items) {
|
|
73
|
+
if (!item || typeof item !== 'object') continue;
|
|
74
|
+
consider(item.url, null, item.description || null, seen, out);
|
|
75
|
+
if (item.children) walkMenuItems(item.children, seen, out);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function collectStaticRouteDefinitions(routesSource = '') {
|
|
80
|
+
if (!routesSource) return [];
|
|
81
|
+
|
|
82
|
+
const routes = [];
|
|
83
|
+
try {
|
|
84
|
+
const ast = parse(routesSource, {
|
|
85
|
+
sourceType: 'unambiguous',
|
|
86
|
+
plugins: ['jsx', 'typescript'],
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const visit = (node) => {
|
|
90
|
+
if (!node || typeof node !== 'object') return;
|
|
91
|
+
|
|
92
|
+
if (node.type === 'JSXElement') {
|
|
93
|
+
const opening = node.openingElement;
|
|
94
|
+
if (
|
|
95
|
+
opening.name.type === 'JSXIdentifier' &&
|
|
96
|
+
opening.name.name === 'Route'
|
|
97
|
+
) {
|
|
98
|
+
const pathAttribute = opening.attributes.find(
|
|
99
|
+
(attribute) =>
|
|
100
|
+
attribute.type === 'JSXAttribute' &&
|
|
101
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
102
|
+
attribute.name.name === 'path',
|
|
103
|
+
);
|
|
104
|
+
const pathValue = pathAttribute?.value;
|
|
105
|
+
const routePath =
|
|
106
|
+
pathValue?.type === 'StringLiteral'
|
|
107
|
+
? pathValue.value
|
|
108
|
+
: pathValue?.type === 'JSXExpressionContainer' &&
|
|
109
|
+
pathValue.expression.type === 'StringLiteral'
|
|
110
|
+
? pathValue.expression.value
|
|
111
|
+
: '';
|
|
112
|
+
|
|
113
|
+
if (routePath.startsWith('/')) {
|
|
114
|
+
const block = routesSource.slice(node.start, node.end);
|
|
115
|
+
const titleMatch = block.match(
|
|
116
|
+
/\btitle\s*[:=]\s*['"`]([^'"`]+)['"`]/i,
|
|
117
|
+
);
|
|
118
|
+
const descMatch = block.match(
|
|
119
|
+
/\bdescription\s*[:=]\s*['"`]([^'"`]+)['"`]/i,
|
|
120
|
+
);
|
|
121
|
+
routes.push({
|
|
122
|
+
path: routePath,
|
|
123
|
+
title: titleMatch ? titleMatch[1] : null,
|
|
124
|
+
description: descMatch ? descMatch[1] : null,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const value of Object.values(node)) {
|
|
131
|
+
if (Array.isArray(value)) {
|
|
132
|
+
for (const child of value) visit(child);
|
|
133
|
+
} else if (value && typeof value === 'object') {
|
|
134
|
+
visit(value);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
visit(ast);
|
|
140
|
+
} catch {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return routes;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {{ menus?: Record<string, any>, routesSource?: string, existingSlugs?: string[] }} opts
|
|
149
|
+
* @returns {Array<{ slug: string, title: string, description?: string, template: string }>}
|
|
150
|
+
*/
|
|
151
|
+
export function collectFallbackPageRoutes({
|
|
152
|
+
menus = {},
|
|
153
|
+
routesSource = '',
|
|
154
|
+
existingSlugs = [],
|
|
155
|
+
} = {}) {
|
|
156
|
+
const seen = new Set(
|
|
157
|
+
(existingSlugs || [])
|
|
158
|
+
.map((s) => String(s).replace(/^\/+|\/+$/g, ''))
|
|
159
|
+
.filter(Boolean),
|
|
160
|
+
);
|
|
161
|
+
const out = [];
|
|
162
|
+
|
|
163
|
+
if (routesSource) {
|
|
164
|
+
for (const route of collectStaticRouteDefinitions(routesSource)) {
|
|
165
|
+
consider(route.path, route.title, route.description, seen, out);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
for (const [location, items] of Object.entries(menus || {})) {
|
|
170
|
+
if (location.startsWith('_')) continue;
|
|
171
|
+
walkMenuItems(items, seen, out);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return out;
|
|
175
|
+
}
|
package/lib/diagnostics.js
CHANGED
|
@@ -44,11 +44,16 @@ export function analyzeHydrationIslands(themeRoot, mapping) {
|
|
|
44
44
|
* @param {{ heavyCount: number, results: any[] }} analysis
|
|
45
45
|
*/
|
|
46
46
|
export function printDiagnosticsReport(analysis) {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (process.env.FORGEWP_VERBOSE !== '1') return;
|
|
48
|
+
console.log(
|
|
49
|
+
`\n🩺 ${pc.bold(pc.bgCyan(pc.black(' FORGEWP HYDRATION DIAGNOSTICS ')))}`,
|
|
50
|
+
);
|
|
51
|
+
|
|
49
52
|
if (analysis.results.length === 0) {
|
|
50
|
-
console.log(
|
|
51
|
-
|
|
53
|
+
console.log(
|
|
54
|
+
` ${pc.dim('No active hydration islands detected in this project.')}`,
|
|
55
|
+
);
|
|
56
|
+
console.log('─'.repeat(60) + '\n');
|
|
52
57
|
return;
|
|
53
58
|
}
|
|
54
59
|
|
|
@@ -57,23 +62,37 @@ export function printDiagnosticsReport(analysis) {
|
|
|
57
62
|
const formattedSize = `${item.sizeKb.toFixed(1)} kB`;
|
|
58
63
|
|
|
59
64
|
if (item.isHeavy) {
|
|
60
|
-
console.log(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
console.log(`
|
|
64
|
-
console.log(`
|
|
65
|
-
console.log(
|
|
65
|
+
console.log(
|
|
66
|
+
` ⚠️ ${pc.yellow(pc.bold(paddedName))} ${pc.red(pc.bold(formattedSize))} ${pc.bgRed(pc.black(' HEAVY '))}`,
|
|
67
|
+
);
|
|
68
|
+
console.log(` ${pc.dim('👉 Chunk Path:')} dist/${item.chunkPath}`);
|
|
69
|
+
console.log(` ${pc.dim('👉 Recommendations:')}`);
|
|
70
|
+
console.log(
|
|
71
|
+
` 1. Split static markup sections out of the component into static wrappers.`,
|
|
72
|
+
);
|
|
73
|
+
console.log(
|
|
74
|
+
` 2. Avoid heavy external packages inside the island (or lazy load them inside React).`,
|
|
75
|
+
);
|
|
76
|
+
console.log(
|
|
77
|
+
` 3. Switch to a non-blocking trigger strategy (e.g. trigger="interaction" or trigger="idle").`,
|
|
78
|
+
);
|
|
66
79
|
} else {
|
|
67
80
|
console.log(` ✅ ${pc.green(paddedName)} ${pc.green(formattedSize)}`);
|
|
68
81
|
}
|
|
69
82
|
}
|
|
70
83
|
|
|
71
|
-
console.log(
|
|
84
|
+
console.log('─'.repeat(60));
|
|
72
85
|
if (analysis.heavyCount > 0) {
|
|
73
|
-
console.log(
|
|
74
|
-
|
|
86
|
+
console.log(
|
|
87
|
+
` ${pc.red(pc.bold('WARNING:'))} Found ${analysis.heavyCount} heavy interactive hydration island(s).`,
|
|
88
|
+
);
|
|
89
|
+
console.log(
|
|
90
|
+
` 💡 Keep JS bundles light. Modern Web Vitals rewards minimal runtime overhead!\n`,
|
|
91
|
+
);
|
|
75
92
|
} else {
|
|
76
|
-
console.log(
|
|
93
|
+
console.log(
|
|
94
|
+
` ${pc.green(pc.bold('SYSTEM OPTIMIZED:'))} All interactive hydration islands are within healthy thresholds (<100 kB).\n`,
|
|
95
|
+
);
|
|
77
96
|
}
|
|
78
97
|
}
|
|
79
98
|
|
|
@@ -85,7 +104,12 @@ export function printDiagnosticsReport(analysis) {
|
|
|
85
104
|
* @param {any} config
|
|
86
105
|
* @returns {string} Path to the generated report
|
|
87
106
|
*/
|
|
88
|
-
export function generateVisualReport(
|
|
107
|
+
export function generateVisualReport(
|
|
108
|
+
themeRoot,
|
|
109
|
+
analysis,
|
|
110
|
+
islandsWithProps,
|
|
111
|
+
config,
|
|
112
|
+
) {
|
|
89
113
|
const islands = islandsWithProps.map((item) => {
|
|
90
114
|
const analysisMatch = analysis.results.find((a) => a.name === item.name);
|
|
91
115
|
return {
|
|
@@ -100,13 +124,13 @@ export function generateVisualReport(themeRoot, analysis, islandsWithProps, conf
|
|
|
100
124
|
// Calculate dynamic performance score
|
|
101
125
|
let score = 100;
|
|
102
126
|
let totalJsKb = 0;
|
|
103
|
-
|
|
127
|
+
|
|
104
128
|
for (const island of islands) {
|
|
105
129
|
totalJsKb += island.sizeKb;
|
|
106
130
|
if (island.isHeavy) {
|
|
107
131
|
score -= 15;
|
|
108
132
|
}
|
|
109
|
-
if (island.trigger ===
|
|
133
|
+
if (island.trigger === 'load') {
|
|
110
134
|
score -= 10;
|
|
111
135
|
}
|
|
112
136
|
if (island.smartDiscovered) {
|
|
@@ -116,25 +140,25 @@ export function generateVisualReport(themeRoot, analysis, islandsWithProps, conf
|
|
|
116
140
|
|
|
117
141
|
const violations = analysis.violations || [];
|
|
118
142
|
for (const v of violations) {
|
|
119
|
-
if (v.type ===
|
|
120
|
-
else if (v.type ===
|
|
121
|
-
else if (v.type ===
|
|
143
|
+
if (v.type === 'metadata') score -= 5;
|
|
144
|
+
else if (v.type === 'loop') score -= 15;
|
|
145
|
+
else if (v.type === 'url') score -= 3;
|
|
122
146
|
}
|
|
123
147
|
|
|
124
148
|
score -= Math.floor(totalJsKb / 10);
|
|
125
149
|
score = Math.max(10, Math.min(100, score));
|
|
126
150
|
|
|
127
|
-
let grade =
|
|
128
|
-
if (score >= 90) grade =
|
|
129
|
-
else if (score >= 80) grade =
|
|
130
|
-
else if (score >= 70) grade =
|
|
131
|
-
else if (score >= 60) grade =
|
|
151
|
+
let grade = 'F';
|
|
152
|
+
if (score >= 90) grade = 'A';
|
|
153
|
+
else if (score >= 80) grade = 'B';
|
|
154
|
+
else if (score >= 70) grade = 'C';
|
|
155
|
+
else if (score >= 60) grade = 'D';
|
|
132
156
|
|
|
133
157
|
const payload = {
|
|
134
158
|
config: {
|
|
135
|
-
name: config.name ||
|
|
136
|
-
slug: config.slug ||
|
|
137
|
-
version: config.version ||
|
|
159
|
+
name: config.name || 'Unknown Theme',
|
|
160
|
+
slug: config.slug || 'unknown',
|
|
161
|
+
version: config.version || '1.0.0',
|
|
138
162
|
},
|
|
139
163
|
score,
|
|
140
164
|
grade,
|
|
@@ -142,15 +166,17 @@ export function generateVisualReport(themeRoot, analysis, islandsWithProps, conf
|
|
|
142
166
|
violations,
|
|
143
167
|
};
|
|
144
168
|
|
|
145
|
-
const outDir = path.join(themeRoot,
|
|
169
|
+
const outDir = path.join(themeRoot, '.forgewp', 'analyzer');
|
|
146
170
|
if (!fs.existsSync(outDir)) {
|
|
147
171
|
fs.mkdirSync(outDir, { recursive: true });
|
|
148
172
|
}
|
|
149
173
|
|
|
150
|
-
const htmlContent = ANALYZER_TEMPLATE.replace(
|
|
174
|
+
const htmlContent = ANALYZER_TEMPLATE.replace('__DATA_PLACEHOLDER__', () =>
|
|
175
|
+
JSON.stringify(payload),
|
|
176
|
+
);
|
|
151
177
|
|
|
152
|
-
const reportPath = path.join(outDir,
|
|
153
|
-
fs.writeFileSync(reportPath, htmlContent,
|
|
178
|
+
const reportPath = path.join(outDir, 'index.html');
|
|
179
|
+
fs.writeFileSync(reportPath, htmlContent, 'utf8');
|
|
154
180
|
|
|
155
181
|
return reportPath;
|
|
156
182
|
}
|
|
@@ -795,4 +821,3 @@ const ANALYZER_TEMPLATE = `<!DOCTYPE html>
|
|
|
795
821
|
</script>
|
|
796
822
|
</body>
|
|
797
823
|
</html>`;
|
|
798
|
-
|