@verbaly/compiler 0.28.0 → 0.29.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
@@ -63,6 +63,7 @@ interface TranslateResult {
63
63
  pending: Record<string, string[]>;
64
64
  }
65
65
  declare function translateCatalogs(cfg: ResolvedConfig, catalogs: Catalogs, provider: TranslateProvider, options?: TranslateOptions): Promise<TranslateResult>;
66
+ declare function resolveProvider(cfg: ResolvedConfig, model?: string): Promise<TranslateProvider>;
66
67
  declare function structureMatches(source: string, translated: string): boolean;
67
68
  //#endregion
68
69
  //#region src/config.d.ts
@@ -247,6 +248,7 @@ declare function parseExchangeFile(file: string, localeOverride?: string): Parse
247
248
  //#endregion
248
249
  //#region src/extract.d.ts
249
250
  declare function extractProject(cfg: ResolvedConfig): Promise<MessageRegistry>;
251
+ declare function collectOrigins(cfg: ResolvedConfig): Promise<Record<string, string[]>>;
250
252
  interface SyncResult {
251
253
  added: Record<string, string[]>;
252
254
  }
@@ -381,5 +383,5 @@ interface WrapCodeResult {
381
383
  }
382
384
  declare function wrapCode(code: string, file: string): WrapCodeResult;
383
385
  //#endregion
