@protobufjs/codegen

中文文档
2.0.5发布于 2026/04/28BSD-3-ClauseTypeScriptCommonJS零依赖近 30 天下载 3亿

A minimalistic code generation utility.

原始使用文档

@protobufjs/codegen

npm

A minimalistic code generation utility.

API

  • codegen([functionParams: string[]], [functionName: string]): Codegen
    Begins generating a function.

  • codegen.verbose = false
    When set to true, codegen will log generated code to console. Useful for debugging.

Invoking codegen returns an appender function that appends code to the function's body and returns itself:

  • Codegen(formatString: string, [...formatParams: any]): Codegen
    Appends code to the function's body. The format string can contain placeholders specifying the types of inserted format parameters:

    • %d: Number (integer or floating point value)
    • %f: Floating point value
    • %i: Integer value
    • %j: JSON.stringify'ed value
    • %s: String value
    • %%: Percent sign
  • Codegen([scope: Object.<string,*>]): Function
    Finishes the function and returns it.

  • Codegen.toString([functionNameOverride: string]): string
    Returns the function as a string.

Example

var codegen = require("@protobufjs/codegen");

var add = codegen(["a", "b"], "add") // A function with parameters "a" and "b" named "add"
  ("// awesome comment")             // adds the line to the function's body
  ("return a + b - c + %d", 1)       // replaces %d with 1 and adds the line to the body
  ({ c: 1 });                        // adds "c" with a value of 1 to the function's scope

console.log(add.toString()); // function add(a, b) { return a + b - c + 1 }
console.log(add(1, 2));      // calculates 1 + 2 - 1 + 1 = 3

License: BSD 3-Clause License

版本与发布记录

@protobufjs/codegen 共发布 15 个版本,首次发布于 2016/12/11;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.0.5latest2026/04/286 个月前
2.0.42017/06/109 年前
2.0.32017/04/249 年前
2.0.22017/04/249 年前
2.0.12017/04/239 年前
2.0.02017/04/239 年前

关于 @protobufjs/codegen 的常见问题

@protobufjs/codegen 是什么?

A minimalistic code generation utility.

如何安装 @protobufjs/codegen?

在项目目录运行 npm install @protobufjs/codegen,或使用 pnpm add / yarn add / bun add。

@protobufjs/codegen 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@protobufjs/codegen 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@protobufjs/codegen 可以商用吗?

@protobufjs/codegen 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@protobufjs/codegen 的下载量有多少?

根据 npm 官方下载统计,@protobufjs/codegen 在 2026/10/06 – 2026/11/04 这 30 天内被下载 300,955,076 次,日均约 10,031,836 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@protobufjs/codegen 还在维护吗?

最新版本 2.0.5 发布于 2026/04/28(6 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 1 个;首次发布于 2016/12/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。