1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 02:02:41 +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:
epriestley 2012-03-28 10:13:53 -07:00
parent 29d8fc04e5
commit eaa2ff71d3
5 changed files with 48 additions and 0 deletions

View file

@ -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.

View file

@ -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');

View file

@ -48,6 +48,9 @@ function __warmup__() {
$loader = new PhutilSymbolLoader();
$loader->selectAndLoadSymbols();
// Force load of the Celerity map.
CelerityResourceMap::getInstance();
define('__WARMUP__', true);
}

View file

@ -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;
}
}

View file

@ -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');