mirror of
https://github.com/flarum/framework.git
synced 2024-11-24 23:29:09 +08: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:
parent
bddbf24055
commit
18def302d6
23124
js/admin/dist/app.js
vendored
23124
js/admin/dist/app.js
vendored
File diff suppressed because one or more lines are too long
32695
js/forum/dist/app.js
vendored
32695
js/forum/dist/app.js
vendored
File diff suppressed because one or more lines are too long
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,9 +10,8 @@
|
|||
|
||||
namespace Flarum\Asset;
|
||||
|
||||
use Flarum\Asset\RevisionCompiler;
|
||||
use Less_Parser;
|
||||
use Less_Exception_Parser;
|
||||
use Less_Parser;
|
||||
|
||||
class LessCompiler extends RevisionCompiler
|
||||
{
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
namespace Flarum\Asset;
|
||||
|
||||
use Flarum\Asset\CompilerInterface;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RevisionCompiler implements CompilerInterface
|
||||
|
@ -64,30 +63,41 @@ class RevisionCompiler implements CompilerInterface
|
|||
*/
|
||||
public function getFile()
|
||||
{
|
||||
$revision = $this->getRevision();
|
||||
$old = $this->getRevision();
|
||||
|
||||
$lastModTime = 0;
|
||||
foreach ($this->files as $file) {
|
||||
$lastModTime = max($lastModTime, filemtime($file));
|
||||
}
|
||||
|
||||
$ext = pathinfo($this->filename, PATHINFO_EXTENSION);
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$revision, -strlen($ext) - 1, 0);
|
||||
$current = hash('crc32b', serialize([$lastModTime, $this->getCacheDifferentiator()]));
|
||||
|
||||
if (! ($exists = file_exists($file)) || filemtime($file) < $lastModTime) {
|
||||
$ext = pathinfo($this->filename, PATHINFO_EXTENSION);
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$old, -strlen($ext) - 1, 0);
|
||||
|
||||
$exists = file_exists($file);
|
||||
|
||||
if (! $exists || $old !== $current) {
|
||||
if ($exists) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
$revision = Str::quickRandom();
|
||||
$this->putRevision($revision);
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$revision, -strlen($ext) - 1, 0);
|
||||
$this->putRevision($current);
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$current, -strlen($ext) - 1, 0);
|
||||
file_put_contents($file, $this->compile());
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getCacheDifferentiator()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return string
|
||||
|
|
|
@ -198,10 +198,6 @@ class Formatter
|
|||
$configurator->enableJavaScript();
|
||||
$configurator->javascript->exportMethods = ['preview'];
|
||||
|
||||
$minifier = $configurator->javascript->setMinifier(new MinifyMinifier);
|
||||
$minifier->keepGoing = true;
|
||||
$minifier->cacheDir = $this->cacheDir;
|
||||
|
||||
return $configurator->finalize([
|
||||
'returnParser' => false,
|
||||
'returnRenderer' => false
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Formatter;
|
||||
|
||||
use s9e\TextFormatter\Configurator\JavaScript\Minifier;
|
||||
use MatthiasMullie\Minify;
|
||||
|
||||
class MinifyMinifier extends Minifier
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheDifferentiator()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function minify($src)
|
||||
{
|
||||
$minifier = new Minify\JS($src);
|
||||
|
||||
return $minifier->minify();
|
||||
}
|
||||
}
|
|
@ -110,6 +110,16 @@ class Application extends Container implements ApplicationContract
|
|||
return $this->bound('flarum.config');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function config($key, $default = null)
|
||||
{
|
||||
return array_get($this->make('flarum.config'), $key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Flarum is in debug mode.
|
||||
*
|
||||
|
@ -117,7 +127,7 @@ class Application extends Container implements ApplicationContract
|
|||
*/
|
||||
public function inDebugMode()
|
||||
{
|
||||
return ! $this->isInstalled() || $this->make('flarum.config')['debug'];
|
||||
return ! $this->isInstalled() || $this->config('debug');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -187,7 +187,7 @@ abstract class AbstractClientController extends AbstractHtmlController
|
|||
$public = $this->getAssetDirectory();
|
||||
|
||||
$assets = new AssetManager(
|
||||
new JsCompiler($public, "$this->clientName.js"),
|
||||
new JsCompiler($public, "$this->clientName.js", ! $this->app->config('debug')),
|
||||
new LessCompiler($public, "$this->clientName.css", $this->app->storagePath().'/less')
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user