配置参考
Proto Codegen 通过 JSON 配置文件(默认:proto-codegen.config.json)驱动,可用 CLI 参数覆盖。
完整示例
json
{
"input": {
"protoDir": "./proto",
"include": ["*.proto"],
"exclude": ["*_test.proto"],
"protoPath": "./proto"
},
"output": {
"dir": "./generated",
"clientsDir": "clients",
"typesDir": "types",
"clean": true
},
"generation": {
"generateTypes": true,
"generateClients": true,
"postProcess": true,
"generateIndex": true
},
"tsProto": {
"options": {
"onlyTypes": true,
"useOptionals": "all",
"stringEnums": false,
"unrecognizedEnum": false,
"snakeToCamel": false,
"outputIndex": true,
"exportCommonSymbols": false,
"esModuleInterop": true
}
},
"client": {
"httpClientInterface": "HttpClient",
"clientSuffix": "Client",
"imports": {
"typesPath": "../types",
"typesSubDir": "",
"httpClientPath": "./http-client"
}
},
"publish": {
"name": "@scope/package-name",
"description": "包描述",
"registry": "https://registry.npmjs.org",
"access": "public",
"betaTag": "beta",
"defaultRegistry": "https://registry.npmjs.org",
"modules": [
{
"name": "module-name",
"scope": "@scope",
"description": "模块描述",
"registry": "https://registry.npmjs.org",
"access": "public",
"outDir": "./dist/packages/module-name",
"include": ["*.ts"],
"exclude": [],
"buildTarget": "es2020"
}
]
},
"logging": {
"level": "info",
"verbose": false
}
}配置项说明
input
控制读取哪些 proto 文件。
| 选项 | 说明 | 默认值 |
|---|---|---|
protoDir | proto 文件目录 | ./proto |
include | 包含的文件匹配模式 | ["*.proto"] |
exclude | 排除的文件匹配模式 | ["*_test.proto"] |
protoPath | 计算相对路径用的根路径 | ./proto |
output
控制生成代码写到哪里。
| 选项 | 说明 | 默认值 |
|---|---|---|
dir | 输出目录 | ./generated |
clientsDir | 客户端代码子目录 | clients |
typesDir | 类型定义子目录 | types |
clean | 生成前清空输出目录 | true |
generation
控制生成哪些内容。
| 选项 | 说明 | 默认值 |
|---|---|---|
generateTypes | 生成类型定义 | true |
generateClients | 生成 HTTP 客户端 | true |
postProcess | 对生成的类型进行后处理 | true |
generateIndex | 生成入口文件 | true |
tsProto
透传给 ts-proto 插件。完整选项见 ts-proto 文档。
client
控制生成的 HTTP 客户端层。
| 选项 | 说明 | 默认值 |
|---|---|---|
httpClientInterface | HTTP 客户端接口名称 | HttpClient |
clientSuffix | 客户端类名后缀 | Client |
imports.typesPath | 类型导入路径 | ../types |
imports.typesSubDir | 类型子目录 | `` |
imports.httpClientPath | HTTP 客户端导入路径 | ./http-client |
publish
多模块 npm 发布配置。
| 选项 | 说明 |
|---|---|
name | 发布的包名 |
description | 包描述 |
registry | NPM 仓库地址 |
access | 访问级别(public 或 restricted) |
betaTag | beta 版本标签(默认 beta) |
defaultRegistry | 默认仓库 |
modules | 多模块配置,用于发布多个包 |
logging
| 选项 | 说明 | 默认值 |
|---|---|---|
level | 日志级别(debug、info、warn、error) | info |
verbose | 启用详细日志 | false |