网页
综合
更新时间 2026-10-22 00:58:24
Bun v1.4.4 | Bun Blog
自动蜘蛛
bun.sh
正文约 5927 字
bun fmt is an incredibly fast, Prettier-compatible code formatter that works with your existing Prettier or oxfmt config.
It formats JavaScript, TypeScript, JSX, Flow, CSS, SCSS, Less, HTML, Vue, Svelte, Angular, Lightning Web Components, MJML, JSON, JSONC, JSON5, YAML, TOML, Markdown, MDX, GraphQL and Handlebars. Embedded code is formatted too, like <style> in HTML, code blocks in Markdown and css`...` templates in JavaScript.
In Prettier's own repository (2,616 files, with their config), bun fmt --check takes 60 ms and Prettier takes 17 seconds:
That's 286x faster. On vitest, date-fns, monkeytype and h3, it's 234x to 425x faster than Prettier 3.9.9.
On projects that already use oxfmt, with their own config, it's 3x to 6x faster than oxfmt 0.74.0:
Both tools see the same files, exit 0 and leave the tree unchanged.
Each project is a fresh clone at a pinned commit. The oxfmt projects have no node_modules.
The same runs on one core, and on only vite's JavaScript and TypeScript files ('**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}'):
bun fmt wouldn't exist without Prettier, Biome and Oxc. It's a port of Prettier, and the JavaScript and TypeScript printer is a port of oxc_formatter, which is a port of Biome's formatter.
Thank you to the Prettier, Biome and Oxc teams for years of great work.
Read more in the docs.
--check lists the files that need formatting without writing anything. It exits with code 1 if there are any.
--staged only formats the files you're about to commit and re-stages them, so you don't need lint-staged.
.git/hooks/pre-commit
--changed formats the files that differ from your default branch. Pass a ref to compare against something else.
bun fmt reads .prettierrc*, prettier.config.*, "prettier" in package.json, overrides, .editorconfig and .prettierignore. It also reads .oxfmtrc.json, .oxfmtrc.jsonc and oxfmt.config.ts (or .mts). It accepts the same CLI flags as Prettier or oxfmt, and // prettier-ignore comments work.
Import sorting is built in, so you can leave @trivago/prettier-plugin-sort-imports, @ianvs/prettier-plugin-sort-imports and prettier-plugin-organize-imports in plugins. Languages that need a Prettier plugin, like Astro, fall back to the Prettier installed in your project.
The command is bun fmt, like cargo fmt, go fmt, deno fmt and zig fmt. bun format still runs your format script.
bun fmt --doctor shows how Bun interprets your config and which packages you can uninstall.
All of Prettier's options are supported under the same names. Each one is also available as a flag, like --print-width 100 or --no-semi. bun fmt --init creates the [fmt] section.
sortImports, sortTailwindcss, sortPackageJson and jsdoc are off by default, and don't need a Prettier or oxfmt config. sortImports groups imports, sorts the names in { } and merges duplicate imports from the same module.
In a monorepo, bun fmt walks up from the working directory to the nearest bunfig.toml with a [fmt] section. Without a Prettier config, bun fmt also formats TOML. It skips *.min.js, *.min.css and lockfiles unless you pass them explicitly.
bun fmt does the work of prettier-plugin-tailwindcss, prettier-plugin-packagejson and prettier-plugin-svelte, so you don't need them installed. You can leave them in plugins, and their options are supported.
Tailwind classes are sorted with your project's tailwindcss, version 3 or 4. If it isn't installed, they're left as they are.
We compared the output against Prettier 3.9.9 with version 0.8.1 of the plugin, on Tailwind 4.3.3 and 3.4.19.
About half of the differences are intentional. In className={`header${open ? " active" : ""}`}, the plugin trims " active" to "active", so the element ends up with the class headeractive. bun fmt keeps the space.
For package.json, keys are sorted in the same order as the plugin and values are left untouched. Of 13,670 real-world package.json files, 13,644 (99.8%) are byte-for-byte identical to the output of Prettier 3.9.9 with the plugin. 20 differ only in values that Bun preserves, and the plugin throws on the other 6.
.svelte files are formatted by default, and the output matches prettier-plugin-svelte 4.1.1. With a Prettier config, they're formatted if the config lists the plugin. To skip them, set svelte = false in [fmt] or pass --no-svelte.
If your config uses a plugin that bun fmt doesn't support, like prettier-plugin-jsdoc, it prints an error naming the plugin, skips the files the plugin applies to and exits with code 2.
Prettier's output changes the meaning of the code for these inputs. bun fmt preserves it.
bun fmt --debug-check re-parses each file after formatting and fails if the meaning changed, like Prettier's --debug-check.
Bun's CI runs Prettier 3.9.9's own formatting tests on every commit. A test passes when the output is byte-for-byte identical to Prettier's.
64 of the 83 differences are one intentional case: Prettier drops trailing blank lines from a YAML block scalar, which changes the string.
Another 90 tests are skipped. 66 of them use syntax proposals at stage 2 or below that only Babel parses, and 13 need a Prettier plugin.
With an oxfmt config, bun fmt matches oxfmt 0.72's output on 1,343 of oxfmt's 1,498 fixtures (89.7%).
On 330 open-source repositories, we ran each project's format command with its own formatter and with bun fmt. A run only counts as identical if every file is byte-for-byte the same. Next.js counts as a mismatch: 26,334 files are identical, but bun fmt rejects one file that uses a Babel-only proposal.
Most of the Prettier mismatches come from an older version of Prettier, Prettier plugins or Markdown files.
Cold runs with 8 threads on a Linux x64 machine, best of 3. Both versions report the same number of errors on each project.
Peak memory on MUI's docs drops from 11.2 GB to 1.3 GB. The other four use up to 0.3 GB more.
Of 76 other repositories, 69 are more than 10% faster. Bun's own src/js is slower: 0.20 s, up from 0.11 s.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。