Add option to minify only common (core) files

This commit is contained in:
Kijin Sung 2016-01-10 17:02:26 +09:00
parent 8e05c56e4e
commit da1822d296
7 changed files with 50 additions and 21 deletions

View file

@ -425,7 +425,7 @@ class HTMLDisplayHandler
Context::loadFile(array('./common/css/xe.css', '', '', -1600000), true); Context::loadFile(array('./common/css/xe.css', '', '', -1600000), true);
$original_file_list = array('x', 'common', 'js_app', 'xml_handler', 'xml_js_filter'); $original_file_list = array('x', 'common', 'js_app', 'xml_handler', 'xml_js_filter');
if(Context::getDBInfo()->minify_scripts === 'N') if(Context::getDBInfo()->minify_scripts === 'none')
{ {
Context::loadFile(array('./common/js/jquery-1.x.js', 'head', 'lt IE 9', -1730000), true); Context::loadFile(array('./common/js/jquery-1.x.js', 'head', 'lt IE 9', -1730000), true);
Context::loadFile(array('./common/js/jquery.js', 'head', 'gte IE 9', -1720000), true); Context::loadFile(array('./common/js/jquery.js', 'head', 'gte IE 9', -1720000), true);

View file

@ -93,15 +93,13 @@ class FrontEndFileHandler extends Handler
{ {
$args = array($args); $args = array($args);
} }
if($args[3] > -1500000 && preg_match(HTMLDisplayHandler::$reservedCSS, $args[0]))
$isCommon = preg_match(HTMLDisplayHandler::$reservedCSS, $args[0]) || preg_match(HTMLDisplayHandler::$reservedJS, $args[0]);
if($args[3] > -1500000 && $isCommon)
{ {
return; return;
} }
if($args[3] > -1500000 && preg_match(HTMLDisplayHandler::$reservedJS, $args[0])) $file = $this->getFileInfo($args[0], $args[2], $args[1], $isCommon);
{
return;
}
$file = $this->getFileInfo($args[0], $args[2], $args[1]);
$file->index = (int)$args[3]; $file->index = (int)$args[3];
$availableExtension = array('css' => 1, 'js' => 1); $availableExtension = array('css' => 1, 'js' => 1);
@ -145,15 +143,16 @@ class FrontEndFileHandler extends Handler
* @param string $fileName The file name * @param string $fileName The file name
* @param string $targetIe Target IE of file * @param string $targetIe Target IE of file
* @param string $media Media of file * @param string $media Media of file
* @param bool $forceMinify Whether this file should be minified
* @return stdClass The file information * @return stdClass The file information
*/ */
private function getFileInfo($fileName, $targetIe = '', $media = 'all') private function getFileInfo($fileName, $targetIe = '', $media = 'all', $forceMinify = false)
{ {
static $existsInfo = array(); static $existsInfo = array();
if(self::$minify === null) if(self::$minify === null)
{ {
self::$minify = Context::getDBInfo()->minify_scripts === 'N' ? false : true; self::$minify = Context::getDBInfo()->minify_scripts ?: 'common';
} }
if(isset($existsInfo[$existsKey])) if(isset($existsInfo[$existsKey]))
@ -194,8 +193,22 @@ class FrontEndFileHandler extends Handler
} }
} }
// Decide whether to minify this file
if(self::$minify === 'all')
{
$minify_enabled = true;
}
elseif(self::$minify === 'none')
{
$minify_enabled = false;
}
else
{
$minify_enabled = $forceMinify;
}
// Minify file // Minify file
if(self::$minify && !$file->isMinified && !$file->isExternalURL && !$file->isCachedScript && strpos($file->filePath, 'common/js/plugins') === false) if($minify_enabled && !$file->isMinified && !$file->isExternalURL && !$file->isCachedScript && strpos($file->filePath, 'common/js/plugins') === false)
{ {
if(($file->fileExtension === 'css' || $file->fileExtension === 'js') && file_exists($originalFilePath)) if(($file->fileExtension === 'css' || $file->fileExtension === 'js') && file_exists($originalFilePath))
{ {

View file

@ -71,7 +71,7 @@ class adminAdminView extends admin
Context::set('use_html5', $db_info->use_html5 == 'Y' ? 'Y' : 'N'); Context::set('use_html5', $db_info->use_html5 == 'Y' ? 'Y' : 'N');
Context::set('use_spaceremover', $db_info->use_spaceremover ? $db_info->use_spaceremover : 'Y'); //not use Context::set('use_spaceremover', $db_info->use_spaceremover ? $db_info->use_spaceremover : 'Y'); //not use
Context::set('qmail_compatibility', $db_info->qmail_compatibility == 'Y' ? 'Y' : 'N'); Context::set('qmail_compatibility', $db_info->qmail_compatibility == 'Y' ? 'Y' : 'N');
Context::set('minify_scripts', $db_info->minify_scripts == 'N' ? 'N' : 'Y'); Context::set('minify_scripts', $db_info->minify_scripts ?: 'common');
Context::set('cache_friendly', $db_info->cache_friendly == 'Y' ? 'Y' : 'N'); Context::set('cache_friendly', $db_info->cache_friendly == 'Y' ? 'Y' : 'N');
Context::set('use_db_session', $db_info->use_db_session == 'N' ? 'N' : 'Y'); Context::set('use_db_session', $db_info->use_db_session == 'N' ? 'N' : 'Y');
Context::set('use_mobile_view', $db_info->use_mobile_view == 'Y' ? 'Y' : 'N'); Context::set('use_mobile_view', $db_info->use_mobile_view == 'Y' ? 'Y' : 'N');

View file

@ -765,6 +765,21 @@
<value xml:lang="en"><![CDATA[Auto-minify scripts]]></value> <value xml:lang="en"><![CDATA[Auto-minify scripts]]></value>
<value xml:lang="jp"><![CDATA[スクリプト自動圧縮]]></value> <value xml:lang="jp"><![CDATA[スクリプト自動圧縮]]></value>
</item> </item>
<item name="cmd_minify_all">
<value xml:lang="ko"><![CDATA[모든 파일을 압축]]></value>
<value xml:lang="en"><![CDATA[All files]]></value>
<value xml:lang="jp"><![CDATA[全てのファイルを圧縮]]></value>
</item>
<item name="cmd_minify_common">
<value xml:lang="ko"><![CDATA[공통 파일만 압축]]></value>
<value xml:lang="en"><![CDATA[Common files only]]></value>
<value xml:lang="jp"><![CDATA[共通のファイルを圧縮]]></value>
</item>
<item name="cmd_minify_none">
<value xml:lang="ko"><![CDATA[압축하지 않음]]></value>
<value xml:lang="en"><![CDATA[None]]></value>
<value xml:lang="jp"><![CDATA[圧縮されません]]></value>
</item>
<item name="about_minify_scripts"> <item name="about_minify_scripts">
<value xml:lang="ko"><![CDATA[코어와 모든 모듈에 포함된 CSS, JS 파일들을 자동으로 압축(minify)하여 전송합니다.]]></value> <value xml:lang="ko"><![CDATA[코어와 모든 모듈에 포함된 CSS, JS 파일들을 자동으로 압축(minify)하여 전송합니다.]]></value>
<value xml:lang="en"><![CDATA[Automatically minify all CSS and JS scripts in the Core and all modules.]]></value> <value xml:lang="en"><![CDATA[Automatically minify all CSS and JS scripts in the Core and all modules.]]></value>

View file

@ -228,8 +228,9 @@
<div class="x_control-group"> <div class="x_control-group">
<label class="x_control-label">{$lang->minify_scripts}</label> <label class="x_control-label">{$lang->minify_scripts}</label>
<div class="x_controls"> <div class="x_controls">
<label for="minify_scripts_y" class="x_inline"><input type="radio" name="minify_scripts" id="minify_scripts_y" value="Y" checked="checked"|cond="$minify_scripts=='Y'" /> {$lang->cmd_yes}</label> <label for="minify_scripts_none" class="x_inline"><input type="radio" name="minify_scripts" id="minify_scripts_none" value="none" checked="checked"|cond="$minify_scripts=='none'" /> {$lang->cmd_minify_none}</label>
<label for="minify_scripts_n" class="x_inline"><input type="radio" name="minify_scripts" id="minify_scripts_n" value="N" checked="checked"|cond="$minify_scripts!='Y'" /> {$lang->cmd_no}</label> <label for="minify_scripts_common" class="x_inline"><input type="radio" name="minify_scripts" id="minify_scripts_common" value="common" checked="checked"|cond="$minify_scripts!='all' && $minify_scripts!='none'" /> {$lang->cmd_minify_common}</label>
<label for="minify_scripts_all" class="x_inline"><input type="radio" name="minify_scripts" id="minify_scripts_all" value="all" checked="checked"|cond="$minify_scripts=='all'" /> {$lang->cmd_minify_all}</label>
<br /> <br />
<p class="x_help-block">{$lang->about_minify_scripts}</p> <p class="x_help-block">{$lang->about_minify_scripts}</p>
</div> </div>

View file

@ -97,7 +97,7 @@ class installAdminController extends install
if($qmail_compatibility!='Y') $qmail_compatibility = 'N'; if($qmail_compatibility!='Y') $qmail_compatibility = 'N';
$minify_scripts = Context::get('minify_scripts'); $minify_scripts = Context::get('minify_scripts');
if($minify_scripts!='Y') $minify_scripts = 'N'; if(!$minify_scripts) $minify_scripts = 'common';
$cache_friendly = Context::get('cache_friendly'); $cache_friendly = Context::get('cache_friendly');
if($cache_friendly!='Y') $cache_friendly = 'N'; if($cache_friendly!='Y') $cache_friendly = 'N';

View file

@ -16,7 +16,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
HTMLDisplayHandler::$reservedCSS = '/xxx$/'; HTMLDisplayHandler::$reservedCSS = '/xxx$/';
HTMLDisplayHandler::$reservedJS = '/xxx$/'; HTMLDisplayHandler::$reservedJS = '/xxx$/';
$db_info = Context::getDBInfo() ?: new stdClass; $db_info = Context::getDBInfo() ?: new stdClass;
$db_info->minify_scripts = 'N'; $db_info->minify_scripts = 'none';
Context::setDBInfo($db_info); Context::setDBInfo($db_info);
$this->specify("js(head)", function() use($db_info) { $this->specify("js(head)", function() use($db_info) {
@ -27,7 +27,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/common.js', 'head')); $handler->loadFile(array('./common/js/common.js', 'head'));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'body')); $handler->loadFile(array('./common/js/xml_js_filter.js', 'body'));
if($db_info->minify_scripts === 'N') if($db_info->minify_scripts === 'none')
{ {
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null); $expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
} }
@ -45,7 +45,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/jquery.js', 'body')); $handler->loadFile(array('./common/js/jquery.js', 'body'));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head')); $handler->loadFile(array('./common/js/xml_js_filter.js', 'head'));
if($db_info->minify_scripts === 'N') if($db_info->minify_scripts === 'none')
{ {
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null); $expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
} }
@ -78,7 +78,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
if($db_info->minify_scripts === 'N') if($db_info->minify_scripts === 'none')
{ {
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null); $expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
} }
@ -101,7 +101,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
if($db_info->minify_scripts === 'N') if($db_info->minify_scripts === 'none')
{ {
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null); $expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
} }
@ -138,7 +138,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$handler->loadFile(array('./common/js/jquery.js', 'head', 'ie7')); $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie7'));
$handler->loadFile(array('./common/js/jquery.js', 'head', 'ie8')); $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie8'));
if($db_info->minify_scripts === 'N') if($db_info->minify_scripts === 'none')
{ {
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie6'); $expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie6');
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie7'); $expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie7');
@ -200,7 +200,7 @@ class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
$this->assertEquals($handler->getCssFileList(), $expected); $this->assertEquals($handler->getCssFileList(), $expected);
}); });
$db_info->minify_scripts = 'Y'; $db_info->minify_scripts = 'all';
Context::setDBInfo($db_info); Context::setDBInfo($db_info);
FrontEndFileHandler::$minify = null; FrontEndFileHandler::$minify = null;