Skip to content

Commit

Permalink
feat: add support to minify all files in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Mar 8, 2024
1 parent 656abd6 commit 53cff11
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions RockMigrations.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3088,12 +3088,26 @@ public function migrateYAML($path)
* Usage:
* $rm->minify("/path/to/style.css"); // creates /path/to/style.min.css
* $rm->minify("/path/to/style.css", "/newpath/style.min.css");
*
* Minify all .js and .css files in a folder:
* $rm->minify("/path/to/folder");
*/
public function minify($file, $minFile = null): string|false
{
if (!$this->wire->user->isSuperuser()) return false;
if (!$this->wire->config->debug) return false;

if (is_dir($file)) {
$file = rtrim(Paths::normalizeSeparators($file), "/");
foreach (glob("$file/*.js") as $f) {
if (!str_ends_with($f, '.min.js')) $this->minify($f);
}
foreach (glob("$file/*.css") as $f) {
if (!str_ends_with($f, '.min.css')) $this->minify($f);
}
return false;
}

$ext = pathinfo($file, PATHINFO_EXTENSION);
require_once __DIR__ . "/vendor/autoload.php";
if ($ext == 'css') {
Expand Down

0 comments on commit 53cff11

Please sign in to comment.