@verbaly/compiler 0.79.0 → 0.81.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/README.md +3 -1
- package/dist/{claude-BtdaNZEw.js → claude-XvHG5Neu.js} +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +1 -1
- package/dist/{run-DobJrwby.js → run-DitFmP5s.js} +317 -97
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -231,7 +231,7 @@ import { switchLocale } from 'verbaly/app';
|
|
|
231
231
|
await switchLocale('es');
|
|
232
232
|
```
|
|
233
233
|
|
|
234
|
-
Under a prefix mode that goes to `/es/…`, which is already rendered in Spanish, so there is no catalog to fetch and no flash. Under `no-prefix` it swaps the text where it stands and the address never changes. Either way it remembers the choice, in the cookie a server reads **and** in the storage the pre-paint redirect reads, and it sets `<html lang>` and `<html dir>`. Pass your framework's router so the app survives the switch:
|
|
234
|
+
Under a prefix mode that goes to `/es/…`, which is already rendered in Spanish, so there is no catalog to fetch and no flash. Under `no-prefix` it swaps the text where it stands and the address never changes. Either way it remembers the choice, in the cookie a server reads **and** in the storage the pre-paint redirect reads, and it sets `<html lang>` and `<html dir>`. A single-page app has neither a server nor a redirect, and starts in its source language: read the choice back before the first render with `setLocale(resolveLocale({ supported: locales, path: false })).then(render)`. Pass your framework's router so the app survives the switch:
|
|
235
235
|
|
|
236
236
|
```ts
|
|
237
237
|
await switchLocale('es', { navigate: (path) => router.push(path) });
|
|
@@ -391,6 +391,8 @@ runBuildGate(cfg, registry);
|
|
|
391
391
|
|
|
392
392
|
Your plugin's options go second and `{ plugin: true }` third: when one the command line also reads (`sourceLocale`, `locales`, `dir`, `include`, `exclude`) differs from `verbaly.config`, the build says once to move it there, since `verbaly check` never reads a bundler config.
|
|
393
393
|
|
|
394
|
+
`transformSource` also loads `verbaly/app` in a file that imports `@verbaly/react`, `@verbaly/vue` or `@verbaly/svelte`, on that import's own line, since their hooks read its instance when no provider is above them. `transformCode` alone, which Next's loader calls, does not.
|
|
395
|
+
|
|
394
396
|
A build that scans first, as the Vite plugin does to sync the catalogs, keeps that scan with `extractProject(cfg, { keepSource: true })` and passes it as the fourth argument of `transformSource`: a file it read unchanged is not parsed a second time. Call `reportOutsideInclude(cfg)` once when your build starts: it names every file outside `include` that writes a `` t`…` ``, which no build translates. It reads the project itself, so a cached transform never hides one. `writeDts(cfg, catalog)` writes the types where the config resolves them and returns the file and whether it changed. A build calls `writeBuildTypes(cfg, catalogs)` once it has synced the catalogs: it writes the same file, and a checkout it cannot write gets a warning instead of a failed build.
|
|
395
397
|
|
|
396
398
|
A dev server that writes the catalogs goes through `syncProject`, the same path `extract`, the MCP server and Studio take: it scans (or takes the registry you hold), syncs, writes the catalogs and the types, and keeps `verbaly.lock` in step, in that order; its `findings` say once when that file first appears. Pass `confirm: true` when your registry is built file by file, so a file that changed on disk unseen is read again before its text is written back. `createDevReporter` then says each of its findings once, says it again if it goes away and comes back, and keeps a long list to a few lines.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _t as isModuleNotFound, ut as counted } from "./run-
|
|
1
|
+
import { _t as isModuleNotFound, ut as counted } from "./run-DitFmP5s.js";
|
|
2
2
|
// src/exchange/providers/claude.ts
|
|
3
3
|
const DEFAULT_MODEL = "claude-sonnet-5-5";
|
|
4
4
|
const FALLBACK_MODELS = /* @__PURE__ */ new Set([
|
package/dist/cli.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -56,6 +56,8 @@ interface Analysis {
|
|
|
56
56
|
strayImports: StrayImport[];
|
|
57
57
|
missed: MissedCall[];
|
|
58
58
|
parseError?: string;
|
|
59
|
+
adapterImport?: number;
|
|
60
|
+
appImported?: true;
|
|
59
61
|
}
|
|
60
62
|
interface Collision {
|
|
61
63
|
key: string;
|
|
@@ -367,7 +369,9 @@ interface TransformResult {
|
|
|
367
369
|
code: string;
|
|
368
370
|
map: ReturnType<MagicString['generateMap']>;
|
|
369
371
|
}
|
|
370
|
-
export declare function transformCode(code: string, file: string, analysis?: Analysis
|
|
372
|
+
export declare function transformCode(code: string, file: string, analysis?: Analysis, options?: {
|
|
373
|
+
appImport?: boolean;
|
|
374
|
+
}): TransformResult | null;
|
|
371
375
|
interface PluginOptions extends VerbalyConfig {
|
|
372
376
|
failOnMissing?: boolean;
|
|
373
377
|
liveExtraction?: boolean;
|
|
@@ -509,6 +513,7 @@ interface WrapBlocked {
|
|
|
509
513
|
file: string;
|
|
510
514
|
texts: number;
|
|
511
515
|
client: boolean;
|
|
516
|
+
binding: string;
|
|
512
517
|
}
|
|
513
518
|
interface WrapResult {
|
|
514
519
|
files: number;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ as validatePair, A as check, B as readState, C as isTransformTarget, D as transformSource, E as runBuildGate, F as clearDrafts, G as createDevReporter, H as saveState, I as effectiveDrafts, J as generateDts, K as formatCollision, L as loadState, M as formatCheckWarnings, N as syncProject, O as writeBuildTypes, P as STATE_FILE, Q as validateMessage, R as markDrafts, S as createSourceFilter, T as resolveVirtualId, U as updateState, V as recordTranslations, W as collisionEntries, X as generateRuntimeModule, Y as generateLocaleModule, Z as writeDts, _ as status, a as renderSite, at as stableKey, b as RESOLVED_VIRTUAL_ID, bt as targetLocales, c as formatTranslateFailures, ct as MessageRegistry, d as translateCatalogs, dt as loadCatalogs, et as collectOrigins, f as writeDrafts, ft as needsIcu, g as formatStatusResult, gt as writeCatalog, h as formatDoctorHealth, ht as readCatalog, i as formatRenderWarnings, it as syncCatalogs, j as formatCheckResult, k as transformCode, l as mergeTranslations, lt as clientCatalogs, m as formatDoctorEntry, mt as parseCatalog, nt as pruneCatalogs, o as init, ot as APP_ID, p as doctor, pt as needsRelative, q as formatFinding, r as wrapProject, rt as shippedCatalogs, s as watchTree, st as LOCALE_IMPORT, t as formatCliError, tt as extractProject, u as resolveProvider, ut as counted, v as reportOutsideInclude, vt as loadConfig, w as loadVirtualModule, x as SOURCE_FILE_RE, y as LOCALE_MODULE_PREFIX, yt as resolveConfig, z as outdatedTranslations } from "./run-
|
|
1
|
+
import { $ as validatePair, A as check, B as readState, C as isTransformTarget, D as transformSource, E as runBuildGate, F as clearDrafts, G as createDevReporter, H as saveState, I as effectiveDrafts, J as generateDts, K as formatCollision, L as loadState, M as formatCheckWarnings, N as syncProject, O as writeBuildTypes, P as STATE_FILE, Q as validateMessage, R as markDrafts, S as createSourceFilter, T as resolveVirtualId, U as updateState, V as recordTranslations, W as collisionEntries, X as generateRuntimeModule, Y as generateLocaleModule, Z as writeDts, _ as status, a as renderSite, at as stableKey, b as RESOLVED_VIRTUAL_ID, bt as targetLocales, c as formatTranslateFailures, ct as MessageRegistry, d as translateCatalogs, dt as loadCatalogs, et as collectOrigins, f as writeDrafts, ft as needsIcu, g as formatStatusResult, gt as writeCatalog, h as formatDoctorHealth, ht as readCatalog, i as formatRenderWarnings, it as syncCatalogs, j as formatCheckResult, k as transformCode, l as mergeTranslations, lt as clientCatalogs, m as formatDoctorEntry, mt as parseCatalog, nt as pruneCatalogs, o as init, ot as APP_ID, p as doctor, pt as needsRelative, q as formatFinding, r as wrapProject, rt as shippedCatalogs, s as watchTree, st as LOCALE_IMPORT, t as formatCliError, tt as extractProject, u as resolveProvider, ut as counted, v as reportOutsideInclude, vt as loadConfig, w as loadVirtualModule, x as SOURCE_FILE_RE, y as LOCALE_MODULE_PREFIX, yt as resolveConfig, z as outdatedTranslations } from "./run-DitFmP5s.js";
|
|
2
2
|
// src/catalog/drafts.ts
|
|
3
3
|
function loadDrafts(cfg) {
|
|
4
4
|
return loadState(cfg).drafts;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, watch, writeFileSync } from "node:fs";
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, watch, writeFileSync } from "node:fs";
|
|
3
3
|
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
5
|
import { CATALOG_SCRIPT, LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseIcu, parseTags, safeAttribute } from "verbaly";
|
|
@@ -55,6 +55,7 @@ const HOSTS = [
|
|
|
55
55
|
},
|
|
56
56
|
{
|
|
57
57
|
name: "vite",
|
|
58
|
+
bundler: true,
|
|
58
59
|
dep: "vite",
|
|
59
60
|
pkg: "@verbaly/vite",
|
|
60
61
|
install: ["verbaly", "@verbaly/vite"],
|
|
@@ -62,6 +63,7 @@ const HOSTS = [
|
|
|
62
63
|
},
|
|
63
64
|
{
|
|
64
65
|
name: "webpack",
|
|
66
|
+
bundler: true,
|
|
65
67
|
dep: "webpack",
|
|
66
68
|
pkg: "@verbaly/unplugin",
|
|
67
69
|
install: ["verbaly", "@verbaly/unplugin"],
|
|
@@ -69,6 +71,7 @@ const HOSTS = [
|
|
|
69
71
|
},
|
|
70
72
|
{
|
|
71
73
|
name: "rspack",
|
|
74
|
+
bundler: true,
|
|
72
75
|
dep: "@rspack/core",
|
|
73
76
|
pkg: "@verbaly/unplugin",
|
|
74
77
|
install: ["verbaly", "@verbaly/unplugin"],
|
|
@@ -76,6 +79,7 @@ const HOSTS = [
|
|
|
76
79
|
},
|
|
77
80
|
{
|
|
78
81
|
name: "rollup",
|
|
82
|
+
bundler: true,
|
|
79
83
|
dep: "rollup",
|
|
80
84
|
pkg: "@verbaly/unplugin",
|
|
81
85
|
install: ["verbaly", "@verbaly/unplugin"],
|
|
@@ -83,6 +87,7 @@ const HOSTS = [
|
|
|
83
87
|
},
|
|
84
88
|
{
|
|
85
89
|
name: "esbuild",
|
|
90
|
+
bundler: true,
|
|
86
91
|
dep: "esbuild",
|
|
87
92
|
pkg: "@verbaly/unplugin",
|
|
88
93
|
install: ["verbaly", "@verbaly/unplugin"],
|
|
@@ -103,9 +108,42 @@ function readDependencies(root) {
|
|
|
103
108
|
return {};
|
|
104
109
|
}
|
|
105
110
|
}
|
|
111
|
+
const ADAPTERS = [
|
|
112
|
+
["@verbaly/react", [
|
|
113
|
+
"react",
|
|
114
|
+
"preact",
|
|
115
|
+
"@verbaly/react"
|
|
116
|
+
]],
|
|
117
|
+
["@verbaly/vue", [
|
|
118
|
+
"vue",
|
|
119
|
+
"nuxt",
|
|
120
|
+
"@verbaly/vue"
|
|
121
|
+
]],
|
|
122
|
+
["@verbaly/svelte", [
|
|
123
|
+
"svelte",
|
|
124
|
+
"@sveltejs/kit",
|
|
125
|
+
"@verbaly/svelte"
|
|
126
|
+
]]
|
|
127
|
+
];
|
|
128
|
+
function adaptersIn(deps) {
|
|
129
|
+
return ADAPTERS.filter(([, libs]) => libs.some((lib) => deps[lib])).map(([adapter]) => adapter);
|
|
130
|
+
}
|
|
131
|
+
function componentAdapters(root) {
|
|
132
|
+
return adaptersIn(readDependencies(root));
|
|
133
|
+
}
|
|
106
134
|
function detectHost(root) {
|
|
107
135
|
const deps = readDependencies(root);
|
|
108
|
-
|
|
136
|
+
const host = HOSTS.find((entry) => deps[entry.dep]);
|
|
137
|
+
if (!host?.bundler) return host;
|
|
138
|
+
const install = [
|
|
139
|
+
...host.install.slice(0, 1),
|
|
140
|
+
...adaptersIn(deps),
|
|
141
|
+
...host.install.slice(1)
|
|
142
|
+
];
|
|
143
|
+
return {
|
|
144
|
+
...host,
|
|
145
|
+
install
|
|
146
|
+
};
|
|
109
147
|
}
|
|
110
148
|
const LOCKFILES = [
|
|
111
149
|
["pnpm-lock.yaml", "pnpm"],
|
|
@@ -207,7 +245,9 @@ async function loadConfigFile(root) {
|
|
|
207
245
|
const path = join(root, name);
|
|
208
246
|
if (existsSync(path)) {
|
|
209
247
|
const url = `${pathToFileURL(path).href}?mtime=${statSync(path).mtimeMs}`;
|
|
210
|
-
|
|
248
|
+
const cache = createRequire(import.meta.url).cache;
|
|
249
|
+
delete cache[realpathSync(path)];
|
|
250
|
+
delete cache[resolve(path)];
|
|
211
251
|
return (await import(url)).default ?? {};
|
|
212
252
|
}
|
|
213
253
|
}
|
|
@@ -259,6 +299,10 @@ const READ_BY_CLI = [
|
|
|
259
299
|
"include",
|
|
260
300
|
"exclude"
|
|
261
301
|
];
|
|
302
|
+
const pluginOnly = /* @__PURE__ */ new Map();
|
|
303
|
+
function pluginOnlyOptions(root) {
|
|
304
|
+
return pluginOnly.get(root) ?? "";
|
|
305
|
+
}
|
|
262
306
|
function warnPluginOnly(root, file, overrides) {
|
|
263
307
|
const cli = resolveConfig({
|
|
264
308
|
...file,
|
|
@@ -270,8 +314,12 @@ function warnPluginOnly(root, file, overrides) {
|
|
|
270
314
|
root
|
|
271
315
|
});
|
|
272
316
|
const keys = READ_BY_CLI.filter((key) => overrides[key] !== void 0 && JSON.stringify(cli[key]) !== JSON.stringify(build[key]));
|
|
273
|
-
if (keys.length === 0)
|
|
317
|
+
if (keys.length === 0) {
|
|
318
|
+
pluginOnly.delete(cli.root);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
274
321
|
const names = keys.length > 1 ? `${keys.slice(0, -1).join(", ")} and ${keys.at(-1)}` : keys[0];
|
|
322
|
+
pluginOnly.set(cli.root, names);
|
|
275
323
|
const [verb, pronoun] = keys.length > 1 ? ["are", "them"] : ["is", "it"];
|
|
276
324
|
warnOnce(`${names} ${verb} set in the plugin options, which \`verbaly check\` and \`verbaly extract\` never read: move ${pronoun} to verbaly.config so the command line checks what the build ships`, `plugin-only:${root}`);
|
|
277
325
|
}
|
|
@@ -287,8 +335,8 @@ function withLineEndings(text, existing) {
|
|
|
287
335
|
function emptyCatalog() {
|
|
288
336
|
return Object.create(null);
|
|
289
337
|
}
|
|
290
|
-
function own(
|
|
291
|
-
return Object.hasOwn(
|
|
338
|
+
function own(map, key) {
|
|
339
|
+
return Object.hasOwn(map, key) ? map[key] : void 0;
|
|
292
340
|
}
|
|
293
341
|
function flatView(tree) {
|
|
294
342
|
if (typeof tree !== "object" || tree === null || Object.getPrototypeOf(tree) !== null) return flatten(tree);
|
|
@@ -341,7 +389,7 @@ function isTree(value) {
|
|
|
341
389
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
342
390
|
}
|
|
343
391
|
function loadCatalogs(cfg) {
|
|
344
|
-
const catalogs =
|
|
392
|
+
const catalogs = emptyCatalog();
|
|
345
393
|
for (const locale of cfg.locales) catalogs[locale] = readCatalog(cfg, locale);
|
|
346
394
|
return catalogs;
|
|
347
395
|
}
|
|
@@ -445,6 +493,11 @@ function counted(count, noun, many = `${noun}s`) {
|
|
|
445
493
|
function truncate(text, max) {
|
|
446
494
|
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
447
495
|
}
|
|
496
|
+
function trimTrailingSlashes(text) {
|
|
497
|
+
let end = text.length;
|
|
498
|
+
while (end > 0 && text.charCodeAt(end - 1) === 47) end -= 1;
|
|
499
|
+
return text.slice(0, end);
|
|
500
|
+
}
|
|
448
501
|
function byCodeUnit(a, b) {
|
|
449
502
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
450
503
|
}
|
|
@@ -455,7 +508,7 @@ function isExcluded(key, prefixes) {
|
|
|
455
508
|
function clientCatalogs(cfg, catalogs) {
|
|
456
509
|
const prefixes = cfg.bundle.exclude ?? [];
|
|
457
510
|
if (prefixes.length === 0) return catalogs;
|
|
458
|
-
const out =
|
|
511
|
+
const out = emptyCatalog();
|
|
459
512
|
for (const [locale, catalog] of Object.entries(catalogs)) {
|
|
460
513
|
const kept = emptyCatalog();
|
|
461
514
|
for (const [key, message] of Object.entries(catalog)) if (!isExcluded(key, prefixes)) kept[key] = message;
|
|
@@ -604,11 +657,21 @@ function analyzeScript(code, file, options = {}) {
|
|
|
604
657
|
const strayImports = [];
|
|
605
658
|
const bindings = new Bindings(names);
|
|
606
659
|
const candidates = [];
|
|
660
|
+
let adapterImport;
|
|
661
|
+
let appImported = false;
|
|
662
|
+
const noteLoad = (node) => {
|
|
663
|
+
const source = node.source?.value;
|
|
664
|
+
if (typeof source !== "string" || !loadsAtRuntime(node)) return;
|
|
665
|
+
if (isAppModule(source)) appImported = true;
|
|
666
|
+
else if (ADAPTER_SOURCE.test(source)) adapterImport ??= node.start;
|
|
667
|
+
};
|
|
607
668
|
walk$1(ast.program, (node) => {
|
|
608
669
|
if (node.type === "ImportDeclaration") {
|
|
609
670
|
collectStrayImports(node, file, strayImports);
|
|
610
671
|
bindings.addImport(node);
|
|
611
|
-
|
|
672
|
+
noteLoad(node);
|
|
673
|
+
} else if (node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") noteLoad(node);
|
|
674
|
+
else if (node.type === "VariableDeclarator") bindings.addDeclarator(node);
|
|
612
675
|
else if (node.type === "TaggedTemplateExpression") {
|
|
613
676
|
const tag = node.tag;
|
|
614
677
|
const explicit = explicitId(tag, names);
|
|
@@ -682,16 +745,26 @@ function analyzeScript(code, file, options = {}) {
|
|
|
682
745
|
loose: true
|
|
683
746
|
});
|
|
684
747
|
}
|
|
748
|
+
const analysis = {
|
|
749
|
+
tagged,
|
|
750
|
+
usedKeys,
|
|
751
|
+
strayImports,
|
|
752
|
+
missed
|
|
753
|
+
};
|
|
754
|
+
if (adapterImport !== void 0) analysis.adapterImport = adapterImport;
|
|
755
|
+
if (appImported) analysis.appImported = true;
|
|
685
756
|
return {
|
|
686
|
-
analysis
|
|
687
|
-
tagged,
|
|
688
|
-
usedKeys,
|
|
689
|
-
strayImports,
|
|
690
|
-
missed
|
|
691
|
-
},
|
|
757
|
+
analysis,
|
|
692
758
|
renamed: [...renamed]
|
|
693
759
|
};
|
|
694
760
|
}
|
|
761
|
+
const ADAPTER_SOURCE = /^@verbaly\/(?:react|vue|svelte)(?:\/Trans\.svelte)?$/;
|
|
762
|
+
function loadsAtRuntime(node) {
|
|
763
|
+
if (node.importKind === "type" || node.exportKind === "type") return false;
|
|
764
|
+
const specifiers = node.specifiers ?? [];
|
|
765
|
+
if (specifiers.length === 0) return true;
|
|
766
|
+
return specifiers.some((spec) => spec.importKind !== "type" && spec.exportKind !== "type");
|
|
767
|
+
}
|
|
695
768
|
function lineOf(node) {
|
|
696
769
|
return node.loc?.start.line ?? 1;
|
|
697
770
|
}
|
|
@@ -1101,6 +1174,49 @@ function walk$1(node, visit) {
|
|
|
1101
1174
|
function isNode(value) {
|
|
1102
1175
|
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1103
1176
|
}
|
|
1177
|
+
// src/util/blocks.ts
|
|
1178
|
+
function blocks(source, shapes) {
|
|
1179
|
+
const found = [];
|
|
1180
|
+
const opens = shapes.map(() => void 0);
|
|
1181
|
+
let from = 0;
|
|
1182
|
+
for (;;) {
|
|
1183
|
+
let pick = -1;
|
|
1184
|
+
for (let i = 0; i < shapes.length; i += 1) {
|
|
1185
|
+
if (opens[i] === null) continue;
|
|
1186
|
+
if (opens[i] === void 0 || opens[i][0] < from) opens[i] = search(source, shapes[i].open, from);
|
|
1187
|
+
if (opens[i] && (pick < 0 || opens[i][0] < opens[pick][0])) pick = i;
|
|
1188
|
+
}
|
|
1189
|
+
if (pick < 0) return found;
|
|
1190
|
+
const block = closed(source, shapes[pick], opens[pick]);
|
|
1191
|
+
if (block) {
|
|
1192
|
+
found.push(block);
|
|
1193
|
+
from = block.end;
|
|
1194
|
+
} else opens[pick] = null;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
function closed(source, shape, [start, openEnd]) {
|
|
1198
|
+
const body = shape.openTag ? source.indexOf(">", openEnd) + 1 : openEnd;
|
|
1199
|
+
if (body === 0) return null;
|
|
1200
|
+
const close = search(source, shape.close, body);
|
|
1201
|
+
if (!close) return null;
|
|
1202
|
+
const end = shape.closeTag ? source.indexOf(">", close[1]) + 1 : close[1];
|
|
1203
|
+
if (end === 0) return null;
|
|
1204
|
+
return {
|
|
1205
|
+
start,
|
|
1206
|
+
body,
|
|
1207
|
+
close: close[0],
|
|
1208
|
+
end
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
function search(source, what, from) {
|
|
1212
|
+
if (typeof what === "string") {
|
|
1213
|
+
const at = source.indexOf(what, from);
|
|
1214
|
+
return at < 0 ? null : [at, at + what.length];
|
|
1215
|
+
}
|
|
1216
|
+
what.lastIndex = from;
|
|
1217
|
+
const found = what.exec(source);
|
|
1218
|
+
return found ? [found.index, what.lastIndex] : null;
|
|
1219
|
+
}
|
|
1104
1220
|
// src/scan/sfc.ts
|
|
1105
1221
|
const SFC_FILE_RE = /\.(?:svelte|vue|astro)$/;
|
|
1106
1222
|
function analyzeFile(code, file) {
|
|
@@ -1117,11 +1233,25 @@ function analyzeFile(code, file) {
|
|
|
1117
1233
|
};
|
|
1118
1234
|
}
|
|
1119
1235
|
}
|
|
1120
|
-
const
|
|
1121
|
-
|
|
1122
|
-
|
|
1236
|
+
const SCRIPT_BLOCK = {
|
|
1237
|
+
open: /<script\b/gi,
|
|
1238
|
+
openTag: true,
|
|
1239
|
+
close: /<\/script\s*>/gi
|
|
1240
|
+
};
|
|
1241
|
+
const STYLE_BLOCK = {
|
|
1242
|
+
open: /<style\b/gi,
|
|
1243
|
+
openTag: true,
|
|
1244
|
+
close: /<\/style\s*>/gi
|
|
1245
|
+
};
|
|
1246
|
+
const COMMENT_BLOCK = {
|
|
1247
|
+
open: "<!--",
|
|
1248
|
+
close: "-->"
|
|
1249
|
+
};
|
|
1123
1250
|
const FRONTMATTER_RE = /^(\uFEFF?\s*---\r?\n)([\s\S]*?)\r?\n---(?=\r?\n|$)/;
|
|
1124
|
-
const
|
|
1251
|
+
const MUSTACHE_BLOCK = {
|
|
1252
|
+
open: "{{",
|
|
1253
|
+
close: "}}"
|
|
1254
|
+
};
|
|
1125
1255
|
const DIRECTIVE_RE = /\s(?:v-[\w-]+(?::[^\s=>/]+)?|[:@][^\s=>/]+)\s*=\s*("[^"]*"|'[^']*')/g;
|
|
1126
1256
|
function analyzeSfc(code, file) {
|
|
1127
1257
|
const svelte = file.endsWith(".svelte");
|
|
@@ -1133,6 +1263,8 @@ function analyzeSfc(code, file) {
|
|
|
1133
1263
|
const renamed = /* @__PURE__ */ new Set();
|
|
1134
1264
|
const lineAt = lineCounter(code);
|
|
1135
1265
|
let parseError;
|
|
1266
|
+
let adapterImport;
|
|
1267
|
+
let appImported = false;
|
|
1136
1268
|
const block = (content, offset) => {
|
|
1137
1269
|
const segment = analyzeSegment(content, file, { tNames });
|
|
1138
1270
|
parseError ??= segment.error;
|
|
@@ -1141,11 +1273,8 @@ function analyzeSfc(code, file) {
|
|
|
1141
1273
|
};
|
|
1142
1274
|
const frontmatter = file.endsWith(".astro") ? FRONTMATTER_RE.exec(code) : null;
|
|
1143
1275
|
if (frontmatter?.[2]) block(frontmatter[2], frontmatter[1].length);
|
|
1144
|
-
for (const
|
|
1145
|
-
|
|
1146
|
-
if (content) block(content, match.index + match[1].length);
|
|
1147
|
-
}
|
|
1148
|
-
const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
|
|
1276
|
+
for (const script of blocks(code, [SCRIPT_BLOCK])) if (script.close > script.body) block(code.slice(script.body, script.close), script.body);
|
|
1277
|
+
const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_BLOCK), STYLE_BLOCK), COMMENT_BLOCK);
|
|
1149
1278
|
const markupOptions = {
|
|
1150
1279
|
tNames,
|
|
1151
1280
|
renamed: [...renamed]
|
|
@@ -1163,15 +1292,20 @@ function analyzeSfc(code, file) {
|
|
|
1163
1292
|
merge(analyzeSegment(code.slice(start, end), file, markupOptions).analysis, start, true);
|
|
1164
1293
|
consumedTo = end;
|
|
1165
1294
|
}
|
|
1166
|
-
|
|
1295
|
+
const analysis = {
|
|
1167
1296
|
tagged,
|
|
1168
1297
|
usedKeys,
|
|
1169
1298
|
strayImports,
|
|
1170
|
-
missed
|
|
1171
|
-
...parseError && { parseError }
|
|
1299
|
+
missed
|
|
1172
1300
|
};
|
|
1301
|
+
if (parseError) analysis.parseError = parseError;
|
|
1302
|
+
if (adapterImport !== void 0) analysis.adapterImport = adapterImport;
|
|
1303
|
+
if (appImported) analysis.appImported = true;
|
|
1304
|
+
return analysis;
|
|
1173
1305
|
function merge(analysis, offset, markupSegment) {
|
|
1174
1306
|
if (!analysis) return;
|
|
1307
|
+
if (analysis.adapterImport !== void 0) adapterImport ??= analysis.adapterImport + offset;
|
|
1308
|
+
if (analysis.appImported) appImported = true;
|
|
1175
1309
|
const lines = lineAt(offset) - 1;
|
|
1176
1310
|
for (const msg of analysis.tagged) tagged.push({
|
|
1177
1311
|
...msg,
|
|
@@ -1224,8 +1358,14 @@ function analyzeSegment(code, file, options) {
|
|
|
1224
1358
|
};
|
|
1225
1359
|
}
|
|
1226
1360
|
}
|
|
1227
|
-
function blank(source,
|
|
1228
|
-
|
|
1361
|
+
function blank(source, shape) {
|
|
1362
|
+
let out = "";
|
|
1363
|
+
let at = 0;
|
|
1364
|
+
for (const { start, end } of blocks(source, [shape])) {
|
|
1365
|
+
out += source.slice(at, start) + " ".repeat(end - start);
|
|
1366
|
+
at = end;
|
|
1367
|
+
}
|
|
1368
|
+
return out + source.slice(at);
|
|
1229
1369
|
}
|
|
1230
1370
|
function braceRanges(markup) {
|
|
1231
1371
|
const ranges = [];
|
|
@@ -1245,7 +1385,7 @@ function braceRanges(markup) {
|
|
|
1245
1385
|
}
|
|
1246
1386
|
function vueExpressionRanges(markup) {
|
|
1247
1387
|
const ranges = [];
|
|
1248
|
-
for (const
|
|
1388
|
+
for (const { body, close } of blocks(markup, [MUSTACHE_BLOCK])) ranges.push([body, close]);
|
|
1249
1389
|
for (const match of markup.matchAll(DIRECTIVE_RE)) {
|
|
1250
1390
|
const valueStart = match.index + match[0].length - match[1].length + 1;
|
|
1251
1391
|
ranges.push([valueStart, valueStart + match[1].length - 2]);
|
|
@@ -1327,9 +1467,9 @@ async function collectOrigins(cfg, reuse) {
|
|
|
1327
1467
|
return origins;
|
|
1328
1468
|
}
|
|
1329
1469
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
1330
|
-
const added =
|
|
1470
|
+
const added = emptyCatalog();
|
|
1331
1471
|
const replaced = [];
|
|
1332
|
-
const source = catalogs[cfg.sourceLocale]
|
|
1472
|
+
const source = own(catalogs, cfg.sourceLocale) ?? (catalogs[cfg.sourceLocale] = emptyCatalog());
|
|
1333
1473
|
for (const [key, msg] of registry.messages()) {
|
|
1334
1474
|
const before = own(source, key);
|
|
1335
1475
|
if (before === msg.message) continue;
|
|
@@ -1343,7 +1483,7 @@ function syncCatalogs(cfg, catalogs, registry) {
|
|
|
1343
1483
|
const needed = Object.keys(source);
|
|
1344
1484
|
for (const locale of cfg.locales) {
|
|
1345
1485
|
if (locale === cfg.sourceLocale) continue;
|
|
1346
|
-
const catalog = catalogs[locale]
|
|
1486
|
+
const catalog = own(catalogs, locale) ?? (catalogs[locale] = emptyCatalog());
|
|
1347
1487
|
for (const key of needed) if (own(catalog, key) === void 0) {
|
|
1348
1488
|
catalog[key] = "";
|
|
1349
1489
|
(added[locale] ??= []).push(key);
|
|
@@ -1357,17 +1497,14 @@ function syncCatalogs(cfg, catalogs, registry) {
|
|
|
1357
1497
|
function shippedCatalogs(cfg, catalogs, registry, options = {}) {
|
|
1358
1498
|
const source = flatten(catalogs[cfg.sourceLocale] ?? {});
|
|
1359
1499
|
for (const [key, msg] of registry.messages()) if (options.newKeys !== false || own(source, key) !== void 0) source[key] = msg.message;
|
|
1360
|
-
return {
|
|
1361
|
-
...catalogs,
|
|
1362
|
-
[cfg.sourceLocale]: source
|
|
1363
|
-
};
|
|
1500
|
+
return Object.assign(emptyCatalog(), catalogs, { [cfg.sourceLocale]: source });
|
|
1364
1501
|
}
|
|
1365
1502
|
function pruneCatalogs(cfg, catalogs, registry) {
|
|
1366
|
-
const removed =
|
|
1503
|
+
const removed = emptyCatalog();
|
|
1367
1504
|
if (registry.parseErrors().length > 0) return removed;
|
|
1368
1505
|
const keep = /* @__PURE__ */ new Set([...registry.messages().keys(), ...registry.usedKeys().keys()]);
|
|
1369
1506
|
for (const locale of cfg.locales) {
|
|
1370
|
-
const catalog = catalogs
|
|
1507
|
+
const catalog = own(catalogs, locale);
|
|
1371
1508
|
if (!catalog) continue;
|
|
1372
1509
|
for (const key of Object.keys(catalog)) if (!keep.has(key)) {
|
|
1373
1510
|
delete catalog[key];
|
|
@@ -1651,7 +1788,7 @@ function covered(list, entry) {
|
|
|
1651
1788
|
if (!Array.isArray(list)) return false;
|
|
1652
1789
|
return list.some((item) => {
|
|
1653
1790
|
if (typeof item !== "string") return false;
|
|
1654
|
-
const pattern = item.replace(/^\.\//, "")
|
|
1791
|
+
const pattern = trimTrailingSlashes(item.replace(/^\.\//, ""));
|
|
1655
1792
|
return picomatch(pattern)(entry) || picomatch(`${pattern}/**/*`)(entry);
|
|
1656
1793
|
});
|
|
1657
1794
|
}
|
|
@@ -1704,11 +1841,14 @@ export function adoptPage(options) {
|
|
|
1704
1841
|
}
|
|
1705
1842
|
|
|
1706
1843
|
`;
|
|
1844
|
+
function loaderKey(locale) {
|
|
1845
|
+
return locale === "__proto__" ? "[\"__proto__\"]" : JSON.stringify(locale);
|
|
1846
|
+
}
|
|
1707
1847
|
function generateRuntimeModule(cfg, options = {}) {
|
|
1708
1848
|
const importPath = options.localeImport ?? ((locale) => "@verbaly/app/locale/" + locale);
|
|
1709
1849
|
const others = cfg.locales.filter((locale) => locale !== cfg.sourceLocale);
|
|
1710
1850
|
const src = JSON.stringify(cfg.sourceLocale);
|
|
1711
|
-
const loaders = others.map((locale) => ` ${
|
|
1851
|
+
const loaders = others.map((locale) => ` ${loaderKey(locale)}: () => import('${importPath(locale)}'),`).join("\n");
|
|
1712
1852
|
const icu = options.icu ? ",\n parseIcu" : "";
|
|
1713
1853
|
const rel = options.relative ? ",\n relativeFormatter" : "";
|
|
1714
1854
|
const slice = options.inlineCatalog ? ",\n inlineMessages" : "";
|
|
@@ -1717,6 +1857,7 @@ function generateRuntimeModule(cfg, options = {}) {
|
|
|
1717
1857
|
createVerbaly,
|
|
1718
1858
|
localeFromPath as readPath,
|
|
1719
1859
|
localePath as writePath,
|
|
1860
|
+
persistLocale,
|
|
1720
1861
|
switchLocale as runSwitch${icu}${rel}${slice},
|
|
1721
1862
|
} from 'verbaly';
|
|
1722
1863
|
import source from '${importPath(cfg.sourceLocale)}';
|
|
@@ -1795,9 +1936,11 @@ export function subscribe(listener) {
|
|
|
1795
1936
|
return v.subscribe(listener);
|
|
1796
1937
|
}
|
|
1797
1938
|
|
|
1939
|
+
// <html lang> and dir follow; no choice is saved, since a startup read would freeze the browser's
|
|
1798
1940
|
export async function setLocale(locale) {
|
|
1799
1941
|
await v.loadLocale(locale);
|
|
1800
1942
|
v.setLocale(locale);
|
|
1943
|
+
persistLocale(locale, false);
|
|
1801
1944
|
}
|
|
1802
1945
|
${options.extraExports ?? ""}`;
|
|
1803
1946
|
}
|
|
@@ -2106,8 +2249,8 @@ function loadState(cfg) {
|
|
|
2106
2249
|
for (const side of lock) checkVersion(statePath(cfg), side.version);
|
|
2107
2250
|
const [legacyState = [], legacyDrafts = []] = legacyPaths(cfg).map((path) => readSides(path) ?? []);
|
|
2108
2251
|
const state = {
|
|
2109
|
-
drafts:
|
|
2110
|
-
fingerprints:
|
|
2252
|
+
drafts: emptyCatalog(),
|
|
2253
|
+
fingerprints: emptyCatalog()
|
|
2111
2254
|
};
|
|
2112
2255
|
const kept = lock.length > 1 ? keptStamp(cfg) : void 0;
|
|
2113
2256
|
[...lock, ...legacyState].forEach((source, index) => {
|
|
@@ -2164,12 +2307,13 @@ function readState(cfg) {
|
|
|
2164
2307
|
...marked
|
|
2165
2308
|
};
|
|
2166
2309
|
} catch (error) {
|
|
2310
|
+
const problem = error.message.replace(/^\[verbaly\] /, "");
|
|
2167
2311
|
return {
|
|
2168
2312
|
state: {
|
|
2169
|
-
drafts:
|
|
2170
|
-
fingerprints:
|
|
2313
|
+
drafts: emptyCatalog(),
|
|
2314
|
+
fingerprints: emptyCatalog()
|
|
2171
2315
|
},
|
|
2172
|
-
problem
|
|
2316
|
+
problem,
|
|
2173
2317
|
...marked
|
|
2174
2318
|
};
|
|
2175
2319
|
}
|
|
@@ -2207,7 +2351,7 @@ function refreshState(cfg, catalogs, previous) {
|
|
|
2207
2351
|
const flat = flatCatalogs(catalogs);
|
|
2208
2352
|
const source = flat[cfg.sourceLocale] ?? {};
|
|
2209
2353
|
const shown = new Set(targetLocales(cfg).filter((locale) => Object.hasOwn(flat, locale) && existsSync(catalogPath(cfg, locale))));
|
|
2210
|
-
const drafts =
|
|
2354
|
+
const drafts = emptyCatalog();
|
|
2211
2355
|
for (const [locale, keys] of Object.entries(previous.drafts)) {
|
|
2212
2356
|
if (!shown.has(locale)) {
|
|
2213
2357
|
drafts[locale] = keys;
|
|
@@ -2216,7 +2360,7 @@ function refreshState(cfg, catalogs, previous) {
|
|
|
2216
2360
|
const live = keys.filter((key) => own(flat[locale], key));
|
|
2217
2361
|
if (live.length) drafts[locale] = live;
|
|
2218
2362
|
}
|
|
2219
|
-
const fingerprints =
|
|
2363
|
+
const fingerprints = emptyCatalog();
|
|
2220
2364
|
for (const [locale, stamps] of Object.entries(previous.fingerprints)) if (!shown.has(locale)) fingerprints[locale] = stamps;
|
|
2221
2365
|
for (const locale of shown) {
|
|
2222
2366
|
const stamps = refreshStamps(source, flat[locale], previous.fingerprints[locale] ?? {});
|
|
@@ -2261,7 +2405,7 @@ function recordTranslations(cfg, source, writes) {
|
|
|
2261
2405
|
return state;
|
|
2262
2406
|
}
|
|
2263
2407
|
function effectiveDrafts(drafts, catalogs) {
|
|
2264
|
-
const out =
|
|
2408
|
+
const out = emptyCatalog();
|
|
2265
2409
|
for (const [locale, keys] of Object.entries(drafts)) {
|
|
2266
2410
|
const catalog = catalogs[locale] ?? {};
|
|
2267
2411
|
const live = keys.filter((key) => own(catalog, key));
|
|
@@ -2326,17 +2470,17 @@ function translationOf(stamped) {
|
|
|
2326
2470
|
return stamped.slice(stamped.indexOf(".") + 1);
|
|
2327
2471
|
}
|
|
2328
2472
|
function flatCatalogs(catalogs) {
|
|
2329
|
-
const out =
|
|
2473
|
+
const out = emptyCatalog();
|
|
2330
2474
|
for (const [locale, catalog] of Object.entries(catalogs)) out[locale] = flatten(catalog);
|
|
2331
2475
|
return out;
|
|
2332
2476
|
}
|
|
2333
2477
|
function serializeState(state) {
|
|
2334
|
-
const drafts =
|
|
2478
|
+
const drafts = emptyCatalog();
|
|
2335
2479
|
for (const locale of Object.keys(state.drafts).sort()) {
|
|
2336
2480
|
const keys = [...new Set(state.drafts[locale])].sort();
|
|
2337
2481
|
if (keys.length) drafts[locale] = keys;
|
|
2338
2482
|
}
|
|
2339
|
-
const fingerprints =
|
|
2483
|
+
const fingerprints = emptyCatalog();
|
|
2340
2484
|
for (const locale of Object.keys(state.fingerprints).sort()) {
|
|
2341
2485
|
const entries = state.fingerprints[locale] ?? {};
|
|
2342
2486
|
const keys = Object.keys(entries).sort();
|
|
@@ -2400,7 +2544,7 @@ function isObject(value) {
|
|
|
2400
2544
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2401
2545
|
}
|
|
2402
2546
|
function readDrafts(value) {
|
|
2403
|
-
const out =
|
|
2547
|
+
const out = emptyCatalog();
|
|
2404
2548
|
if (!isObject(value)) return out;
|
|
2405
2549
|
for (const [locale, keys] of Object.entries(value)) if (Array.isArray(keys)) out[locale] = keys.filter((key) => typeof key === "string");
|
|
2406
2550
|
return out;
|
|
@@ -2411,7 +2555,7 @@ function readMigrated(value) {
|
|
|
2411
2555
|
return locales.length ? locales : void 0;
|
|
2412
2556
|
}
|
|
2413
2557
|
function readFingerprints(value) {
|
|
2414
|
-
const out =
|
|
2558
|
+
const out = emptyCatalog();
|
|
2415
2559
|
if (!isObject(value)) return out;
|
|
2416
2560
|
for (const [locale, entries] of Object.entries(value)) {
|
|
2417
2561
|
if (!isObject(entries)) continue;
|
|
@@ -2439,7 +2583,7 @@ async function syncProject(cfg, options = {}) {
|
|
|
2439
2583
|
pruned,
|
|
2440
2584
|
pruneBlocked,
|
|
2441
2585
|
findings: scanFindings(cfg, registry, replaced),
|
|
2442
|
-
written:
|
|
2586
|
+
written: emptyCatalog()
|
|
2443
2587
|
};
|
|
2444
2588
|
if (options.dryRun) return result;
|
|
2445
2589
|
const changed = /* @__PURE__ */ new Set([...Object.keys(added), ...Object.keys(pruned)]);
|
|
@@ -2484,12 +2628,12 @@ function gatePasses(result) {
|
|
|
2484
2628
|
function warningCount(result) {
|
|
2485
2629
|
return result.broken.filter((entry) => entry.severity === "warning").length + result.extra.length + result.divergent.length + result.outdated.length;
|
|
2486
2630
|
}
|
|
2487
|
-
function check(cfg, catalogs, registry, fingerprints =
|
|
2631
|
+
function check(cfg, catalogs, registry, fingerprints = emptyCatalog()) {
|
|
2488
2632
|
return gatePass(() => checkPass(cfg, catalogs, registry, fingerprints));
|
|
2489
2633
|
}
|
|
2490
2634
|
function checkPass(cfg, catalogs, registry, fingerprints) {
|
|
2491
2635
|
const extracted = registry.messages();
|
|
2492
|
-
const flat =
|
|
2636
|
+
const flat = emptyCatalog();
|
|
2493
2637
|
for (const locale of cfg.locales) flat[locale] = flatView(catalogs[locale] ?? {});
|
|
2494
2638
|
const source = flat[cfg.sourceLocale] ?? {};
|
|
2495
2639
|
const shipped = shippedCatalogs(cfg, flat, registry);
|
|
@@ -2629,7 +2773,7 @@ function formatBuildReport(result, sourceLocale, root, cap = 5) {
|
|
|
2629
2773
|
}
|
|
2630
2774
|
function checkNextSteps(result, cliReachable = true) {
|
|
2631
2775
|
const steps = [];
|
|
2632
|
-
if (result.missing.length > 0) steps.push("missing: run `npx verbaly
|
|
2776
|
+
if (result.missing.length > 0) steps.push("missing: fill the empty translations in your catalogs, or run `npx verbaly translate` (`npx verbaly extract` adds a key a catalog does not have yet)");
|
|
2633
2777
|
if (result.unknown.length > 0) steps.push("unknown keys: the key used in the code is in no catalog, fix the key or run `npx verbaly extract`");
|
|
2634
2778
|
if (result.broken.some((entry) => entry.severity === "error")) steps.push("broken: a translation has to keep the params, tags and plural cases of its source message");
|
|
2635
2779
|
if (result.collisions.length > 0) steps.push("collisions: give each text its own key in t.id(…), or write the same text in every place");
|
|
@@ -2714,12 +2858,14 @@ function quote(text, single) {
|
|
|
2714
2858
|
if (!single) return JSON.stringify(text);
|
|
2715
2859
|
return `'${text.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
2716
2860
|
}
|
|
2717
|
-
function transformCode(code, file, analysis) {
|
|
2861
|
+
function transformCode(code, file, analysis, options = {}) {
|
|
2718
2862
|
const own = analysis ?? analyzeFile(code, file);
|
|
2719
2863
|
if (!analysis && own.parseError) warnParseError(file, own.parseError);
|
|
2720
2864
|
const { tagged } = own;
|
|
2721
|
-
|
|
2865
|
+
const appAt = options.appImport && !own.appImported ? own.adapterImport : void 0;
|
|
2866
|
+
if (tagged.length === 0 && appAt === void 0) return null;
|
|
2722
2867
|
const s = new MagicString(code);
|
|
2868
|
+
if (appAt !== void 0) s.appendLeft(appAt, `import '${APP_ID}'; `);
|
|
2723
2869
|
for (const msg of tagged) {
|
|
2724
2870
|
const seen = /* @__PURE__ */ new Set();
|
|
2725
2871
|
const entries = msg.params.filter((p) => !seen.has(p.name) && seen.add(p.name));
|
|
@@ -2776,7 +2922,7 @@ function transformSource(code, id, registry, scan) {
|
|
|
2776
2922
|
for (const msg of analysis.tagged) messages[msg.key] ??= msg.message;
|
|
2777
2923
|
return {
|
|
2778
2924
|
messages,
|
|
2779
|
-
result: transformCode(code, id, analysis) ?? null
|
|
2925
|
+
result: transformCode(code, id, analysis, { appImport: true }) ?? null
|
|
2780
2926
|
};
|
|
2781
2927
|
}
|
|
2782
2928
|
function writeBuildTypes(cfg, catalogs) {
|
|
@@ -2795,7 +2941,16 @@ function runBuildGate(cfg, registry, failOnMissing) {
|
|
|
2795
2941
|
if (failOnMissing === true && !gatePasses(found)) throw new Error(`[verbaly] build blocked\n${formatCheckResult(found, cfg.root)}\n${checkNextSteps(found, cliReachable(cfg.root))}`);
|
|
2796
2942
|
reportGate(cfg, found);
|
|
2797
2943
|
const warnings = warningCount(found);
|
|
2798
|
-
if (warnings > 0)
|
|
2944
|
+
if (warnings > 0) {
|
|
2945
|
+
const once = onceMoved(cfg.root);
|
|
2946
|
+
const read = once ? `\`npx verbaly check\` reads them${once}` : "run `npx verbaly check` to read them";
|
|
2947
|
+
warnOnce(`check has ${counted(warnings, "warning")}, and the build still passes: ${read}`, "gate:warnings");
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
function onceMoved(root) {
|
|
2951
|
+
const moved = pluginOnlyOptions(root);
|
|
2952
|
+
if (!moved) return "";
|
|
2953
|
+
return ` once ${moved} ${moved.includes(" and ") ? "move" : "moves"} to verbaly.config`;
|
|
2799
2954
|
}
|
|
2800
2955
|
const reported = /* @__PURE__ */ new Map();
|
|
2801
2956
|
function reportGate(cfg, found) {
|
|
@@ -2823,7 +2978,7 @@ function reportGate(cfg, found) {
|
|
|
2823
2978
|
...lines.map((line) => line.startsWith(" ") ? line : `[verbaly] ${line}`),
|
|
2824
2979
|
"[verbaly] to fix them:",
|
|
2825
2980
|
...steps.split("\n").map((step) => ` ${step}`),
|
|
2826
|
-
|
|
2981
|
+
`[verbaly] \`npx verbaly check\` lists every one${onceMoved(cfg.root)}, and in your CI it stops a release on them`
|
|
2827
2982
|
];
|
|
2828
2983
|
console.warn(block.join("\n"));
|
|
2829
2984
|
}
|
|
@@ -2899,9 +3054,9 @@ async function filesOutsideInclude(cfg) {
|
|
|
2899
3054
|
return found;
|
|
2900
3055
|
}
|
|
2901
3056
|
// src/gate/status.ts
|
|
2902
|
-
function status(cfg, catalogs, registry, drafts =
|
|
3057
|
+
function status(cfg, catalogs, registry, drafts = emptyCatalog(), fingerprints = emptyCatalog()) {
|
|
2903
3058
|
const extracted = registry.messages();
|
|
2904
|
-
const flat =
|
|
3059
|
+
const flat = emptyCatalog();
|
|
2905
3060
|
for (const locale of cfg.locales) flat[locale] = flatView(catalogs[locale] ?? {});
|
|
2906
3061
|
const source = flat[cfg.sourceLocale] ?? {};
|
|
2907
3062
|
const needed = /* @__PURE__ */ new Set([...extracted.keys(), ...Object.keys(source)]);
|
|
@@ -3086,6 +3241,31 @@ function readRoutingChoice(root, configFile) {
|
|
|
3086
3241
|
return false;
|
|
3087
3242
|
}
|
|
3088
3243
|
}
|
|
3244
|
+
function readAstroRouting(root, source) {
|
|
3245
|
+
for (const file of [
|
|
3246
|
+
"astro.config.mjs",
|
|
3247
|
+
"astro.config.js",
|
|
3248
|
+
"astro.config.ts",
|
|
3249
|
+
"astro.config.mts"
|
|
3250
|
+
]) {
|
|
3251
|
+
let text;
|
|
3252
|
+
try {
|
|
3253
|
+
text = readFileSync(join(root, file), "utf8");
|
|
3254
|
+
} catch {
|
|
3255
|
+
continue;
|
|
3256
|
+
}
|
|
3257
|
+
if (!/\bi18n\s*:/.test(text) || /\brouting\s*:\s*['"]manual['"]/.test(text)) return void 0;
|
|
3258
|
+
if (/\bprefixDefaultLocale\s*:\s*true\b/.test(text)) return {
|
|
3259
|
+
mode: "prefix-all",
|
|
3260
|
+
file
|
|
3261
|
+
};
|
|
3262
|
+
const atRoot = /\bdefaultLocale\s*:\s*['"]([^'"]+)['"]/.exec(text)?.[1];
|
|
3263
|
+
return atRoot && atRoot !== source ? void 0 : {
|
|
3264
|
+
mode: "prefix-except-source",
|
|
3265
|
+
file
|
|
3266
|
+
};
|
|
3267
|
+
}
|
|
3268
|
+
}
|
|
3089
3269
|
const ROUTING_SAYS = {
|
|
3090
3270
|
"prefix-except-source": "the source locale has no prefix, every other locale does",
|
|
3091
3271
|
"prefix-all": "every locale has a prefix, the source included",
|
|
@@ -3131,7 +3311,7 @@ async function doctor(cfg) {
|
|
|
3131
3311
|
else warn("config", "no config file, running on defaults", "run `npx verbaly init`");
|
|
3132
3312
|
if (cliReachable(cfg.root)) ok("cli", "the verbaly command is available in this project");
|
|
3133
3313
|
else warn("cli", "the verbaly command is not linked in node_modules/.bin", CLI_INSTALL_FIX);
|
|
3134
|
-
const catalogs =
|
|
3314
|
+
const catalogs = emptyCatalog();
|
|
3135
3315
|
let catalogsHealthy = true;
|
|
3136
3316
|
if (!existsSync(cfg.dir)) {
|
|
3137
3317
|
catalogsHealthy = false;
|
|
@@ -3178,8 +3358,16 @@ async function doctor(cfg) {
|
|
|
3178
3358
|
if (cfg.routing === "no-prefix" && mirrors) warn("routing", "routing is \"no-prefix\" but a render section is configured, and render writes one url tree per locale", "drop the render section, or set routing to \"prefix-except-source\" so the helpers agree with the urls");
|
|
3179
3359
|
else if (cfg.routing === "prefix-all" && mirrors) warn("routing", `routing is "prefix-all" but render keeps ${cfg.sourceLocale} at the root, so its links point at a /${cfg.sourceLocale}/ tree nobody wrote`, "set routing to \"prefix-except-source\", the url tree render writes");
|
|
3180
3360
|
else {
|
|
3181
|
-
const
|
|
3182
|
-
|
|
3361
|
+
const named = configFile ? readRoutingChoice(cfg.root, configFile) : false;
|
|
3362
|
+
const astro = cfg.host === "astro" && cfg.routing === "no-prefix" ? readAstroRouting(cfg.root, cfg.sourceLocale) : void 0;
|
|
3363
|
+
if (astro && named) warn("routing", `routing is "no-prefix" in ${configFile}, but @verbaly/astro uses ${astro.mode}: Astro's i18n in ${astro.file} puts the language in the url`, `remove routing from ${configFile}, or remove i18n from ${astro.file}`);
|
|
3364
|
+
else if (astro) {
|
|
3365
|
+
const how = ` (from Astro's i18n in ${astro.file}, which @verbaly/astro follows)`;
|
|
3366
|
+
ok("routing", `${astro.mode}${how}: ${ROUTING_SAYS[astro.mode]}`);
|
|
3367
|
+
} else {
|
|
3368
|
+
const how = named ? "" : " (from your setup, no routing set)";
|
|
3369
|
+
ok("routing", `${cfg.routing}${how}: ${ROUTING_SAYS[cfg.routing]}`);
|
|
3370
|
+
}
|
|
3183
3371
|
}
|
|
3184
3372
|
const deps = readDependencies(cfg.root);
|
|
3185
3373
|
const host = detectHost(cfg.root);
|
|
@@ -3713,9 +3901,9 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
3713
3901
|
const targets = targetLocales(cfg, options.locales);
|
|
3714
3902
|
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
3715
3903
|
const result = {
|
|
3716
|
-
translated:
|
|
3717
|
-
invalid:
|
|
3718
|
-
pending:
|
|
3904
|
+
translated: emptyCatalog(),
|
|
3905
|
+
invalid: emptyCatalog(),
|
|
3906
|
+
pending: emptyCatalog(),
|
|
3719
3907
|
failed: []
|
|
3720
3908
|
};
|
|
3721
3909
|
const jobs = [];
|
|
@@ -3847,7 +4035,7 @@ function formatTranslateFailures(failed) {
|
|
|
3847
4035
|
async function resolveProvider(cfg, model) {
|
|
3848
4036
|
const configured = cfg.translate.provider;
|
|
3849
4037
|
if (typeof configured === "function") return configured;
|
|
3850
|
-
const { claudeProvider } = await import("./claude-
|
|
4038
|
+
const { claudeProvider } = await import("./claude-XvHG5Neu.js");
|
|
3851
4039
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
3852
4040
|
}
|
|
3853
4041
|
function structureMatches(source, translated) {
|
|
@@ -3945,13 +4133,13 @@ function exportMobile(cfg, catalogs, format, dir, source, targets) {
|
|
|
3945
4133
|
function importCatalogs(cfg, catalogs, files, options = {}) {
|
|
3946
4134
|
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
3947
4135
|
const result = {
|
|
3948
|
-
imported:
|
|
3949
|
-
rejected:
|
|
3950
|
-
skipped:
|
|
3951
|
-
unknown:
|
|
4136
|
+
imported: emptyCatalog(),
|
|
4137
|
+
rejected: emptyCatalog(),
|
|
4138
|
+
skipped: emptyCatalog(),
|
|
4139
|
+
unknown: emptyCatalog(),
|
|
3952
4140
|
unmatched: [],
|
|
3953
4141
|
mapped: [],
|
|
3954
|
-
sources:
|
|
4142
|
+
sources: emptyCatalog()
|
|
3955
4143
|
};
|
|
3956
4144
|
for (const file of files) {
|
|
3957
4145
|
const parsed = parseExchangeFile(file, options.locale);
|
|
@@ -3996,7 +4184,7 @@ function importCatalogs(cfg, catalogs, files, options = {}) {
|
|
|
3996
4184
|
if (!options.dryRun) catalog[key] = text;
|
|
3997
4185
|
(result.imported[locale] ??= []).push(key);
|
|
3998
4186
|
const seen = parsed.sources[key];
|
|
3999
|
-
if (seen) (result.sources[locale] ??=
|
|
4187
|
+
if (seen) (result.sources[locale] ??= emptyCatalog())[key] = seen;
|
|
4000
4188
|
}
|
|
4001
4189
|
}
|
|
4002
4190
|
return result;
|
|
@@ -4665,6 +4853,12 @@ async function init(input = {}) {
|
|
|
4665
4853
|
const next = host ? [...missing.length ? [installCommand(root, missing)] : [], host.wire] : ["run \"verbaly extract\" after writing your first t`…` message"];
|
|
4666
4854
|
const others = detectLibraries(root);
|
|
4667
4855
|
if (found && others.length) next.push(`port the ${others.join(" and ")} catalogs: npx verbaly migrate reports, --write applies`);
|
|
4856
|
+
const source = scaffold.sourceLocale ?? "en";
|
|
4857
|
+
const catalogs = readdirSync(dir).filter((name) => name.endsWith(".json") && name[0] !== ".");
|
|
4858
|
+
if (!scaffold.locales?.some((locale) => locale !== source) && catalogs.length <= 1) {
|
|
4859
|
+
const other = source === "es" ? "en" : "es";
|
|
4860
|
+
next.push(`add a language: locales: ['${source}', '${other}'] in ${configFile}, then npx verbaly extract`);
|
|
4861
|
+
}
|
|
4668
4862
|
return {
|
|
4669
4863
|
created,
|
|
4670
4864
|
skipped,
|
|
@@ -4981,11 +5175,11 @@ function pageSlice(v, locale, used) {
|
|
|
4981
5175
|
return slice;
|
|
4982
5176
|
}
|
|
4983
5177
|
function sameUrl(written, url) {
|
|
4984
|
-
return url !== void 0 && written
|
|
5178
|
+
return url !== void 0 && trimTrailingSlashes(written) === trimTrailingSlashes(url);
|
|
4985
5179
|
}
|
|
4986
5180
|
function publicPath(rel) {
|
|
4987
5181
|
const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
|
|
4988
|
-
return path.length > 1 ? path
|
|
5182
|
+
return path.length > 1 ? trimTrailingSlashes(path) : path;
|
|
4989
5183
|
}
|
|
4990
5184
|
function linkFor(links, name) {
|
|
4991
5185
|
return links && Object.hasOwn(links, name) ? links[name] : void 0;
|
|
@@ -5036,7 +5230,7 @@ function stripBase(path, base) {
|
|
|
5036
5230
|
return rest.startsWith("/") ? rest : path;
|
|
5037
5231
|
}
|
|
5038
5232
|
function normalizeBase(base) {
|
|
5039
|
-
const trimmed = base?.replace(/^\/+/, "")
|
|
5233
|
+
const trimmed = trimTrailingSlashes(base?.replace(/^\/+/, "") ?? "");
|
|
5040
5234
|
return trimmed ? `/${trimmed}` : "";
|
|
5041
5235
|
}
|
|
5042
5236
|
const REDIRECT_BODY = "(function(){var T=/^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|[0-9]{3}))?$/i;function M(t,g){t=String(t||\"\");if(S.indexOf(t)>=0)return t;if(g&&!T.test(t))return;while(t){var i=t.lastIndexOf(\"-\");if(i<0)return;t=t.slice(0,i);if(S.indexOf(t)>=0)return t}}var p=location.pathname;if(B){if(p.indexOf(B)!==0)return;p=p.slice(B.length)||\"/\";if(p.charAt(0)!==\"/\")return}if(M(p.split(\"/\")[1],1))return;var w;if(K)try{w=M(localStorage.getItem(K))}catch(e){}if(!w){var l=navigator.languages||[navigator.language];for(var i=0;i<l.length&&!w;i++)w=M(l[i])}if(!w||w===D)return;location.replace(B+\"/\"+w+(N&&p===\"/\"?\"\":p)+location.search+location.hash)})()";
|
|
@@ -5113,7 +5307,8 @@ async function renderSite(cfg, options = {}) {
|
|
|
5113
5307
|
const locales = options.locales ?? cfg.locales;
|
|
5114
5308
|
const attribute = options.attribute ?? cfg.render.attribute;
|
|
5115
5309
|
const base = normalizeBase(options.base ?? cfg.render.base);
|
|
5116
|
-
const
|
|
5310
|
+
const wantedUrl = options.baseUrl ?? cfg.render.baseUrl;
|
|
5311
|
+
const baseUrl = typeof wantedUrl === "string" ? trimTrailingSlashes(wantedUrl) : void 0;
|
|
5117
5312
|
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
5118
5313
|
const patterns = options.exclude ?? cfg.render.exclude ?? [];
|
|
5119
5314
|
const excluded = patterns.length > 0 ? picomatch(patterns) : () => false;
|
|
@@ -5143,7 +5338,7 @@ async function renderSite(cfg, options = {}) {
|
|
|
5143
5338
|
ignore: locales.map((locale) => `${locale}/**`)
|
|
5144
5339
|
});
|
|
5145
5340
|
const pages = new Set(files.map((file) => publicPath(relative(site, file).replace(/\\/g, "/"))));
|
|
5146
|
-
const missing =
|
|
5341
|
+
const missing = emptyCatalog();
|
|
5147
5342
|
const untranslatedHead = [];
|
|
5148
5343
|
const urls = [];
|
|
5149
5344
|
for (const file of files) {
|
|
@@ -5233,15 +5428,26 @@ function parseArgs$1(raw, attribute) {
|
|
|
5233
5428
|
return;
|
|
5234
5429
|
}
|
|
5235
5430
|
}
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
|
|
5241
|
-
|
|
5242
|
-
|
|
5431
|
+
const OPAQUE_BLOCKS = [
|
|
5432
|
+
{
|
|
5433
|
+
open: "<!--",
|
|
5434
|
+
close: /(?<=<!--)-?>|--!?>/g
|
|
5435
|
+
},
|
|
5436
|
+
{
|
|
5437
|
+
open: /<script\b/gi,
|
|
5438
|
+
close: /<\/script(?=[\t\n\f\r />])/gi,
|
|
5439
|
+
closeTag: true
|
|
5440
|
+
},
|
|
5441
|
+
{
|
|
5442
|
+
open: /<style\b/gi,
|
|
5443
|
+
close: /<\/style(?=[\t\n\f\r />])/gi,
|
|
5444
|
+
closeTag: true
|
|
5243
5445
|
}
|
|
5244
|
-
|
|
5446
|
+
];
|
|
5447
|
+
function protectedRanges(html) {
|
|
5448
|
+
return blocks(html, OPAQUE_BLOCKS).map(({ start, end }) => {
|
|
5449
|
+
return [html.startsWith("<!--", start) ? start : html.indexOf(">", start) + 1, end];
|
|
5450
|
+
});
|
|
5245
5451
|
}
|
|
5246
5452
|
function findClose(html, tagName, from, inSkip) {
|
|
5247
5453
|
const re = new RegExp(`<${tagName}(?=[\\s/>])(?:"[^"]*"|'[^']*'|[^"'>])*>|</${tagName}\\s*>`, "gi");
|
|
@@ -5448,6 +5654,17 @@ const CODE_TAGS = /* @__PURE__ */ new Set([
|
|
|
5448
5654
|
const T_NAMES = /* @__PURE__ */ new Set(["t"]);
|
|
5449
5655
|
const LETTER = /\p{L}/u;
|
|
5450
5656
|
const WHITESPACE = /\s/;
|
|
5657
|
+
const JSX_ADAPTERS = /* @__PURE__ */ new Set(["@verbaly/react", "@verbaly/vue"]);
|
|
5658
|
+
function jsxAdapters(root) {
|
|
5659
|
+
const named = componentAdapters(root).filter((adapter) => JSX_ADAPTERS.has(adapter));
|
|
5660
|
+
return named.length > 0 ? named : ["@verbaly/react"];
|
|
5661
|
+
}
|
|
5662
|
+
function bindingFor(cfg, adapters, client) {
|
|
5663
|
+
if (cfg.host !== "next") return `in the component: \`const t = useT()\` from ${adapters().join(" or ")}`;
|
|
5664
|
+
const onClient = "`const t = useT()` from @verbaly/next/client";
|
|
5665
|
+
if (client) return `"use client", so ${onClient}`;
|
|
5666
|
+
return `no "use client": a Server Component takes \`const t = await getT()\` from @verbaly/next/server, a client one ${onClient}`;
|
|
5667
|
+
}
|
|
5451
5668
|
async function wrapProject(cfg, options = {}) {
|
|
5452
5669
|
const files = (await glob(cfg.include, {
|
|
5453
5670
|
cwd: cfg.root,
|
|
@@ -5461,6 +5678,8 @@ async function wrapProject(cfg, options = {}) {
|
|
|
5461
5678
|
skipped: [],
|
|
5462
5679
|
blocked: []
|
|
5463
5680
|
};
|
|
5681
|
+
let found;
|
|
5682
|
+
const adapters = () => found ??= jsxAdapters(cfg.root);
|
|
5464
5683
|
for (const file of files) {
|
|
5465
5684
|
const code = readFileSync(file, "utf8");
|
|
5466
5685
|
const label = relative(cfg.root, file).replaceAll("\\", "/");
|
|
@@ -5470,10 +5689,12 @@ async function wrapProject(cfg, options = {}) {
|
|
|
5470
5689
|
if (out.code === void 0) continue;
|
|
5471
5690
|
result.changed.push(label);
|
|
5472
5691
|
if (!out.hasT) {
|
|
5692
|
+
const binding = bindingFor(cfg, adapters, out.client);
|
|
5473
5693
|
result.blocked.push({
|
|
5474
5694
|
file: label,
|
|
5475
5695
|
texts: out.wrapped.length,
|
|
5476
|
-
client: out.client
|
|
5696
|
+
client: out.client,
|
|
5697
|
+
binding
|
|
5477
5698
|
});
|
|
5478
5699
|
continue;
|
|
5479
5700
|
}
|
|
@@ -5718,7 +5939,9 @@ async function wrapCommand(cfg, values) {
|
|
|
5718
5939
|
const texts = result.wrapped.filter((entry) => !blocked.has(entry.file));
|
|
5719
5940
|
const verb = values.write ? "wrapped" : "would wrap";
|
|
5720
5941
|
const note = values.write ? "" : " (report only, use --write to apply)";
|
|
5721
|
-
|
|
5942
|
+
let counts = `${counted(texts.length, "text")} in ${counted(done.length, "file")}`;
|
|
5943
|
+
const waiting = result.wrapped.length - texts.length;
|
|
5944
|
+
if (!values.write && waiting > 0) counts += `, and ${counted(waiting, "text")} in ${counted(blocked.size, "file")} once t is in scope`;
|
|
5722
5945
|
console.log(`[verbaly] ${verb} ${counts}${note}`);
|
|
5723
5946
|
for (const entry of values.write ? texts : result.wrapped) {
|
|
5724
5947
|
const attr = entry.kind === "attribute" ? `${entry.attribute} → ` : "";
|
|
@@ -5730,10 +5953,7 @@ async function wrapCommand(cfg, values) {
|
|
|
5730
5953
|
}
|
|
5731
5954
|
if (result.blocked.length > 0) {
|
|
5732
5955
|
console.log(` nothing written in ${counted(result.blocked.length, "file")} with no t in scope:`);
|
|
5733
|
-
for (const entry of result.blocked) {
|
|
5734
|
-
const how = entry.client ? "\"use client\", so const t = useT()" : "no \"use client\": check which side renders it before you pick the binding";
|
|
5735
|
-
console.log(` ${entry.file} ${counted(entry.texts, "text")}, ${how}`);
|
|
5736
|
-
}
|
|
5956
|
+
for (const entry of result.blocked) console.log(` ${entry.file} ${counted(entry.texts, "text")}, ${entry.binding}`);
|
|
5737
5957
|
console.log(" next: add t there, run verbaly wrap --write again, then verbaly extract");
|
|
5738
5958
|
} else if (values.write && done.length > 0) console.log(" next: run verbaly extract");
|
|
5739
5959
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verbaly/compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.81.0",
|
|
4
4
|
"description": "Message extraction and type-safe codegen for Verbaly.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -46,13 +46,13 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@babel/parser": "^8.0
|
|
49
|
+
"@babel/parser": "^8.1.0",
|
|
50
50
|
"jiti": "^2.7.0",
|
|
51
51
|
"jsonc-parser": "^3.3.1",
|
|
52
52
|
"magic-string": "^1.4.5",
|
|
53
53
|
"picomatch": "^4.0.7",
|
|
54
54
|
"tinyglobby": "^0.2.17",
|
|
55
|
-
"verbaly": "0.
|
|
55
|
+
"verbaly": "0.81.0"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"@anthropic-ai/sdk": ">=0.110.0"
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@anthropic-ai/sdk": "^1.
|
|
66
|
+
"@anthropic-ai/sdk": "^1.6.0",
|
|
67
67
|
"@types/picomatch": "^4.0.3",
|
|
68
68
|
"esbuild": "^0.28.2",
|
|
69
69
|
"fast-check": "^4.10.2",
|