@verbaly/compiler 0.38.0 → 0.40.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.
@@ -15,6 +15,7 @@ function resolveConfig(config = {}) {
15
15
  root,
16
16
  dir,
17
17
  sourceLocale,
18
+ routing: config.routing ?? "prefix-except-source",
18
19
  locales: [...locales],
19
20
  include: config.include ?? ["{src,app}/**/*.{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}"],
20
21
  exclude: config.exclude ?? ["**/node_modules/**", "**/dist/**"],
@@ -23,6 +24,16 @@ function resolveConfig(config = {}) {
23
24
  render: config.render ?? {}
24
25
  };
25
26
  }
27
+ const CONFIG_FILES = [
28
+ "verbaly.config.js",
29
+ "verbaly.config.mjs",
30
+ "verbaly.config.ts",
31
+ "verbaly.config.mts",
32
+ "verbaly.config.json"
33
+ ];
34
+ function findConfigFile(root) {
35
+ return CONFIG_FILES.find((name) => existsSync(join(root, name)));
36
+ }
26
37
  async function loadConfigFile(root) {
27
38
  for (const name of ["verbaly.config.js", "verbaly.config.mjs"]) {
28
39
  const path = join(root, name);
@@ -63,6 +74,6 @@ function definedOnly(config) {
63
74
  return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== void 0));
64
75
  }
65
76
  //#endregion
66
- export { targetLocales as i, loadConfig as n, resolveConfig as r, isModuleNotFound as t };
77
+ export { targetLocales as a, resolveConfig as i, isModuleNotFound as n, loadConfig as r, findConfigFile as t };
67
78
 
