@verbaly/compiler 0.49.0 → 0.51.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
@@ -2,7 +2,7 @@
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, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
5
+ import { CATALOG_SCRIPT, 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";
@@ -793,7 +793,7 @@ function renderParamType(types) {
793
793
  ]) members.add(m);
794
794
  return [...members].join(" | ");
795
795
  }
796
- function generateDts(catalog) {
796
+ function generateDts(catalog, options = {}) {
797
797
  const lines = [];
798
798
  for (const [key, message] of Object.entries(catalog).sort(([a], [b]) => a.localeCompare(b))) {
799
799
  const params = collectParams(message);
@@ -826,7 +826,7 @@ ${lines.join("\n")}
826
826
  path?: string;
827
827
  base?: string;
828
828
  }): string | undefined;
829
- export function loadMessages(locale: string): Promise<Record<string, string>>;
829
+ export function loadMessages(locale: string): Promise<Record<string, string>>;${options.inlineCatalog ? "\n export function adoptPage(options?: {\n root?: ParentNode;\n path?: string;\n }): string | undefined;" : ""}
830
830
  export function createInstance(
831
831
  options?: import('verbaly').VerbalyOptions<VerbalyKey>,
832
832
  ): import('verbaly').Verbaly<VerbalyKey>;
@@ -865,7 +865,7 @@ declare module 'virtual:verbaly/locale/*' {
865
865
  }
866
866
  function writeDts(cfg, catalog, file) {
867
867
  file ??= join(cfg.root, "verbaly.d.ts");
868
- const content = generateDts(catalog);
868
+ const content = generateDts(catalog, { inlineCatalog: cfg.render.inlineCatalog === true });
869
869
  try {
870
870
  if (readFileSync(file, "utf8") === content) return;
871
871
  } catch {}
@@ -1584,7 +1584,7 @@ async function doctor(cfg) {
1584
1584
  if (source && scanning && cfg.dts !== false) {
1585
1585
  const dtsPath = cfg.dts ?? join(cfg.root, "verbaly.d.ts");
1586
1586
  if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
1587
- else if (readFileSync(dtsPath, "utf8") !== generateDts(source)) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
1587
+ else if (readFileSync(dtsPath, "utf8") !== generateDts(source, { inlineCatalog: cfg.render.inlineCatalog === true })) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
1588
1588
  else ok("types", "verbaly.d.ts is up to date");
1589
1589
  }
1590
1590
  const registry = await extractProject(cfg);
@@ -2438,6 +2438,8 @@ function localeInstance(catalogs, locale, sourceLocale) {
2438
2438
  }
2439
2439
  const HREFLANG_OPEN = "<!--verbaly:hreflang-->";
2440
2440
  const HREFLANG_CLOSE = "<!--/verbaly:hreflang-->";
2441
+ const CATALOG_OPEN = "<!--verbaly:catalog-->";
2442
+ const CATALOG_CLOSE = "<!--/verbaly:catalog-->";
2441
2443
  const REDIRECT_OPEN = "<!--verbaly:redirect-->";
2442
2444
  const REDIRECT_CLOSE = "<!--/verbaly:redirect-->";
2443
2445
  const VOID = new Set(VOID_TAGS);
@@ -2456,6 +2458,7 @@ function renderHtml(html, options) {
2456
2458
  const t = v.t;
2457
2459
  const ms = new MagicString(html);
2458
2460
  const missing = /* @__PURE__ */ new Set();
2461
+ const used = /* @__PURE__ */ new Set();
2459
2462
  const skip = protectedRanges(html);
2460
2463
  const inSkip = (index) => skip.some(([from, to]) => index >= from && index < to);
2461
2464
  const filled = [];
@@ -2498,6 +2501,7 @@ function renderHtml(html, options) {
2498
2501
  if (key) {
2499
2502
  if (!v.has(key)) missing.add(key);
2500
2503
  else if (!VOID.has(tagName) && !attrChunk.trimEnd().endsWith("/")) {
2504
+ used.add(key);
2501
2505
  const close = findClose(html, tagName, openEnd, inSkip);
2502
2506
  if (close) {
2503
2507
  const text = t(key, args);
@@ -2523,6 +2527,7 @@ function renderHtml(html, options) {
2523
2527
  missing.add(attrKey);
2524
2528
  continue;
2525
2529
  }
2530
+ used.add(attrKey);
2526
2531
  const value = safeAttribute(name, t(attrKey, args));
2527
2532
  if (value !== void 0) setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, value);
2528
2533
  }
@@ -2530,11 +2535,24 @@ function renderHtml(html, options) {
2530
2535
  }
2531
2536
  if (options.alternates?.length) injectAlternates(ms, html, options.alternates);
2532
2537
  if (options.redirect) injectRedirect(ms, html, options.redirect);
2538
+ if (options.inlineCatalog && options.locale !== sourceLocale) {
2539
+ const slice = pageSlice(v, options.locale, used);
2540
+ if (Object.keys(slice).length > 0) injectCatalog(ms, html, slice);
2541
+ }
2533
2542
  return {
2534
2543
  html: ms.toString(),
2535
- missing: [...missing]
2544
+ missing: [...missing],
2545
+ used: [...used]
2536
2546
  };
2537
2547
  }
2548
+ function pageSlice(v, locale, used) {
2549
+ const slice = {};
2550
+ for (const key of used) {
2551
+ const found = v.inspect(key);
2552
+ slice[key] = found?.from === locale ? found.source : "";
2553
+ }
2554
+ return slice;
2555
+ }
2538
2556
  function publicPath(rel) {
2539
2557
  const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
2540
2558
  return path.length > 1 ? path.replace(/\/+$/, "") : path;
@@ -2602,6 +2620,22 @@ function injectAlternates(ms, html, alternates) {
2602
2620
  const head = /<\/head\s*>/i.exec(html);
2603
2621
  if (head) ms.appendLeft(head.index, block);
2604
2622
  }
2623
+ function injectCatalog(ms, html, messages) {
2624
+ const json = JSON.stringify(messages).replace(/<\//g, "<\\/");
2625
+ const tag = `<script ${CATALOG_SCRIPT} type="application/json">${json}<\/script>`;
2626
+ const block = `${CATALOG_OPEN}${tag}${CATALOG_CLOSE}`;
2627
+ const from = html.indexOf(CATALOG_OPEN);
2628
+ if (from !== -1) {
2629
+ const to = html.indexOf(CATALOG_CLOSE, from);
2630
+ if (to !== -1) {
2631
+ const end = to + 23;
2632
+ if (html.slice(from, end) !== block) ms.overwrite(from, end, block);
2633
+ return;
2634
+ }
2635
+ }
2636
+ const head = /<\/head\s*>/i.exec(html);
2637
+ if (head) ms.appendLeft(head.index, block);
2638
+ }
2605
2639
  const TITLE_TAG = /<title\b([^>]*)>/i;
2606
2640
  const REFRESH_META = /<meta\b[^>]*http-equiv\s*=\s*["']?refresh/i;
2607
2641
  const NOINDEX_META = /<meta\b[^>]*name\s*=\s*["']?robots["']?[^>]*content\s*=\s*["'][^"']*noindex/i;
@@ -2674,7 +2708,8 @@ async function renderSite(cfg, options = {}) {
2674
2708
  base,
2675
2709
  pages
2676
2710
  },
2677
- redirect: routed && locale === cfg.sourceLocale ? script : void 0
2711
+ redirect: routed && locale === cfg.sourceLocale ? script : void 0,
2712
+ inlineCatalog: cfg.render.inlineCatalog === true
2678
2713
  });
2679
2714
  for (const key of result.missing) {
2680
2715
  const list = missing[locale] ??= [];