mirror of
https://github.com/flarum/core.git
synced 2025-10-12 07:24: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:
@@ -106,9 +106,13 @@ class AssetManager
|
||||
return $this->js->getFile();
|
||||
}
|
||||
|
||||
public function flush()
|
||||
public function flushCss()
|
||||
{
|
||||
$this->less->flush();
|
||||
}
|
||||
|
||||
public function flushJs()
|
||||
{
|
||||
$this->js->flush();
|
||||
}
|
||||
}
|
||||
|
@@ -11,54 +11,44 @@
|
||||
namespace Flarum\Asset;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Cache\Repository;
|
||||
use MatthiasMullie\Minify;
|
||||
use s9e\TextFormatter\Configurator\JavaScript\Minifiers\ClosureCompilerService;
|
||||
|
||||
class JsCompiler extends RevisionCompiler
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
* @var Repository
|
||||
*/
|
||||
protected $minify;
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $filename
|
||||
* @param bool $minify
|
||||
* @param bool $watch
|
||||
* @param Repository $cache
|
||||
*/
|
||||
public function __construct($path, $filename, $minify = false)
|
||||
public function __construct($path, $filename, $watch = false, Repository $cache = null)
|
||||
{
|
||||
parent::__construct($path, $filename);
|
||||
parent::__construct($path, $filename, $watch);
|
||||
|
||||
$this->minify = $minify;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function format($string)
|
||||
protected function format($string)
|
||||
{
|
||||
return $string.";\n";
|
||||
}
|
||||
if (! $this->watch) {
|
||||
$key = 'js.'.sha1($string);
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function compile()
|
||||
{
|
||||
$output = parent::compile();
|
||||
|
||||
if ($this->minify) {
|
||||
set_time_limit(60);
|
||||
|
||||
try {
|
||||
$output = $this->minifyWithClosureCompilerService($output);
|
||||
} catch (Exception $e) {
|
||||
$output = $this->minifyWithFallback($output);
|
||||
}
|
||||
$string = $this->cache->rememberForever($key, function () use ($string) {
|
||||
return $this->minify($string);
|
||||
});
|
||||
}
|
||||
|
||||
return $output;
|
||||
return $string.";\n";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +56,24 @@ class JsCompiler extends RevisionCompiler
|
||||
*/
|
||||
protected function getCacheDifferentiator()
|
||||
{
|
||||
return $this->minify;
|
||||
return $this->watch;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $source
|
||||
* @return string
|
||||
*/
|
||||
protected function minify($source)
|
||||
{
|
||||
set_time_limit(60);
|
||||
|
||||
try {
|
||||
$source = $this->minifyWithClosureCompilerService($source);
|
||||
} catch (Exception $e) {
|
||||
$source = $this->minifyWithFallback($source);
|
||||
}
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -23,11 +23,12 @@ class LessCompiler extends RevisionCompiler
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $filename
|
||||
* @param bool $watch
|
||||
* @param string $cachePath
|
||||
*/
|
||||
public function __construct($path, $filename, $cachePath)
|
||||
public function __construct($path, $filename, $watch, $cachePath)
|
||||
{
|
||||
parent::__construct($path, $filename);
|
||||
parent::__construct($path, $filename, $watch);
|
||||
|
||||
$this->cachePath = $cachePath;
|
||||
}
|
||||
|
@@ -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