Skip to content

配置参考 ​

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 文件。

选项说明默认值
protoDirproto 文件目录./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 客户端层。

选项说明默认值
httpClientInterfaceHTTP 客户端接口名称HttpClient
clientSuffix客户端类名后缀Client
imports.typesPath类型导入路径../types
imports.typesSubDir类型子目录``
imports.httpClientPathHTTP 客户端导入路径./http-client

publish ​

多模块 npm 发布配置。

选项说明
name发布的包名
description包描述
registryNPM 仓库地址
access访问级别(public 或 restricted)
betaTagbeta 版本标签(默认 beta)
defaultRegistry默认仓库
modules多模块配置,用于发布多个包

logging ​

选项说明默认值
level日志级别(debug、info、warn、error)info
verbose启用详细日志false