1. 8.5.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer
  2. 8.0.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer
  3. 8.1.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer
  4. 8.2.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer
  5. 8.3.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer
  6. 8.4.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer
  7. 8.6.x core/lib/Drupal/Core/Asset/JsOptimizer.php JsOptimizer

Optimizes a JavaScript asset.


Expanded class hierarchy of JsOptimizer

1 file declares its use of JsOptimizer
JsOptimizerUnitTest.php in core/tests/Drupal/Tests/Core/Asset/JsOptimizerUnitTest.php
1 string reference to 'JsOptimizer'
core.services.yml in core/core.services.yml


core/lib/Drupal/Core/Asset/JsOptimizer.php, line 10


View source
class JsOptimizer implements AssetOptimizerInterface {

   * {@inheritdoc}
  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.');

    // 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;

   * Processes the contents of a javascript asset for cleanup.
   * @param string $contents
   *   The contents of the javascript asset.
   * @return string
   *   Contents of the javascript asset.
  public function clean($contents) {

    // Remove JS source and source mapping urls or these may cause 404 errors.
    $contents = preg_replace('/\\/\\/(#|@)\\s(sourceURL|sourceMappingURL)=\\s*(\\S*?)\\s*$/m', '', $contents);
    return $contents;



Contains filters are case sensitive
Namesort descending Modifiers Type Description Overrides
JsOptimizer::clean public function Processes the contents of a javascript asset for cleanup. Overrides AssetOptimizerInterface::clean
JsOptimizer::optimize public function Optimizes an asset. Overrides AssetOptimizerInterface::optimize