UNPKG

vite-plugin-vue-i18n-typescript

Version:

Type-safe Vue i18n translations. Auto-generates TypeScript types from JSON locale files. Catch translation errors at compile time with full IDE autocomplete.

145 lines (141 loc) 4.5 kB
import './vite-env-override-components.js'; import { LogLevel, LoggerOptions, LogOptions, LogErrorOptions, LogType, Logger } from 'vite'; /** * Create a simple console logger compatible with Vite's Logger interface */ declare function createConsoleLogger(debugEnabled?: boolean, prefix?: string): Logger; declare function createColoredLogger(level?: LogLevel | 'debug', options?: LoggerOptions): { debug: (msg: string, options?: LogOptions) => void; info: (msg: string, options?: LogOptions) => void; warn: (msg: string, options?: LogOptions) => void; error: (msg: string, options?: LogErrorOptions) => void; warnOnce: (msg: string, options?: LogOptions) => void; clearScreen: (type: LogType) => void; hasErrorLogged: (error: Error) => boolean; hasWarned: boolean; }; /** * Options for the plugin. * * This plugin generates TypeScript definitions from JSON locale files, * providing type-safe i18n keys for your Vue application. */ interface VirtualKeysDtsOptions { root?: string; fileBatchSize?: number; include?: string | string[]; exclude?: string | string[]; getLocaleFromPath?: (absFilePath: string, root: string) => string | null; merge?: "deep" | "shallow"; transformJson?: (json: unknown, absFilePath: string) => unknown; debug?: boolean; /** * The virtual module ID for the generated locale module. * Usually you don't need to change this. * @default "virtual:vue-i18n-types" */ sourceId?: string; /** * Path for the TypeScript type definitions file (.d.ts). * * @default "./src/vite-env-override.d.ts" * @example "src/types/i18n.types.d.ts" */ typesPath?: string; /** * Optional banner comment at the top of the generated file. */ banner?: string; /** * Base locale to use for generating TypeScript key paths. * The plugin will introspect this locale's messages to generate the type definitions. * * @default "de" */ baseLocale?: string; /** * Path for the virtual module file (.ts). * If specified, the virtual module will be generated as a physical file. * This can be useful for debugging or when you want to see the actual locale data. * * @default undefined (virtual module only served dynamically) * @example "src/i18n/i18n.virtual.gen.ts" */ virtualFilePath?: string; } /** * JSON types for message structure */ type JSONValue = string | number | boolean | null | JSONObject | JSONArray; interface JSONObject { [key: string]: JSONValue; } type JSONArray = JSONValue[]; type CustomLogger = ReturnType<typeof createColoredLogger>; /** * Options for standalone type generation */ interface GenerateTypesOptions extends VirtualKeysDtsOptions { /** * Root directory for the project (defaults to current working directory) */ root?: string; /** * Enable verbose logging */ verbose?: boolean; } /** * Result of type generation */ interface GenerateTypesResult { /** * Number of files written */ filesWritten: number; /** * Total files that were checked */ totalFiles: number; /** * List of generated file paths (relative to root) */ generatedFiles: string[]; /** * Performance metrics */ durations: { content: number; write: number; total: number; }; /** * Detected locales */ locales: string[]; /** * Number of locale files processed */ localeFilesCount: number; /** * Absolute paths of all locale files that were processed */ localeFiles: string[]; } interface GenerationOptions extends Omit<VirtualKeysDtsOptions, 'exclude' | 'extends'> { root: string; sourceId: string; typesPath: string; getLocaleFromPath: (absPath: string, root: string) => string | null; baseLocale: string; merge: "deep" | "shallow"; virtualFilePath?: string; mergeFunction: (a: any, b: any) => any; include: string[]; exclude: string[]; debug: boolean; transformJson?: (json: unknown, absPath: string) => unknown; verbose: boolean; logger: CustomLogger; } export { type CustomLogger as C, type GenerationOptions as G, type JSONObject as J, type VirtualKeysDtsOptions as V, type JSONValue as a, type JSONArray as b, type GenerateTypesOptions as c, type GenerateTypesResult as d, createColoredLogger as e, createConsoleLogger as f };