@verbaly/compiler 0.46.0 → 0.47.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 CHANGED
@@ -8,7 +8,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 = node.arguments[0];
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 === "Identifier" && imported.name === "t") out.push({
1041
- name: "t",
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
  });
@@ -3357,7 +3388,7 @@ async function runCli(args = process.argv.slice(2)) {
3357
3388
  return;
3358
3389
  }
3359
3390
  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)}`;
3391
+ 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
3392
  console.error(`${report}\n${checkNextSteps(result)}`);
3362
3393
  process.exitCode = 1;
3363
3394
  return;