vite-plugin-vue-i18n-types
Version:
Type-safe Vue i18n translations. Auto-generates TypeScript types from JSON locale files. Catch translation errors at compile time with full IDE autocomplete.
136 lines (133 loc) • 3.77 kB
TypeScript
/**
* Options for the unplugin-vue-i18n-dts-generation Vite plugin.
*
* This plugin generates TypeScript definitions from JSON locale files,
* providing type-safe i18n keys for your Vue application.
*/
type VirtualKeysDtsOptions = {
include?: string | string[];
exclude?: string | string[];
getLocaleFromPath?: (absFilePath: string, root: string) => string | null;
merge?: "deep" | "shallow";
transformJson?: (json: unknown, absFilePath: string) => unknown;
devUrlPath?: string;
emit?: {
/**
* Flag to inline locale data file in the build output.
* @default false
*/
inlineDataInBuild?: boolean;
/**
* Flag to emit the combined messages JSON file during build.
* When enabled, writes messages.json alongside your bundle.
* @default false
*/
emitJson?: boolean;
};
debug?: boolean;
/**
* The virtual module ID for the generated locale module.
* Usually you don't need to change this.
* @default "virtual:unplug-i18n-dts-generation"
*/
sourceId?: string;
/**
* Path for the TypeScript type definitions file (.d.ts).
*
* @default "./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;
};
/**
* 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[];
}
/**
* Generate TypeScript definitions from Vue i18n locale files
*
* @param options - Configuration options for generation
* @returns Promise with generation result
*
* @example
* ```typescript
* import { generateI18nTypes } from 'unplugin-vue-i18n-dts-generation/api'
*
* const result = await generateI18nTypes({
* baseLocale: 'en',
* include: ['src/locales/*.json'],
* typesPath: 'src/i18n/i18n.gen.d.ts',
* verbose: true
* })
*
* console.log(`Generated ${result.filesWritten} files for ${result.locales.length} locales`)
* ```
*/
declare function generateI18nTypes(options?: GenerateTypesOptions): Promise<GenerateTypesResult>;
export { type GenerateTypesOptions, type GenerateTypesResult, generateI18nTypes };