Same name in other branches
- 5.0.x advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::optimize()
- 6.0.x advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::optimize()
- 8.x-4.x advagg_js_minify/src/Asset/JsMinifier.php \Drupal\advagg_js_minify\Asset\JsMinifier::optimize()
Overrides SingleAssetOptimizerBase::optimize
File
-
advagg_js_minify/
src/ Asset/ JsMinifier.php, line 34
Class
- JsMinifier
- Optimizes a JavaScript asset.
Namespace
Drupal\advagg_js_minify\AssetCode
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;
}