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.

v1.1.0 2025-06-29 09:47 UTC

This package is auto-updated.

Last update: 2025-06-29 11:09:16 UTC


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

  1. 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
๐Ÿ”ง Version Details

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

```