What is prettier-plugin-astro?
The prettier-plugin-astro package is a plugin for Prettier that adds support for formatting Astro files. Astro is a modern static site builder that allows you to use components from various frameworks like React, Vue, and Svelte. This plugin ensures that your Astro files are consistently formatted according to Prettier's rules.
What are prettier-plugin-astro's main functionalities?
Formatting Astro Components
This feature allows you to format Astro components, ensuring that the HTML, JavaScript, and any other embedded code are properly indented and styled according to Prettier's rules.
<!-- Example Astro Component -->
---
const title = 'Hello, World!'
---
<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
</body>
</html>
Formatting Embedded JavaScript
This feature ensures that any JavaScript code embedded within your Astro files is formatted correctly, maintaining consistency and readability.
<!-- Example Astro Component with JS -->
---
const items = [1, 2, 3, 4, 5]
---
<ul>
{items.map(item => (
<li key={item}>{item}</li>
))}
</ul>
Formatting Embedded CSS
This feature ensures that any CSS code embedded within your Astro files is formatted according to Prettier's rules, making your styles consistent and easy to read.
<!-- Example Astro Component with CSS -->
<style>
h1 {
color: blue;
}
</style>
<h1>Hello, World!</h1>
Other packages similar to prettier-plugin-astro
prettier-plugin-svelte
The prettier-plugin-svelte package is a Prettier plugin that adds support for formatting Svelte files. Like prettier-plugin-astro, it ensures that your Svelte components are consistently formatted. However, it is specific to Svelte and does not support the multi-framework nature of Astro.
prettier-plugin-vue
The prettier-plugin-vue package is a Prettier plugin that adds support for formatting Vue.js single-file components (SFCs). It ensures that your Vue components are consistently formatted, but it is specific to Vue.js and does not support the multi-framework nature of Astro.
Official Prettier plugin adding support for formatting .astro
files
Installation
npm i --save-dev prettier-plugin-astro prettier
To customize formatting behavior, see the Configuration section below
Using with the Prettier CLI
When using the CLI, Prettier will automatically pick up the plugin
prettier -w .
pnpm support
Due to an upstream issue in Prettier, the plugin-search-dir
parameter should be set to the current directory when using pnpm or Prettier won't be able to find the plugin automatically
prettier -w --plugin-search-dir=. .
Using in VS Code
First install the VS Code Prettier extension and add the following settings to your VS Code configuration so VS Code is aware that Prettier can be used for Astro files:
{
"prettier.documentSelectors": ["**/*.astro"]
}
Additionally, you should set Prettier as the default formatter for Astro files or VS Code will ask you to choose a formatter everytime you format since the Astro VS Code extension also includes a formatter for Astro files:
{
"[astro]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
When submitting issues about formatting in VS Code, first make sure you're actually using Prettier to format your files and not the Astro VS Code extension included formatter
pnpm support
Due to an upstream issue, Prettier inside VS Code isn't able to automatically infer the right parser to use for Astro files when using pnpm
As such, add the following settings to your .prettierrc.cjs
config file:
module.exports = {
plugins: [require.resolve('prettier-plugin-astro')],
overrides: [
{
files: '*.astro',
options: {
parser: 'astro',
},
},
],
};
The require.resolve
call can alternatively be changed to a direct path, like such: plugins: ["./node_modules/prettier-plugin-astro"]
for usage inside a non-JS config file
Configuration
Most options from Prettier will work with the plugin and can be set in a configuration file or through CLI flags.
Astro Allow Shorthand
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} />
)
Please note that at the time of writing, the shorthand form is not currently supported inside the Astro VS Code extension
Default | CLI Override | API Override |
---|
false | --astro-allow-shorthand <bool> | astroAllowShorthand: <bool> |
Example .prettierrc.cjs
{
astroAllowShorthand: false;
}
Contributing
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 generously supported by Netlify, Storyblok, and several other amazing organizations.
❤️ Sponsor Astro! ❤️