#19692161 Use minified JS or CSS files if __DEBUG__ is not true.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8314 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
taggon 2011-04-14 12:29:12 +00:00
parent a2851cc242
commit 0bdf86ef26
14 changed files with 9397 additions and 11 deletions

1
tools/index.html Normal file
View file

@ -0,0 +1 @@

171
tools/minify.php Normal file
View file

@ -0,0 +1,171 @@
<?php
/**
* Minify
* This script comnbines multiple JavaScript or CSS files with minifying.
* PHP 5.3.0 or above version is required.
*
* Usage : php minify.php [TARGET_DIR ...]
* TARGET_DIR use the current working directory as a default path.
*
* @author NHN(developer@xpressengine.com)
*/
if(version_compare(PHP_VERSION, '5.3.0', '<')) {
echo "PHP 5.3.0 or above version is required.";
exit(1);
}
function main() {
$argv = $_SERVER['argv'];
$argc = $_SERVER['argc'];
// get target directories
if($argc < 2) $dirs = array($_SERVER['PWD']);
else $dirs = array_slice($argv, 1);
$dirs = array_map('realpath', $dirs);
$dirs = array_map('add_dirsep', $dirs);
array_walk($dirs, 'execute');
}
// add directory separator
function add_dirsep($path) {
if(substr($path,-1) != DIRECTORY_SEPARATOR) $path .= DIRECTORY_SEPARATOR;
return $path;
}
function execute($dir) {
echo "Processing : {$dir}\n";
// parse config file if it exists
echo " Finding predefined configuration file...";
$config = read_config($dir);
echo " Done\n";
// merge
foreach($config['merge'] as $target=>$files) {
merge($files, $target, $dir);
}
// files to skip
$files_to_skip = $config['skip'];
foreach($files_to_skip as $idx=>$file) {
if($file) $files_to_skip[$idx] = realpath($dir.trim($file));
}
echo " Minifying JavaScript files...";
$js_files = get_target_files('js', $dir, $files_to_skip);
if(count($js_files) && !class_exists('JSMinPlus')) {
require dirname(__FILE__).'/minify/jsminplus/jsminplus.php';
}
foreach($js_files as $file) {
if(!is_readable($file)) continue;
$target = preg_replace('@\.js$@', '.min.js', $file);
$content = file_get_contents($file);
// save copyright to preserve it
if(preg_match('@^[ \t]*(/\*\*.+?\*/)@s', $content, $matches)) {
$copyright = $matches[1]."\n";
} else {
$copyright = '';
}
file_put_contents($target, $copyright.JSMinPlus::minify($content));
echo '.';
}
echo " Done\n";
echo " Minifying CSS files...";
$css_files = get_target_files('css', $dir, $files_to_skip);
if(count($css_files) && !class_exists('CssMin')) {
require dirname(__FILE__).'/minify/cssmin/cssmin.php';
}
foreach($css_files as $file) {
if(!is_readable($file)) continue;
$target = preg_replace('@\.css$@', '.min.css', $file);
$content = file_get_contents($file);
file_put_contents($target, $copyright.CssMin::minify($content, $option));
echo '.';
}
echo " Done\n";
}
function read_config($dir) {
$default = array('option'=>array(), 'skip'=>array(), 'merge'=>array());
$file = $dir.'minify.ini';
if(!is_readable($file)) return $default;
$config_str = file_get_contents($file);
$config_str = preg_replace_callback('/(\[(?:skip|merge *>> *.+?)\])([\s\S]+?)(?=\[|$)/', 'transform_config_str', $config_str);
$config = parse_ini_string($config_str, 1);
if($config === false) return $default;
if(is_array($config['skip'])) $config['skip'] = array_keys($config['skip']);
foreach($config as $section=>$value) {
if(preg_match('/merge *>> *(.+)/', $section, $match)) {
if(!is_array($config['merge'])) $config['merge'] = array();
$config['merge'][trim($match[1])] = array_keys($value);
unset($config[$section]);
}
}
if(is_array($config['option'])) $config = array_merge($config['option'], $config);
$config = array_merge($default, $config);
return $config;
}
function transform_config_str($matches) {
if(!$matches[2]) return $matches[0];
$values = preg_replace('/$/m', '=', trim($matches[2]));
return "{$matches[1]}\n{$values}\n\n";
}
function merge($files, $target, $base_dir) {
if(!is_array($files)) return false;
$body = '';
$is_css = !!preg_match('/\.css$/', $target);
foreach($files as $file) {
$file = $base_dir.trim($file);
if(!is_readable($file)) continue;
$content = trim(file_get_contents($file));
if($is_css && $body) $content = preg_replace('/^@.+?;/m', '', $content);
if($content) $body .= $content."\n";
}
if ($body) {
$file_count = count($files);
echo " Merging {$file_count} files to create {$target} file...";
file_put_contents($base_dir.$target, $body);
echo " Done\n";
}
}
function get_target_files($ext, $dir, $files_to_skip) {
$files = glob("{$dir}*.{$ext}");
$skips = glob("{$dir}*.min.{$ext}");
$skips = array_merge($skips, $files_to_skip);
$files = array_diff($files, $skips);
return $files;
}
// run main function
error_reporting(E_ALL & ~E_NOTICE);
main();

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,62 @@
================================================================================
JSMin+
================================================================================
A javascript minification tool written in PHP
by Tweakers.net / Tino Zijdel <crisp@tweakers.net>
Weblog: http://crisp.tweakblogs.net/blog/cat/716
License: MPL 1.1/GPL 2.0/LGPL 2.1 (see sourcecode)
================================================================================
VERSION HISTORY
================================================================================
17-05-2009 - version 1.3
- Fixed loop-constructs and if with empty body
- Fixed hook:colon precedence issues (https://bugzilla.mozilla.org/show_bug.cgi?id=492445)
- Combine concats of strings with same quotestyle
- Combine var-statements
--------------------------------------------------------------------------------
18-04-2009 - version 1.2
- Fixed crash in PHP 5.2.9 when matching large comments or strings
(due to PCRE backtracking bug)
- Don't add curly braces around statements for a CASE label
- Always put statements for IF between curly braces when followed by an ELSE
- Save some more bytes by omitting the space between RETURN and the return
expression in certain cases
--------------------------------------------------------------------------------
12-04-2009 - version 1.1
- Improved tokenizer performance by reading smaller chunks for matching
- Bugfix: jumplabels for break/continue statements were not written to output
- Improved stringmatching: cases like '\' and '<newline>' now throw unterminated
string literal error
- Fixed linenumber offset bug caused by JScript conditional compilation blocks
- nest() always calls Statement() so no need to use call_user_func() with a parm
--------------------------------------------------------------------------------
09-04-2009 - version 1.0
Initial version
================================================================================
KNOWN ISSUES
================================================================================
- JScript conditional compilation support is incomplete
================================================================================
DOWNLOAD LOCATION
================================================================================
Latest version:
version 1.3 : http://files.tweakers.net/jsminplus/jsminplus.zip
Previous versions:
version 1.2 : http://files.tweakers.net/jsminplus/jsminplus-1.2.zip
version 1.1 : http://files.tweakers.net/jsminplus/jsminplus-1.1.zip
version 1.0 : http://files.tweakers.net/jsminplus/jsminplus-1.0.zip

File diff suppressed because it is too large Load diff