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

Minify a JS string using jsmin+.

Parameters

string $contents: Javascript string.

File

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

Class

JsMinifier
Optimizes a JavaScript asset.

Namespace

Drupal\advagg_js_minify\Asset

Code

public function minifyJsminplus(&$contents, $path) {
    $contents_before = $contents;
    // Only include jsminplus.inc if the JSMinPlus class doesn't exist.
    if (!class_exists('\\JSMinPlus')) {
        include $this->moduleExtensionList
            ->getPath('advagg_js_minify') . '/jsminplus.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 {
        // JSMin+ the contents of the aggregated file.
        $contents = \JSMinPlus::minify($contents);
        // Capture any output from JSMinPlus.
        $error = trim(ob_get_contents());
        if (!empty($error)) {
            throw new \Exception($error);
        }
    } catch (\Exception $e) {
        // Log exception thrown by JSMin+ and roll back to uncompressed content.
        $this->logger
            ->warning('JSMinPlus had a possible error minifying: @file. Using uncompressed version. Error: ' . $e->getMessage(), [
            '@file' => $path,
        ]);
        $contents = $contents_before;
    }
    ob_end_clean();
}