mirror of
https://github.com/flarum/core.git
synced 2025-10-12 23:44:27 +02:00
Minify each JS file individually, caching the result
This means that the expensive minification process will only be run for a file if it hasn't before. Greatly speeds up extension enabling/disabling. Also: - Don't check file last modification times in production for a bit of extra perf. - Only flush CSS when theme settings are changed. This speeds up the page reload a bit.
This commit is contained in:
@@ -24,14 +24,21 @@ class RevisionCompiler implements CompilerInterface
|
||||
*/
|
||||
protected $strings = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $watch;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $filename
|
||||
* @param bool $watch
|
||||
*/
|
||||
public function __construct($path, $filename)
|
||||
public function __construct($path, $filename, $watch = false)
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->filename = $filename;
|
||||
$this->watch = $watch;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,18 +70,21 @@ class RevisionCompiler implements CompilerInterface
|
||||
*/
|
||||
public function getFile()
|
||||
{
|
||||
$old = $this->getRevision();
|
||||
|
||||
$lastModTime = 0;
|
||||
foreach ($this->files as $file) {
|
||||
$lastModTime = max($lastModTime, filemtime($file));
|
||||
}
|
||||
|
||||
$current = hash('crc32b', serialize([$lastModTime, $this->getCacheDifferentiator()]));
|
||||
$old = $current = $this->getRevision();
|
||||
|
||||
$ext = pathinfo($this->filename, PATHINFO_EXTENSION);
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$old, -strlen($ext) - 1, 0);
|
||||
|
||||
if ($this->watch) {
|
||||
$cacheDifferentiator = [$this->getCacheDifferentiator()];
|
||||
|
||||
foreach ($this->files as $source) {
|
||||
$cacheDifferentiator[] = [$source, filemtime($source)];
|
||||
}
|
||||
|
||||
$current = hash('crc32b', serialize($cacheDifferentiator));
|
||||
}
|
||||
|
||||
$exists = file_exists($file);
|
||||
|
||||
if (! $exists || $old !== $current) {
|
||||
@@ -83,6 +93,7 @@ class RevisionCompiler implements CompilerInterface
|
||||
}
|
||||
|
||||
$this->putRevision($current);
|
||||
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$current, -strlen($ext) - 1, 0);
|
||||
file_put_contents($file, $this->compile());
|
||||
}
|
||||
@@ -115,7 +126,7 @@ class RevisionCompiler implements CompilerInterface
|
||||
$output = '';
|
||||
|
||||
foreach ($this->files as $file) {
|
||||
$output .= $this->format(file_get_contents($file));
|
||||
$output .= $this->formatFile($file);
|
||||
}
|
||||
|
||||
foreach ($this->strings as $callback) {
|
||||
@@ -125,6 +136,15 @@ class RevisionCompiler implements CompilerInterface
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @return string
|
||||
*/
|
||||
protected function formatFile($file)
|
||||
{
|
||||
return $this->format(file_get_contents($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
Reference in New Issue
Block a user