vite-plugin-ts-types-to-json
v1.0.0
Published
Vite plugin and CLI to generate JSON field maps from exported TypeScript types and interfaces
Maintainers
Readme
vite-plugin-ts-types-to-json
Vite plugin and CLI that reads TypeScript source files, follows imported DTO types, and generates JSON maps for exported type aliases and interfaces from the input file.
Install
npm install -D vite-plugin-ts-types-to-jsonVite plugin (recommended)
// vite.config.ts
import { defineConfig } from 'vite'
import { tsTypesToJson } from 'vite-plugin-ts-types-to-json'
export default defineConfig({
plugins: [
tsTypesToJson({
entries: [
{
input: 'src/interfaces/auth.interface.ts',
output: 'src/locales/interfaces/auth.fa.json',
},
],
watch: ['src/api/data-contracts.ts'],
}),
],
resolve: {
alias: {
'@': '/absolute/path/to/src',
},
},
})Plugin options
| Option | Description |
| --- | --- |
| entries | { input, output }[] relative to Vite root |
| watch | Extra files that trigger regeneration |
| aliases | Extra path aliases merged with Vite resolve.alias |
| cleanNames | IAuthLoginReq → AuthLoginRequest |
| includeAllInterfaces | Include imported interfaces too |
| buildOnly | Skip generation during dev |
| verbose | Log updated files |
The plugin:
- runs on
devandbuild - uses Vite
resolve.aliasautomatically (@/supported) - watches source files and optional dependency files
CLI
npx vite-plugin-ts-types-to-json src/interfaces/auth.interface.ts \
-o src/locales/interfaces/auth.fa.json \
--alias @/=./srcOutput example
Input:
export type IAuthLoginReq = ILoginRequestDTO
export type IAuthLoginRes = ILoginResponseResultDTO['data']Output:
{
"IAuthLoginReq": {
"branchCode": "branchCode",
"username": "username",
"password": "password"
},
"IAuthLoginRes": {
"access_token": "access_token",
"expires_in": "expires_in"
}
}Behavior
- Only exported type aliases and interfaces from the input file
- Imported files are parsed only to resolve field shapes
- Supports
type Foo = IBarandtype Foo = IBar['data'] - Primitive
datafields (e.g.boolean) are skipped
Development
npm install
npm run build
npm run dev -- src/auth.interface.ts -o output.json --alias @/=./src -vLicense
MIT
