@verbaly/compiler 0.50.0 → 0.52.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/index.d.ts CHANGED
@@ -196,7 +196,10 @@ interface RuntimeModuleOptions {
196
196
  }
197
197
  export declare function generateRuntimeModule(cfg: ResolvedConfig, options?: RuntimeModuleOptions): string;
198
198
  export declare function generateLocaleModule(catalog: Catalog): string;
199
- export declare function generateDts(catalog: Catalog): string;
199
+ interface DtsOptions {
200
+ inlineCatalog?: boolean;
201
+ }
202
+ export declare function generateDts(catalog: Catalog, options?: DtsOptions): string;
200
203
  export declare function writeDts(cfg: ResolvedConfig, catalog: Catalog, file?: string): void;
201
204
  //#endregion
202
205
  //#region src/transform.d.ts
@@ -364,5 +367,5 @@ interface RenderSiteResult {
364
367
  export declare function renderSite(cfg: ResolvedConfig, options?: RenderSiteOptions): Promise<RenderSiteResult>;
365
368
  export declare function formatRenderWarnings(result: RenderSiteResult, sourceLocale: string): string[];
366
369
  //#endregion
367
- export type { BrokenEntry, BundleConfig, Catalog, Catalogs, CheckResult, DoctorEntry, DoctorResult, Drafts, GlossaryEntry, Host, InitOptions, InitResult, IssueSeverity, LocaleStatus, MissingEntry, PluginOptions, RedirectConfig, RenderConfig, RenderSiteOptions, RenderSiteResult, ResolvedConfig, RuntimeModuleOptions, StatusResult, SyncResult, TransformResult, TranslateConfig, TranslateFailure, TranslateOptions, TranslateProgress, TranslateProvider, TranslateRequest, TranslateResult, UnknownEntry, VerbalyConfig, WrapBlocked, WrapEntry, WrapOptions, WrapResult, WrapSkip };
370
+ export type { BrokenEntry, BundleConfig, Catalog, Catalogs, CheckResult, DoctorEntry, DoctorResult, Drafts, DtsOptions, GlossaryEntry, Host, InitOptions, InitResult, IssueSeverity, LocaleStatus, MissingEntry, PluginOptions, RedirectConfig, RenderConfig, RenderSiteOptions, RenderSiteResult, ResolvedConfig, RuntimeModuleOptions, StatusResult, SyncResult, TransformResult, TranslateConfig, TranslateFailure, TranslateOptions, TranslateProgress, TranslateProvider, TranslateRequest, TranslateResult, UnknownEntry, VerbalyConfig, WrapBlocked, WrapEntry, WrapOptions, WrapResult, WrapSkip };
368
371
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -943,16 +943,34 @@ function renderParamType(types) {
943
943
  ]) members.add(m);
944
944
  return [...members].join(" | ");
945
945
  }
