momentum81/php-remove-unused-css
Composer 安装命令:
composer require momentum81/php-remove-unused-css
包简介
PHP Remove Unused CSS is a tool to remove unused CSS from your website using PHP
README 文档
README
PHP Remove Unused CSS is a tool to remove unused CSS from your website using PHP. Developed by Momentum 81 - https://momentum81.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 momentum81/php-remove-unused-css
Example
$removeUnusedCss = new \Momentum81\PhpRemoveUnusedCss\RemoveUnusedCssBasic(); $removeUnusedCss->whitelist('.fab', '.far', '.fal') ->styleSheets(public_path('**/*.css')) ->htmlFiles(resource_path('**/*.blade.php')) ->setFilenameSuffix('.refactored.min') ->minify() ->refactor() ->saveFiles();
Classes
The are two main ways of using the package:
- Basic
- Complete (In Development, not yet available)
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 {}
Complete Class
In Development. This method attempts to be smarter and where possible traverse the DOM as much as it can (When using a templating system this is infinitely more difficult if your views are not cached, so the system can only do so well here).
Available Methods
| Method | Description |
|---|---|
whitelist(...$selectors) |
Here you can provice multipleCSS selectors to whitelist, ensuring they remain in the CSS even if they are not present in the HTML. |
styleSheets(...$styleSheets) |
Here you can provide glob compatible absolute paths to the stylesheets you want to refactor. For example in Laravel, you could use public_path('**/*.css'). |
htmlFiles(...$htmlFiles) |
Here you can provide glob compatible absolute paths to the HTML files (Can be any text file type, not just .html) that you want to scan for selectors to keep in your refactored CSS. For example in Laravel, you could use resource_path('**/*.blade.php'). |
setFilenameSuffix($string) |
By default the platform will overwrite the stylesheets it finds (When saving as files), here you can provide a suffix for the file name - this will get appended before the file type. So stylesheet.css could become stylesheet.refactored.min.css. The default value is .refactored.min. IMPORTANT: If you do not specify this method, your original files will be OVERWRITTEN if you use saveFiles()! |
minify($bool) |
If specified, this will perform minification on the CSS before it's saved/returned, using the package matthiasmullie/minify (https://github.com/matthiasmullie/minify) |
skipComment($bool) |
Specify if we should skip the comment at the top of the CSS file |
comment($string) |
Specify custom text for the comment at the top of the CSS file - use the variable :time: for the date and time to be added in its place. Don't forget to add your opening and closing tags for the comment: /* and */. |
refactor() |
This method performs the refactoring. |
saveFiles() |
This will save the new (Or overwritten) files |
returnAsText() |
This will return the refactored CSS as an array of files with text rather than writing them to real files. |
momentum81/php-remove-unused-css 适用场景与选型建议
momentum81/php-remove-unused-css 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 346 次下载、GitHub Stars 达 18, 最近一次更新时间为 2020 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「momentum81」 「php-remove-unused-css」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 momentum81/php-remove-unused-css 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 momentum81/php-remove-unused-css 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 momentum81/php-remove-unused-css 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 346
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-03