68
- //# sourceMappingURL=config-D8gkaunO.js.map
79
+ //# sourceMappingURL=config-B6MP3zwt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-B6MP3zwt.js","names":[],"sources":["../src/pseudo.ts","../src/config.ts"],"sourcesContent":["import type { Catalog, Catalogs } from './catalog';\nimport type { ResolvedConfig } from './config';\n\nexport const PSEUDO_LOCALE = 'en-XA';\n\nconst ACCENTS: Record<string, string> = {\n a: 'á',\n b: 'ƀ',\n c: 'ç',\n d: 'đ',\n e: 'é',\n f: 'ƒ',\n g: 'ğ',\n h: 'ĥ',\n i: 'í',\n j: 'ĵ',\n k: 'ķ',\n l: 'ĺ',\n m: 'ɱ',\n n: 'ñ',\n o: 'ó',\n p: 'þ',\n q: 'ǫ',\n r: 'ŕ',\n s: 'š',\n t: 'ţ',\n u: 'ú',\n v: 'ṽ',\n w: 'ŵ',\n x: 'ẋ',\n y: 'ý',\n z: 'ž',\n A: 'Á',\n B: 'Ɓ',\n C: 'Ç',\n D: 'Đ',\n E: 'É',\n F: 'Ƒ',\n G: 'Ğ',\n H: 'Ĥ',\n I: 'Í',\n J: 'Ĵ',\n K: 'Ķ',\n L: 'Ĺ',\n M: 'Ḿ',\n N: 'Ñ',\n O: 'Ó',\n P: 'Þ',\n Q: 'Ǫ',\n R: 'Ŕ',\n S: 'Š',\n T: 'Ţ',\n U: 'Ú',\n V: 'Ṽ',\n W: 'Ŵ',\n X: 'Ẍ',\n Y: 'Ý',\n Z: 'Ž',\n};\n\nconst TAG_AT = /<\\/?[a-zA-Z][\\w-]*\\/?>/y;\n\n// params, variant blocks, tags and escapes survive verbatim\nexport function pseudoLocalize(message: string): string {\n let out = '';\n let letters = 0;\n let i = 0;\n while (i < message.length) {\n const two = message.slice(i, i + 2);\n if (two === '{{' || two === '}}' || two === '||' || two === '##') {\n out += two;\n i += 2;\n continue;\n }\n const ch = message[i]!;\n if (ch === '{') {\n const end = matchBrace(message, i);\n out += message.slice(i, end);\n i = end;\n continue;\n }\n if (ch === '<') {\n TAG_AT.lastIndex = i;\n const m = TAG_AT.exec(message);\n if (m) {\n out += m[0];\n i += m[0].length;\n continue;\n }\n }\n const mapped = ACCENTS[ch];\n if (mapped) {\n out += mapped;\n letters += 1;\n } else {\n out += ch;\n }\n i += 1;\n }\n const pad = '~'.repeat(Math.ceil(letters / 3));\n return `⟦${out}${pad ? ' ' + pad : ''}⟧`;\n}\n\nfunction matchBrace(message: string, start: number): number {\n let depth = 0;\n for (let i = start; i < message.length; i++) {\n const two = message.slice(i, i + 2);\n if (two === '{{' || two === '}}') {\n i += 1;\n continue;\n }\n const ch = message[i];\n if (ch === '{') depth += 1;\n else if (ch === '}') {\n depth -= 1;\n if (depth === 0) return i + 1;\n }\n }\n return message.length; // unbalanced → verbatim\n}\n\n// regenerates the whole pseudo catalog from the source catalog\nexport function pseudoCatalogs(\n cfg: ResolvedConfig,\n catalogs: Catalogs,\n locale: string = PSEUDO_LOCALE,\n): string[] {\n const source = catalogs[cfg.sourceLocale] ?? {};\n const target: Catalog = {};\n for (const [key, msg] of Object.entries(source)) {\n target[key] = msg ? pseudoLocalize(msg) : '';\n }\n catalogs[locale] = target;\n return Object.keys(target).filter((key) => target[key]);\n}\n","import { existsSync, readFileSync, readdirSync } from 'node:fs';\nimport { join, resolve } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport type { RichLink, Routing } from 'verbaly';\nimport { PSEUDO_LOCALE } from './pseudo';\nimport type { TranslateProvider } from './translate';\n\n// a term maps to how it must come out: one rendering for every locale, or one per locale\nexport type GlossaryEntry = string | Record<string, string>;\n\nexport interface TranslateConfig {\n provider?: 'claude' | TranslateProvider;\n model?: string;\n batchSize?: number;\n concurrency?: number;\n retries?: number;\n instructions?: string;\n glossary?: Record<string, GlossaryEntry>;\n}\n\nexport interface RedirectConfig {\n on?: 'root' | 'all';\n storageKey?: string | false;\n}\n\nexport interface RenderConfig {\n links?: Record<string, RichLink>;\n site?: string;\n attribute?: string;\n base?: string;\n baseUrl?: string;\n hreflang?: boolean;\n sitemap?: boolean | string;\n redirect?: boolean | RedirectConfig;\n clean?: boolean;\n}\n\nexport interface VerbalyConfig {\n root?: string;\n dir?: string;\n sourceLocale?: string;\n locales?: string[];\n routing?: Routing;\n include?: string[];\n exclude?: string[];\n dts?: string | false;\n translate?: TranslateConfig;\n render?: RenderConfig;\n}\n\nexport interface ResolvedConfig {\n root: string;\n dir: string;\n sourceLocale: string;\n locales: string[];\n routing: Routing;\n include: string[];\n exclude: string[];\n dts: string | false | undefined;\n translate: TranslateConfig;\n render: RenderConfig;\n}\n\nexport function resolveConfig(config: VerbalyConfig = {}): ResolvedConfig {\n const root = resolve(config.root ?? process.cwd());\n const dir = resolve(root, config.dir ?? 'locales');\n const sourceLocale = config.sourceLocale ?? 'en';\n\n const locales = new Set<string>([sourceLocale, ...(config.locales ?? [])]);\n if (existsSync(dir)) {\n for (const file of readdirSync(dir)) {\n if (file.endsWith('.json') && !file.startsWith('.') && file !== `${PSEUDO_LOCALE}.json`) {\n locales.add(file.slice(0, -5));\n }\n }\n }\n\n return {\n root,\n dir,\n sourceLocale,\n // what render has always written, so a project that never says it keeps the urls it had\n routing: config.routing ?? 'prefix-except-source',\n locales: [...locales],\n include: config.include ?? ['{src,app}/**/*.{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}'],\n exclude: config.exclude ?? ['**/node_modules/**', '**/dist/**'],\n dts: typeof config.dts === 'string' ? resolve(root, config.dts) : config.dts,\n translate: config.translate ?? {},\n render: config.render ?? {},\n };\n}\n\nconst CONFIG_FILES = [\n 'verbaly.config.js',\n 'verbaly.config.mjs',\n 'verbaly.config.ts',\n 'verbaly.config.mts',\n 'verbaly.config.json',\n];\n\nexport function findConfigFile(root: string): string | undefined {\n return CONFIG_FILES.find((name) => existsSync(join(root, name)));\n}\n\nexport async function loadConfigFile(root: string): Promise<VerbalyConfig> {\n for (const name of ['verbaly.config.js', 'verbaly.config.mjs']) {\n const path = join(root, name);\n if (existsSync(path)) {\n const mod = (await import(pathToFileURL(path).href)) as { default?: VerbalyConfig };\n return mod.default ?? {};\n }\n }\n for (const name of ['verbaly.config.ts', 'verbaly.config.mts']) {\n const path = join(root, name);\n if (existsSync(path)) return loadTsConfig(path);\n }\n const jsonPath = join(root, 'verbaly.config.json');\n if (existsSync(jsonPath)) {\n return JSON.parse(readFileSync(jsonPath, 'utf8')) as VerbalyConfig;\n }\n return {};\n}\n\nasync function loadTsConfig(path: string): Promise<VerbalyConfig> {\n try {\n const { bundleRequire } = await import('bundle-require');\n const { mod } = await bundleRequire<{ default?: VerbalyConfig }>({ filepath: path });\n return mod.default ?? {};\n } catch (error) {\n if (isModuleNotFound(error, 'esbuild')) {\n throw new Error(`[verbaly] ${path} needs esbuild: install it with pnpm add -D esbuild`, {\n cause: error,\n });\n }\n throw error;\n }\n}\n\nexport function isModuleNotFound(error: unknown, name: string): boolean {\n return (\n error instanceof Error &&\n (error as { code?: string }).code === 'ERR_MODULE_NOT_FOUND' &&\n error.message.includes(name)\n );\n}\n\n// non-source locales a command may write to (optional CLI override, always config-bounded)\nexport function targetLocales(cfg: ResolvedConfig, override?: string[]): string[] {\n return (override ?? cfg.locales).filter(\n (locale) => locale !== cfg.sourceLocale && cfg.locales.includes(locale),\n );\n}\n\n// file config + inline overrides\nexport async function loadConfig(\n root: string,\n overrides: VerbalyConfig = {},\n): Promise<ResolvedConfig> {\n const fileConfig = await loadConfigFile(root);\n return resolveConfig({ ...fileConfig, ...definedOnly(overrides), root });\n}\n\nfunction definedOnly(config: VerbalyConfig): VerbalyConfig {\n return Object.fromEntries(\n Object.entries(config).filter(([, value]) => value !== undefined),\n ) as VerbalyConfig;\n}\n"],"mappings":";;;;;AC+DA,SAAgB,cAAc,SAAwB,CAAC,GAAmB;CACxE,MAAM,OAAO,QAAQ,OAAO,QAAQ,QAAQ,IAAI,CAAC;CACjD,MAAM,MAAM,QAAQ,MAAM,OAAO,OAAO,SAAS;CACjD,MAAM,eAAe,OAAO,gBAAgB;CAE5C,MAAM,0BAAU,IAAI,IAAY,CAAC,cAAc,GAAI,OAAO,WAAW,CAAC,CAAE,CAAC;CACzE,IAAI,WAAW,GAAG,GACX;OAAA,MAAM,QAAQ,YAAY,GAAG,GAChC,IAAI,KAAK,SAAS,OAAO,KAAK,CAAC,KAAK,WAAW,GAAG,KAAK,SAAS,cAC9D,QAAQ,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC;CAAA;CAKnC,OAAO;EACL;EACA;EACA;EAEA,SAAS,OAAO,WAAW;EAC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AiB,OAAgB,MAAuB;CACtE,OACE,iBAAiB,SAChB,MAA4B,SAAS,0BACtC,MAAM,QAAQ,SAAS,IAAI;AAE/B;AAGA,SAAgB,cAAc,KAAqB,UAA+B;CAChF,QAAQ,YAAY,IAAI,QAAA,CAAS,QAC9B,WAAW,WAAW,IAAI,gBAAgB,IAAI,QAAQ,SAAS,MAAM,CACxE;AACF;AAGA,eAAsB,WACpB,MACA,YAA2B,CAAC,GACH;CAEzB,OAAO,cAAc;EAAE,GAAG,MADD,eAAe,IAAI;EACN,GAAG,YAAY,SAAS;EAAG;CAAK,CAAC;AACzE;AAEA,SAAS,YAAY,QAAsC;CACzD,OAAO,OAAO,YACZ,OAAO,QAAQ,MAAM,CAAC,CAAC,QAAQ,GAAG,WAAW,UAAU,KAAA,CAAS,CAClE;AACF"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RichLink } from "verbaly";
1
+ import { RichLink, Routing } from "verbaly";
2
2
  import MagicString from "magic-string";
