乐闻世界logo
搜索文章和话题

How does Prettier's caching mechanism work and how to use cache to improve performance?

2月21日 16:56

Prettier Caching Mechanism Explained

Prettier 2.0+ introduced a built-in caching mechanism that can significantly improve performance for repeated formatting, especially in large projects.

Caching Mechanism Principle

Prettier's cache is based on hash values of file content and configuration:

  1. First Formatting: Parse and format the file, store result in cache
  2. Subsequent Formatting: Compare hash values of file content and configuration
  3. Cache Hit: If hash values are same, use cached result directly
  4. Cache Miss: Reformat file and update cache

Using Cache

Basic Usage:

bash
# Enable cache formatting prettier --write --cache "**/*.js" # Specify cache directory prettier --write --cache --cache-location .prettier-cache "**/*.js"

Cache Strategy:

bash
# Content-based cache (default) prettier --write --cache --cache-strategy content "**/*.js" # Metadata-based cache prettier --write --cache --cache-strategy metadata "**/*.js"

Cache Configuration Options

1. cache-location Specify cache file storage location:

bash
# Default location prettier --write --cache "**/*.js" # Custom location prettier --write --cache --cache-location .my-cache "**/*.js" # Use relative path prettier --write --cache --cache-location node_modules/.cache/prettier "**/*.js"

2. cache-strategy Choose cache strategy:

  • content: Based on file content (default, more accurate but slightly slower)
  • metadata: Based on file metadata (faster but potentially less accurate)

3. Clear Cache

bash
# Clear cache and reformat prettier --write --cache --cache-strategy content "**/*.js" # Manually delete cache directory rm -rf .prettier-cache

Cache Best Practices

1. CI/CD Environment

yaml
# GitHub Actions - name: Format with cache run: | prettier --write --cache "**/*.js" # Cache directory will be automatically saved and restored

2. Local Development

bash
# Configure in package.json { "scripts": { "format": "prettier --write --cache \"src/**/*.js\"", "format:check": "prettier --check --cache \"src/**/*.js\"" } }

3. Git Hooks

json
// .husky/pre-commit { "lint-staged": { "*.{js,jsx,ts,tsx}": [ "prettier --write --cache" ] } }

Performance Comparison

Without Cache:

bash
# First format 1000 files time prettier --write "**/*.js" # Actual execution: ~5s

With Cache:

bash
# First format 1000 files time prettier --write --cache "**/*.js" # Actual execution: ~5s # Second format (no changes) time prettier --write --cache "**/*.js" # Actual execution: ~0.5s (10x improvement)

Cache Considerations

1. Configuration Changes

  • Modifying Prettier configuration automatically invalidates cache
  • Ensure configuration files are in version control
  • Team members use same configuration

2. File Changes

  • File content changes automatically update cache
  • Deleting files cleans corresponding cache
  • Renaming files regenerates cache

3. Cache Management

  • Regularly clean cache directory
  • Cache .prettier-cache directory in CI
  • Monitor cache size

Troubleshooting

Cache Not Working:

bash
# Check cache directory ls -la .prettier-cache # Clear cache and reformat rm -rf .prettier-cache prettier --write --cache "**/*.js"

Cache Too Large:

bash
# Check cache size du -sh .prettier-cache # Clean old cache find .prettier-cache -type f -mtime +7 -delete

By properly using Prettier's caching mechanism, you can significantly improve formatting performance, especially in large projects.

标签:Prettier