public function UglifyJsFilter::filterDump

Run the asset through UglifyJs

Overrides FilterInterface::filterDump

See also

Assetic\Filter\FilterInterface::filterDump()

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/UglifyJsFilter.php, line 91

Class

UglifyJsFilter
UglifyJs filter.

Namespace

Assetic\Filter

Code

public function filterDump(AssetInterface $asset) {
  $pb = $this
    ->createProcessBuilder($this->nodeBin ? array(
    $this->nodeBin,
    $this->uglifyjsBin,
  ) : array(
    $this->uglifyjsBin,
  ));
  if ($this->noCopyright) {
    $pb
      ->add('--no-copyright');
  }
  if ($this->beautify) {
    $pb
      ->add('--beautify');
  }
  if ($this->unsafe) {
    $pb
      ->add('--unsafe');
  }
  if (false === $this->mangle) {
    $pb
      ->add('--no-mangle');
  }

  // input and output files
  $input = tempnam(sys_get_temp_dir(), 'input');
  $output = tempnam(sys_get_temp_dir(), 'output');
  file_put_contents($input, $asset
    ->getContent());
  $pb
    ->add('-o')
    ->add($output)
    ->add($input);
  $proc = $pb
    ->getProcess();
  $code = $proc
    ->run();
  unlink($input);
  if (0 !== $code) {
    if (file_exists($output)) {
      unlink($output);
    }
    if (127 === $code) {
      throw new \RuntimeException('Path to node executable could not be resolved.');
    }
    throw FilterException::fromProcess($proc)
      ->setInput($asset
      ->getContent());
  }
  if (!file_exists($output)) {
    throw new \RuntimeException('Error creating output file.');
  }
  $uglifiedJs = file_get_contents($output);
  unlink($output);
  $asset
    ->setContent($uglifiedJs);
}