3
3
  //#region src/catalog.d.ts
4
4
  type Catalog = Record<string, string>;
@@ -12,27 +12,52 @@ interface TranslateRequest {
12
12
  targetLocale: string;
13
13
  messages: Record<string, string>;
14
14
  origins?: Record<string, string[]>;
15
+ instructions?: string;
16
+ glossary?: Record<string, string>;
15
17
  }
16
18
  type TranslateProvider = (request: TranslateRequest) => Promise<Record<string, string>>;
19
+ interface TranslateProgress {
20
+ locale: string;
21
+ batch: number;
22
+ batches: number;
23
+ keys: number;
24
+ error?: string;
25
+ }
17
26
  interface TranslateOptions {
18
27
  locales?: string[];
19
28
  batchSize?: number;
29
+ concurrency?: number;
30
+ retries?: number;
31
+ retryDelay?: number;
20
32
  dryRun?: boolean;
21
33
  origins?: Record<string, string[]>;
34
+ onProgress?: (progress: TranslateProgress) => void;
35
+ }
36
+ interface TranslateFailure {
37
+ locale: string;
38
+ keys: string[];
39
+ error: string;
22
40
  }
23
41
  interface TranslateResult {
24
42
  translated: Record<string, string[]>;
25
43
  invalid: Record<string, string[]>;
26
44
  pending: Record<string, string[]>;
45
+ failed: TranslateFailure[];
27
46
  }
