Skip to content

Commit

Permalink
feat: add new minify feature
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Feb 28, 2023
1 parent faa0b29 commit aebf4c0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
13 changes: 11 additions & 2 deletions Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
class Asset extends WireData
{

public $basename;
public $comment;
public $debug;
public $dir;
public $ext;
public $basename;
public $filename;
public $m;
public $minify = 'auto';
public $path;
public $suffix;
public $url;
public $comment;

public function __construct($file, $suffix = '')
{
Expand All @@ -29,6 +32,9 @@ public function __construct($file, $suffix = '')
$this->suffix = $suffix;
$this->ext = strtolower(pathinfo($this->path, PATHINFO_EXTENSION));
$this->basename = pathinfo($this->path, PATHINFO_BASENAME);

$this->dir = dirname($this->path) . "/";
$this->filename = pathinfo($this->path, PATHINFO_FILENAME);
}

/**
Expand Down Expand Up @@ -92,9 +98,12 @@ public function __debugInfo()
{
return [
'basename' => $this->basename,
'filename' => $this->filename,
'path' => $this->path,
'dir' => $this->dir,
'url' => $this->url,
'm' => $this->m,
'minify' => $this->minify,
'suffix' => $this->suffix,
'ext' => $this->ext,
'comment' => $this->comment,
Expand Down
56 changes: 52 additions & 4 deletions AssetsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class AssetsArray extends \ProcessWire\WireArray
{

public $minify = false;
public $name;

/** @var array */
Expand All @@ -22,14 +23,26 @@ public function __construct(string $name)
}

/**
* Add file to assets array
*
* Usage:
* $rf->styles()->add("/path/to/file.css");
*
* Set noMinify property:
* $rf->styles()->add("/path/to/file.css", '', ['noMinify' => true]);
*
* @return self
*/
public function add($file, $suffix = '')
public function add($file, $suffix = '', $properties = [])
{
$debug = $this->getDebugNote($file);
if (is_string($file)) $file = new Asset($file, $suffix);
foreach ($properties as $k => $v) $file->$k = $v;
// prevent adding file multiple times
if ($this->get('path=' . $file->path)) return $this;
if ($exists = $this->get('path=' . $file->path)) {
foreach ($properties as $k => $v) $exists->$k = $v;
return $this;
}
$file->debug = $debug;
parent::add($file);
return $this;
Expand Down Expand Up @@ -92,11 +105,23 @@ public function comment($str, $prepend = false): self
return $this;
}

public function minify($bool): self
{
$this->minify = $bool;
return $this;
}

/**
* Auto-create minified version of asset
* See docs here: https://github.com/baumrock/RockFrontend/wiki/Auto-Minify-Feature
*/
public function minify(Asset $asset): Asset
public function minifyAsset(Asset $asset): Asset
{
if ($this->minify) return $this->minifyForced($asset);
else return $this->minifyAuto($asset);
}

public function minifyAuto(Asset $asset): Asset
{
if (!$this->rockfrontend()->isEnabled('minify')) return $asset;

Expand All @@ -113,7 +138,7 @@ public function minify(Asset $asset): Asset
$min = $asset->path;
$nomin = substr($min, 0, strlen($min) - strlen($ending)) . "." . $asset->ext;

// if no unminified file exists we return instantly
// if no unminified file exists we return the asset as is
if (!is_file($nomin)) return $asset;

// a non-minified file exists, so we check if it has been updated
Expand All @@ -127,6 +152,29 @@ public function minify(Asset $asset): Asset
return $asset;
}

public function minifyForced(Asset $asset): Asset
{
if ($asset instanceof AssetComment) return $asset;
if ($asset->minify === false) return $asset;
if ($asset->minify === 'auto' and !$this->minify) return $asset;
if (substr($asset->path, -8) === '.min.css') return $asset;
if (substr($asset->path, -7) === '.min.js') return $asset;

$nomin = $asset->path;
if ($asset->ext == 'css') $min = $asset->dir . $asset->filename . ".min.css";
else $min = $asset->dir . $asset->filename . ".min.js";

$asset = new Asset($min);
if ($this->rockfrontend()->isNewer($nomin, $min)) {
require_once __DIR__ . "/vendor/autoload.php";
if ($asset->ext == 'js') $minify = new \MatthiasMullie\Minify\JS($nomin);
else $minify = new \MatthiasMullie\Minify\CSS($nomin);
$minify->minify($min);
$asset->comment = 'foo bar';
}
return $asset;
}

/**
* Get options value
* @return mixed
Expand Down
4 changes: 2 additions & 2 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function getModuleInfo()
{
return [
'title' => 'RockFrontend',
'version' => '2.25.0',
'version' => '2.26.0',
'summary' => 'Module for easy frontend development',
'autoload' => true,
'singular' => true,
Expand Down Expand Up @@ -289,7 +289,7 @@ function (HookEvent $event) {

public function ___addAlfredStyles()
{
$this->styles()->add($this->path . "Alfred.css");
$this->styles()->add($this->path . "Alfred.css", "", ['minify' => false]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ScriptsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function ___renderAssets($opt): string
$out = '';
foreach ($this as $asset) {
if ($asset->ext === 'less') continue;
$asset = $this->minify($asset);
$asset = $this->minifyAsset($asset);
$out .= $this->renderTag($asset, $opt, 'script');
}
return $out;
Expand Down
2 changes: 1 addition & 1 deletion StylesArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function ___renderAssets($opt): string
$out = '';
foreach ($this as $asset) {
if ($asset->ext === 'less') continue;
$asset = $this->minify($asset);
$asset = $this->minifyAsset($asset);
$asset = $this->postCSS($asset);
$out .= $this->renderTag($asset, $opt, 'style');
}
Expand Down

0 comments on commit aebf4c0

Please sign in to comment.