Same name in other branches
  1. 5.0.x advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::minifyJshrink()
  2. 6.0.x advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::minifyJshrink()
  3. 8.x-4.x advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::minifyJshrink()

Minify a JS string using jshrink.

Parameters

string $contents: Javascript string.

File

advagg_js_minify/src/Asset/JsMinifier.php, line 239

Class

JsMinifier
Optimizes a JavaScript asset.

Namespace

Drupal\advagg_js_minify\Asset

Code

public function minifyJshrink(&$contents) {
    $contents_before = $contents;
    // Only include jshrink.inc if the JShrink\Minifier class doesn't exist.
    if (!class_exists('\\JShrink\\Minifier')) {
        include drupal_get_path('module', 'advagg_js_minify') . '/jshrink.inc';
        $nesting_level = ini_get('xdebug.max_nesting_level');
        if (!empty($nesting_level) && $nesting_level < 200) {
            ini_set('xdebug.max_nesting_level', 200);
        }
    }
    ob_start();
    try {
        // JShrink the contents of the aggregated file.
        // @codingStandardsIgnoreLine
        $contents = \JShrink\Minifier::minify($contents, [
            'flaggedComments' => FALSE,
        ]);
        // Capture any output from JShrink.
        $error = trim(ob_get_contents());
        if (!empty($error)) {
            throw new \Exception($error);
        }
    } catch (\Exception $e) {
        // Log the JShrink exception and rollback to uncompressed content.
        $this->logger
            ->warning($e->getMessage(), []);
        $contents = $contents_before;
    }
    ob_end_clean();
}