384
- export { type Alternate, type Analysis, type AnalyzeOptions, type Catalog, type Catalogs, type CheckResult, type ClaudeProviderOptions, DRAFTS_FILE, type DoctorEntry, type DoctorResult, type Drafts, type ExchangeFormat, type ExportFormat, type ExportOptions, type ExportResult, type ExportedFile, type ImportOptions, type ImportResult, type InitOptions, type InitResult, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type MobileFormat, PSEUDO_LOCALE, type ParamType, type PluginOptions, RESOLVED_VIRTUAL_ID, type RenderConfig, type RenderHtmlOptions, type RenderHtmlResult, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SFC_FILE_RE, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TaggedMessage, type TaggedParam, type TransComponent, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type UsedKey, VIRTUAL_ID, type VerbalyConfig, type WatchProjectOptions, type WrapCodeResult, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
386
+ export { type Alternate, type Analysis, type AnalyzeOptions, type Catalog, type Catalogs, type CheckResult, type ClaudeProviderOptions, DRAFTS_FILE, type DoctorEntry, type DoctorResult, type Drafts, type ExchangeFormat, type ExportFormat, type ExportOptions, type ExportResult, type ExportedFile, type ImportOptions, type ImportResult, type InitOptions, type InitResult, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type MobileFormat, PSEUDO_LOCALE, type ParamType, type PluginOptions, RESOLVED_VIRTUAL_ID, type RenderConfig, type RenderHtmlOptions, type RenderHtmlResult, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SFC_FILE_RE, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TaggedMessage, type TaggedParam, type TransComponent, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type UsedKey, VIRTUAL_ID, type VerbalyConfig, type WatchProjectOptions, type WrapCodeResult, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
385
387
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -7,6 +7,18 @@ import { pathToFileURL } from "node:url";
7
7
  import picomatch from "picomatch";
8
8
  import MagicString from "magic-string";
9
9
  import { glob } from "tinyglobby";
10
+ //#region \0rolldown/runtime.js
11
+ var __defProp = Object.defineProperty;
12
+ var __exportAll = (all, no_symbols) => {
13
+ let target = {};
14
+ for (var name in all) __defProp(target, name, {
15
+ get: all[name],
16
+ enumerable: true
17
+ });
18
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
19
+ return target;
20
+ };
21
+ //#endregion
10
22
  //#region src/key.ts
11
23
  function stableKey(message) {
12
24
  return createHash("sha256").update(message).digest("base64url").slice(0, 8);
@@ -284,6 +296,8 @@ const COMMENT_RE = /<!--[\s\S]*?-->/g;
284
296
  const FRONTMATTER_RE = /^(\uFEFF?\s*---\r?\n)([\s\S]*?)\r?\n---(?=\r?\n|$)/;
285
297
  const CANDIDATE_SVELTE = /(?<![\w$])\$?t(?=[`(]|\.id\()/g;
286
298
  const CANDIDATE_VUE = /(?<![\w$])t(?=[`(]|\.id\()/g;
299
+ const MUSTACHE_RE = /\{\{[\s\S]*?\}\}/g;
300
+ const DIRECTIVE_RE = /\s(?:v-[\w-]+(?::[^\s=>/]+)?|[:@][^\s=>/]+)\s*=\s*("[^"]*"|'[^']*')/g;
287
301
  function analyzeSfc(code, file) {
288
302
  const svelte = file.endsWith(".svelte");
289
303
  const options = svelte ? { tNames: ["t", "$t"] } : void 0;
@@ -298,10 +312,14 @@ function analyzeSfc(code, file) {
298
312
  }
299
313
  const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
300
314
  const candidates = svelte ? CANDIDATE_SVELTE : CANDIDATE_VUE;
315
+ const ranges = file.endsWith(".vue") ? vueExpressionRanges(markup) : braceRanges(markup);
316
+ let range = 0;
301
317
  let consumedTo = 0;
302
318
  for (const match of markup.matchAll(candidates)) {
303
319
  const start = match.index;
304
320
  if (start < consumedTo) continue;
321
+ while (range < ranges.length && ranges[range][1] <= start) range += 1;
322
+ if (range >= ranges.length || start < ranges[range][0]) continue;
305
323
  const end = expressionExtent(markup, start);
306
324
  if (end === void 0) continue;
307
325
  merge(analyzeSegment(code.slice(start, end), file, options), start, true);
@@ -339,6 +357,31 @@ function analyzeSegment(code, file, options) {
339
357
  function blank(source, re) {
340
358
  return source.replace(re, (m) => " ".repeat(m.length));
341
359
  }
360
+ function braceRanges(markup) {
361
+ const ranges = [];
362
+ let i = 0;
363
+ while (i < markup.length) {
364
+ if (markup[i] === "{") {
365
+ const end = balancedEnd(markup, i);
366
+ if (end !== void 0) {
367
+ ranges.push([i + 1, end - 1]);
368
+ i = end;
369
+ continue;
370
+ }
371
+ }
372
+ i += 1;
373
+ }
374
+ return ranges;
375
+ }
376
+ function vueExpressionRanges(markup) {
377
+ const ranges = [];
378
+ for (const match of markup.matchAll(MUSTACHE_RE)) ranges.push([match.index + 2, match.index + match[0].length - 2]);
379
+ for (const match of markup.matchAll(DIRECTIVE_RE)) {
380
+ const valueStart = match.index + match[0].length - match[1].length + 1;
381
+ ranges.push([valueStart, valueStart + match[1].length - 2]);
382
+ }
383
+ return ranges.sort((a, b) => a[0] - b[0]);
384
+ }
342
385
  function expressionExtent(code, start) {
343
386
  let i = start;
344
387
  if (code[i] === "$") i += 1;
@@ -1005,6 +1048,12 @@ async function extractProject(cfg) {
1005
1048
  for (const file of files) registry.update(file, analyzeFile(readFileSync(file, "utf8"), file));
1006
1049
  return registry;
1007
1050
  }
1051
+ async function collectOrigins(cfg) {
1052
+ const registry = await extractProject(cfg);
1053
+ const origins = {};
1054
+ for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
1055
+ return origins;
1056
+ }
1008
1057
  function syncCatalogs(cfg, catalogs, registry) {
1009
1058
  const added = {};
1010
1059
  const source = catalogs[cfg.sourceLocale] ??= {};
@@ -1656,6 +1705,12 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
1656
1705
  }
1657
1706
  return result;
1658
1707
  }
1708
+ async function resolveProvider(cfg, model) {
1709
+ const configured = cfg.translate.provider;
1710
+ if (typeof configured === "function") return configured;
1711
+ const { claudeProvider } = await Promise.resolve().then(() => claude_exports);
1712
+ return claudeProvider({ model: model ?? cfg.translate.model });
1713
+ }
1659
1714
  function structureMatches(source, translated) {
1660
1715
  return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
1661
1716
  }
@@ -1890,6 +1945,12 @@ function csvRows(content) {
1890
1945
  }
1891
1946
  //#endregion
1892
1947
  //#region src/providers/claude.ts
1948
+ var claude_exports = /* @__PURE__ */ __exportAll({
1949
+ batchFormat: () => batchFormat,
1950
+ buildPrompt: () => buildPrompt,
1951
+ claudeProvider: () => claudeProvider,
1952
+ loadSdk: () => loadSdk
1953
+ });
1893
1954
  const DEFAULT_MODEL = "claude-sonnet-5";
1894
1955
  const SYSTEM = `You translate UI strings for the Verbaly i18n library.
1895
1956
  Rules:
@@ -2500,6 +2561,6 @@ function line(node) {
2500
2561
  return node.loc?.start?.line ?? 1;
2501
2562
  }
2502
2563
  //#endregion
2503
- export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, PSEUDO_LOCALE, RESOLVED_VIRTUAL_ID, SFC_FILE_RE, SOURCE_FILE_RE, VIRTUAL_ID, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
2564
+ export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, PSEUDO_LOCALE, RESOLVED_VIRTUAL_ID, SFC_FILE_RE, SOURCE_FILE_RE, VIRTUAL_ID, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
2504
2565
 
2505
2566
  //# sourceMappingURL=index.js.map