falschenzug / php-remove-unused-css
PHP Remove Unused CSS is a tool to remove unused CSS from your website using PHP
Requires
- php: ^8.1
- matthiasmullie/minify: ^1.3
This package is not auto-updated.
Last update: 2024-11-07 02:54:21 UTC
README
PHP Remove Unused CSS is a tool to remove unused CSS from your website using PHP. Developed by Momentum 81 - https://momentum81.com. Updated by https://medienweide.com
- IMPORTANT! MAKE SURE YOU READ THE GUIDE FIRST OR YOU MAY INADVERTENTLY OVERWRITE YOUR CSS
The main idea is to first compile your CSS into as few files as possible, then you would remove the extra classes using this package, then use this (or another) package to minify the CSS.
Often this is done with JS however that can raise issues if you want to work this into a pure PHP development flow.
Installation
Installation via composer:
composer require Flaschenzug/php-remove-unused-css
(original Version: composer require Momentum81/php-remove-unused-css )
Example
$removeUnusedCss = new \Momentum81\PhpRemoveUnusedCss\RemoveUnusedCssBasic(); $removeUnusedCss->whitelist('.fab', '.far', '.fal') ->styleSheets(['ARRAY','WITH','paths']) ->htmlFiles(['ARRAY','WITH','paths']) ->setFilenameSuffix('.refactored.min') ->minify() ->refactor() ->saveFiles();
Classes
- Basic
Basic Class
The basic class is created using RemoveUnusedCssBasic
. This is essentially a 'dumb' system that won't traverse the DOM in any way and will just include a selector if it's lowest level appears in the CSS.
That said, this can still provide some significant savings in file size, especially when you're using a package like Bootstrap.
$removeUnusedCss = new \Momentum81\PhpRemoveUnusedCss\RemoveUnusedCssBasic();
The basic class only weakly matches, lets look at the following HTML:
<div> <span class="hello">Hello World</span> </div>
The following CSS Classes would match and be kept, despite the .hello
class being used in the HTML not being inside a parent element using the class .test
:
.test .hello {} .test .hello::after {}