To set up code formatting in your Vite project, I'll recommend using Prettier, a popular, opinionated code formatter that works well with vanilla JavaScript projects. It's easy to integrate and ensures consistent code style across your team.
Here's a step-by-step guide to get you started. This setup is framework-agnostic and should work seamlessly with your Vite project.
✅ 1. Install Prettier
First, install Prettier as a development dependency:
npm install --save-dev prettier
This adds Prettier to your devDependencies in package.json.
✅ 2. Create a Prettier Configuration File
Create a file named .prettierrc in the root of your project. This file defines the formatting rules. Here's a basic example:
// .prettierrc
{
"singleQuote": true, // Use single quotes instead of double quotes
"semi": true, // Add semicolons at the end of statements
"tabWidth": 2, // Use 2 spaces for indentation
"trailingComma": "es5", // Add trailing commas where possible
"printWidth": 80 // Wrap lines at 80 characters
}
- You can customize these rules based on your preferences. Check the Prettier docs for more options.
- If you prefer a JavaScript config, you can use
.prettierrc.js instead:
// .prettierrc.js
module.exports = {
singleQuote: true,
semi: true,
tabWidth: 2,
trailingComma: 'es5',
printWidth: 80,
};
✅ 3. Add a Format Script to package.json
Update your package.json to include a script for running Prettier. This allows you to format your code with a simple command.
Open package.json and add (or modify) the scripts section:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"format": "prettier --write 'src/**/*.{js,html,css}'" // Format JS, HTML, and CSS files in the src folder
}
}
- Explanation:
prettier --write: This command formats files in place.
'src/**/*.{js,html,css}': This glob pattern tells Prettier to format all .js, .html, and .css files in the src directory (and subdirectories). Adjust the pattern if your files are in a different location.
Now you can run the formatter with:
npm run format
This will automatically format all specified files.
✅ 4. Set Up Auto-Formatting in Your Editor
To make formatting automatic, integrate Prettier with your code editor. This way, code is formatted as you save files.
-
VS Code (Recommended):
- Install the "Prettier - Code formatter" extension from the VS Code marketplace.
- Open your VS Code settings (File > Preferences > Settings or
Ctrl + ,).
- Search for "Format On Save" and enable it:
- Restart VS Code.
-
Other Editors:
- Visual Studio: Use the Prettier extension or configure it via plugins.
- WebStorm/IntelliJ: Enable Prettier in Settings > Languages & Frameworks > JavaScript > Prettier.
- Vim/Neovim: Install a plugin like
vim-prettier and configure it to run on save.
- Sublime Text: Use the Prettier plugin and set up a save hook.
With this, your code will be formatted automatically whenever you save a file.
✅ 5. Optional: Integrate with ESLint for Linting and Formatting
If you want more than just formatting (e.g., catching errors and enforcing code style), combine Prettier with ESLint. ESLint can lint your code, and you can make it use Prettier for formatting.
-
Install ESLint and Prettier Integration:
npm install --save-dev eslint eslint-config-prettier
-
Create an ESLint Configuration File:
Create a file named .eslintrc.json in your project root:
// .eslintrc.json
{
"extends": [
"eslint:recommended", // Use ESLint's recommended rules
"prettier" // Integrate with Prettier to avoid conflicts
],
"env": {
"browser": true, // Enable browser globals
"es2021": true // Support modern JavaScript
},
"rules": {
// Add custom rules here if needed
}
}
- The
"prettier" extension ensures ESLint doesn't conflict with Prettier's formatting rules.
-
Update the Format Script:
Modify your package.json script to run both ESLint and Prettier:
"scripts": {
"format": "prettier --write 'src/**/*.{js,html,css}' && eslint --fix 'src/**/*.{js,ts}'"
}
eslint --fix: Automatically fixes fixable ESLint issues.
-
Run ESLint:
-
Auto-Fix on Commit (with Husky):
To enforce formatting before commits, use Husky and lint-staged. This runs formatters only on changed files.
-
Install Husky and lint-staged:
npm install --save-dev husky lint-staged
-
Add Husky and lint-staged configs to package.json:
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,html,css}": [
"prettier --write",
"eslint --fix"
]
}
}
-
Now, when you commit changes, Prettier and ESLint will automatically format and lint the affected files.
✅ 6. Run and Test
🎉 Summary
- Basic Setup: Install Prettier, create a
.prettierrc file, and add a format script to package.json.
- Auto-Formatting: Integrate with your editor for on-save formatting.
- Advanced: Add ESLint for linting and use Husky for pre-commit hooks to enforce rules.
- Why This Works with Vite: Vite doesn't handle formatting itself, but this setup is lightweight and runs independently. It won't interfere with Vite's build process.
If you encounter any issues (e.g., with specific file types or conflicts), feel free to share more details! If you're using TypeScript, I can adjust this for .ts files.