Skip to content

Commit

Permalink
added UglifyJs2Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed Jan 9, 2013
1 parent ba1905e commit 36807f5
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 18 deletions.
105 changes: 105 additions & 0 deletions src/Assetic/Filter/UglifyJs2Filter.php
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2012 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Assetic\Filter;

use Assetic\Asset\AssetInterface;
use Assetic\Exception\FilterException;

/**
* UglifyJs2 filter.
*
* @link http://lisperator.net/uglifyjs
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
*/
class UglifyJs2Filter extends BaseNodeFilter
{
private $uglifyjsBin;
private $nodeBin;
private $compress;
private $beautify;
private $mangle;

public function __construct($uglifyjsBin = '/usr/bin/uglifyjs', $nodeBin = null)
{
$this->uglifyjsBin = $uglifyjsBin;
$this->nodeBin = $nodeBin;
}

public function setCompress($compress)
{
$this->compress = $compress;
}

public function setBeautify($beautify)
{
$this->beautify = $beautify;
}

public function setMangle($mangle)
{
$this->mangle = $mangle;
}

public function filterLoad(AssetInterface $asset)
{
}

public function filterDump(AssetInterface $asset)
{
$pb = $this->createProcessBuilder($this->nodeBin
? array($this->nodeBin, $this->uglifyjsBin)
: array($this->uglifyjsBin));

if ($this->compress) {
$pb->add('--compress');
}

if ($this->beautify) {
$pb->add('--beautify');
}

if ($this->mangle) {
$pb->add('--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.');
}

$asset->setContent(file_get_contents($output));

unlink($output);
}
}
116 changes: 116 additions & 0 deletions tests/Assetic/Test/Filter/UglifyJs2FilterTest.php
@@ -0,0 +1,116 @@
<?php

/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2012 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Assetic\Test\Filter;

use Assetic\Asset\FileAsset;
use Assetic\Filter\UglifyJs2Filter;
use Symfony\Component\Process\ProcessBuilder;

/**
* @group integration
*/
class UglifyJs2FilterTest extends FilterTestCase
{
private $asset;
private $filter;

protected function setUp()
{
$uglifyjsBin = $this->findExecutable('uglifyjs', 'UGLIFYJS2_BIN');
$nodeBin = $this->findExecutable('node', 'NODE_BIN');
if (!$uglifyjsBin) {
$this->markTestSkipped('Unable to find `uglifyjs` executable.');
}

// verify uglifyjs version
$pb = new ProcessBuilder($nodeBin ? array($nodeBin, $uglifyjsBin) : array($uglifyjsBin));
$pb->add('--version');
if (isset($_SERVER['NODE_PATH'])) {
$pb->setEnv('NODE_PATH', $_SERVER['NODE_PATH']);
}
if (0 !== $pb->getProcess()->run()) {
$this->markTestSkipped('Incorrect version of UglifyJs');
}

$this->asset = new FileAsset(__DIR__.'/fixtures/uglifyjs/script.js');
$this->asset->load();

$this->filter = new UglifyJs2Filter($uglifyjsBin, $nodeBin);
}

protected function tearDown()
{
$this->asset = null;
$this->filter = null;
}

public function testUglify()
{
$this->filter->filterDump($this->asset);

$expected = '(function(){var foo=new Array(1,2,3,4);var bar=Array(a,b,c);var var1=new Array(5);var var2=new Array(a);function bar(foo){var2.push(foo);return foo}var foo=function(var1){return var1};foo("abc123");bar("abc123")})();';
$this->assertEquals($expected, $this->asset->getContent());
}

public function testCompress()
{
$this->filter->setCompress(true);
$this->filter->filterDump($this->asset);

$expected = '(function(){function bar(foo){return var2.push(foo),foo}var foo=[1,2,3,4],bar=[a,b,c];Array(5);var var2=Array(a),foo=function(var1){return var1};foo("abc123"),bar("abc123")})();';
$this->assertEquals($expected, $this->asset->getContent());
}

public function testMangle()
{
$this->filter->setMangle(true);
$this->filter->filterDump($this->asset);

$expected = '(function(){var r=new Array(1,2,3,4);var n=Array(a,b,c);var u=new Array(5);var e=new Array(a);function n(r){e.push(r);return r}var r=function(r){return r};r("abc123");n("abc123")})();';
$this->assertEquals($expected, $this->asset->getContent());
}

public function testCompressAndMangle()
{
$this->filter->setCompress(true);
$this->filter->setMangle(true);
$this->filter->filterDump($this->asset);

$expected = '(function(){function r(r){return u.push(r),r}var n=[1,2,3,4],r=[a,b,c];Array(5);var u=Array(a),n=function(r){return r};n("abc123"),r("abc123")})();';
$this->assertEquals($expected, $this->asset->getContent());
}

public function testBeautify()
{
$this->filter->setBeautify(true);
$this->filter->filterDump($this->asset);

$expected = <<<JS
(function() {
var foo = new Array(1, 2, 3, 4);
var bar = Array(a, b, c);
var var1 = new Array(5);
var var2 = new Array(a);
function bar(foo) {
var2.push(foo);
return foo;
}
var foo = function(var1) {
return var1;
};
foo("abc123");
bar("abc123");
})();
JS;
$this->assertEquals($expected, $this->asset->getContent());
}
}
38 changes: 20 additions & 18 deletions tests/Assetic/Test/Filter/UglifyJsFilterTest.php
Expand Up @@ -60,9 +60,9 @@ public function testUglify()
$expected = <<<JS
/**
* Copyright
*/function bar(e){return var2.push(e),e}var foo=new Array(1,2,3,4),bar=Array(a,b,c),var1=new Array(5),var2=new Array(a),foo=function(e){return e};
*/(function(){function t(e){return r.push(e),e}var e=new Array(1,2,3,4),t=Array(a,b,c),n=new Array(5),r=new Array(a),e=function(e){return e};e("abc123"),t("abc123")})();
JS;
$this->assertSame($expected, $this->asset->getContent());
$this->assertEquals($expected, $this->asset->getContent());
}

