@verbaly/compiler 0.46.0 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +39 -8
- package/dist/cli.js.map +1 -1
- package/dist/config-iLrfRYDB.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +43 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +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":";;;;;AC0EA,SAAgB,cAAc,SAAwB,CAAC,GAAmB;CACxE,MAAM,OAAO,QAAQ,OAAO,QAAQ,QAAQ,IAAI,CAAC;CACjD,MAAM,MAAM,QAAQ,MAAM,OAAO,OAAO,SAAS;CACjD,MAAM,eAAe,OAAO,gBAAgB;CAE5C,MAAM,kBAAkB,OAAO,YAAY,KAAA;CAC3C,MAAM,0BAAU,IAAI,IAAY,CAAC,cAAc,GAAI,OAAO,WAAW,CAAC,CAAE,CAAC;CAEzE,IAAI,CAAC,mBAAmB,WAAW,GAAG,GAC/B;OAAA,MAAM,QAAQ,YAAY,GAAG,GAChC,IAAI,KAAK,SAAS,OAAO,KAAK,CAAC,KAAK,WAAW,GAAG,KAAK,SAAS,cAC9D,QAAQ,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC;CAAA;CAKnC,OAAO;EACL;EACA;EACA;EACA;EAEA,SAAS,OAAO,YAAY,OAAO,SAAS,yBAAyB;EAErE,KAAK,OAAO;EACZ,UAAU,OAAO;EACjB,SAAS,CAAC,GAAG,OAAO;EACpB,SAAS,OAAO,WAAW,CAAC,yDAAyD;EACrF,SAAS,OAAO,WAAW,CAAC,sBAAsB,YAAY;EAC9D,KAAK,OAAO,OAAO,QAAQ,WAAW,QAAQ,MAAM,OAAO,GAAG,IAAI,OAAO;EACzE,WAAW,OAAO,aAAa,CAAC;EAChC,QAAQ,OAAO,UAAU,CAAC;EAC1B,QAAQ,OAAO,UAAU,CAAC;CAC5B;A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,MACA,YAA2B,CAAC,GACH;CAEzB,OAAO,cAAc;EAAE,GAAG,MADD,eAAe,IAAI;EACN,GAAG,YAAY,SAAS;EAAG;CAAK,CAAC;AACzE;AAEA,SAAS,YAAY,QAAsC;CACzD,OAAO,OAAO,YACZ,OAAO,QAAQ,MAAM,CAAC,CAAC,QAAQ,GAAG,WAAW,UAAU,KAAA,CAAS,CAClE;AACF"}
|
|
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 exclude?: string[];\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":";;;;;AC2EA,SAAgB,cAAc,SAAwB,CAAC,GAAmB;CACxE,MAAM,OAAO,QAAQ,OAAO,QAAQ,QAAQ,IAAI,CAAC;CACjD,MAAM,MAAM,QAAQ,MAAM,OAAO,OAAO,SAAS;CACjD,MAAM,eAAe,OAAO,gBAAgB;CAE5C,MAAM,kBAAkB,OAAO,YAAY,KAAA;CAC3C,MAAM,0BAAU,IAAI,IAAY,CAAC,cAAc,GAAI,OAAO,WAAW,CAAC,CAAE,CAAC;CAEzE,IAAI,CAAC,mBAAmB,WAAW,GAAG,GAC/B;OAAA,MAAM,QAAQ,YAAY,GAAG,GAChC,IAAI,KAAK,SAAS,OAAO,KAAK,CAAC,KAAK,WAAW,GAAG,KAAK,SAAS,cAC9D,QAAQ,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC;CAAA;CAKnC,OAAO;EACL;EACA;EACA;EACA;EAEA,SAAS,OAAO,YAAY,OAAO,SAAS,yBAAyB;EAErE,KAAK,OAAO;EACZ,UAAU,OAAO;EACjB,SAAS,CAAC,GAAG,OAAO;EACpB,SAAS,OAAO,WAAW,CAAC,yDAAyD;EACrF,SAAS,OAAO,WAAW,CAAC,sBAAsB,YAAY;EAC9D,KAAK,OAAO,OAAO,QAAQ,WAAW,QAAQ,MAAM,OAAO,GAAG,IAAI,OAAO;EACzE,WAAW,OAAO,aAAa,CAAC;EAChC,QAAQ,OAAO,UAAU,CAAC;EAC1B,QAAQ,OAAO,UAAU,CAAC;CAC5B;AACF;AAEA,MAAM,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AC,GACH;CAEzB,OAAO,cAAc;EAAE,GAAG,MADD,eAAe,IAAI;EACN,GAAG,YAAY,SAAS;EAAG;CAAK,CAAC;AACzE;AAEA,SAAS,YAAY,QAAsC;CACzD,OAAO,OAAO,YACZ,OAAO,QAAQ,MAAM,CAAC,CAAC,QAAQ,GAAG,WAAW,UAAU,KAAA,CAAS,CAClE;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -70,6 +70,7 @@ interface BundleConfig {
|
|
|
70
70
|
}
|
|
71
71
|
interface RenderConfig {
|
|
72
72
|
links?: Record<string, RichLink>;
|
|
73
|
+
exclude?: string[];
|
|
73
74
|
site?: string;
|
|
74
75
|
attribute?: string;
|
|
75
76
|
base?: string;
|
|
@@ -246,7 +247,7 @@ interface CheckResult {
|
|
|
246
247
|
broken: BrokenEntry[];
|
|
247
248
|
}
|
|
248
249
|
declare function check(cfg: ResolvedConfig, catalogs: Catalogs, registry: MessageRegistry): CheckResult;
|
|
249
|
-
declare function formatCheckResult(result: CheckResult): string;
|
|
250
|
+
declare function formatCheckResult(result: CheckResult, root?: string): string;
|
|
250
251
|
declare function formatCheckWarnings(result: CheckResult): string;
|
|
251
252
|
//#endregion
|
|
252
253
|
//#region src/drafts.d.ts
|
|
@@ -330,6 +331,7 @@ interface RenderSiteOptions {
|
|
|
330
331
|
base?: string;
|
|
331
332
|
baseUrl?: string;
|
|
332
333
|
hreflang?: boolean;
|
|
334
|
+
exclude?: string[];
|
|
333
335
|
sitemap?: boolean | string;
|
|
334
336
|
redirect?: boolean | RedirectConfig;
|
|
335
337
|
clean?: boolean;
|
package/dist/index.js
CHANGED
|
@@ -242,6 +242,7 @@ const SKIP_KEYS = /* @__PURE__ */ new Set([
|
|
|
242
242
|
]);
|
|
243
243
|
const DEFAULT_T_NAMES = ["t"];
|
|
244
244
|
const JSX_FILE_RE = /\.(?:[cm]?jsx?|tsx)$/;
|
|
245
|
+
const DEFINE_KEYS = "defineKeys";
|
|
245
246
|
function analyze(code, file, options = {}) {
|
|
246
247
|
const names = new Set(options.tNames ?? DEFAULT_T_NAMES);
|
|
247
248
|
const ast = parse$1(code, {
|
|
@@ -273,8 +274,13 @@ function analyze(code, file, options = {}) {
|
|
|
273
274
|
});
|
|
274
275
|
} else if (node.type === "CallExpression") {
|
|
275
276
|
const callee = node.callee;
|
|
277
|
+
const args = node.arguments;
|
|
278
|
+
if (callee.type === "Identifier" && callee.name === DEFINE_KEYS) {
|
|
279
|
+
collectDeclaredKeys(args[0], file, usedKeys);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
276
282
|
if (!isTReference(callee, names)) return;
|
|
277
|
-
const first =
|
|
283
|
+
const first = args[0];
|
|
278
284
|
if (first?.type === "StringLiteral") usedKeys.push({
|
|
279
285
|
key: first.value,
|
|
280
286
|
file
|
|
@@ -287,6 +293,19 @@ function analyze(code, file, options = {}) {
|
|
|
287
293
|
strayImports
|
|
288
294
|
};
|
|
289
295
|
}
|
|
296
|
+
function collectDeclaredKeys(node, file, out) {
|
|
297
|
+
if (node?.type !== "ObjectExpression") return;
|
|
298
|
+
for (const property of node.properties) {
|
|
299
|
+
if (property.type !== "ObjectProperty") continue;
|
|
300
|
+
const value = property.value;
|
|
301
|
+
if (value.type === "StringLiteral") {
|
|
302
|
+
if (value.value !== "") out.push({
|
|
303
|
+
key: value.value,
|
|
304
|
+
file
|
|
305
|
+
});
|
|
306
|
+
} else if (value.type === "ObjectExpression") collectDeclaredKeys(value, file, out);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
290
309
|
const VERBALY_PACKAGE = /^(?:verbaly|@verbaly\/[\w-]+)$/;
|
|
291
310
|
function collectStrayImports(node, file, out) {
|
|
292
311
|
const source = node.source;
|
|
@@ -294,8 +313,10 @@ function collectStrayImports(node, file, out) {
|
|
|
294
313
|
for (const spec of node.specifiers) {
|
|
295
314
|
if (spec.type !== "ImportSpecifier") continue;
|
|
296
315
|
const imported = spec.imported;
|
|
297
|
-
if (imported.type
|
|
298
|
-
|
|
316
|
+
if (imported.type !== "Identifier") continue;
|
|
317
|
+
const name = imported.name;
|
|
318
|
+
if (name === "t" || name === DEFINE_KEYS) out.push({
|
|
319
|
+
name,
|
|
299
320
|
source: source.value,
|
|
300
321
|
file
|
|
301
322
|
});
|
|
@@ -948,6 +969,11 @@ export function localeFromPath(options) {
|
|
|
948
969
|
return readPath({ ...options, supported: locales });
|
|
949
970
|
}
|
|
950
971
|
|
|
972
|
+
// identity: it exists so the compiler can see keys that live in a module instead of at a call site
|
|
973
|
+
export function defineKeys(keys) {
|
|
974
|
+
return keys;
|
|
975
|
+
}
|
|
976
|
+
|
|
951
977
|
// the whole switch a language control needs, in either mode: catalog or navigation, then persistence
|
|
952
978
|
export function switchLocale(locale, options) {
|
|
953
979
|
return runSwitch(v, locale, { supported: locales, sourceLocale, routing, ...options });
|
|
@@ -1021,6 +1047,9 @@ ${lines.join("\n")}
|
|
|
1021
1047
|
}
|
|
1022
1048
|
export type VerbalyKey = keyof VerbalyMessages & string;
|
|
1023
1049
|
|
|
1050
|
+
export type VerbalyKeyTree = VerbalyKey | { readonly [name: string]: VerbalyKeyTree };
|
|
1051
|
+
export function defineKeys<const T extends VerbalyKeyTree>(keys: T): T;
|
|
1052
|
+
|
|
1024
1053
|
export const verbaly: import('verbaly').Verbaly<VerbalyKey>;
|
|
1025
1054
|
|
|
1026
1055
|
export const sourceLocale: string;
|
|
@@ -1223,7 +1252,8 @@ function check(cfg, catalogs, registry) {
|
|
|
1223
1252
|
broken
|
|
1224
1253
|
};
|
|
1225
1254
|
}
|
|
1226
|
-
function formatCheckResult(result) {
|
|
1255
|
+
function formatCheckResult(result, root) {
|
|
1256
|
+
const show = (file) => root ? relative(root, file).replaceAll("\\", "/") : file;
|
|
1227
1257
|
const lines = [];
|
|
1228
1258
|
if (result.missing.length > 0) {
|
|
1229
1259
|
lines.push("missing translations:");
|
|
@@ -1234,7 +1264,7 @@ function formatCheckResult(result) {
|
|
|
1234
1264
|
}
|
|
1235
1265
|
if (result.unknown.length > 0) {
|
|
1236
1266
|
lines.push("unknown keys (not in any catalog):");
|
|
1237
|
-
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.join(", ")})`);
|
|
1267
|
+
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.map(show).join(", ")})`);
|
|
1238
1268
|
}
|
|
1239
1269
|
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
1240
1270
|
if (errors.length > 0) {
|
|
@@ -1335,7 +1365,7 @@ function runBuildGate(cfg, registry, failOnMissing) {
|
|
|
1335
1365
|
missing: []
|
|
1336
1366
|
} : found;
|
|
1337
1367
|
if (gatePasses(result)) return;
|
|
1338
|
-
throw new Error(`[verbaly] build blocked\n${formatCheckResult(result)}\n${checkNextSteps(result, cliReachable(cfg.root))}`);
|
|
1368
|
+
throw new Error(`[verbaly] build blocked\n${formatCheckResult(result, cfg.root)}\n${checkNextSteps(result, cliReachable(cfg.root))}`);
|
|
1339
1369
|
}
|
|
1340
1370
|
//#endregion
|
|
1341
1371
|
//#region src/drafts.ts
|
|
@@ -2209,6 +2239,10 @@ function injectAlternates(ms, html, alternates) {
|
|
|
2209
2239
|
}
|
|
2210
2240
|
const TITLE_TAG = /<title\b([^>]*)>/i;
|
|
2211
2241
|
const REFRESH_META = /<meta\b[^>]*http-equiv\s*=\s*["']?refresh/i;
|
|
2242
|
+
const NOINDEX_META = /<meta\b[^>]*name\s*=\s*["']?robots["']?[^>]*content\s*=\s*["'][^"']*noindex/i;
|
|
2243
|
+
function isIndexable(html) {
|
|
2244
|
+
return !REFRESH_META.test(html) && !NOINDEX_META.test(html);
|
|
2245
|
+
}
|
|
2212
2246
|
function headIsBound(html, attr) {
|
|
2213
2247
|
if (REFRESH_META.test(html)) return true;
|
|
2214
2248
|
const title = TITLE_TAG.exec(html);
|
|
@@ -2221,6 +2255,8 @@ async function renderSite(cfg, options = {}) {
|
|
|
2221
2255
|
const base = normalizeBase(options.base ?? cfg.render.base);
|
|
2222
2256
|
const baseUrl = (options.baseUrl ?? cfg.render.baseUrl)?.replace(/\/+$/, "");
|
|
2223
2257
|
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
2258
|
+
const patterns = options.exclude ?? cfg.render.exclude ?? [];
|
|
2259
|
+
const excluded = patterns.length > 0 ? picomatch(patterns) : () => false;
|
|
2224
2260
|
const sitemap = options.sitemap ?? cfg.render.sitemap ?? false;
|
|
2225
2261
|
const wantSitemap = sitemap !== false && baseUrl !== void 0;
|
|
2226
2262
|
const clean = options.clean ?? cfg.render.clean ?? false;
|
|
@@ -2254,7 +2290,7 @@ async function renderSite(cfg, options = {}) {
|
|
|
2254
2290
|
const rel = relative(site, file).replace(/\\/g, "/");
|
|
2255
2291
|
if (locales.length > 1 && !headIsBound(html, attribute ?? "data-verbaly")) untranslatedHead.push(rel);
|
|
2256
2292
|
const alternates = wantHreflang ? pageAlternates(baseUrl, rel, locales, cfg.sourceLocale) : [];
|
|
2257
|
-
if (wantHreflang) urls.push({
|
|
2293
|
+
if (wantHreflang && isIndexable(html) && !excluded(rel)) urls.push({
|
|
2258
2294
|
rel,
|
|
2259
2295
|
alternates
|
|
2260
2296
|
});
|