/**
  * @phutil-external-symbol function jsShrink
  */
 public function transformResource($path, $data)
 {
     $type = self::getResourceType($path);
     switch ($type) {
         case 'css':
             $data = $this->replaceCSSPrintRules($path, $data);
             $data = $this->replaceCSSVariables($path, $data);
             $data = preg_replace_callback('@url\\s*\\((\\s*[\'"]?.*?)\\)@s', nonempty($this->translateURICallback, array($this, 'translateResourceURI')), $data);
             break;
     }
     if (!$this->minify) {
         return $data;
     }
     // Some resources won't survive minification (like Raphael.js), and are
     // marked so as not to be minified.
     if (strpos($data, '@' . 'do-not-minify') !== false) {
         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':
             // If `jsxmin` is available, use it. jsxmin is the Javelin minifier and
             // produces the smallest output, but is complicated to build.
             if (Filesystem::binaryExists('jsxmin')) {
                 $future = new ExecFuture('jsxmin __DEV__:0');
                 $future->write($data);
                 list($err, $result) = $future->resolve();
                 if (!$err) {
                     $data = $result;
                     break;
                 }
             }
             // If `jsxmin` is not available, use `JsShrink`, which doesn't compress
             // quite as well but is always available.
             $root = dirname(phutil_get_library_root('phabricator'));
             require_once $root . '/externals/JsShrink/jsShrink.php';
             $data = jsShrink($data);
             break;
     }
     return $data;
 }
Beispiel #2
0
<?php

include dirname(__FILE__) . "/../jsShrink.php";
foreach (glob(dirname(__FILE__) . "/input/*") as $filename) {
    $file = file_get_contents($filename);
    $shrinked = jsShrink($file);
    $expect = dirname(__FILE__) . "/expect/" . basename($filename);
    if (!file_exists($expect)) {
        file_put_contents($expect, $shrinked);
        echo basename($filename) . " created.\n";
    } elseif ($shrinked !== file_get_contents($expect)) {
        echo basename($filename) . " failed.\n";
    }
}