You are here

public function JsOptimizer::optimize in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Asset/JsOptimizer.php \Drupal\Core\Asset\JsOptimizer::optimize()

Optimizes an asset.

Parameters

array $asset: An asset.

Return value

string The optimized asset's contents.

Overrides AssetOptimizerInterface::optimize

File

core/lib/Drupal/Core/Asset/JsOptimizer.php, line 20
Contains \Drupal\Core\Asset\JsOptimizer.

Class

JsOptimizer
Optimizes a JavaScript asset.

Namespace

Drupal\Core\Asset

Code

public function optimize(array $js_asset) {
  if ($js_asset['type'] !== 'file') {
    throw new \Exception('Only file JavaScript assets can be optimized.');
  }
  if ($js_asset['type'] === 'file' && !$js_asset['preprocess']) {
    throw new \Exception('Only file JavaScript assets with preprocessing enabled can be optimized.');
  }

  // If a BOM is found, convert the file to UTF-8, then use substr() to
  // remove the BOM from the result.
  $data = file_get_contents($js_asset['data']);
  if ($encoding = Unicode::encodingFromBOM($data)) {
    $data = Unicode::substr(Unicode::convertToUtf8($data, $encoding), 1);
  }
  elseif (isset($js_asset['attributes']['charset'])) {
    $data = Unicode::convertToUtf8($data, $js_asset['attributes']['charset']);
  }

  // No-op optimizer: no optimizations are applied to JavaScript assets.
  return $data;
}