JsOptimizer.php in Drupal 9
File
core/lib/Drupal/Core/Asset/JsOptimizer.php
View source
<?php
namespace Drupal\Core\Asset;
use Drupal\Component\Utility\Unicode;
class JsOptimizer implements AssetOptimizerInterface {
public function optimize(array $js_asset) {
if ($js_asset['type'] !== 'file') {
throw new \Exception('Only file JavaScript assets can be optimized.');
}
if (!$js_asset['preprocess']) {
throw new \Exception('Only file JavaScript assets with preprocessing enabled can be optimized.');
}
$data = file_get_contents($js_asset['data']);
if ($encoding = Unicode::encodingFromBOM($data)) {
$data = mb_substr(Unicode::convertToUtf8($data, $encoding), 1);
}
elseif (isset($js_asset['attributes']['charset'])) {
$data = Unicode::convertToUtf8($data, $js_asset['attributes']['charset']);
}
return $data;
}
public function clean($contents) {
$contents = preg_replace('/\\/\\/(#|@)\\s(sourceURL|sourceMappingURL)=\\s*(\\S*?)\\s*$/m', '', $contents);
return $contents;
}
}