网页 综合 更新时间 2026-11-14 05:15:08

Formatter | Bun Docs

自动蜘蛛 bun.sh 正文约 4800 字
Formatter | Bun Docs
Bun's fast, built-in formatter for JavaScript, TypeScript, CSS, HTML, JSON, YAML, Markdown, and more
Bun ships with a fast, built-in code formatter. There's nothing to install and nothing to configure.
bun fmt rewrites the files in the current directory that need it and prints their names. It skips node_modules, anything in your .gitignore or .git/info/exclude, minified files (*.min.js, *.min.css) and lock files such as package-lock.json.
To format specific files, pass paths or globs.
Embedded code is formatted too: <script> and <style> in HTML, Vue, and Svelte, code blocks in Markdown, and css`...`, html`...`, and graphql`...` templates in JavaScript.
A syntax error inside a <script> or <style> of a Svelte component is a warning. That block stays as written, and the rest of the file is formatted.
--check lists the files that need formatting and writes nothing.
The exit code is 1 if a file needs formatting, 0 if none do, and 2 if something went wrong, like a syntax error. That makes it the command for CI.
Options live in bunfig.toml. To create the section, run bun fmt --init.
All of Prettier's options work, under the same names. Each one is also a flag, like --print-width 100 or --no-semi.
bun fmt reads indent_style, indent_size, tab_width, max_line_length, and end_of_line from .editorconfig. bunfig.toml overrides them.
Every .gitignore in the repository counts, whichever directory you run bun fmt from. Outside a repository, those in the current directory and below it count. With a Prettier config, only the .gitignore in the current directory does, because that's the only one Prettier reads.
To skip more than .gitignore does, list the files in bunfig.toml. A .prettierignore file works too.
bun fmt uses Prettier's comment, so existing ones keep working. It applies to the next statement, element, or rule.
In JSX it's {/* prettier-ignore */}, in CSS /* prettier-ignore */, in HTML and Markdown <!-- prettier-ignore -->, and in YAML and GraphQL # prettier-ignore.
To pick your own groups, use a table. See format.sortImports.
Classes are sorted in Tailwind's recommended order, in class and className and in calls to clsx, cn, cva, twMerge, and a few more. They are sorted with your project's tailwindcss, version 3 or 4, and are left as they are if it isn't installed.
If your .prettierrc lists prettier-plugin-tailwindcss, sorting is already on, with that plugin's options and defaults.
To name your own functions or your stylesheet, use a table.
Keys go in the conventional order: name, version, description, and so on.
If your .prettierrc lists prettier-plugin-packagejson, sorting is already on and uses that plugin's order.
.svelte files are formatted by default, like prettier-plugin-svelte formats them. To change how, use a table. These are the defaults:
To leave .svelte files alone, turn it off.
Pipe text through bun fmt. It prints the result and writes nothing. The path picks the language and the options. The file doesn't have to exist.
No path to give? Name the parser instead. Your config still applies, except for what goes by a file name: overrides, .editorconfig, and ignore files.
--staged formats only the files you're about to commit and stages them again, so you don't need lint-staged.
If a file has both staged and unstaged changes, Bun looks at the staged version, which is what the commit gets. It doesn't write such a file, because staging it again would commit the rest. If the staged version needs formatting, Bun names the file and exits with code 2.
Config files, such as bunfig.toml and .prettierrc, are read from disk.
--changed formats the files that differ from your default branch. Pass a ref to compare with something else.
Already have a .prettierrc or an .oxfmtrc.json? bun fmt reads it and prints what that tool prints. Change the script and you're done.
If package.json has a fmt script, bun fmt runs that script. Inside the script, bun fmt is the formatter. If package.json has "fmt" in it but can't be parsed, as in the middle of a merge, bun fmt can't tell whether that is a script: it formats nothing and exits with code 2.
Writing a tool that calls Bun in projects you don't control? Pass --no-script: bun fmt --no-script is always the formatter.
Astro isn't built in. If your .prettierrc lists its plugin, Bun sends those files to your installed Prettier. To see which files those are, and which packages you can uninstall:
See the guides: Migrate from Prettier and Migrate from oxfmt.
Write nothing. Exit with code 1 if a file needs formatting
Like --check, but print only the file names
Format standard input as this file and print the result
Add a [fmt] section to bunfig.toml
Show what Bun does with your Prettier or oxfmt config, and which packages you can remove
Run the formatter even if the project has a fmt script
访问原文 返回搜索 https://bun.sh/docs/format
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。