A Vite plugin that formats output files with Prettier after bundle generation.
This plugin is particularly useful when you're building a library with Vite in library mode.
With this plugin, you can ensure that all your library's distribution files maintain consistent and clean formatting according to your Prettier standards.
This plugin hooks into Vite's build process and executes at the closeBundle phase (after bundle generation). It will:
- Find all files in the output directory (specified by build.outDir in your Vite config).
- Read each file's content.
- Format the content using Prettier with your existing configuration.
- Write the formatted content back to the file.
You'll first need to install Vite and Prettier:
npm install --save-dev vite prettier
Then install vite-plugin-prettier-format
:
npm install --save-dev vite-plugin-prettier-format
Add vite-plugin-prettier-format
to your Vite config file:
// vite.config.js
import { defineConfig } from 'vite'
import prettierFormat from 'vite-plugin-prettier-format'
export default defineConfig({
plugins: [prettierFormat()],
})
The plugin uses your project's Prettier configuration automatically. It attempts to resolve the Prettier config for each file using Prettier's built-in resolveConfig function.
Make sure you have a Prettier configuration file in your project (e.g., .prettierrc
, .prettierrc.js
, etc.).
MIT © Azat S.