mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-18 01:38: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.)
|
// settings are the defaults.)
|
||||||
'celerity.force-disk-reads' => false,
|
'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,
|
// You can respond to various application events by installing listeners,
|
||||||
// which will receive callbacks when interesting things occur. Specify a list
|
// which will receive callbacks when interesting things occur. Specify a list
|
||||||
// of classes which extend PhabricatorEventListener here.
|
// of classes which extend PhabricatorEventListener here.
|
||||||
|
|
|
@ -22,5 +22,6 @@ return array(
|
||||||
'celerity.force-disk-reads' => true,
|
'celerity.force-disk-reads' => true,
|
||||||
'phabricator.show-stack-traces' => true,
|
'phabricator.show-stack-traces' => true,
|
||||||
'phabricator.show-error-callout' => true,
|
'phabricator.show-error-callout' => true,
|
||||||
|
'celerity.minify' => false,
|
||||||
|
|
||||||
) + phabricator_read_config_file('default');
|
) + phabricator_read_config_file('default');
|
||||||
|
|
|
@ -48,6 +48,9 @@ function __warmup__() {
|
||||||
$loader = new PhutilSymbolLoader();
|
$loader = new PhutilSymbolLoader();
|
||||||
$loader->selectAndLoadSymbols();
|
$loader->selectAndLoadSymbols();
|
||||||
|
|
||||||
|
// Force load of the Celerity map.
|
||||||
|
CelerityResourceMap::getInstance();
|
||||||
|
|
||||||
define('__WARMUP__', true);
|
define('__WARMUP__', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,6 +81,7 @@ final class CelerityResourceController extends AphrontController {
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = new AphrontFileResponse();
|
$response = new AphrontFileResponse();
|
||||||
|
$data = $this->minifyData($data, $type);
|
||||||
$response->setContent($data);
|
$response->setContent($data);
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 'css':
|
case 'css':
|
||||||
|
@ -101,4 +102,42 @@ final class CelerityResourceController extends AphrontController {
|
||||||
return $response;
|
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('phabricator', 'infrastructure/env');
|
||||||
|
|
||||||
phutil_require_module('phutil', 'filesystem');
|
phutil_require_module('phutil', 'filesystem');
|
||||||
|
phutil_require_module('phutil', 'future/exec');
|
||||||
phutil_require_module('phutil', 'moduleutils');
|
phutil_require_module('phutil', 'moduleutils');
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue