Skip to content

Commit

Permalink
Added CssMinFilter, JsMinFilter, updated LessFilter + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed Apr 29, 2020
1 parent a7d0305 commit 41098be
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ indent_size = 3
charset = utf-8
trim_trailing_whitespace = true

[{*.svg, .htaccess}]
[{*.svg, .htaccess, *.expected}]
insert_final_newline = false
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/Filter/CssMinFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace WebLoader\Filter;

use tubalmartin\CssMin\Minifier;
use WebLoader\Compiler;

class CssMinFilter
{

/**
* Minify code
*/
public function __invoke(string $code, Compiler $compiler, string $file = ''): string
{
$minifier = new Minifier;
return $minifier->run($code);
}
}
21 changes: 21 additions & 0 deletions src/Filter/JsMinFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace WebLoader\Filter;

use JShrink\Minifier;
use WebLoader\Compiler;

class JsMinFilter
{
public function __invoke(string $code, Compiler $compiler, string $file = ''): ?string
{
$result = Minifier::minify($code);

if (!$result) {
return null;
} else {
return (string) $result;
}
}
}
39 changes: 9 additions & 30 deletions src/Filter/LessFilter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace WebLoader\Filter;

use lessc;
use Less_Parser;
use Nette\SmartObject;
use WebLoader\Compiler;

/**
Expand All @@ -15,41 +15,20 @@
*/
class LessFilter
{
use SmartObject;

/** @var lessc|null */
private $lc;


public function __construct(?lessc $lc = null)
private function getLessParser(): Less_Parser
{
$this->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;
Expand Down
41 changes: 41 additions & 0 deletions tests/Filter/CssMinFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace WebLoader\Test\Filter;

use PHPUnit\Framework\TestCase;
use WebLoader\Compiler;
use WebLoader\DefaultOutputNamingConvention;
use WebLoader\FileCollection;
use WebLoader\Filter\CssMinFilter;

class CssMinFilterTest extends TestCase
{
/** @var CssMinFilter */
private $filter;

/** @var Compiler */
private $compiler;


protected function setUp(): void
{
$this->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);
}
}
41 changes: 41 additions & 0 deletions tests/Filter/JsMinFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace WebLoader\Test\Filter;

use PHPUnit\Framework\TestCase;
use WebLoader\Compiler;
use WebLoader\DefaultOutputNamingConvention;
use WebLoader\FileCollection;
use WebLoader\Filter\JsMinFilter;

class JsMinFilterTest extends TestCase
{
/** @var JsMinFilter */
private $filter;

/** @var Compiler */
private $compiler;


protected function setUp(): void
{
$this->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);
}
}
7 changes: 4 additions & 3 deletions tests/Filter/LessFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
Expand All @@ -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);
}
}
57 changes: 57 additions & 0 deletions tests/fixtures/cssmin.css
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions tests/fixtures/cssmin.css.expected
Original file line number Diff line number Diff line change
@@ -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}
9 changes: 9 additions & 0 deletions tests/fixtures/jsmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;(function() {
window.alert('Hello World!');

if (true === false) {
window.alert('Paradox');
} else {
window.alert('All is well.');
}
})();
1 change: 1 addition & 0 deletions tests/fixtures/jsmin.js.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;(function(){window.alert('Hello World!');if(true===false){window.alert('Paradox');}else{window.alert('All is well.');}})();

0 comments on commit 41098be

Please sign in to comment.