Skip to content

Commit

Permalink
feat: improve asset minification features
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Mar 27, 2024
1 parent ef48176 commit d3a07ba
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
55 changes: 46 additions & 9 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Latte\Engine;
use Latte\Runtime\Html;
use LogicException;
use MatthiasMullie\Minify\Exceptions\IOException;
use RockFrontend\Asset;
use RockFrontend\LiveReload;
use RockFrontend\Manifest;
Expand Down Expand Up @@ -310,18 +311,16 @@ private function addAlfredMarkup(string &$html): void
$faketag = "<div edit=title hidden>title</div>";
$html = str_replace("</body", "$faketag</body", $html);
}

private function addRockFrontendJS(): void
{
if (!$this->isEnabled('RockFrontend.js')) return;
$file = __DIR__ . "/RockFrontend.js";
if ($this->wire->config->debug) {
// load the non-minified script
$this->scripts('rockfrontend')->add($file, "defer");
// when logged in as superuser we make sure to create the minified
// file even if the non-minified version is used.
if ($this->wire->user->isSuperuser()) $this->minifyFile($file);
} else $this->scripts('rockfrontend')->add($this->minifyFile($file), "defer");
$file = $min = __DIR__ . "/RockFrontend.js";

// while developing we create a minified js file
if ($this->isDev()) $min = $this->minifyFile($file);

// add file to scripts array
$this->scripts('rockfrontend')->add($min, "defer");
}

public function ___addAlfredStyles()
Expand Down Expand Up @@ -1448,6 +1447,26 @@ public function isActive($menuItem, $page = null)
return $active->has($menuItem);
}

/**
* Check wether the environment is DDEV or not
*/
public function isDDEV(): bool
{
return !!getenv('DDEV_HOSTNAME');
}

/**
* Internal flag for development
* @return bool
*/
public function isDev(): bool
{
if (!$this->wire->config->debug) return false;
if (!$this->wire->user->isSuperuser()) return false;
if (!$this->isDDEV()) return false;
return true;
}

/**
* Is given file newer than the comparison file?
* Returns true if comparison file does not exist
Expand Down Expand Up @@ -1683,6 +1702,24 @@ public function minifyFile($file, $minFile = null): string
return $minFile->path;
}

/**
* Minify all files in given directory
* @param string $directory
* @param array $options
* @return void
* @throws IOException
*/
public function minifyFiles(string $directory, array $options = []): void
{
$dir = $this->toPath($directory);
$files = $this->wire->files->find($dir, $options);
foreach ($files as $file) {
if (str_ends_with($file, ".min.js")) continue;
if (str_ends_with($file, ".min.css")) continue;
$this->minifyFile($file);
}
}

/**
* Helper to display markup only once
*
Expand Down
14 changes: 11 additions & 3 deletions classes/AssetsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ public function getDebugNote($file = null)
*
* @return self
*/
public function addAll($path, $suffix = '', $levels = 2, $ext = ['js'])
{
public function addAll(
$path,
$suffix = '',
$levels = 2,
$ext = ['js'],
$endsWith = null,
) {
/** @var RockFrontend $rf */
$rf = $this->wire('modules')->get('RockFrontend');
$path = $rf->getPath($path);
Expand All @@ -114,7 +119,10 @@ public function addAll($path, $suffix = '', $levels = 2, $ext = ['js'])
'recursive' => $levels,
'extensions' => $ext,
]);
foreach ($files as $f) $this->add($f, $suffix);
foreach ($files as $f) {
if ($endsWith && !str_ends_with($f, $endsWith)) continue;
$this->add($f, $suffix);
}
return $this;
}

Expand Down
9 changes: 7 additions & 2 deletions classes/StylesArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class StylesArray extends AssetsArray
*
* @return self
*/
public function addAll($path, $suffix = '', $levels = 2, $ext = ['css', 'less'])
{
public function addAll(
$path,
$suffix = '',
$levels = 2,
$ext = ['css', 'less'],
$endsWith = null,
) {
return parent::addAll($path, $suffix, $levels, $ext);
}

Expand Down

0 comments on commit d3a07ba

Please sign in to comment.