acorn-jsx

中文文档
5.3.2发布于 2021/07/09MIT零依赖近 30 天下载 5.6亿

Modern, fast React.js JSX parser

原始使用文档
文档目录3 节
  1. Transpiler
  2. Usage
  3. License

Acorn-JSX

Build Status NPM version

This is plugin for Acorn - a tiny, fast JavaScript parser, written completely in JavaScript.

It was created as an experimental alternative, faster React.js JSX parser. Later, it replaced the official parser and these days is used by many prominent development tools.

Transpiler

Please note that this tool only parses source code to JSX AST, which is useful for various language tools and services. If you want to transpile your code to regular ES5-compliant JavaScript with source map, check out Babel and Buble transpilers which use acorn-jsx under the hood.

Usage

Requiring this module provides you with an Acorn plugin that you can use like this:

var acorn = require("acorn");
var jsx = require("acorn-jsx");
acorn.Parser.extend(jsx()).parse("my(<jsx/>, 'code');");

Note that official spec doesn't support mix of XML namespaces and object-style access in tag names (#27) like in <namespace:Object.Property />, so it was deprecated in [email protected]. If you still want to opt-in to support of such constructions, you can pass the following option:

acorn.Parser.extend(jsx({ allowNamespacedObjects: true }))

Also, since most apps use pure React transformer, a new option was introduced that allows to prohibit namespaces completely:

acorn.Parser.extend(jsx({ allowNamespaces: false }))

Note that by default allowNamespaces is enabled for spec compliancy.

License

This plugin is issued under the MIT license.

版本与发布记录

acorn-jsx 共发布 39 个版本,首次发布于 2014/07/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.3.2latest2021/07/095 年前
5.3.12020/09/086 年前
5.3.02020/09/086 年前
5.2.02020/02/266 年前
5.1.02019/10/147 年前
5.0.22019/08/207 年前

关于 acorn-jsx 的常见问题

acorn-jsx 是什么?

Modern, fast React.js JSX parser

如何安装 acorn-jsx?

在项目目录运行 npm install acorn-jsx,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:acorn(^6.0.0 || ^7.0.0 || ^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

acorn-jsx 支持 TypeScript 吗?

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

acorn-jsx 可以商用吗?

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

acorn-jsx 的下载量有多少?

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

acorn-jsx 还在维护吗?

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