我们很高兴地宣布 Oxfmt 的 alpha 版本发布,这是一款由 Rust 驱动、兼容 Prettier 的代码格式化工具。首个版本主要支持 JavaScript 和 TypeScript,更多语言支持将很快推出。
🌐 We’re excited to announce the alpha release of Oxfmt, a Rust-powered, Prettier-compatible code formatter. This first release focuses on JavaScript and TypeScript, with support for additional languages coming soon.
Oxfmt 是基于以下目标设计的:
🌐 Oxfmt is designed with these goals in mind:
- 性能: 在首次运行且无缓存的情况下,比 Prettier 快 30 倍以上,比 Biome 快 3 倍以上(基准测试)。
- 兼容性: 更美观兼容,因此你可以在现有项目中轻松采用 Oxfmt。
- 开发者体验: 即将推出的功能包括导入排序、扩展的格式化选项以及对 Prettier 插件的支持。
快速开始
🌐 Quick Start
将 oxfmt 添加到你的项目中:
🌐 Add oxfmt to your project:
npm add -D oxfmt@latestpnpm add -D oxfmt@latestyarn add -D oxfmt@latestbun add -D oxfmt@latestdeno add -D npm:oxfmt@latestOxfmt 遵循 Prettier 的配置格式。如果你使用带有 JSON 配置文件的 Prettier,你可以将其重命名为 .oxfmtrc.jsonc:
🌐 Oxfmt follows Prettier’s configuration format. If you are using Prettier with a JSON configuration file, you can rename it to .oxfmtrc.jsonc:
cp .prettierrc.json .oxfmtrc.jsonc你也可以从这个 .oxfmtrc.jsonc 配置示例开始:
🌐 You can also start from this .oxfmtrc.jsonc configuration example:
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
// Use 80 if migrating from Prettier; 100 is the Oxfmt default!
"printWidth": 80,
"ignorePatterns": [] // Same as `.prettierignore`
}接下来,将 oxfmt 添加到你的 package.json 脚本中:
🌐 Next, add oxfmt to your package.json scripts:
"scripts": {
"format": "oxfmt"
}或者,请参阅安装指南获取详细说明。
🌐 Alternatively, refer to the installation guide for detailed instructions.
性能
🌐 Performance
Oxfmt 非常快。我们在 Outline 仓库上的基准测试结果显示:
🌐 Oxfmt is incredibly fast. Our benchmark results on the Outline repository show:
- 比 Prettier 的实验性 CLI(无缓存)快 30 倍以上
- 通过 Oxc 的解析器使用
@prettier/plugin-oxc,速度比 Prettier 快 20 倍以上 - 比 Biome(另一个基于 Rust 的格式化器)快 3 倍以上
有关详细的基准测试设置,请参阅以下仓库:
🌐 For detailed benchmark setup, please refer to the following repository:
oxc-project/bench-formatter
https://github.com/oxc-project/bench-formatter
设计
🌐 Design
Oxc 团队优先考虑与现有生态系统的兼容性,即使是大规模代码库的迁移也能轻松完成。
🌐 The Oxc team prioritizes compatibility with existing ecosystems, making migrations straightforward, even for large codebases.
代码格式化结果
🌐 Code formatting results
Oxfmt 的格式化方式与 Prettier 的 JavaScript 格式化一致。如果你今天迁移到 Oxfmt,相比 Prettier 不应该看到任何格式上的差异。
🌐 Oxfmt matches Prettier’s JavaScript formatting. If you are migrating to Oxfmt today, you shouldn’t see any formatting differences compared to Prettier.
在从旧版本的 Prettier 迁移时,你可能会看到一些细微的差异(查看迁移示例),因为我们发现了 Prettier 格式化中可以改进的地方。我们一直在积极与 Prettier 团队合作,通过直接向 Prettier 提交错误报告和拉取请求。许多这些改进已在最近的 Prettier 3.7 版本中实现。
🌐 You might see minor differences when migrating from older versions of Prettier (See an example migration) because we identified areas in which Prettier’s formatting could be improved. We have been actively collaborating with the Prettier team by submitting bug reports and pull requests directly to Prettier. Many of these improvements landed in the recent Prettier 3.7 release.
Oxfmt 目前通过了 Prettier 大约 95% 的 JavaScript 和 TypeScript 测试,我们希望能与 Prettier 团队合作,随着时间的推移在格式上趋于一致。
🌐 Oxfmt currently passes around 95% of Prettier’s JavaScript and TypeScript tests, and we hope to work with the Prettier team to converge on formatting over time.
printWidth: 100 默认
🌐 printWidth: 100 by default
我们选择 printWidth: 100 作为默认的行长度,而不是 Prettier 的 80。我们的原因包括:
🌐 We chose printWidth: 100 as the default line length instead of Prettier's 80. Our reasons include:
- 由于类型注解,TypeScript 代码往往比 JavaScript 代码更长。
- 导入语句中通常包含许多项目。
- 更大的屏幕提供更多的水平空间。
- 导致 LLM 令牌数量略少。
虽然 Oxfmt 仍然兼容 Prettier,但它使用不同的默认打印宽度——100 个字符。如果你想在从 Prettier 迁移时避免出现大差异,请明确将打印宽度设置为 80。
🌐 While Oxfmt remains compatible with Prettier, it uses a different default print width of 100 characters. If you want to avoid large diffs when migrating from Prettier, explicitly set the print width to 80.
ignorePatterns
虽然 Oxfmt 支持 .prettierignore,它也支持 ignorePatterns 配置选项,将所有配置合并到一个文件中。
🌐 While Oxfmt supports .prettierignore, it also supports an ignorePatterns configuration option to consolidate all configuration into a single file.
配置
🌐 Configuration
Prettier 的 JSON 配置文件与Oxfmt兼容。在最简单的情况下,迁移配置的过程如下:
🌐 Prettier JSON configuration files are compatible with Oxfmt. In the simplest case, migrating your config looks like this:
cp .prettierrc.json .oxfmtrc.jsonc如果你的编辑器支持 JSON 语言服务器,在将 oxfmt 添加到你的 devDependencies 之后,你可以从这个最小模板开始:
🌐 If your editor supports the JSON language server, you can start with this minimal template after adding oxfmt to your devDependencies:
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
"ignorePatterns": []
}虽然在这个 alpha 版本中我们尚未支持 Prettier 的所有配置选项,但我们确实支持以下主要选项:printWidth、tabWidth、useTabs、semi、singleQuote、quoteProps、jsxSingleQuote、trailingComma、bracketSpacing、objectWrap、bracketSameLine、arrowParens、endOfLine 和 singleAttributePerLine。
🌐 While we don’t yet support all of Prettier’s configuration options in this alpha release, we do support the following major options: printWidth, tabWidth, useTabs, semi, singleQuote, quoteProps, jsxSingleQuote, trailingComma, bracketSpacing, objectWrap, bracketSameLine, arrowParens, endOfLine, and singleAttributePerLine.
查看我们文档中的所有选项。
🌐 Check out all of the options in our documentation.
终端输出
🌐 Terminal Output
Oxfmt 的默认行为等同于 prettier . --write,提供与 cargo fmt 相同的用户体验,并且不会产生任何输出。你可以使用 --check 显示格式差异,并在 CI 流水线中强制使用 oxfmt。
🌐 Oxfmt's default behavior is equivalent to prettier . --write, providing the same UX as cargo fmt and producing no output. You can use --check to display formatting differences and enforce oxfmt usage in CI pipelines.
Beta 版本发布计划
🌐 Beta Release Plans
以下是我们对测试版发布的计划:
🌐 Here are our plans for the beta release:
- 支持更多文件格式 - 比如
.json文件 - 添加对嵌入式语言格式的支持 - JS 中的 CSS 或 JS 中的 GraphQL
- 内置排序和美化功能,例如 排序导入
- Prettier 插件
- Oxfmt 的 Node.js API
- 禁用文件末尾换行
--migrate prettier- … 以及你的功能请求
你可以在这里跟踪我们向测试版发布的进展:
🌐 You can track our progress towards the beta release here:
Formatter Beta ·里程碑#15·OXC-PROJECT/OXC
https://github.com/oxc-project/oxc/milestone/15
我们也计划在未来的版本中放宽一些格式上的要求。
🌐 We also plan to relax some of the formatting opinions in future versions.
下一步
🌐 Next Steps
请参阅 Oxfmt 文档 中的完整安装指南。
🌐 See the full installation guide in the Oxfmt docs.
报告问题
🌐 Reporting Issues
有关格式差异,请参阅 https://github.com/oxc-project/oxc/discussions/14669。此外,已知问题可通过 标签 区分。
🌐 For formatting differences, please refer to https://github.com/oxc-project/oxc/discussions/14669. Additionally, known issues are distinguished by labels.
如果你发现其他问题,请在 GitHub 上使用专用模板创建一个问题。
🌐 If you find any other issues, please create an issue with dedicated template on GitHub.
加入社区
🌐 Join the Community
RFC: 格式化器 · oxc-project/oxc · 讨论 #13608
https://github.com/oxc-project/oxc/discussions/13608
我们欢迎你的反馈,以帮助Oxfmt变得更好!
🌐 We welcome your feedback to help make Oxfmt even better!
致谢
🌐 Acknowledgements
Oxfmt 建立在 biome_formatter 基础设施的一个分支上,我们要感谢 Biome 团队,特别是 @ematipico 和 @MichaReiser。我们还要感谢 Prettier 团队以及 @fisker 与我们在格式兼容性方面的合作。
🌐 Oxfmt builds on a fork of the biome_formatter infrastructure, and we’d like to thank the Biome team, especially @ematipico and @MichaReiser. We’d also like to thank the Prettier team and @fisker for collaborating with us on formatting compatibility.