public function testUnsafeUglify()
Expand All @@ -73,9 +73,9 @@ public function testUnsafeUglify()
$expected = <<<JS
/**
* Copyright
*/function bar(e){return var2.push(e),e}var foo=[1,2,3,4],bar=[a,b,c],var1=Array(5),var2=Array(a),foo=function(e){return e};
*/(function(){function t(e){return r.push(e),e}var e=[1,2,3,4],t=[a,b,c],n=Array(5),r=Array(a),e=function(e){return e};e("abc123"),t("abc123")})();
JS;
$this->assertSame($expected, $this->asset->getContent());
$this->assertEquals($expected, $this->asset->getContent());
}

public function testBeautifyUglify()
Expand All @@ -86,38 +86,40 @@ public function testBeautifyUglify()
$expected = <<<JS
/**
* Copyright
*/function bar(e) {
return var2.push(e), e;
}
var foo = new Array(1, 2, 3, 4), bar = Array(a, b, c), var1 = new Array(5), var2 = new Array(a), foo = function(e) {
return e;
};
*/(function() {
function t(e) {
return r.push(e), e;
}
var e = new Array(1, 2, 3, 4), t = Array(a, b, c), n = new Array(5), r = new Array(a), e = function(e) {
return e;
};
e("abc123"), t("abc123");
})();
JS;

$this->assertSame($expected, $this->asset->getContent());
$this->assertEquals($expected, $this->asset->getContent());
}

public function testMangleUglify()
public function testNoMangleUglify()
{
$this->filter->setMangle(true);
$this->filter->setMangle(false);
$this->filter->filterDump($this->asset);

$expected = <<<JS
/**
* Copyright
*/function bar(e){return var2.push(e),e}var foo=new Array(1,2,3,4),bar=Array(a,b,c),var1=new Array(5),var2=new Array(a),foo=function(e){return e};
*/(function(){function bar(foo){return var2.push(foo),foo}var foo=new Array(1,2,3,4),bar=Array(a,b,c),var1=new Array(5),var2=new Array(a),foo=function(var1){return var1};foo("abc123"),bar("abc123")})();
JS;

$this->assertSame($expected, $this->asset->getContent());
$this->assertEquals($expected, $this->asset->getContent());
}

public function testNoCopyrightUglify()
{
$this->filter->setNoCopyright(true);
$this->filter->filterDump($this->asset);

$expected = 'function bar(e){return var2.push(e),e}var foo=new Array(1,2,3,4),bar=Array(a,b,c),var1=new Array(5),var2=new Array(a),foo=function(e){return e};';
$this->assertSame($expected, $this->asset->getContent());
$expected = '(function(){function t(e){return r.push(e),e}var e=new Array(1,2,3,4),t=Array(a,b,c),n=new Array(5),r=new Array(a),e=function(e){return e};e("abc123"),t("abc123")})();';
$this->assertEquals($expected, $this->asset->getContent());
}
}
7 changes: 7 additions & 0 deletions tests/Assetic/Test/Filter/fixtures/uglifyjs/script.js
Expand Up @@ -2,6 +2,8 @@
* Copyright
*/

(function() {

var foo = new Array(1, 2, 3, 4);
var bar = Array(a, b, c);
var var1 = new Array(5);
Expand All @@ -16,3 +18,8 @@ function bar(foo) {
var foo = function (var1) {
return var1;
}

foo('abc123')
bar('abc123')

})()

0 comments on commit 36807f5

Please sign in to comment.