@verbaly/compiler 0.46.0 → 0.48.0
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/dist/cli.js +48 -23
- package/dist/cli.js.map +1 -1
- package/dist/config-iLrfRYDB.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +52 -22
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/cli.js
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
4
|
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
5
|
-
import { LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
5
|
+
import { LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import { glob } from "tinyglobby";
|
|
8
8
|
import { parse as parse$1 } from "@babel/parser";
|
|
9
9
|
import { createHash } from "node:crypto";
|
|
10
10
|
import MagicString from "magic-string";
|
|
11
|
-
import "picomatch";
|
|
11
|
+
import picomatch from "picomatch";
|
|
12
12
|
//#region src/catalog.ts
|
|
13
13
|
function catalogPath(cfg, locale) {
|
|
14
14
|
return join(cfg.dir, `${locale}.json`);
|
|
@@ -651,7 +651,8 @@ function check(cfg, catalogs, registry) {
|
|
|
651
651
|
broken
|
|
652
652
|
};
|
|
653
653
|
}
|
|
654
|
-
function formatCheckResult(result) {
|
|
654
|
+
function formatCheckResult(result, root) {
|
|
655
|
+
const show = (file) => root ? relative(root, file).replaceAll("\\", "/") : file;
|
|
655
656
|
const lines = [];
|
|
656
657
|
if (result.missing.length > 0) {
|
|
657
658
|
lines.push("missing translations:");
|
|
@@ -662,7 +663,7 @@ function formatCheckResult(result) {
|
|
|
662
663
|
}
|
|
663
664
|
if (result.unknown.length > 0) {
|
|
664
665
|
lines.push("unknown keys (not in any catalog):");
|
|
665
|
-
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.join(", ")})`);
|
|
666
|
+
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.map(show).join(", ")})`);
|
|
666
667
|
}
|
|
667
668
|
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
668
669
|
if (errors.length > 0) {
|
|
@@ -809,6 +810,9 @@ ${lines.join("\n")}
|
|
|
809
810
|
}
|
|
810
811
|
export type VerbalyKey = keyof VerbalyMessages & string;
|
|
811
812
|
|
|
813
|
+
export type VerbalyKeyTree = VerbalyKey | { readonly [name: string]: VerbalyKeyTree };
|
|
814
|
+
export function defineKeys<const T extends VerbalyKeyTree>(keys: T): T;
|
|
815
|
+
|
|
812
816
|
export const verbaly: import('verbaly').Verbaly<VerbalyKey>;
|
|
813
817
|
|
|
814
818
|
export const sourceLocale: string;
|
|
@@ -985,6 +989,7 @@ const SKIP_KEYS = /* @__PURE__ */ new Set([
|
|
|
985
989
|
]);
|
|
986
990
|
const DEFAULT_T_NAMES = ["t"];
|
|
987
991
|
const JSX_FILE_RE = /\.(?:[cm]?jsx?|tsx)$/;
|
|
992
|
+
const DEFINE_KEYS = "defineKeys";
|
|
988
993
|
function analyze(code, file, options = {}) {
|
|
989
994
|
const names = new Set(options.tNames ?? DEFAULT_T_NAMES);
|
|
990
995
|
const ast = parse$1(code, {
|
|
@@ -1016,8 +1021,13 @@ function analyze(code, file, options = {}) {
|
|
|
1016
1021
|
});
|
|
1017
1022
|
} else if (node.type === "CallExpression") {
|
|
1018
1023
|
const callee = node.callee;
|
|
1024
|
+
const args = node.arguments;
|
|
1025
|
+
if (callee.type === "Identifier" && callee.name === DEFINE_KEYS) {
|
|
1026
|
+
collectDeclaredKeys(args[0], file, usedKeys);
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1019
1029
|
if (!isTReference(callee, names)) return;
|
|
1020
|
-
const first =
|
|
1030
|
+
const first = args[0];
|
|
1021
1031
|
if (first?.type === "StringLiteral") usedKeys.push({
|
|
1022
1032
|
key: first.value,
|
|
1023
1033
|
file
|
|
@@ -1030,6 +1040,19 @@ function analyze(code, file, options = {}) {
|
|
|
1030
1040
|
strayImports
|
|
1031
1041
|
};
|
|
1032
1042
|
}
|
|
1043
|
+
function collectDeclaredKeys(node, file, out) {
|
|
1044
|
+
if (node?.type !== "ObjectExpression") return;
|
|
1045
|
+
for (const property of node.properties) {
|
|
1046
|
+
if (property.type !== "ObjectProperty") continue;
|
|
1047
|
+
const value = property.value;
|
|
1048
|
+
if (value.type === "StringLiteral") {
|
|
1049
|
+
if (value.value !== "") out.push({
|
|
1050
|
+
key: value.value,
|
|
1051
|
+
file
|
|
1052
|
+
});
|
|
1053
|
+
} else if (value.type === "ObjectExpression") collectDeclaredKeys(value, file, out);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1033
1056
|
const VERBALY_PACKAGE = /^(?:verbaly|@verbaly\/[\w-]+)$/;
|
|
1034
1057
|
function collectStrayImports(node, file, out) {
|
|
1035
1058
|
const source = node.source;
|
|
@@ -1037,8 +1060,10 @@ function collectStrayImports(node, file, out) {
|
|
|
1037
1060
|
for (const spec of node.specifiers) {
|
|
1038
1061
|
if (spec.type !== "ImportSpecifier") continue;
|
|
1039
1062
|
const imported = spec.imported;
|
|
1040
|
-
if (imported.type
|
|
1041
|
-
|
|
1063
|
+
if (imported.type !== "Identifier") continue;
|
|
1064
|
+
const name = imported.name;
|
|
1065
|
+
if (name === "t" || name === DEFINE_KEYS) out.push({
|
|
1066
|
+
name,
|
|
1042
1067
|
source: source.value,
|
|
1043
1068
|
file
|
|
1044
1069
|
});
|
|
@@ -2579,6 +2604,10 @@ function injectAlternates(ms, html, alternates) {
|
|
|
2579
2604
|
}
|
|
2580
2605
|
const TITLE_TAG = /<title\b([^>]*)>/i;
|
|
2581
2606
|
const REFRESH_META = /<meta\b[^>]*http-equiv\s*=\s*["']?refresh/i;
|
|
2607
|
+
const NOINDEX_META = /<meta\b[^>]*name\s*=\s*["']?robots["']?[^>]*content\s*=\s*["'][^"']*noindex/i;
|
|
2608
|
+
function isIndexable(html) {
|
|
2609
|
+
return !REFRESH_META.test(html) && !NOINDEX_META.test(html);
|
|
2610
|
+
}
|
|
2582
2611
|
function headIsBound(html, attr) {
|
|
2583
2612
|
if (REFRESH_META.test(html)) return true;
|
|
2584
2613
|
const title = TITLE_TAG.exec(html);
|
|
@@ -2591,6 +2620,8 @@ async function renderSite(cfg, options = {}) {
|
|
|
2591
2620
|
const base = normalizeBase(options.base ?? cfg.render.base);
|
|
2592
2621
|
const baseUrl = (options.baseUrl ?? cfg.render.baseUrl)?.replace(/\/+$/, "");
|
|
2593
2622
|
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
2623
|
+
const patterns = options.exclude ?? cfg.render.exclude ?? [];
|
|
2624
|
+
const excluded = patterns.length > 0 ? picomatch(patterns) : () => false;
|
|
2594
2625
|
const sitemap = options.sitemap ?? cfg.render.sitemap ?? false;
|
|
2595
2626
|
const wantSitemap = sitemap !== false && baseUrl !== void 0;
|
|
2596
2627
|
const clean = options.clean ?? cfg.render.clean ?? false;
|
|
@@ -2624,7 +2655,7 @@ async function renderSite(cfg, options = {}) {
|
|
|
2624
2655
|
const rel = relative(site, file).replace(/\\/g, "/");
|
|
2625
2656
|
if (locales.length > 1 && !headIsBound(html, attribute ?? "data-verbaly")) untranslatedHead.push(rel);
|
|
2626
2657
|
const alternates = wantHreflang ? pageAlternates(baseUrl, rel, locales, cfg.sourceLocale) : [];
|
|
2627
|
-
if (wantHreflang) urls.push({
|
|
2658
|
+
if (wantHreflang && isIndexable(html) && !excluded(rel)) urls.push({
|
|
2628
2659
|
rel,
|
|
2629
2660
|
alternates
|
|
2630
2661
|
});
|
|
@@ -2668,24 +2699,18 @@ function formatRenderWarnings(result, sourceLocale) {
|
|
|
2668
2699
|
if (result.untranslatedHead.length > 0) lines.push(` ${counted(result.untranslatedHead.length, "page")} of ${result.files} ship the same <title> in every locale (${result.untranslatedHead[0]}), so search results read in ${sourceLocale}`, ` fix: bind the head too, <title data-verbaly="key"> and a meta through data-verbaly-attr`);
|
|
2669
2700
|
return lines;
|
|
2670
2701
|
}
|
|
2671
|
-
function
|
|
2702
|
+
function pagePath(rel) {
|
|
2672
2703
|
const path = rel.replace(/(^|\/)index\.html$/, "$1");
|
|
2673
|
-
return
|
|
2704
|
+
return path ? `/${path}` : "/";
|
|
2674
2705
|
}
|
|
2675
2706
|
function pageAlternates(baseUrl, rel, locales, sourceLocale) {
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
});
|
|
2683
|
-
}
|
|
2684
|
-
alternates.push({
|
|
2685
|
-
hreflang: "x-default",
|
|
2686
|
-
href: pageUrl(baseUrl, "", rel)
|
|
2707
|
+
return alternateLinks({
|
|
2708
|
+
supported: locales,
|
|
2709
|
+
sourceLocale,
|
|
2710
|
+
path: pagePath(rel),
|
|
2711
|
+
routing: "prefix-except-source",
|
|
2712
|
+
baseUrl
|
|
2687
2713
|
});
|
|
2688
|
-
return alternates;
|
|
2689
2714
|
}
|
|
2690
2715
|
function buildSitemap(urls) {
|
|
2691
2716
|
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls.flatMap(({ alternates }) => {
|
|
@@ -3357,7 +3382,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3357
3382
|
return;
|
|
3358
3383
|
}
|
|
3359
3384
|
const brokenCount = result.broken.filter((entry) => entry.severity === "error").length;
|
|
3360
|
-
const report = reporter === "github" ? `[verbaly] check failed: ${result.missing.length} missing, ${result.unknown.length} unknown, ${brokenCount} broken` : `[verbaly] check failed\n${formatCheckResult(result)}`;
|
|
3385
|
+
const report = reporter === "github" ? `[verbaly] check failed: ${result.missing.length} missing, ${result.unknown.length} unknown, ${brokenCount} broken` : `[verbaly] check failed\n${formatCheckResult(result, cfg.root)}`;
|
|
3361
3386
|
console.error(`${report}\n${checkNextSteps(result)}`);
|
|
3362
3387
|
process.exitCode = 1;
|
|
3363
3388
|
return;
|