Setting up Prettier
Quickly configure Prettier for your Astro project!
Prettier Plugin for Astro
Section titled “Prettier Plugin for Astro”Start by installing prettier & prettier-plugin-astro using your preferred package manager:
npm install --save-dev prettier prettier-plugin-astropnpm add -D prettier prettier-plugin-astroyarn add --dev prettier prettier-plugin-astroNext, update your Prettier config in the project’s root to include prettier-plugin-astro plugin. This example will use .prettierrc.json:
{ "plugins": ["prettier-plugin-astro"], "overrides": [ { "files": "*.astro", "options": { "parser": "astro" } } ]}That’s it. Run prettier --write . command with your preferred package manager to format all of the astro files in the project.
Additionally if you are using the VSCode Prettier Extension, you can set the following VSCode Config in your User/Workspace Settings:
{ "prettier.documentSelectors": ["**/*.astro"], "[astro]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }}and now Prettier will format any selected document.
Adding an NPM script
Section titled “Adding an NPM script”You can use Prettier CLI with your preferred package manager by adding following scripts in package.json of your project’s directory:
{ "scripts": { "format:check": "prettier --check .", "format:write": "prettier --write ." }}Prettier Plugin for Tailwind CSS
Section titled “Prettier Plugin for Tailwind CSS”When using the Tailwind CSS integration, there is another dev dependency for automated class sorting: prettier-plugin-tailwindcss.
Start by installing prettier-plugin-tailwindcss using your preferred package manager:
npm install --save-dev prettier-plugin-tailwindcsspnpm add -D prettier-plugin-tailwindcssyarn add --dev prettier-plugin-tailwindcssNext, update the Prettier Config to include following plugins. This example will use .prettierrc:
{ "plugins": ["prettier-plugin-astro"], "plugins": ["prettier-plugin-astro", "prettier-plugin-tailwindcss"], "overrides": [ { "files": "*.astro", "options": { "parser": "astro", } } ]}Make sure to place the Tailwind CSS plugin at the end. Additionally, you might need to reload your VSCode Window if Prettier is not working as expected.