mirror of
https://github.com/flarum/core.git
synced 2025-10-15 00:44:40 +02:00
Bundle unminified JS; minify via ClosureCompilerService when in production
Falls back to a less effective minification library if ClosureCompilerService errors or is unavailable. Minification takes a while (20 seconds or so), but it only happens when assets are modified. Still, this means enabling/disabling extensions is taking far too long. Possible solutions: - Don't minify initially; set a process running in the background to do minification, and server unminified assets in the meantime. - Refactor compiler to send each JS file to CCS individually, only if that particular file has been modified. flarum/gulp has also been updated to no longer support uglification. closes #582
This commit is contained in:
@@ -10,10 +10,29 @@
|
||||
|
||||
namespace Flarum\Asset;
|
||||
|
||||
use Flarum\Asset\RevisionCompiler;
|
||||
use Exception;
|
||||
use MatthiasMullie\Minify;
|
||||
use s9e\TextFormatter\Configurator\JavaScript\Minifiers\ClosureCompilerService;
|
||||
|
||||
class JsCompiler extends RevisionCompiler
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $minify;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $filename
|
||||
* @param bool $minify
|
||||
*/
|
||||
public function __construct($path, $filename, $minify = false)
|
||||
{
|
||||
parent::__construct($path, $filename);
|
||||
|
||||
$this->minify = $minify;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -21,4 +40,59 @@ class JsCompiler extends RevisionCompiler
|
||||
{
|
||||
return $string.";\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function getCacheDifferentiator()
|
||||
{
|
||||
return $this->minify;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $source
|
||||
* @return string
|
||||
*/
|
||||
protected function minifyWithClosureCompilerService($source)
|
||||
{
|
||||
$minifier = new ClosureCompilerService;
|
||||
|
||||
$minifier->compilationLevel = 'SIMPLE_OPTIMIZATIONS';
|
||||
$minifier->timeout = 60;
|
||||
|
||||
$output = $minifier->minify($source);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $source
|
||||
* @return string
|
||||
*/
|
||||
protected function minifyWithFallback($source)
|
||||
{
|
||||
$minifier = new Minify\JS($source);
|
||||
|
||||
return $minifier->minify();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user