946
+ const SLICE_HELPERS = `function readSlice(root, path) {
947
+ const locale = readPath({ supported: locales, path });
948
+ if (!locale || locale === sourceLocale) return undefined;
949
+ const messages = inlineMessages(root);
950
+ return messages ? { locale, messages } : undefined;
951
+ }
952
+
953
+ export function adoptPage(options) {
954
+ const slice = readSlice(options?.root, options?.path);
955
+ if (slice) v.addMessages(slice.locale, slice.messages, { partial: true });
956
+ return slice ? slice.locale : undefined;
957
+ }
958
+
959
+ `;
946
960
  function generateRuntimeModule(cfg, options = {}) {
947
961
  const importPath = options.localeImport ?? ((locale) => `virtual:verbaly/locale/${locale}`);
948
962
  const others = cfg.locales.filter((locale) => locale !== cfg.sourceLocale);
949
963
  const src = JSON.stringify(cfg.sourceLocale);
950
964
  const loaders = others.map((locale) => ` ${JSON.stringify(locale)}: () => import('${importPath(locale)}'),`).join("\n");
965
+ const icu = options.icu ? ",\n parseIcu" : "";
966
+ const rel = options.relative ? ",\n relativeFormatter" : "";
967
+ const slice = options.inlineCatalog ? ",\n inlineMessages" : "";
968
+ const slices = options.inlineCatalog ? SLICE_HELPERS : "";
951
969
  return `import {
952
970
  createVerbaly,
953
971
  localeFromPath as readPath,
954
972
  localePath as writePath,
955
- switchLocale as runSwitch${options.icu ? ",\n parseIcu" : ""}${options.relative ? ",\n relativeFormatter" : ""}${options.inlineCatalog ? ",\n inlineMessages" : ""},
973
+ switchLocale as runSwitch${icu}${rel}${slice},
956
974
  } from 'verbaly';
957
975
  import source from '${importPath(cfg.sourceLocale)}';
958
976
 
@@ -990,8 +1008,8 @@ export async function loadMessages(locale) {
990
1008
  return loader ? (await loader()).default : {};
991
1009
  }
992
1010
 
993
- ${options.inlineCatalog ? "// the slice `verbaly render` inlined here: enough for what this page shows, so nothing is fetched\nfunction pageSlice() {\n const locale = readPath({ supported: locales });\n if (!locale || locale === sourceLocale) return undefined;\n const messages = inlineMessages();\n return messages ? { locale, messages } : undefined;\n}\n\n" : ""}// per-request/per-instance factory (SSR): the singleton below is browser/SPA-only
994
- export function createInstance(options) {${options.inlineCatalog ? "\n const page = pageSlice();" : ""}
1011
+ ${slices}// per-request/per-instance factory (SSR): the singleton below is browser/SPA-only
1012
+ export function createInstance(options) {${options.inlineCatalog ? "\n const page = readSlice();" : ""}
995
1013
  return createVerbaly({
996
1014
  locale: ${options.inlineCatalog ? `page ? page.locale : ${src}` : src},
997
1015
  fallback: ${src},
@@ -1030,7 +1048,7 @@ ${options.extraExports ?? ""}`;
1030
1048
  function generateLocaleModule(catalog) {
1031
1049
  return `export default ${JSON.stringify(catalog)};\n`;
1032
1050
  }
1033
- function generateDts(catalog) {
1051
+ function generateDts(catalog, options = {}) {
1034
1052
  const lines = [];
1035
1053
  for (const [key, message] of Object.entries(catalog).sort(([a], [b]) => a.localeCompare(b))) {
1036
1054
  const params = collectParams(message);
@@ -1063,7 +1081,7 @@ ${lines.join("\n")}
1063
1081
  path?: string;
1064
1082
  base?: string;
1065
1083
  }): string | undefined;
1066
- export function loadMessages(locale: string): Promise<Record<string, string>>;
1084
+ 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;" : ""}
1067
1085
  export function createInstance(
1068
1086
  options?: import('verbaly').VerbalyOptions<VerbalyKey>,
1069
1087
  ): import('verbaly').Verbaly<VerbalyKey>;
@@ -1102,7 +1120,7 @@ declare module 'virtual:verbaly/locale/*' {
1102
1120
  }
1103
1121
  function writeDts(cfg, catalog, file) {
1104
1122
  file ??= join(cfg.root, "verbaly.d.ts");
1105
- const content = generateDts(catalog);
1123
+ const content = generateDts(catalog, { inlineCatalog: cfg.render.inlineCatalog === true });
1106
1124
  try {
1107
1125
  if (readFileSync(file, "utf8") === content) return;
1108
1126
  } catch {}
@@ -1775,7 +1793,7 @@ async function doctor(cfg) {
1775
1793
  if (source && scanning && cfg.dts !== false) {
1776
1794
  const dtsPath = cfg.dts ?? join(cfg.root, "verbaly.d.ts");
1777
1795
  if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
1778
- else if (readFileSync(dtsPath, "utf8") !== generateDts(source)) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
1796
+ else if (readFileSync(dtsPath, "utf8") !== generateDts(source, { inlineCatalog: cfg.render.inlineCatalog === true })) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
1779
1797
  else ok("types", "verbaly.d.ts is up to date");
1780
1798
  }
1781
1799
  const registry = await extractProject(cfg);
@@ -2170,6 +2188,10 @@ function renderHtml(html, options) {
2170
2188
  if (tagName === "a" || tagName === "area") {
2171
2189
  const inside = mirroredHref(attrs.get("href"), mirror);
2172
2190
  if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", inside);
2191
+ } else if (tagName === "form" || tagName === "button" || tagName === "input") {
2192
+ const name = tagName === "form" ? "action" : "formaction";
2193
+ const inside = mirroredHref(attrs.get(name), mirror);
2194
+ if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, inside);
2173
2195
  } else if (tagName === "meta" && attrs.get("http-equiv")?.toLowerCase() === "refresh") {
2174
2196
  const content = attrs.get("content") ?? "";
2175
2197
  const url = /^([^;]*;\s*url\s*=\s*)(.*)$/i.exec(decodeEntities(content));
@@ -2189,11 +2211,17 @@ function renderHtml(html, options) {
2189
2211
  if (close) {
2190
2212
  const text = t(key, args);
2191
2213
  const own = parseArgs(attrs.get(linksAttr));
2192
- const links = own ? globalLinks ? {
2214
+ const merged = own ? globalLinks ? {
2193
2215
  ...globalLinks,
2194
2216
  ...own
2195
2217
  } : own : globalLinks;
2196
- const content = attrs.has(richAttr) ? richToHtml(parseTags(text), richTags, links) : escapeHtml(text);
2218
+ const links = mirror ? mirroredLinks(merged, mirror) : merged;
2219
+ const nodes = attrs.has(richAttr) ? parseTags(text) : [];
2220
+ if (mirror && links) {
2221
+ const carried = usedLinks(nodes, links);
2222
+ if (carried && JSON.stringify(carried) !== JSON.stringify(own)) setAttribute(ms, html, chunkStart, openEnd, attrChunk, linksAttr, JSON.stringify(carried));
2223
+ }
2224
+ const content = attrs.has(richAttr) ? richToHtml(nodes, richTags, links) : escapeHtml(text);
2197
2225
  if (html.slice(openEnd, close.contentEnd) !== content) {
2198
2226
  if (openEnd === close.contentEnd) ms.appendLeft(openEnd, content);
2199
2227
  else ms.overwrite(openEnd, close.contentEnd, content);
@@ -2232,7 +2260,7 @@ function pageSlice(v, locale, used) {
2232
2260
  const slice = {};
2233
2261
  for (const key of used) {
2234
2262
  const found = v.inspect(key);
2235
- if (found?.from === locale) slice[key] = found.source;
2263
+ slice[key] = found?.from === locale ? found.source : "";
2236
2264
  }
2237
2265
  return slice;
2238
2266
  }
@@ -2240,6 +2268,31 @@ function publicPath(rel) {
2240
2268
  const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
2241
2269
  return path.length > 1 ? path.replace(/\/+$/, "") : path;
2242
2270
  }
2271
+ function usedLinks(nodes, links) {
2272
+ const out = {};
2273
+ const walk = (list) => {
2274
+ for (const node of list) {
2275
+ if (typeof node === "string") continue;
2276
+ if (links[node.name] !== void 0) out[node.name] = links[node.name];
2277
+ walk(node.children);
2278
+ }
2279
+ };
2280
+ walk(nodes);
2281
+ return Object.keys(out).length > 0 ? out : void 0;
2282
+ }
2283
+ function mirroredLinks(links, mirror) {
2284
+ if (!links) return links;
2285
+ const out = {};
2286
+ for (const [name, link] of Object.entries(links)) {
2287
+ const inside = mirroredHref(typeof link === "string" ? link : link.href, mirror);
2288
+ if (inside === void 0) out[name] = link;
2289
+ else out[name] = typeof link === "string" ? inside : {
2290
+ ...link,
2291
+ href: inside
2292
+ };
2293
+ }
2294
+ return out;
2295
+ }
2243
2296
  function mirroredHref(raw, mirror) {
2244
2297
  if (raw === void 0) return void 0;
2245
2298
  const href = decodeEntities(raw);