Skip to content

Commit

Permalink
fix: less not recompiling when using variables from PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Oct 30, 2023
1 parent 7e0ba90 commit 7f805a1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
31 changes: 24 additions & 7 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function init()
self::permission_alfred,
"Is allowed to use ALFRED frontend editing"
);
$this->createCSS();
$this->lessToCss($this->path . "Alfred.less");

// hooks
$this->addHookAfter("ProcessPageEdit::buildForm", $this, "hideLayoutField");
Expand Down Expand Up @@ -600,19 +600,36 @@ public function consentOptout($name, $script, $condition = true)

/**
* Create CSS from LESS file
* @return void
*
* This will only be executed for superusers as it is intended to be used
* on dev environments to parse module styles on the fly.
*
* Usage:
* $rockfrontend->lessToCss("/path/to/file.less", $minify = true);
*/
private function createCSS()
public function lessToCss($lessFile, $minify = true): void
{
if (!$this->wire->user->isSuperuser()) return;
$css = $this->path . "Alfred.css";
$lessFile = $this->path . "Alfred.less";
if (filemtime($css) > filemtime($lessFile)) return;

// get path of less file
$lessFile = $this->getFile($lessFile);
if (!is_file($lessFile)) throw new WireException("$lessFile not found");

// get path of css file
$css = substr($lessFile, 0, -5) . ".css";

// if css file is newer we don't do anything
if (@filemtime($css) > @filemtime($lessFile)) return;

// we need to create CSS from less
if (!$less = $this->wire->modules->get("Less")) return;
/** @var Less $less */
$less->addFile($lessFile);
$less->saveCSS($css);
$this->message("Created $css from $lessFile");

// if minify option is true we minify the file
// and return the path of the minified css file
if ($minify) $this->minifyFile($css);
}

public function createCustomLess(HookEvent $event): void
Expand Down
51 changes: 39 additions & 12 deletions classes/StylesArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private function parseLessFiles($opt, $cacheName)
$less = $this->wire->modules->get('Less');
$lessCache = $this->wire->cache->get($cacheName);
$lessCurrent = ''; // string to store file info
$m = 0;
$mtime = 0;
$parse = false;
$entries = new WireArray();
$intro = "Recompile $cacheName";
Expand All @@ -82,34 +82,59 @@ private function parseLessFiles($opt, $cacheName)
}
$less->addFile($asset->path);
$parse = true;
if ($asset->m > $m) $m = $asset->m;
if ($asset->m > $mtime) $mtime = $asset->m;
$lessCurrent .= $asset->path . "|" . $asset->m . "--";
}

// if a modification timestamp is set in the options we apply it now
// this is necessary for making LESS recompile if less variables in
// PHP have been changed without updating any less files
$mtime = max($mtime, $opt->lessVarsModified);

// prepare variables needed for recompile check
$cssPath = $this->wire->config->paths->root . ltrim($opt->cssDir, "/");
$cssFile = $cssPath . $opt->cssName . ".css";
$lessCacheArray = $this->getChangedFiles($lessCache, $lessCurrent);

// we have a less parser installed and some less files to parse
if ($less and $parse) {
$recompile = false;

// if it is a livereload stream we do not recompile
if ($this->rockfrontend()->isLiveReload) $recompile = false;

// if css file does not exist we recompile
elseif (!is_file($cssFile)) {
$this->log("$intro: $cssFile does not exist.");
$recompile = true;
} elseif ($lessCurrent !== $lessCache) {
}

// cache strings are different
// that means a file or a timestamp has changed
elseif ($lessCurrent !== $lessCache) {
// show info which file changed to log
foreach ($lessCacheArray as $str) {
$parts = explode("|", $str);
$url = $this->rockfrontend()->toUrl($parts[0]);
$this->log("$intro: Change detected in $url");
}
$recompile = true;
} elseif ($this->wire->session->get(RockFrontend::recompile)) {
}

// nothing changed so far, check for mtime variable
elseif ($mtime > filemtime($cssFile)) {
$recompile = true;
$this->log("$intro: Change detected in less variables from PHP file");
}

// maybe recompile is forced by the session flag?
elseif ($this->wire->session->get(RockFrontend::recompile)) {
$this->log("$intro: Forced by RockFrontend::recompile.");
$recompile = true;
} else {
// check if any of the less files in RockFrontend module folder have changed
}

// check if any of the less files in RockFrontend folder have changed
else {
foreach (glob(__DIR__ . "/less/*.less") as $f) {
if (filemtime($f) > filemtime($cssFile)) {
$this->log("$intro: $f changed.");
Expand All @@ -118,12 +143,7 @@ private function parseLessFiles($opt, $cacheName)
}
}

// create css file
$url = str_replace(
$this->wire->config->paths->root,
$this->wire->config->urls->root,
$cssFile
);
// recompile CSS
if ($recompile) {
if (!is_dir($cssPath)) $this->wire->files->mkdir($cssPath);
$less->setOptions([
Expand All @@ -139,6 +159,8 @@ private function parseLessFiles($opt, $cacheName)
$less->saveCss($cssFile);
$this->wire->cache->save($cacheName, $lessCurrent);
$this->wire->session->set(RockFrontend::recompile, false);

$url = $this->rockfrontend()->toUrl($cssFile);
$this->log("RockFrontend recompiled $url");
}

Expand Down Expand Up @@ -205,6 +227,11 @@ public function render($options = [])
'cssDir' => $this->cssDir ?: "/site/templates/bundle/",
'cssName' => $this->name,
'sourcemaps' => $this->wire->config->debug,

// manual file modification timestamp provided by calling module
// this is needed in RockPdf to make the less update when variables
// in PHP changed but no less file was changed
'lessVarsModified' => false,
]);
$opt->setArray($options);

Expand Down

0 comments on commit 7f805a1

Please sign in to comment.