From 41098bea3c9a2aa0b2641db03298675d8604a3d0 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 15:02:16 +0200 Subject: [PATCH] Added CssMinFilter, JsMinFilter, updated LessFilter + tests --- .editorconfig | 2 +- composer.json | 4 ++- src/Filter/CssMinFilter.php | 20 +++++++++++ src/Filter/JsMinFilter.php | 21 +++++++++++ src/Filter/LessFilter.php | 39 +++++--------------- tests/Filter/CssMinFilterTest.php | 41 +++++++++++++++++++++ tests/Filter/JsMinFilterTest.php | 41 +++++++++++++++++++++ tests/Filter/LessFilterTest.php | 7 ++-- tests/fixtures/cssmin.css | 57 ++++++++++++++++++++++++++++++ tests/fixtures/cssmin.css.expected | 1 + tests/fixtures/jsmin.js | 9 +++++ tests/fixtures/jsmin.js.expected | 1 + 12 files changed, 208 insertions(+), 35 deletions(-) create mode 100755 src/Filter/CssMinFilter.php create mode 100755 src/Filter/JsMinFilter.php create mode 100755 tests/Filter/CssMinFilterTest.php create mode 100755 tests/Filter/JsMinFilterTest.php create mode 100755 tests/fixtures/cssmin.css create mode 100755 tests/fixtures/cssmin.css.expected create mode 100755 tests/fixtures/jsmin.js create mode 100755 tests/fixtures/jsmin.js.expected diff --git a/.editorconfig b/.editorconfig index a412170..8502e51 100755 --- a/.editorconfig +++ b/.editorconfig @@ -10,5 +10,5 @@ indent_size = 3 charset = utf-8 trim_trailing_whitespace = true -[{*.svg, .htaccess}] +[{*.svg, .htaccess, *.expected}] insert_final_newline = false diff --git a/composer.json b/composer.json index 3c7d06a..cc9ab73 100755 --- a/composer.json +++ b/composer.json @@ -52,7 +52,9 @@ "roave/security-advisories": "dev-master", "kylekatarnls/coffeescript": "1.3.*", "symfony/console": "^4.2.9|^5.0.0", - "phpstan/phpstan-mockery": "^0.12.5" + "phpstan/phpstan-mockery": "^0.12.5", + "tubalmartin/cssmin": "^4.1", + "tedivm/jshrink": "^1.3" }, "scripts": { "phpstan": "./phpstan/phpstan", diff --git a/src/Filter/CssMinFilter.php b/src/Filter/CssMinFilter.php new file mode 100755 index 0000000..e28e417 --- /dev/null +++ b/src/Filter/CssMinFilter.php @@ -0,0 +1,20 @@ +run($code); + } +} diff --git a/src/Filter/JsMinFilter.php b/src/Filter/JsMinFilter.php new file mode 100755 index 0000000..4596033 --- /dev/null +++ b/src/Filter/JsMinFilter.php @@ -0,0 +1,21 @@ +lc = $lc; - } - - - private function getLessC(): lessc - { - // lazy loading - if (empty($this->lc)) { - $this->lc = new lessc(); - } - - return clone $this->lc; + return new Less_Parser; } - /** - * Invoke filter - * @param string $code - * @param Compiler $loader - * @param string $file - * @return string - */ public function __invoke(string $code, Compiler $loader, string $file): string { if (pathinfo($file, PATHINFO_EXTENSION) === 'less') { - $lessc = $this->getLessC(); - $lessc->importDir = pathinfo($file, PATHINFO_DIRNAME) . '/'; - return $lessc->compile($code); + $parser = $this->getLessParser(); + $parser->parseFile($file); + return $parser->getCss(); } return $code; diff --git a/tests/Filter/CssMinFilterTest.php b/tests/Filter/CssMinFilterTest.php new file mode 100755 index 0000000..df3dc2b --- /dev/null +++ b/tests/Filter/CssMinFilterTest.php @@ -0,0 +1,41 @@ +filter = new CssMinFilter(); + + $files = new FileCollection(__DIR__ . '/../fixtures'); + @mkdir($outputDir = __DIR__ . '/../temp/'); + $this->compiler = new Compiler($files, new DefaultOutputNamingConvention(), $outputDir); + } + + + public function testMinify(): void + { + $file = __DIR__ . '/../fixtures/cssmin.css'; + $minified = $this->filter->__invoke( + (string) file_get_contents($file), + $this->compiler, + $file + ); + $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/cssmin.css.expected'), $minified); + } +} diff --git a/tests/Filter/JsMinFilterTest.php b/tests/Filter/JsMinFilterTest.php new file mode 100755 index 0000000..084a032 --- /dev/null +++ b/tests/Filter/JsMinFilterTest.php @@ -0,0 +1,41 @@ +filter = new JsMinFilter(); + + $files = new FileCollection(__DIR__ . '/../fixtures'); + @mkdir($outputDir = __DIR__ . '/../temp/'); + $this->compiler = new Compiler($files, new DefaultOutputNamingConvention(), $outputDir); + } + + + public function testMinify(): void + { + $file = __DIR__ . '/../fixtures/jsmin.js'; + $minified = $this->filter->__invoke( + (string) file_get_contents($file), + $this->compiler, + $file + ); + $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/jsmin.js.expected'), $minified); + } +} diff --git a/tests/Filter/LessFilterTest.php b/tests/Filter/LessFilterTest.php index ce505a2..ebd73d8 100755 --- a/tests/Filter/LessFilterTest.php +++ b/tests/Filter/LessFilterTest.php @@ -20,7 +20,7 @@ class LessFilterTest extends TestCase protected function setUp(): void { - $this->filter = new LessFilter(new \lessc()); + $this->filter = new LessFilter(); $files = new FileCollection(__DIR__ . '/../fixtures'); @mkdir($outputDir = __DIR__ . '/../temp/'); @@ -31,11 +31,12 @@ protected function setUp(): void public function testReplace(): void { $file = __DIR__ . '/../fixtures/style.less'; - $less = $this->filter->__invoke( + $minified = $this->filter->__invoke( (string) file_get_contents($file), $this->compiler, $file ); - $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/style.less.expected'), $less); + + $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/style.less.expected'), $minified); } } diff --git a/tests/fixtures/cssmin.css b/tests/fixtures/cssmin.css new file mode 100755 index 0000000..6ad0010 --- /dev/null +++ b/tests/fixtures/cssmin.css @@ -0,0 +1,57 @@ +.clearFix { + display: block; + zoom: 1; +} +.clearFix:after { + content: " "; + display: block; + font-size: 0; + height: 0; + clear: both; + visibility: hidden; +} +div.banners { + display: block; + zoom: 1; + padding: 0 0 20px; + margin: 0 10px; + border-bottom: #f4f4f4 1px solid; +} +div.banners:after { + content: " "; + display: block; + font-size: 0; + height: 0; + clear: both; + visibility: hidden; +} +div.banners > div { + float: left; + width: 610px; + height: 194px; + background: #f9f2e8; + margin: 20px 0 0 20px; + position: relative; +} +div.banners > div h3 { + width: auto; + color: #be2025; + font-weight: 600; + padding: 10px 20px; + margin: 10px 10px 0 10px; + text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3); + display: inline-block; + font-size: 24px; +} +div.banners > div h3, +div.banners > div p { + color: #ffffff; +} +div.banners > div p { + font-size: 13px; + max-width: 360px; + padding: 10px; +} +div.banners > div p strong { + font-weight: 600; +} diff --git a/tests/fixtures/cssmin.css.expected b/tests/fixtures/cssmin.css.expected new file mode 100755 index 0000000..10ae4dd --- /dev/null +++ b/tests/fixtures/cssmin.css.expected @@ -0,0 +1 @@ +.clearFix{display:block;zoom:1}.clearFix:after{content:" ";display:block;font-size:0;height:0;clear:both;visibility:hidden}div.banners{display:block;zoom:1;padding:0 0 20px;margin:0 10px;border-bottom:#f4f4f4 1px solid}div.banners:after{content:" ";display:block;font-size:0;height:0;clear:both;visibility:hidden}div.banners>div{float:left;width:610px;height:194px;background:#f9f2e8;margin:20px 0 0 20px;position:relative}div.banners>div h3{width:auto;color:#be2025;font-weight:600;padding:10px 20px;margin:10px 10px 0;text-shadow:0 2px 0 rgba(0,0,0,.3);display:inline-block;font-size:24px}div.banners>div h3,div.banners>div p{color:#fff}div.banners>div p{font-size:13px;max-width:360px;padding:10px}div.banners>div p strong{font-weight:600} \ No newline at end of file diff --git a/tests/fixtures/jsmin.js b/tests/fixtures/jsmin.js new file mode 100755 index 0000000..2dd0288 --- /dev/null +++ b/tests/fixtures/jsmin.js @@ -0,0 +1,9 @@ +;(function() { + window.alert('Hello World!'); + + if (true === false) { + window.alert('Paradox'); + } else { + window.alert('All is well.'); + } +})(); diff --git a/tests/fixtures/jsmin.js.expected b/tests/fixtures/jsmin.js.expected new file mode 100755 index 0000000..238dd79 --- /dev/null +++ b/tests/fixtures/jsmin.js.expected @@ -0,0 +1 @@ +;(function(){window.alert('Hello World!');if(true===false){window.alert('Paradox');}else{window.alert('All is well.');}})(); \ No newline at end of file