You are here

public function JsMinifier::optimize in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::optimize()

Optimize the asset's content.

Parameters

string $content: The content to optimize.

array $asset: A core asset array.

array $data: The cache data.

Return value

string The optimized content.

Overrides SingleAssetOptimizerBase::optimize

File

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

Class

JsMinifier
Optimizes a JavaScript asset.

Namespace

Drupal\advagg_js_minify\Asset

Code

public function optimize($contents, array $asset, array $data) {

  // Do nothing if js file minification is disabled.
  if (!($minifier = $this->config
    ->get('minifier'))) {
    return $contents;
  }

  // Do not re-minify if the file is already minified.
  if ($this
    ->isMinified($contents)) {
    return $contents;
  }
  $this
    ->clean($contents, $asset);
  $contents_original = $contents;
  $function = $this
    ->getFunction($minifier);
  if (!is_callable($function)) {
    return $contents;
  }
  $arguments = [
    &$contents,
    $asset['data'],
  ];
  call_user_func_array($function, $arguments);
  $contents = trim($contents);

  // Ensure that $data ends with ; or }.
  if (strpbrk(substr(trim($contents), -1), ';})') === FALSE) {
    $contents .= ';';
  }
  if (!$this
    ->isMinificationSuccess($contents, $contents_original)) {
    return $contents_original;
  }
  return $contents;
}