28
47
  declare function translateCatalogs(cfg: ResolvedConfig, catalogs: Catalogs, provider: TranslateProvider, options?: TranslateOptions): Promise<TranslateResult>;
48
+ declare function formatTranslateFailures(failed: TranslateFailure[]): string[];
29
49
  declare function resolveProvider(cfg: ResolvedConfig, model?: string): Promise<TranslateProvider>;
30
50
  //#endregion
31
51
  //#region src/config.d.ts
52
+ type GlossaryEntry = string | Record<string, string>;
32
53
  interface TranslateConfig {
33
54
  provider?: 'claude' | TranslateProvider;
34
55
  model?: string;
35
56
  batchSize?: number;
57
+ concurrency?: number;
58
+ retries?: number;
59
+ instructions?: string;
60
+ glossary?: Record<string, GlossaryEntry>;
36
61
  }
37
62
  interface RedirectConfig {
38
63
  on?: 'root' | 'all';
@@ -54,6 +79,7 @@ interface VerbalyConfig {
54
79
  dir?: string;
55
80
  sourceLocale?: string;
56
81
  locales?: string[];
82
+ routing?: Routing;
57
83
  include?: string[];
58
84
  exclude?: string[];
59
85
  dts?: string | false;
@@ -65,6 +91,7 @@ interface ResolvedConfig {
65
91
  dir: string;
66
92
  sourceLocale: string;
67
93
  locales: string[];
94
+ routing: Routing;
68
95
  include: string[];
69
96
  exclude: string[];
70
97
  dts: string | false | undefined;
@@ -231,6 +258,45 @@ declare function formatStatusResult(result: StatusResult): string;
231
258
  //#region src/text.d.ts
232
259
  declare function counted(count: number, noun: string, many?: string): string;
233
260
  //#endregion
261
+ //#region src/doctor.d.ts
262
+ interface DoctorEntry {
263
+ level: 'ok' | 'warn' | 'error';
264
+ check: string;
265
+ message: string;
266
+ fix?: string;
267
+ }
268
+ interface DoctorResult {
269
+ ok: boolean;
270
+ entries: DoctorEntry[];
271
+ }
272
+ declare function formatDoctorEntry(entry: DoctorEntry): string;
273
+ declare function doctor(cfg: ResolvedConfig): Promise<DoctorResult>;
274
+ //#endregion
275
+ //#region src/wrap.d.ts
276
+ interface WrapEntry {
277
+ file: string;
278
+ line: number;
279
+ text: string;
280
+ kind: 'text' | 'attribute';
281
+ attribute?: string;
282
+ }
283
+ interface WrapSkip {
284
+ file: string;
285
+ line: number;
286
+ text: string;
287
+ reason: string;
288
+ }
289
+ interface WrapResult {
290
+ files: number;
291
+ changed: string[];
292
+ wrapped: WrapEntry[];
293
+ skipped: WrapSkip[];
294
+ }
295
+ interface WrapOptions {
296
+ write?: boolean;
297
+ }
298
+ declare function wrapProject(cfg: ResolvedConfig, options?: WrapOptions): Promise<WrapResult>;
299
+ //#endregion
234
300
  //#region src/render.d.ts
235
301
  interface RenderSiteOptions {
236
302
  site?: string;
@@ -249,8 +315,10 @@ interface RenderSiteResult {
249
315
  files: number;
250
316
  locales: string[];
251
317
  missing: Record<string, string[]>;
318
+ untranslatedHead: string[];
252
319
  }
253
320
  declare function renderSite(cfg: ResolvedConfig, options?: RenderSiteOptions): Promise<RenderSiteResult>;
321
+ declare function formatRenderWarnings(result: RenderSiteResult, sourceLocale: string): string[];
254
322
  //#endregion
255
- export { type BrokenEntry, type Catalog, type Catalogs, type CheckResult, type Drafts, type IssueSeverity, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type PluginOptions, RESOLVED_VIRTUAL_ID, type RedirectConfig, type RenderConfig, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type VerbalyConfig, check, collectOrigins, counted, createSourceFilter, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, writeCatalog, writeDts };
323
+ export { type BrokenEntry, type Catalog, type Catalogs, type CheckResult, type DoctorEntry, type DoctorResult, type Drafts, type GlossaryEntry, type IssueSeverity, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type PluginOptions, RESOLVED_VIRTUAL_ID, type RedirectConfig, type RenderConfig, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TransformResult, type TranslateConfig, type TranslateFailure, type TranslateOptions, type TranslateProgress, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type VerbalyConfig, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, check, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, wrapProject, writeCatalog, writeDts };
256
324
  //# sourceMappingURL=index.d.ts.map