1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-24 14:42:50 +02:00

Merge pull request #556 from glensc/closure-compiler-options

add support for closure compiler options
This commit is contained in:
Elan Ruusamäe
2016-12-06 20:17:15 +02:00
committed by GitHub
11 changed files with 84 additions and 31 deletions

1
.gitignore vendored
View File

@@ -8,3 +8,4 @@
/composer.lock
/vendor
/.php_cs.cache
/tests/compiler.jar

View File

@@ -25,8 +25,14 @@ cache:
install:
- composer update --no-interaction --prefer-source
before_script:
- wget -c https://dl.google.com/closure-compiler/compiler-latest.zip -O vendor/compiler-latest.zip
- unzip -fd vendor/closure-compiler vendor/compiler-latest.zip
- ln -sfn ../$(echo vendor/closure-compiler/closure-compiler-*.jar) tests/compiler.jar
- java -jar tests/compiler.jar --version
script:
- composer validate
- phpunit
- phpunit --verbose
# vim:ts=2:sw=2:et

View File

@@ -24,19 +24,12 @@
*
* </code>
*
* @todo unit tests, $options docs
* @todo more options support (or should just passthru them all?)
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author Elan Ruusamäe <glen@delfi.ee>
*/
class Minify_ClosureCompiler
{
const OPTION_CHARSET = 'charset';
const OPTION_COMPILATION_LEVEL = 'compilation_level';
const OPTION_WARNING_LEVEL = 'warning_level';
public static $isDebug = false;
/**
@@ -61,6 +54,17 @@ class Minify_ClosureCompiler
*/
public static $javaExecutable = 'java';
/**
* Default command line options passed to closure-compiler
*
* @var array
*/
public static $defaultOptions = array(
'charset' => 'utf-8',
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
'warning_level' => 'QUIET',
);
/**
* Minify a Javascript string
*
@@ -137,7 +141,8 @@ class Minify_ClosureCompiler
$this->checkJar(self::$jarFile);
$server = array(
self::$javaExecutable,
'-jar', escapeshellarg(self::$jarFile)
'-jar',
escapeshellarg(self::$jarFile)
);
return $server;
@@ -151,24 +156,13 @@ class Minify_ClosureCompiler
{
$args = array();
$o = array_merge(
array(
self::OPTION_CHARSET => 'utf-8',
self::OPTION_COMPILATION_LEVEL => 'SIMPLE_OPTIMIZATIONS',
self::OPTION_WARNING_LEVEL => 'QUIET',
),
$options = array_merge(
static::$defaultOptions,
$userOptions
);
$charsetOption = $o[self::OPTION_CHARSET];
if (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $charsetOption)) {
$args[] = "--charset {$charsetOption}";
}
foreach (array(self::OPTION_COMPILATION_LEVEL, self::OPTION_WARNING_LEVEL) as $opt) {
if ($o[$opt]) {
$args[] = "--{$opt} " . escapeshellarg($o[$opt]);
}
foreach ($options as $key => $value) {
$args[] = "--{$key} " . escapeshellarg($value);
}
return $args;

View File

@@ -24,9 +24,7 @@ class LessSourceTest extends TestCase
touch($includedLess);
$mtime1 = filemtime($mainLess);
var_dump($mtime1);
$mtime2 = filemtime($includedLess);
var_dump($mtime2);
$max = max($mtime1, $mtime2);

View File

@@ -63,7 +63,7 @@ class MinifyClosureCompilerTest extends TestCase
$src = "function unused() {};";
$minExpected = '';
$options = array(
Minify_ClosureCompiler::OPTION_COMPILATION_LEVEL => 'ADVANCED_OPTIMIZATIONS'
'compilation_level' => 'ADVANCED_OPTIMIZATIONS'
);
$minOutput = Minify_ClosureCompiler::minify($src, $options);
$this->assertSame($minExpected, $minOutput, 'advanced optimizations');
@@ -80,12 +80,36 @@ class MinifyClosureCompilerTest extends TestCase
{
$this->assertHasJar();
$src = file_get_contents(self::$test_files . '/bug-513.js');
$src = $this->getDataFile('bug-513.js');
$minExpected = 'var a=4;';
$minOutput = Minify_ClosureCompiler::minify($src);
$this->assertSame($minExpected, $minOutput, 'advanced optimizations');
}
/**
* Test that language_in parameter has effect.
*/
public function testLanguageOptions()
{
$this->assertHasJar();
$src = $this->getDataFile('js/jscomp.polyfill.js');
$exp = $this->getDataFile('js/jscomp.polyfill.min.js');
$options = array(
'language_in' => 'ECMASCRIPT3',
);
$res = Minify_ClosureCompiler::minify($src, $options);
$this->assertSame($exp, $res);
$options = array(
'language_in' => 'ECMASCRIPT6',
);
$exp = $this->getDataFile('js/jscomp.polyfilled.min.js');
$res = Minify_ClosureCompiler::minify($src, $options);
$this->assertSame($exp, $res);
}
protected function assertHasJar()
{
$this->assertNotEmpty(Minify_ClosureCompiler::$jarFile);

View File

@@ -20,7 +20,7 @@ class MinifyNailgunClosureCompilerTest extends TestCase
/**
* Test minimisation with the minimum necessary settings
*/
public function test2()
public function test1()
{
$this->assertHasJar();
$src = "
@@ -40,8 +40,10 @@ class MinifyNailgunClosureCompilerTest extends TestCase
protected function assertHasJar()
{
$this->assertNotEmpty(Minify_ClosureCompiler::$jarFile);
$this->assertNotEmpty(Minify_NailgunClosureCompiler::$ngJarFile);
try {
$this->assertFileExists(Minify_ClosureCompiler::$jarFile, "Have closure compiler compiler.jar");
$this->assertFileExists(Minify_NailgunClosureCompiler::$ngJarFile, "Have nailgun.jar");
} catch (Exception $e) {
$this->markTestSkipped($e->getMessage());
}

View File

@@ -24,9 +24,7 @@ class ScssSourceTest extends TestCase
touch($includedLess);
$mtime1 = filemtime($mainLess);
var_dump($mtime1);
$mtime2 = filemtime($includedLess);
var_dump($mtime2);
$max = max($mtime1, $mtime2);

View File

@@ -47,4 +47,23 @@ class TestCase extends PHPUnit_Framework_TestCase
$this->assertSame($data, $displayed, "$id display");
$this->assertEquals($data, $cache->fetch($id), "$id fetch");
}
/**
* Read data file, assert that it exists and is not empty.
* As a side effect calls trim() to fight against different Editors that insert or strip final newline.
*
* @param string $filename
* @return string
*/
protected function getDataFile($filename)
{
$path = self::$test_files . '/' . $filename;
$this->assertFileExists($path);
$contents = file_get_contents($path);
$this->assertNotEmpty($contents);
$contents = trim($contents);
$this->assertNotEmpty($contents);
return $contents;
}
}

View File

@@ -0,0 +1,7 @@
(function() {
/**
* @type {string}
*/
var $array = jQuery.find('#div');
print($array.find('a'));
})();

View File

@@ -0,0 +1 @@
(function(){var a=jQuery.find("#div");print(a.find("a"))})();

View File

@@ -0,0 +1,3 @@
var $jscomp={scope:{},findInternal:function(a,c,b){a instanceof String&&(a=String(a));for(var d=a.length,e=0;e<d;e++){var f=a[e];if(c.call(b,f,e,a))return{i:e,v:f}}return{i:-1,v:void 0}}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,b){if(b.get||b.set)throw new TypeError("ES3 does not support getters and setters.");a!=Array.prototype&&a!=Object.prototype&&(a[c]=b.value)};
$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(a,c,b,d){if(c){b=$jscomp.global;a=a.split(".");for(d=0;d<a.length-1;d++){var e=a[d];e in b||(b[e]={});b=b[e]}a=a[a.length-1];d=b[a];c=c(d);c!=d&&null!=c&&$jscomp.defineProperty(b,a,{configurable:!0,writable:!0,value:c})}};
$jscomp.polyfill("Array.prototype.find",function(a){return a?a:function(a,b){return $jscomp.findInternal(this,a,b).v}},"es6-impl","es3");(function(){var a=jQuery.find("#div");print(a.find("a"))})();