Official Prettier plugin adding support for formatting .astro files.
First install Prettier and the plugin:
npm i --save-dev prettier prettier-plugin-astroInstallation with pnpm
pnpm add --save-dev prettier prettier-plugin-astroThen add the plugin to your Prettier configuration:
// .prettierrc.mjs
/** @type {import("prettier").Config} */
export default {
plugins: ['prettier-plugin-astro'],
};For optimal compatibility with the different package managers and Prettier plugins, we recommend manually specifying the parser to use for Astro files in your Prettier config as shown in the example below:
// .prettierrc.mjs
/** @type {import("prettier").Config} */
export default {
plugins: ['prettier-plugin-astro'],
overrides: [
{
files: '*.astro',
options: {
parser: 'astro',
},
},
],
};To customize formatting behavior, see the Configuration section below.
Note > The Astro VS Code extension uses Prettier and this plugin (
prettier-plugin-astro) to format your code. You will only need to install this plugin separately for formatting if:
- You are not using Astro's VS Code extension.
- You want to use features of the Prettier extension that not supported by Astro's own VS Code extension, such as the toolbar panel showing Prettier's status.
Install the VS Code Prettier extension and add the following settings to your VS Code configuration:
{
"prettier.documentSelectors": ["**/*.astro"],
"[astro]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}The settings above ensure that VS Code is aware that Prettier can be used for Astro files, and sets Prettier as the default formatter for Astro files.
When submitting issues about formatting your .astro files in VS Code, please specify which extension you are using to format your files: Astro's own extension or the Prettier extension.
Most options from Prettier will work with the plugin and can be set in a configuration file or through CLI flags.
Set if attributes with the same name as their expression should be formatted to the short form automatically (for example, if enabled <element name={name} /> will become simply <element {name} />)
| Default | CLI Override | API Override |
|---|---|---|
false |
--astro-allow-shorthand <bool> |
astroAllowShorthand: <bool> |
If you are using another tool to format your JavaScript code, like Biome for example, it is possible to skip formatting the frontmatter.
| Default | CLI Override | API Override |
|---|---|---|
false |
--astro-skip-frontmatter <bool> |
astroSkipFrontmatter: <bool> |
Mirror of compressHTML in astro.config.mjs. It tells the formatter which whitespace the compiler will collapse, so that reformatting a file never changes what it renders.
Astro's own default is 'jsx' (since Astro 7.0.0), where a whitespace run containing a newline is dropped and a same-line space is content. Set this to match your project if you have changed compressHTML; true and false are accepted as aliases for 'html' and 'none' so the value can be copied across verbatim.
| Default | CLI Override | API Override |
|---|---|---|
'jsx' |
--astro-compress-html <html|jsx|none> |
astroCompressHTML: "html" | "jsx" | "none" |
{
astroAllowShorthand: false;
astroSkipFrontmatter: false;
astroCompressHTML: 'jsx';
}Pull requests of any size and any skill level are welcome, no contribution is too small. Changes to the Astro Prettier Plugin are subject to Astro Governance and should adhere to the Astro Style Guide.
See CONTRIBUTING.md for instructions on how to set up your development environment.
Astro is free, open source software made possible by these wonderful sponsors.