Skip to content

Commit

Permalink
Added option to exclude files from minification
Browse files Browse the repository at this point in the history
Closes #75
Merge remote-tracking branch 'pullrequest/exclude_minification' into develop
  • Loading branch information
Stolz committed Oct 1, 2015
2 parents d6b9f46 + 1427d5c commit 00dba87
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 15 deletions.
18 changes: 15 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ Regex to match against a filename/url to determine if it is a JavaScript asset.



* Visibility: **protected**


### $no_minification_regex

protected string $no_minification_regex = '/.[-.]min\.(css|js)$/i'

Regex to match against a filename/url to determine if it should not be minified by pipeline.



* Visibility: **protected**


Expand Down Expand Up @@ -446,11 +457,11 @@ Calculate the pipeline hash.



### gatherLinks
### packLinks

string gatherLinks(array $links)
string packLinks(array $links, \Closure $minifier)

Download and concatenate the content of several links.
Download, concatenate and minifiy the content of several links.



Expand All @@ -459,6 +470,7 @@ Download and concatenate the content of several links.

#### Arguments
* $links **array**
* $minifier **Closure**



Expand Down
7 changes: 7 additions & 0 deletions src/Laravel/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
*/
//'js_regex' => '/.\.js$/i',

/**
* Regex to match against a filename/url to determine if it should not be minified by pipeline.
*
* @var string
*/
//'no_minification_regex' => '/.[-.]min\.(css|js)$/i',

/**
* Absolute path to the public directory of your App (WEBROOT).
* Required if you enable the pipeline.
Expand Down
35 changes: 23 additions & 12 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Manager
*/
protected $js_regex = '/.\.js$/i';

/**
* Regex to match against a filename/url to determine if it should not be minified by pipeline.
*
* @var string
*/
protected $no_minification_regex = '/.[-.]min\.(css|js)$/i';

/**
* Absolute path to the public directory of your App (WEBROOT).
* Required if you enable the pipeline.
Expand Down Expand Up @@ -175,7 +182,7 @@ public function __construct(array $options = array())
public function config(array $config)
{
// Set regex options
foreach(array('asset_regex', 'css_regex', 'js_regex') as $option)
foreach(array('asset_regex', 'css_regex', 'js_regex', 'no_minification_regex') as $option)
if(isset($config[$option]) and (@preg_match($config[$option], null) !== false))
$this->$option = $config[$option];

Expand Down Expand Up @@ -465,20 +472,17 @@ protected function pipeline(array $assets, $extension, $subdirectory, Closure $m
if(file_exists($absolute_path))
return $relative_path;

// Concatenate files
$buffer = $this->gatherLinks($assets);

// Minifiy
$minified = $minifier->__invoke($buffer);
// Download, concatenate and minifiy files
$buffer = $this->packLinks($assets, $minifier);

// Write minified file
file_put_contents($absolute_path, $minified);
file_put_contents($absolute_path, $buffer);

// Write gziped file
if($gzipAvailable = (function_exists('gzencode') and $this->pipeline_gzip !== false))
{
$level = ($this->pipeline_gzip === true) ? -1 : intval($this->pipeline_gzip);
file_put_contents("$absolute_path.gz", gzencode($minified, $level));
file_put_contents("$absolute_path.gz", gzencode($buffer, $level));
}

// Hook for pipeline event
Expand Down Expand Up @@ -524,16 +528,20 @@ protected function calculatePipelineHash(array $assets)
}

/**
* Download and concatenate the content of several links.
* Download, concatenate and minifiy the content of several links.
*
* @param array $links
* @param array $links
* @param Closure $minifier
* @return string
*/
protected function gatherLinks(array $links)
protected function packLinks(array $links, Closure $minifier)
{
$buffer = '';
foreach($links as $link)
{
$originalLink = $link;

// Get real link path
if($this->isRemoteLink($link))
{
// Add current protocol to agnostic links
Expand All @@ -551,7 +559,10 @@ protected function gatherLinks(array $links)
}

// Fetch link content
$buffer .= ($this->fetch_command instanceof Closure) ? $this->fetch_command->__invoke($link) : file_get_contents($link);
$content = ($this->fetch_command instanceof Closure) ? $this->fetch_command->__invoke($link) : file_get_contents($link);

// Minify
$buffer .= (preg_match($this->no_minification_regex, $originalLink)) ? $content : $minifier->__invoke($content);

// Avoid JavaScript minification problems
$buffer .= PHP_EOL;
Expand Down
45 changes: 45 additions & 0 deletions tests/AssetsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,51 @@ public function testDetectAndAddCollection()
$this->assertStringEndsWith($asset2, array_pop($assets2));
}

public function testRegexOptions(){

$files = [
'.css', // Not an asset
'foo.CSS',
'foomin.css',
'foo.min.css', // Skip from minification
'foo-MIN.css', // Skip from minification

'.js', // Not an asset
'foo.JS',
'foomin.js',
'foo.min.js', // Skip from minification
'foo-MIN.js', // Skip from minification
];

// Test asset detection
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'asset_regex');
$matching = array_filter($files, function ($file) use ($regex) {
return 1 === preg_match($regex, $file);
});
$this->assertEquals(8, count($matching));

// Test CSS asset detection
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'css_regex');
$matching = array_filter($files, function ($file) use ($regex) {
return 1 === preg_match($regex, $file);
});
$this->assertEquals(4, count($matching));

// Test JS asset detection
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'js_regex');
$matching = array_filter($files, function ($file) use ($regex) {
return 1 === preg_match($regex, $file);
});
$this->assertEquals(4, count($matching));

// Test minification skip detection
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'no_minification_regex');
$matching = array_filter($files, function ($file) use ($regex) {
return 1 === preg_match($regex, $file);
});
$this->assertEquals(4, count($matching));
}

protected static function getMethod($name) {
$class = new ReflectionClass('Stolz\Assets\Manager');
$method = $class->getMethod($name);
Expand Down

0 comments on commit 00dba87

Please sign in to comment.