Use JsShrink
if jsxmin
is not available
Summary: If `jsxmin` is not available, use a pure PHP implementation instead (JsShrink). Test Plan: - Ran `arc lint --lintall` on all JS and fixed every relevant warning. - Forced minification on and browsed around the site using JS behaviors. Didn't hit anything problematic. Reviewers: vrana, btrahan Reviewed By: vrana CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D5670
This commit is contained in:
45
externals/JsShrink/jsShrink.php
vendored
Normal file
45
externals/JsShrink/jsShrink.php
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/** Remove spaces and comments from JavaScript code
|
||||
* @param string code with commands terminated by semicolon
|
||||
* @return string shrinked code
|
||||
* @link http://vrana.github.com/JsShrink/
|
||||
* @author Jakub Vrana, http://www.vrana.cz/
|
||||
* @copyright 2007 Jakub Vrana
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
|
||||
*/
|
||||
function jsShrink($input) {
|
||||
return preg_replace_callback('(
|
||||
(?:
|
||||
(^|[-+\([{}=,:;!%^&*|?~]|/(?![/*])|return|throw) # context before regexp
|
||||
(?:\s|//[^\n]*+\n|/\*(?:[^*]|\*(?!/))*+\*/)* # optional space
|
||||
(/(?![/*])(?:\\\\[^\n]|[^[\n/\\\\]|\[(?:\\\\[^\n]|[^]])++)+/) # regexp
|
||||
|(^
|
||||
|\'(?:\\\\.|[^\n\'\\\\])*\'
|
||||
|"(?:\\\\.|[^\n"\\\\])*"
|
||||
|([0-9A-Za-z_$]+)
|
||||
|([-+]+)
|
||||
|.
|
||||
)
|
||||
)(?:\s|//[^\n]*+\n|/\*(?:[^*]|\*(?!/))*+\*/)* # optional space
|
||||
)sx', 'jsShrinkCallback', "$input\n");
|
||||
}
|
||||
|
||||
function jsShrinkCallback($match) {
|
||||
static $last = '';
|
||||
$match += array_fill(1, 5, null); // avoid E_NOTICE
|
||||
list(, $context, $regexp, $result, $word, $operator) = $match;
|
||||
if ($word != '') {
|
||||
$result = ($last == 'word' ? "\n" : ($last == 'return' ? " " : "")) . $result;
|
||||
$last = ($word == 'return' || $word == 'throw' || $word == 'break' ? 'return' : 'word');
|
||||
} elseif ($operator) {
|
||||
$result = ($last == $operator[0] ? "\n" : "") . $result;
|
||||
$last = $operator[0];
|
||||
} else {
|
||||
if ($regexp) {
|
||||
$result = $context . ($context == '/' ? "\n" : "") . $regexp;
|
||||
}
|
||||
$last = '';
|
||||
}
|
||||
return $result;
|
||||
}
|
23
externals/JsShrink/readme.txt
vendored
Normal file
23
externals/JsShrink/readme.txt
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
JsShrink - Remove spaces and comments from JavaScript code
|
||||
Available in PHP and JavaScript
|
||||
Requires statements ending by semicolon, use JSHint or JSLint to verify.
|
||||
|
||||
http://vrana.github.com/JsShrink/
|
||||
|
||||
Usage PHP:
|
||||
<?php
|
||||
include "jsShrink.php";
|
||||
echo jsShrink($code);
|
||||
?>
|
||||
|
||||
Usage JavaScript:
|
||||
<script type="text/javascript" src="jsShrink.js"></script>
|
||||
<script type="text/javascript">
|
||||
textarea.value = jsShrink(code);
|
||||
</script>
|
||||
|
||||
Note:
|
||||
Google Closure Compiler is much more powerful and efficient tool.
|
||||
JsShrink was created for those looking for PHP or JavaScript only solution.
|
||||
Most other JS minifiers are not able to process valid JavaScript code:
|
||||
http://php.vrana.cz/minifikace-javascriptu.php#srovnani
|
Reference in New Issue
Block a user