deshithemes / css-cleaner
๐ Laravel CSS Optimizer: Purge unused CSS, Minify styles, Boost website speed & SEO. Zero-config setup for Tailwind, Bootstrap. 30%+ smaller CSS files guaranteed.
Requires
- php: ^8.1
- ext-fileinfo: *
- ext-json: *
- illuminate/support: ^10.0 || ^11.0
- symfony/process: ^6.0 || ^7.0
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
Suggests
- ext-mbstring: For better string handling in CSS processing
- ext-zip: For potential future archive features
- node: >=16.0.0 required for PurgeCSS functionality
README
๐ฅ Laravel package to automatically purge and minify CSS files in the public directory. Clean, optimized, and beginner-friendly with powerful Artisan commands. Made by Ruman ๐ช
๐จ Laravel CSS Cleaner
Remove Unused CSS โข Minify โข Optimize โ All in One for Laravel
Created with ๐ by Ruman
๐ฆ What is This?
Laravel CSS Cleaner is a developer-friendly Laravel package that:
โ
Removes unused CSS
โ
Minifies your styles
โ
Optimizes Laravelโs public CSS automatically
โ
Works with Tailwind, Bootstrap, or custom styles
โ
Requires zero manual searching for dead styles!
๐ Features
- ๐ Auto-scans the entire
/public
directory for.css
files - โ๏ธ Removes unused classes using PurgeCSS
- ๐จ Minifies and compresses CSS after purging
- ๐ Keeps your structure โ creates a
/public/css/optimized/
folder - ๐๏ธ Artisan-powered: Clean, Minify, or Optimize with one command
- ๐ง Configurable safelist to preserve dynamic classes like
modal
,fade
,active
, etc.
Feature | Icon | Description |
---|---|---|
Auto Purge | ๐งน | Removes unused CSS classes |
Smart Minification | โจ | Compresses CSS files |
Safelist Protection | ๐ก๏ธ | Preserves dynamic classes |
Zero Config | ๐ฏ | Works out-of-the-box |
Progress Tracking | ๐ | Real-time optimization stats |
โ๏ธ Installation
- Install the package:
composer require DeshiThemes/css-cleaner
Publish the config file:
php artisan vendor:publish --tag=css-cleaner-config
or, if not working or not genarating file under config/csscleaner.php
then run command (optional)
php artisan vendor:publish --tag=css-cleaner-config --force
Install the required Node.js dependency:
npm install @fullhuman/postcss-purgecss --save-dev
โ๏ธ Configuration
The configuration file will be published to: config/csscleaner.php
return [ /** * ๐ CSS Source Directory * * Tip: For better performance, specify exact subdirectory when possible: * 'css_path' => public_path('css'), */ 'css_path' => public_path(), /** * ๐ Output Directory * * Important: Directory will be created automatically * but parent directory must exist! */ 'output_path' => public_path('css/optimized'), /** * ๐ Content Files to Scan * * Pro Tip: Add all file types that might contain CSS class names: * - Blade templates * - JavaScript components * - Markdown files (if using) */ 'content_paths' => [ 'resources/views/**/*.blade.php', 'resources/js/**/*.vue', // 'storage/framework/views/*.php' // Uncomment for cached views ], /** * ๐ก๏ธ Safelist Configuration * * Formats: * - Exact: 'active' * - Wildcard: 'modal-*' * - Regex: '/^tooltip/' * * Always include: * - JS-toggled classes * - Animation classes * - Dynamically generated classes */ 'safelist' => [ // Core classes 'active', 'show', 'collapse', // Bootstrap specific 'modal', 'fade', 'collapsing', '/^carousel/', '/^tooltip/', '/^bs-tooltip/', // Add your project-specific classes here ], /** * โ๏ธ PurgeCSS Options * * Recommendation: Keep keyframes and fontFace * unless you're manually handling them */ 'purge_options' => [ 'keyframes' => true, // Keep @keyframes 'fontFace' => true, // Keep @font-face 'variables' => false, // Remove unused CSS variables 'rejected' => false // Don't log removed selectors ], /** * โ๏ธ Minification Options * * Warning: preserve_license=false will remove all comments * including legal/license blocks! */ 'minify_options' => [ 'remove_comments' => true, // Remove all comments 'preserve_license' => false, // Remove even license comments 'advanced' => true // Enable advanced optimizations ] ];
You can customize which classes to always keep and where output should go.
๐งฉ Artisan Commands
๐งฉ Command ๐ก Description โ Usage Example
๐งผ css:purge Purges unused CSS from all files in /public php artisan css:purge โ๏ธ css:minify Minifies the cleaned CSS files php artisan css:minify ๐ css:optimize Runs both purge + minify together php artisan css:optimize
๐งช Beginner-Friendly Usage ๐งผ Purge Only
php artisan css:purge
๐งน This removes unused CSS classes from .css
files and puts them in:
/public/css/cleaned/
โ๏ธ Minify Only
php artisan css:minify
๐ฏ After purging, use this to compress styles โ whitespace and comments are removed.
๐ Optimize (Purge + Minify)
php artisan css:optimize
๐ฅ Best option for production โ a full clean-up + compression in one step.
๐ Example: Input vs Output Input Files:
public/css/style.css public/assets/vendor/bootstrap.css
After css:optimize, Output:
public/css/cleaned/css/style.css public/css/cleaned/assets/vendor/bootstrap.css
โ Folder structure is preserved โ Only used classes remain โ Files are compressed
๐ฅ Command Reference
๐ ๏ธ Project Commands
Command | Icon | Description | Example |
---|---|---|---|
php artisan css:purge |
๐งน | Remove unused CSS | php artisan css:purge --safelist="active,modal-*" |
php artisan css:minify |
โจ | Minify CSS files | php artisan css:minify --path=public/css |
php artisan css:optimize |
๐ | Purge + Minify together | php artisan css:optimize --stats |
๐ Global Commands
Command | Icon | Description | Example |
---|---|---|---|
css-cleaner purge |
๐๐งน | Global purge | css-cleaner purge --path=/var/www/project |
css-cleaner minify |
๐โจ | Global minify | css-cleaner minify --output=dist |
css-cleaner optimize |
๐๐ | Global optimization | css-cleaner optimize --verbose |
๐จ Command Colors & Styles
Regular command (white)
php artisan css:purge
Optional flags (cyan)
php artisan css:optimize --stats
Path arguments (yellow)
php artisan css:minify --path=public/css
Safelist values (green)
php artisan css:purge --safelist="active,show"
๐ ๏ธ Advanced Usage
1. Custom Paths
php artisan css:optimize --css-path=public/assets --output=public/dist
2. Safelist Patterns
php artisan css:purge --safelist="active,modal-*,/^tooltip/"
3. CI/CD Integration (GitHub Actions)
- name: Optimize CSS run: | php artisan css:optimize git add public/css/cleaned git commit -m "Optimized CSS"
4. Dry Run (Test Only)
php artisan css:purge --dry-run
5. Get detailed output with --verbose:
css-cleaner optimize --verbose
6. Preserve folder structure with:
php artisan css:optimize --keep-structure
๐ When to Use
Situation | Icon | Benefit |
---|---|---|
Before Production | ๐ | Boost performance by 50-90% |
Bootstrap/Tailwind Sites | ๐งน | Remove 80-95% unused CSS |
Portfolio/Business Sites | โก | Faster loading pages |
After View Updates | ๐ | Keep CSS lean |
CI/CD Pipelines | ๐ค | Automate optimization |
๐ก Pro Tips
1. Always safelist JS classes
--safelist="show,collapse,fade"
2. Run after every deploy
php artisan css:optimize
3. Add to deployment scripts
# forge.sh or github-actions.yml
๐ฅ Compatibility
๐ก Core Requirements: PHP 8.1+ โข Laravel 10+ โข Node.js 16+Technology | Icon | Version | Highlight |
---|---|---|---|
PHP | ๐ | 8.1 - 8.4 |
โ Fully Supported |
Laravel | ๐ฏ | 10.x - 12.x |
โ Optimized |
Bootstrap | ๐ ฑ๏ธ | 4.x - 5.x |
โ ๏ธ v3 needs config |
Tailwind | ๐ช๏ธ | All versions |
โ Perfect match |
Laravel Mix | ๐จ | v6+ |
โ Works great |
Vite | โก | v3+ |
โ Post-build only |
Custom CSS | โจ | Any .css |
โ Universal |
Verify your environment matches:
php -v # Requires 8.1+
composer show laravel/framework # Requires 10+
node -v # Requires 16+
Project Type | Benefit of Using This Package |
---|---|
๐ eCommerce (Bootstrap) | Smaller styles, faster checkout UX |
๐ LMS Site (Tailwind) | Less bloat, faster page loads |
๐งโ๐จ Portfolio | Light footprint for static sites |
๐ข Business Website | Quick, optimized performance |
๐ Admin Panel | Clean dashboard assets |
๐ Benchmark Results
Scenario | Original Size | Optimized Size | Reduction |
---|---|---|---|
Bootstrap 5 | 187 KB | 24 KB | 87% |
Tailwind | 1.2 MB | 28 KB | 98% |
๐ Global Command Setup
1. Install Globally
composer global require deshithemes/css-cleaner
2. Add to PATH
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc source ~/.bashrc
3. Verify Installation
css-cleaner --version
Global Usage Examples
Optimize any project
css-cleaner optimize --path=/var/www/project/public
# Minify specific directory
css-cleaner minify --path=./public/css
๐ ๏ธ Troubleshooting
Permission Issues
sudo chown -R $USER:$USER ~/.composer/
Command Not Found
composer global bin deshithemes/css-cleaner install
Windows Users
# Use full path to composer binaries php C:\Users\You\AppData\Roaming\Composer\vendor\bin\css-cleaner optimize
โ FAQ
๐ฎ Will this break my JavaScript classes?
No! The safelist protects dynamic classes. Test in staging first.โฑ How often should I run this?
Before every production deployment or after CSS changes.โก Does this work with Vite?
Yes! Works with any build system (Vite, Mix, etc.)๐ Support
โจ Crafted with โค๏ธ by Ruman
```