escodegen

中文文档
2.1.0发布于 2023/06/30BSD-2-ClauseCommonJS近 30 天下载 2.1亿

ECMAScript code generator

原始使用文档
文档目录5 节
  1. Escodegen
  2. Install
  3. Usage
  4. Building browser bundle / minified browser bundle
  5. License

Escodegen

npm version Build Status Dependency Status devDependency Status

Escodegen (escodegen) is an ECMAScript (also popularly known as JavaScript) code generator from Mozilla's Parser API AST. See the online generator for a demo.

Install

Escodegen can be used in a web browser:

<script src="escodegen.browser.js"></script>

escodegen.browser.js can be found in tagged revisions on GitHub.

Or in a Node.js application via npm:

npm install escodegen

Usage

A simple example: the program

escodegen.generate({
    type: 'BinaryExpression',
    operator: '+',
    left: { type: 'Literal', value: 40 },
    right: { type: 'Literal', value: 2 }
});

produces the string '40 + 2'.

See the API page for options. To run the tests, execute npm test in the root directory.

Building browser bundle / minified browser bundle

At first, execute npm install to install the all dev dependencies. After that,

npm run-script build

will generate escodegen.browser.js, which can be used in browser environments.

And,

npm run-script build-min

will generate the minified file escodegen.browser.min.js.

License

Escodegen

Copyright (C) 2012 Yusuke Suzuki (twitter: @Constellation) and other contributors.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

版本与发布记录

escodegen 共发布 61 个版本,首次发布于 2012/03/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2023/06/303 年前
2.0.02020/08/066 年前
1.14.32020/06/236 年前
1.14.22020/06/046 年前
1.14.12020/02/066 年前
1.14.02020/02/066 年前

关于 escodegen 的常见问题

escodegen 是什么?

ECMAScript code generator

如何安装 escodegen?

在项目目录运行 npm install escodegen,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 esgenerate、escodegen,可以用 npx esgenerate 直接运行。

escodegen 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/escodegen 是否存在。

escodegen 支持 ESM 还是 CommonJS?

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

escodegen 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

escodegen 可以商用吗?

escodegen 使用 BSD-2-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

escodegen 的下载量有多少?

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

escodegen 还在维护吗?

最新版本 2.1.0 发布于 2023/06/30(3 年前);至今共发布 61 个版本,其中过去 12 个月发布了 0 个;首次发布于 2012/03/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。