@verbaly/compiler 0.44.0 → 0.46.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 +25 -2
- package/dist/{claude-BspJ1dOt.js → claude-B-vethK9.js} +2 -2
- package/dist/{claude-BspJ1dOt.js.map → claude-B-vethK9.js.map} +1 -1
- package/dist/cli.js +522 -388
- package/dist/cli.js.map +1 -1
- package/dist/{config-cXLsNoMP.js → config-iLrfRYDB.js} +6 -3
- package/dist/config-iLrfRYDB.js.map +1 -0
- package/dist/index.d.ts +16 -1
- package/dist/index.js +227 -99
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/dist/config-cXLsNoMP.js.map +0 -1
|
@@ -7,14 +7,16 @@ function resolveConfig(config = {}) {
|
|
|
7
7
|
const root = resolve(config.root ?? process.cwd());
|
|
8
8
|
const dir = resolve(root, config.dir ?? "locales");
|
|
9
9
|
const sourceLocale = config.sourceLocale ?? "en";
|
|
10
|
+
const localesDeclared = config.locales !== void 0;
|
|
10
11
|
const locales = /* @__PURE__ */ new Set([sourceLocale, ...config.locales ?? []]);
|
|
11
|
-
if (existsSync(dir)) {
|
|
12
|
+
if (!localesDeclared && existsSync(dir)) {
|
|
12
13
|
for (const file of readdirSync(dir)) if (file.endsWith(".json") && !file.startsWith(".") && file !== `en-XA.json`) locales.add(file.slice(0, -5));
|
|
13
14
|
}
|
|
14
15
|
return {
|
|
15
16
|
root,
|
|
16
17
|
dir,
|
|
17
18
|
sourceLocale,
|
|
19
|
+
localesDeclared,
|
|
18
20
|
routing: config.routing ?? (config.render ? "prefix-except-source" : "no-prefix"),
|
|
19
21
|
icu: config.icu,
|
|
20
22
|
relative: config.relative,
|
|
@@ -23,7 +25,8 @@ function resolveConfig(config = {}) {
|
|
|
23
25
|
exclude: config.exclude ?? ["**/node_modules/**", "**/dist/**"],
|
|
24
26
|
dts: typeof config.dts === "string" ? resolve(root, config.dts) : config.dts,
|
|
25
27
|
translate: config.translate ?? {},
|
|
26
|
-
render: config.render ?? {}
|
|
28
|
+
render: config.render ?? {},
|
|
29
|
+
bundle: config.bundle ?? {}
|
|
27
30
|
};
|
|
28
31
|
}
|
|
29
32
|
const CONFIG_FILES = [
|
|
@@ -78,4 +81,4 @@ function definedOnly(config) {
|
|
|
78
81
|
//#endregion
|
|
79
82
|
export { targetLocales as a, resolveConfig as i, isModuleNotFound as n, loadConfig as r, findConfigFile as t };
|
|
80
83
|
|
|
81
|
-
//# sourceMappingURL=config-
|
|
84
|
+
//# sourceMappingURL=config-iLrfRYDB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-iLrfRYDB.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 BundleConfig {\n exclude?: string[];\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 icu?: boolean;\n relative?: boolean;\n include?: string[];\n exclude?: string[];\n dts?: string | false;\n translate?: TranslateConfig;\n render?: RenderConfig;\n bundle?: BundleConfig;\n}\n\nexport interface ResolvedConfig {\n root: string;\n dir: string;\n sourceLocale: string;\n locales: string[];\n localesDeclared: boolean;\n routing: Routing;\n icu: boolean | undefined;\n relative: boolean | undefined;\n include: string[];\n exclude: string[];\n dts: string | false | undefined;\n translate: TranslateConfig;\n render: RenderConfig;\n bundle: BundleConfig;\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 localesDeclared = config.locales !== undefined;\n const locales = new Set<string>([sourceLocale, ...(config.locales ?? [])]);\n // a declared list is the list: discovery is for a project that never wrote one down\n if (!localesDeclared && 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 localesDeclared,\n // the mode follows the surface: render is what builds a url tree per locale, so it is the tell\n routing: config.routing ?? (config.render ? 'prefix-except-source' : 'no-prefix'),\n // the catalogs decide on their own; these only force it for messages that land after the build\n icu: config.icu,\n relative: config.relative,\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 bundle: config.bundle ?? {},\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":"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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -65,6 +65,9 @@ interface RedirectConfig {
|
|
|
65
65
|
on?: 'root' | 'all';
|
|
66
66
|
storageKey?: string | false;
|
|
67
67
|
}
|
|
68
|
+
interface BundleConfig {
|
|
69
|
+
exclude?: string[];
|
|
70
|
+
}
|
|
68
71
|
interface RenderConfig {
|
|
69
72
|
links?: Record<string, RichLink>;
|
|
70
73
|
site?: string;
|
|
@@ -89,12 +92,14 @@ interface VerbalyConfig {
|
|
|
89
92
|
dts?: string | false;
|
|
90
93
|
translate?: TranslateConfig;
|
|
91
94
|
render?: RenderConfig;
|
|
95
|
+
bundle?: BundleConfig;
|
|
92
96
|
}
|
|
93
97
|
interface ResolvedConfig {
|
|
94
98
|
root: string;
|
|
95
99
|
dir: string;
|
|
96
100
|
sourceLocale: string;
|
|
97
101
|
locales: string[];
|
|
102
|
+
localesDeclared: boolean;
|
|
98
103
|
routing: Routing;
|
|
99
104
|
icu: boolean | undefined;
|
|
100
105
|
relative: boolean | undefined;
|
|
@@ -103,6 +108,7 @@ interface ResolvedConfig {
|
|
|
103
108
|
dts: string | false | undefined;
|
|
104
109
|
translate: TranslateConfig;
|
|
105
110
|
render: RenderConfig;
|
|
111
|
+
bundle: BundleConfig;
|
|
106
112
|
}
|
|
107
113
|
declare function resolveConfig(config?: VerbalyConfig): ResolvedConfig;
|
|
108
114
|
declare function loadConfig(root: string, overrides?: VerbalyConfig): Promise<ResolvedConfig>;
|
|
@@ -163,6 +169,9 @@ declare class MessageRegistry {
|
|
|
163
169
|
origins(): Map<string, string[]>;
|
|
164
170
|
}
|
|
165
171
|
//#endregion
|
|
172
|
+
//#region src/bundle.d.ts
|
|
173
|
+
declare function clientCatalogs(cfg: ResolvedConfig, catalogs: Catalogs): Catalogs;
|
|
174
|
+
//#endregion
|
|
166
175
|
//#region src/extract.d.ts
|
|
167
176
|
declare function extractProject(cfg: ResolvedConfig): Promise<MessageRegistry>;
|
|
168
177
|
declare function collectOrigins(cfg: ResolvedConfig): Promise<Record<string, string[]>>;
|
|
@@ -294,11 +303,17 @@ interface WrapSkip {
|
|
|
294
303
|
text: string;
|
|
295
304
|
reason: string;
|
|
296
305
|
}
|
|
306
|
+
interface WrapBlocked {
|
|
307
|
+
file: string;
|
|
308
|
+
texts: number;
|
|
309
|
+
client: boolean;
|
|
310
|
+
}
|
|
297
311
|
interface WrapResult {
|
|
298
312
|
files: number;
|
|
299
313
|
changed: string[];
|
|
300
314
|
wrapped: WrapEntry[];
|
|
301
315
|
skipped: WrapSkip[];
|
|
316
|
+
blocked: WrapBlocked[];
|
|
302
317
|
}
|
|
303
318
|
interface WrapOptions {
|
|
304
319
|
write?: boolean;
|
|
@@ -328,5 +343,5 @@ interface RenderSiteResult {
|
|
|
328
343
|
declare function renderSite(cfg: ResolvedConfig, options?: RenderSiteOptions): Promise<RenderSiteResult>;
|
|
329
344
|
declare function formatRenderWarnings(result: RenderSiteResult, sourceLocale: string): string[];
|
|
330
345
|
//#endregion
|
|
331
|
-
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, needsIcu, needsRelative, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, wrapProject, writeCatalog, writeDts };
|
|
346
|
+
export { type BrokenEntry, type BundleConfig, 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 WrapBlocked, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, check, clientCatalogs, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, needsIcu, needsRelative, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, wrapProject, writeCatalog, writeDts };
|
|
332
347
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as targetLocales, i as resolveConfig, r as loadConfig, t as findConfigFile } from "./config-
|
|
1
|
+
import { a as targetLocales, i as resolveConfig, r as loadConfig, t as findConfigFile } from "./config-iLrfRYDB.js";
|
|
2
2
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { dirname, join, relative } from "node:path";
|
|
4
4
|
import { LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
@@ -112,6 +112,55 @@ function needsRelative(catalogs) {
|
|
|
112
112
|
return false;
|
|
113
113
|
}
|
|
114
114
|
//#endregion
|
|
115
|
+
//#region src/text.ts
|
|
116
|
+
function counted(count, noun, many = `${noun}s`) {
|
|
117
|
+
return `${count} ${count === 1 ? noun : many}`;
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/bundle.ts
|
|
121
|
+
function isExcluded(key, prefixes) {
|
|
122
|
+
return prefixes.some((prefix) => key === prefix || key.startsWith(`${prefix}.`));
|
|
123
|
+
}
|
|
124
|
+
function clientCatalogs(cfg, catalogs) {
|
|
125
|
+
const prefixes = cfg.bundle.exclude ?? [];
|
|
126
|
+
if (prefixes.length === 0) return catalogs;
|
|
127
|
+
const out = {};
|
|
128
|
+
for (const [locale, catalog] of Object.entries(catalogs)) {
|
|
129
|
+
const kept = {};
|
|
130
|
+
for (const [key, message] of Object.entries(catalog)) if (!isExcluded(key, prefixes)) kept[key] = message;
|
|
131
|
+
out[locale] = kept;
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
function auditBundle(cfg, catalogs, registry) {
|
|
136
|
+
const prefixes = cfg.bundle.exclude ?? [];
|
|
137
|
+
if (prefixes.length === 0) return [];
|
|
138
|
+
const source = Object.keys(catalogs[cfg.sourceLocale] ?? {});
|
|
139
|
+
const used = registry.usedKeys();
|
|
140
|
+
const issues = [];
|
|
141
|
+
for (const prefix of prefixes) {
|
|
142
|
+
if (!source.some((key) => isExcluded(key, [prefix]))) {
|
|
143
|
+
issues.push({
|
|
144
|
+
prefix,
|
|
145
|
+
problem: `matches no key in ${cfg.sourceLocale}.json, so it excludes nothing`,
|
|
146
|
+
fix: "check the spelling against your catalog, or drop it from bundle.exclude"
|
|
147
|
+
});
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const files = /* @__PURE__ */ new Set();
|
|
151
|
+
for (const [key, where] of used) if (isExcluded(key, [prefix])) for (const file of where) files.add(file);
|
|
152
|
+
if (files.size > 0) issues.push({
|
|
153
|
+
prefix,
|
|
154
|
+
problem: `is read by t() in ${counted(files.size, "file")}, where it resolves to the key itself`,
|
|
155
|
+
fix: "pre-render those pages with render, load the group with addMessages, or stop excluding it"
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return issues;
|
|
159
|
+
}
|
|
160
|
+
function formatBundleIssue(issue) {
|
|
161
|
+
return `bundle.exclude "${issue.prefix}" ${issue.problem}`;
|
|
162
|
+
}
|
|
163
|
+
//#endregion
|
|
115
164
|
//#region src/warn.ts
|
|
116
165
|
const warned = /* @__PURE__ */ new Set();
|
|
117
166
|
function warnOnce(message, dedupeKey = message) {
|
|
@@ -1032,6 +1081,87 @@ function writeDts(cfg, catalog, file) {
|
|
|
1032
1081
|
writeFileSync(file, content);
|
|
1033
1082
|
}
|
|
1034
1083
|
//#endregion
|
|
1084
|
+
//#region src/init.ts
|
|
1085
|
+
const HOSTS = [
|
|
1086
|
+
{
|
|
1087
|
+
name: "nuxt",
|
|
1088
|
+
dep: "nuxt",
|
|
1089
|
+
pkg: "@verbaly/nuxt",
|
|
1090
|
+
wire: "add '@verbaly/nuxt' to the modules in nuxt.config"
|
|
1091
|
+
},
|
|
1092
|
+
{
|
|
1093
|
+
name: "next",
|
|
1094
|
+
dep: "next",
|
|
1095
|
+
pkg: "@verbaly/next",
|
|
1096
|
+
wire: "wrap the export of next.config with withVerbaly"
|
|
1097
|
+
},
|
|
1098
|
+
{
|
|
1099
|
+
name: "sveltekit",
|
|
1100
|
+
dep: "@sveltejs/kit",
|
|
1101
|
+
pkg: "@verbaly/vite",
|
|
1102
|
+
wire: "add verbaly() to the plugins in vite.config (per-request locale: @verbaly/sveltekit)"
|
|
1103
|
+
},
|
|
1104
|
+
{
|
|
1105
|
+
name: "astro",
|
|
1106
|
+
dep: "astro",
|
|
1107
|
+
pkg: "@verbaly/astro",
|
|
1108
|
+
wire: "add verbaly() to the integrations in astro.config"
|
|
1109
|
+
},
|
|
1110
|
+
{
|
|
1111
|
+
name: "vite",
|
|
1112
|
+
dep: "vite",
|
|
1113
|
+
pkg: "@verbaly/vite",
|
|
1114
|
+
wire: "add verbaly() to the plugins in vite.config"
|
|
1115
|
+
},
|
|
1116
|
+
{
|
|
1117
|
+
name: "webpack",
|
|
1118
|
+
dep: "webpack",
|
|
1119
|
+
pkg: "@verbaly/unplugin",
|
|
1120
|
+
wire: "add the verbaly webpack plugin to your build config"
|
|
1121
|
+
},
|
|
1122
|
+
{
|
|
1123
|
+
name: "rspack",
|
|
1124
|
+
dep: "@rspack/core",
|
|
1125
|
+
pkg: "@verbaly/unplugin",
|
|
1126
|
+
wire: "add the verbaly rspack plugin to your build config"
|
|
1127
|
+
},
|
|
1128
|
+
{
|
|
1129
|
+
name: "rollup",
|
|
1130
|
+
dep: "rollup",
|
|
1131
|
+
pkg: "@verbaly/unplugin",
|
|
1132
|
+
wire: "add the verbaly rollup plugin to your build config"
|
|
1133
|
+
},
|
|
1134
|
+
{
|
|
1135
|
+
name: "esbuild",
|
|
1136
|
+
dep: "esbuild",
|
|
1137
|
+
pkg: "@verbaly/unplugin",
|
|
1138
|
+
wire: "add the verbaly esbuild plugin to your build config"
|
|
1139
|
+
}
|
|
1140
|
+
];
|
|
1141
|
+
const WIRING_PACKAGES = [...new Set(HOSTS.map((host) => host.pkg))];
|
|
1142
|
+
function readDependencies(root) {
|
|
1143
|
+
const path = join(root, "package.json");
|
|
1144
|
+
if (!existsSync(path)) return {};
|
|
1145
|
+
try {
|
|
1146
|
+
const pkg = JSON.parse(readFileSync(path, "utf8"));
|
|
1147
|
+
return {
|
|
1148
|
+
...pkg.dependencies,
|
|
1149
|
+
...pkg.devDependencies
|
|
1150
|
+
};
|
|
1151
|
+
} catch {
|
|
1152
|
+
return {};
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
function cliReachable(root) {
|
|
1156
|
+
const bin = join(root, "node_modules", ".bin");
|
|
1157
|
+
return existsSync(join(bin, "verbaly")) || existsSync(join(bin, "verbaly.CMD"));
|
|
1158
|
+
}
|
|
1159
|
+
const CLI_INSTALL_FIX = "add `@verbaly/compiler` to your dev dependencies, then `npx verbaly` resolves";
|
|
1160
|
+
function detectHost(root) {
|
|
1161
|
+
const deps = readDependencies(root);
|
|
1162
|
+
return HOSTS.find((host) => deps[host.dep]);
|
|
1163
|
+
}
|
|
1164
|
+
//#endregion
|
|
1035
1165
|
//#region src/check.ts
|
|
1036
1166
|
function gatePasses(result) {
|
|
1037
1167
|
return result.missing.length === 0 && result.unknown.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
@@ -1113,11 +1243,12 @@ function formatCheckResult(result) {
|
|
|
1113
1243
|
}
|
|
1114
1244
|
return lines.join("\n");
|
|
1115
1245
|
}
|
|
1116
|
-
function checkNextSteps(result) {
|
|
1246
|
+
function checkNextSteps(result, cliReachable = true) {
|
|
1117
1247
|
const steps = [];
|
|
1118
1248
|
if (result.missing.length > 0) steps.push("missing: run `npx verbaly extract` to scaffold the keys, then fill them (or run `npx verbaly translate`)");
|
|
1119
1249
|
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`");
|
|
1120
1250
|
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");
|
|
1251
|
+
if (steps.length > 0 && !cliReachable) steps.push(`the verbaly command is not linked in this project: ${CLI_INSTALL_FIX}`);
|
|
1121
1252
|
return steps.join("\n");
|
|
1122
1253
|
}
|
|
1123
1254
|
function formatCheckWarnings(result) {
|
|
@@ -1166,11 +1297,12 @@ function resolveVirtualId(id) {
|
|
|
1166
1297
|
if (id === "virtual:verbaly" || id.startsWith(`virtual:verbaly/`)) return "\0" + id;
|
|
1167
1298
|
}
|
|
1168
1299
|
function loadVirtualModule(id, cfg, catalogs) {
|
|
1300
|
+
const client = clientCatalogs(cfg, catalogs);
|
|
1169
1301
|
if (id === RESOLVED_VIRTUAL_ID) return generateRuntimeModule(cfg, {
|
|
1170
|
-
icu: cfg.icu ?? needsIcu(
|
|
1171
|
-
relative: cfg.relative ?? needsRelative(
|
|
1302
|
+
icu: cfg.icu ?? needsIcu(client),
|
|
1303
|
+
relative: cfg.relative ?? needsRelative(client)
|
|
1172
1304
|
});
|
|
1173
|
-
if (id.startsWith(LOCALE_MODULE_PREFIX)) return generateLocaleModule(
|
|
1305
|
+
if (id.startsWith(LOCALE_MODULE_PREFIX)) return generateLocaleModule(client[id.slice(LOCALE_MODULE_PREFIX.length)] ?? {});
|
|
1174
1306
|
}
|
|
1175
1307
|
function isTransformTarget(id) {
|
|
1176
1308
|
return SOURCE_FILE_RE.test(id) && !id.includes("node_modules") && !id.startsWith("\0");
|
|
@@ -1195,13 +1327,15 @@ function transformSource(code, id, registry) {
|
|
|
1195
1327
|
};
|
|
1196
1328
|
}
|
|
1197
1329
|
function runBuildGate(cfg, registry, failOnMissing) {
|
|
1198
|
-
const
|
|
1330
|
+
const catalogs = loadCatalogs(cfg);
|
|
1331
|
+
for (const issue of auditBundle(cfg, catalogs, registry)) warnOnce(`${formatBundleIssue(issue)}\n fix: ${issue.fix}`, `bundle:${issue.prefix}`);
|
|
1332
|
+
const found = check(cfg, catalogs, registry);
|
|
1199
1333
|
const result = failOnMissing === false ? {
|
|
1200
1334
|
...found,
|
|
1201
1335
|
missing: []
|
|
1202
1336
|
} : found;
|
|
1203
1337
|
if (gatePasses(result)) return;
|
|
1204
|
-
throw new Error(`[verbaly] build blocked\n${formatCheckResult(result)}\n${checkNextSteps(result)}`);
|
|
1338
|
+
throw new Error(`[verbaly] build blocked\n${formatCheckResult(result)}\n${checkNextSteps(result, cliReachable(cfg.root))}`);
|
|
1205
1339
|
}
|
|
1206
1340
|
//#endregion
|
|
1207
1341
|
//#region src/drafts.ts
|
|
@@ -1254,11 +1388,6 @@ function effectiveDrafts(drafts, catalogs) {
|
|
|
1254
1388
|
return out;
|
|
1255
1389
|
}
|
|
1256
1390
|
//#endregion
|
|
1257
|
-
//#region src/text.ts
|
|
1258
|
-
function counted(count, noun, many = `${noun}s`) {
|
|
1259
|
-
return `${count} ${count === 1 ? noun : many}`;
|
|
1260
|
-
}
|
|
1261
|
-
//#endregion
|
|
1262
1391
|
//#region src/status.ts
|
|
1263
1392
|
function status(cfg, catalogs, registry, drafts = {}) {
|
|
1264
1393
|
const extracted = registry.messages();
|
|
@@ -1439,7 +1568,7 @@ function formatTranslateFailures(failed) {
|
|
|
1439
1568
|
async function resolveProvider(cfg, model) {
|
|
1440
1569
|
const configured = cfg.translate.provider;
|
|
1441
1570
|
if (typeof configured === "function") return configured;
|
|
1442
|
-
const { claudeProvider } = await import("./claude-
|
|
1571
|
+
const { claudeProvider } = await import("./claude-B-vethK9.js");
|
|
1443
1572
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1444
1573
|
}
|
|
1445
1574
|
function structureMatches(source, translated) {
|
|
@@ -1470,82 +1599,6 @@ function glossaryFor(cfg, locale, messages) {
|
|
|
1470
1599
|
return Object.keys(out).length > 0 ? out : void 0;
|
|
1471
1600
|
}
|
|
1472
1601
|
//#endregion
|
|
1473
|
-
//#region src/init.ts
|
|
1474
|
-
const HOSTS = [
|
|
1475
|
-
{
|
|
1476
|
-
name: "nuxt",
|
|
1477
|
-
dep: "nuxt",
|
|
1478
|
-
pkg: "@verbaly/nuxt",
|
|
1479
|
-
wire: "add '@verbaly/nuxt' to the modules in nuxt.config"
|
|
1480
|
-
},
|
|
1481
|
-
{
|
|
1482
|
-
name: "next",
|
|
1483
|
-
dep: "next",
|
|
1484
|
-
pkg: "@verbaly/next",
|
|
1485
|
-
wire: "wrap the export of next.config with withVerbaly"
|
|
1486
|
-
},
|
|
1487
|
-
{
|
|
1488
|
-
name: "sveltekit",
|
|
1489
|
-
dep: "@sveltejs/kit",
|
|
1490
|
-
pkg: "@verbaly/vite",
|
|
1491
|
-
wire: "add verbaly() to the plugins in vite.config (per-request locale: @verbaly/sveltekit)"
|
|
1492
|
-
},
|
|
1493
|
-
{
|
|
1494
|
-
name: "astro",
|
|
1495
|
-
dep: "astro",
|
|
1496
|
-
pkg: "@verbaly/astro",
|
|
1497
|
-
wire: "add verbaly() to the integrations in astro.config"
|
|
1498
|
-
},
|
|
1499
|
-
{
|
|
1500
|
-
name: "vite",
|
|
1501
|
-
dep: "vite",
|
|
1502
|
-
pkg: "@verbaly/vite",
|
|
1503
|
-
wire: "add verbaly() to the plugins in vite.config"
|
|
1504
|
-
},
|
|
1505
|
-
{
|
|
1506
|
-
name: "webpack",
|
|
1507
|
-
dep: "webpack",
|
|
1508
|
-
pkg: "@verbaly/unplugin",
|
|
1509
|
-
wire: "add the verbaly webpack plugin to your build config"
|
|
1510
|
-
},
|
|
1511
|
-
{
|
|
1512
|
-
name: "rspack",
|
|
1513
|
-
dep: "@rspack/core",
|
|
1514
|
-
pkg: "@verbaly/unplugin",
|
|
1515
|
-
wire: "add the verbaly rspack plugin to your build config"
|
|
1516
|
-
},
|
|
1517
|
-
{
|
|
1518
|
-
name: "rollup",
|
|
1519
|
-
dep: "rollup",
|
|
1520
|
-
pkg: "@verbaly/unplugin",
|
|
1521
|
-
wire: "add the verbaly rollup plugin to your build config"
|
|
1522
|
-
},
|
|
1523
|
-
{
|
|
1524
|
-
name: "esbuild",
|
|
1525
|
-
dep: "esbuild",
|
|
1526
|
-
pkg: "@verbaly/unplugin",
|
|
1527
|
-
wire: "add the verbaly esbuild plugin to your build config"
|
|
1528
|
-
}
|
|
1529
|
-
];
|
|
1530
|
-
const WIRING_PACKAGES = [...new Set(HOSTS.map((host) => host.pkg))];
|
|
1531
|
-
function readDependencies(root) {
|
|
1532
|
-
const path = join(root, "package.json");
|
|
1533
|
-
if (!existsSync(path)) return {};
|
|
1534
|
-
try {
|
|
1535
|
-
const pkg = JSON.parse(readFileSync(path, "utf8"));
|
|
1536
|
-
return {
|
|
1537
|
-
...pkg.dependencies,
|
|
1538
|
-
...pkg.devDependencies
|
|
1539
|
-
};
|
|
1540
|
-
} catch {
|
|
1541
|
-
return {};
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
function detectHost(root) {
|
|
1545
|
-
const deps = readDependencies(root);
|
|
1546
|
-
return HOSTS.find((host) => deps[host.dep]);
|
|
1547
|
-
}
|
|
1548
|
-
//#endregion
|
|
1549
1602
|
//#region src/doctor.ts
|
|
1550
1603
|
const PREVIEW = 5;
|
|
1551
1604
|
function readRoutingChoice(root, configFile) {
|
|
@@ -1593,6 +1646,8 @@ async function doctor(cfg) {
|
|
|
1593
1646
|
const configFile = findConfigFile(cfg.root);
|
|
1594
1647
|
if (configFile) ok("config", `${configFile} found`);
|
|
1595
1648
|
else warn("config", "no config file, running on defaults", "run `npx verbaly init`");
|
|
1649
|
+
if (cliReachable(cfg.root)) ok("cli", "the verbaly command is available in this project");
|
|
1650
|
+
else warn("cli", "the verbaly command is not linked in node_modules/.bin", CLI_INSTALL_FIX);
|
|
1596
1651
|
const catalogs = {};
|
|
1597
1652
|
let catalogsHealthy = true;
|
|
1598
1653
|
if (!existsSync(cfg.dir)) {
|
|
@@ -1618,7 +1673,10 @@ async function doctor(cfg) {
|
|
|
1618
1673
|
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
1619
1674
|
}
|
|
1620
1675
|
}
|
|
1621
|
-
if (catalogsHealthy)
|
|
1676
|
+
if (catalogsHealthy) {
|
|
1677
|
+
const how = cfg.localesDeclared ? "" : ", no locales set, so the catalog files decide";
|
|
1678
|
+
ok("catalogs", `${counted(cfg.locales.length, "locale")} (${cfg.locales.join(", ")}) in ${rel(cfg.dir)}/${how}`);
|
|
1679
|
+
}
|
|
1622
1680
|
}
|
|
1623
1681
|
const source = catalogs[cfg.sourceLocale];
|
|
1624
1682
|
if (source && Object.keys(source).length === 0) warn("source", `source catalog ${cfg.sourceLocale}.json is empty`, "write your first t`…` message and run `npx verbaly extract`");
|
|
@@ -1667,6 +1725,12 @@ async function doctor(cfg) {
|
|
|
1667
1725
|
if (orphans.length > 0) warn("orphans", `${counted(orphans.length, "catalog key")} no longer referenced in code (${preview(orphans)})`, "run `npx verbaly extract --prune` to drop them");
|
|
1668
1726
|
else ok("orphans", "no orphan keys");
|
|
1669
1727
|
}
|
|
1728
|
+
const excluded = cfg.bundle.exclude ?? [];
|
|
1729
|
+
if (excluded.length > 0 && catalogsHealthy) {
|
|
1730
|
+
const issues = auditBundle(cfg, catalogs, registry);
|
|
1731
|
+
for (const issue of issues) warn("bundle", formatBundleIssue(issue), issue.fix);
|
|
1732
|
+
if (issues.length === 0) ok("bundle", `${counted(excluded.length, "group")} kept out of the client (${preview(excluded)})`);
|
|
1733
|
+
}
|
|
1670
1734
|
if (catalogsHealthy) {
|
|
1671
1735
|
const result = check(cfg, catalogs, registry);
|
|
1672
1736
|
if (result.unknown.length > 0) error("keys", `${counted(result.unknown.length, "unknown key")} used in code (${preview(result.unknown.map((u) => u.key))})`, "fix the key or add it to the catalogs (`npx verbaly check` for details)");
|
|
@@ -1700,8 +1764,16 @@ const WRAP_ATTRS = /* @__PURE__ */ new Set([
|
|
|
1700
1764
|
"placeholder",
|
|
1701
1765
|
"aria-label"
|
|
1702
1766
|
]);
|
|
1767
|
+
const CODE_TAGS = /* @__PURE__ */ new Set([
|
|
1768
|
+
"code",
|
|
1769
|
+
"pre",
|
|
1770
|
+
"kbd",
|
|
1771
|
+
"samp",
|
|
1772
|
+
"var"
|
|
1773
|
+
]);
|
|
1703
1774
|
const T_NAMES = /* @__PURE__ */ new Set(["t"]);
|
|
1704
1775
|
const LETTER = /\p{L}/u;
|
|
1776
|
+
const WHITESPACE = /\s/;
|
|
1705
1777
|
async function wrapProject(cfg, options = {}) {
|
|
1706
1778
|
const files = (await glob(cfg.include, {
|
|
1707
1779
|
cwd: cfg.root,
|
|
@@ -1712,7 +1784,8 @@ async function wrapProject(cfg, options = {}) {
|
|
|
1712
1784
|
files: files.length,
|
|
1713
1785
|
changed: [],
|
|
1714
1786
|
wrapped: [],
|
|
1715
|
-
skipped: []
|
|
1787
|
+
skipped: [],
|
|
1788
|
+
blocked: []
|
|
1716
1789
|
};
|
|
1717
1790
|
for (const file of files) {
|
|
1718
1791
|
const code = readFileSync(file, "utf8");
|
|
@@ -1720,10 +1793,17 @@ async function wrapProject(cfg, options = {}) {
|
|
|
1720
1793
|
const out = wrapCode(code, label);
|
|
1721
1794
|
result.wrapped.push(...out.wrapped);
|
|
1722
1795
|
result.skipped.push(...out.skipped);
|
|
1723
|
-
if (out.code
|
|
1724
|
-
|
|
1725
|
-
|
|
1796
|
+
if (out.code === void 0) continue;
|
|
1797
|
+
result.changed.push(label);
|
|
1798
|
+
if (!out.hasT) {
|
|
1799
|
+
result.blocked.push({
|
|
1800
|
+
file: label,
|
|
1801
|
+
texts: out.wrapped.length,
|
|
1802
|
+
client: out.client
|
|
1803
|
+
});
|
|
1804
|
+
continue;
|
|
1726
1805
|
}
|
|
1806
|
+
if (options.write) writeFileSync(file, out.code);
|
|
1727
1807
|
}
|
|
1728
1808
|
return result;
|
|
1729
1809
|
}
|
|
@@ -1740,9 +1820,13 @@ function wrapCode(code, file) {
|
|
|
1740
1820
|
} catch {
|
|
1741
1821
|
return {
|
|
1742
1822
|
wrapped,
|
|
1743
|
-
skipped
|
|
1823
|
+
skipped,
|
|
1824
|
+
hasT: false,
|
|
1825
|
+
client: false
|
|
1744
1826
|
};
|
|
1745
1827
|
}
|
|
1828
|
+
const hasT = declaresT(ast);
|
|
1829
|
+
const client = hasDirective(ast, "use client");
|
|
1746
1830
|
const s = new MagicString(code);
|
|
1747
1831
|
const visited = /* @__PURE__ */ new Set();
|
|
1748
1832
|
let changed = false;
|
|
@@ -1754,10 +1838,14 @@ function wrapCode(code, file) {
|
|
|
1754
1838
|
return changed ? {
|
|
1755
1839
|
code: s.toString(),
|
|
1756
1840
|
wrapped,
|
|
1757
|
-
skipped
|
|
1841
|
+
skipped,
|
|
1842
|
+
hasT,
|
|
1843
|
+
client
|
|
1758
1844
|
} : {
|
|
1759
1845
|
wrapped,
|
|
1760
|
-
skipped
|
|
1846
|
+
skipped,
|
|
1847
|
+
hasT,
|
|
1848
|
+
client
|
|
1761
1849
|
};
|
|
1762
1850
|
function processElement(node) {
|
|
1763
1851
|
visited.add(node);
|
|
@@ -1766,6 +1854,7 @@ function wrapCode(code, file) {
|
|
|
1766
1854
|
const name = opening.name;
|
|
1767
1855
|
if (name.type === "JSXIdentifier" && name.name === "Trans") return markSubtree(node);
|
|
1768
1856
|
if (hasVerbalyAttr(opening)) return markSubtree(node);
|
|
1857
|
+
if (name.type === "JSXIdentifier" && CODE_TAGS.has(name.name)) return markSubtree(node);
|
|
1769
1858
|
wrapAttributes(opening);
|
|
1770
1859
|
}
|
|
1771
1860
|
const children = node.children ?? [];
|
|
@@ -1786,6 +1875,10 @@ function wrapCode(code, file) {
|
|
|
1786
1875
|
if (meaningful.length === 0) return;
|
|
1787
1876
|
const first = meaningful[0];
|
|
1788
1877
|
const last = meaningful[meaningful.length - 1];
|
|
1878
|
+
if (first.type === "JSXExpressionContainer" && meaningful.length > 1) {
|
|
1879
|
+
skip(parent, snippet(children), "starts with an interpolation: wrap the sentence by hand");
|
|
1880
|
+
return;
|
|
1881
|
+
}
|
|
1789
1882
|
let template = "";
|
|
1790
1883
|
let report = "";
|
|
1791
1884
|
for (let i = children.indexOf(first); i <= children.indexOf(last); i++) {
|
|
@@ -1819,6 +1912,7 @@ function wrapCode(code, file) {
|
|
|
1819
1912
|
} else return;
|
|
1820
1913
|
}
|
|
1821
1914
|
if (!LETTER.test(report)) return;
|
|
1915
|
+
if (isIdentifierLike(report)) return;
|
|
1822
1916
|
const start = first.type === "JSXText" ? first.start + leadingWs(first.value) : first.start;
|
|
1823
1917
|
const end = last.type === "JSXText" ? last.end - trailingWs(last.value) : last.end;
|
|
1824
1918
|
s.overwrite(start, end, "{t`" + template + "`}");
|
|
@@ -1838,7 +1932,7 @@ function wrapCode(code, file) {
|
|
|
1838
1932
|
const value = attr.value;
|
|
1839
1933
|
if (value?.type !== "StringLiteral") continue;
|
|
1840
1934
|
const raw = value.value;
|
|
1841
|
-
if (!LETTER.test(raw)) continue;
|
|
1935
|
+
if (!LETTER.test(raw) || isIdentifierLike(raw)) continue;
|
|
1842
1936
|
s.overwrite(value.start, value.end, "{t`" + escapeTemplate(raw) + "`}");
|
|
1843
1937
|
changed = true;
|
|
1844
1938
|
wrapped.push({
|
|
@@ -1870,6 +1964,40 @@ function wrapCode(code, file) {
|
|
|
1870
1964
|
});
|
|
1871
1965
|
}
|
|
1872
1966
|
}
|
|
1967
|
+
function isIdentifierLike(text) {
|
|
1968
|
+
const trimmed = text.trim();
|
|
1969
|
+
if (WHITESPACE.test(trimmed)) return false;
|
|
1970
|
+
const first = [...trimmed].find((char) => LETTER.test(char));
|
|
1971
|
+
return first !== void 0 && first !== first.toUpperCase();
|
|
1972
|
+
}
|
|
1973
|
+
function hasDirective(program, value) {
|
|
1974
|
+
return (program.directives ?? []).some((d) => d.value?.value === value);
|
|
1975
|
+
}
|
|
1976
|
+
function declaresT(program) {
|
|
1977
|
+
let found = false;
|
|
1978
|
+
walk$1(program, (node) => {
|
|
1979
|
+
if (found) return;
|
|
1980
|
+
if (node.type === "ImportDeclaration") {
|
|
1981
|
+
for (const spec of node.specifiers) if (isTName(spec.local?.name)) found = true;
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
if (node.type === "VariableDeclarator" && bindsT(node.id)) found = true;
|
|
1985
|
+
if (node.type === "FunctionDeclaration" && isTName(node.id?.name)) found = true;
|
|
1986
|
+
if (node.params?.some((param) => bindsT(param))) found = true;
|
|
1987
|
+
});
|
|
1988
|
+
return found;
|
|
1989
|
+
}
|
|
1990
|
+
function bindsT(pattern) {
|
|
1991
|
+
if (!pattern) return false;
|
|
1992
|
+
if (pattern.type === "Identifier") return isTName(pattern.name);
|
|
1993
|
+
if (pattern.type === "AssignmentPattern") return bindsT(pattern.left);
|
|
1994
|
+
if (pattern.type === "ObjectPattern") return pattern.properties.some((prop) => prop.type === "RestElement" ? bindsT(prop.argument) : bindsT(prop.value));
|
|
1995
|
+
if (pattern.type === "ArrayPattern") return pattern.elements.some((el) => bindsT(el ?? void 0));
|
|
1996
|
+
return false;
|
|
1997
|
+
}
|
|
1998
|
+
function isTName(name) {
|
|
1999
|
+
return typeof name === "string" && T_NAMES.has(name);
|
|
2000
|
+
}
|
|
1873
2001
|
function hasVerbalyAttr(opening) {
|
|
1874
2002
|
return opening.attributes.some((attr) => {
|
|
1875
2003
|
if (attr.type !== "JSXAttribute") return false;
|
|
@@ -2270,6 +2398,6 @@ function decodeEntities(text) {
|
|
|
2270
2398
|
return text.replace(/"/g, "\"").replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
2271
2399
|
}
|
|
2272
2400
|
//#endregion
|
|
2273
|
-
export { LOCALE_MODULE_PREFIX, MessageRegistry, RESOLVED_VIRTUAL_ID, SOURCE_FILE_RE, check, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, needsIcu, needsRelative, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, wrapProject, writeCatalog, writeDts };
|
|
2401
|
+
export { LOCALE_MODULE_PREFIX, MessageRegistry, RESOLVED_VIRTUAL_ID, SOURCE_FILE_RE, check, clientCatalogs, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, needsIcu, needsRelative, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, wrapProject, writeCatalog, writeDts };
|
|
2274
2402
|
|
|
2275
2403
|
//# sourceMappingURL=index.js.map
|