mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-09 16:32:39 +01:00
Minify static resources
Summary: For production servers, minify CSS and JS by stripping comments, whitespace, etc. Test Plan: Looked at CSS/JS, it was much smaller. Reviewers: btrahan Reviewed By: btrahan CC: aran, epriestley Maniphest Tasks: T264 Differential Revision: https://secure.phabricator.com/D2034
This commit is contained in:
parent
29d8fc04e5
commit
eaa2ff71d3
5 changed files with 48 additions and 0 deletions
|
@ -789,6 +789,10 @@ return array(
|
|||
// settings are the defaults.)
|
||||
'celerity.force-disk-reads' => false,
|
||||
|
||||
// Minify static resources by removing whitespace and comments. You should
|
||||
// enable this in production, but disable it in development.
|
||||
'celerity.minify' => false,
|
||||
|
||||
// You can respond to various application events by installing listeners,
|
||||
// which will receive callbacks when interesting things occur. Specify a list
|
||||
// of classes which extend PhabricatorEventListener here.
|
||||
|
|
|
@ -22,5 +22,6 @@ return array(
|
|||
'celerity.force-disk-reads' => true,
|
||||
'phabricator.show-stack-traces' => true,
|
||||
'phabricator.show-error-callout' => true,
|
||||
'celerity.minify' => false,
|
||||
|
||||
) + phabricator_read_config_file('default');
|
||||
|
|
|
@ -48,6 +48,9 @@ function __warmup__() {
|
|||
$loader = new PhutilSymbolLoader();
|
||||
$loader->selectAndLoadSymbols();
|
||||
|
||||
// Force load of the Celerity map.
|
||||
CelerityResourceMap::getInstance();
|
||||
|
||||
define('__WARMUP__', true);
|
||||
}
|
||||
|
||||
|
|
|
@ -81,6 +81,7 @@ final class CelerityResourceController extends AphrontController {
|
|||
}
|
||||
|
||||
$response = new AphrontFileResponse();
|
||||
$data = $this->minifyData($data, $type);
|
||||
$response->setContent($data);
|
||||
switch ($type) {
|
||||
case 'css':
|
||||
|
@ -101,4 +102,42 @@ final class CelerityResourceController extends AphrontController {
|
|||
return $response;
|
||||
}
|
||||
|
||||
private function minifyData($data, $type) {
|
||||
if (!PhabricatorEnv::getEnvConfig('celerity.minify')) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
switch ($type) {
|
||||
case 'css':
|
||||
// Remove comments.
|
||||
$data = preg_replace('@/\*.*?\*/@s', '', $data);
|
||||
// Remove whitespace around symbols.
|
||||
$data = preg_replace('@\s*([{}:;,])\s+@', '\1', $data);
|
||||
// Remove unnecessary semicolons.
|
||||
$data = preg_replace('@;}@', '}', $data);
|
||||
// Replace #rrggbb with #rgb when possible.
|
||||
$data = preg_replace(
|
||||
'@#([a-f0-9])\1([a-f0-9])\2([a-f0-9])\3@i',
|
||||
'#\1\2\3',
|
||||
$data);
|
||||
$data = trim($data);
|
||||
break;
|
||||
case 'js':
|
||||
$root = dirname(phutil_get_library_root('phabricator'));
|
||||
$bin = $root.'/externals/javelin/support/jsxmin/jsxmin';
|
||||
|
||||
if (@file_exists($bin)) {
|
||||
$future = new ExecFuture("{$bin} __DEV__:0");
|
||||
$future->write($data);
|
||||
list($err, $result) = $future->resolve();
|
||||
if (!$err) {
|
||||
$data = $result;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ phutil_require_module('phabricator', 'infrastructure/celerity/map');
|
|||
phutil_require_module('phabricator', 'infrastructure/env');
|
||||
|
||||
phutil_require_module('phutil', 'filesystem');
|
||||
phutil_require_module('phutil', 'future/exec');
|
||||
phutil_require_module('phutil', 'moduleutils');
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue