@verbaly/compiler 0.50.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/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);
@@ -2232,7 +2250,7 @@ function pageSlice(v, locale, used) {
2232
2250
  const slice = {};
2233
2251
  for (const key of used) {
2234
2252
  const found = v.inspect(key);
2235
- if (found?.from === locale) slice[key] = found.source;
2253
+ slice[key] = found?.from === locale ? found.source : "";
2236
2254
  }
2237
2255
  return slice;
2238
2256
  }