Skip to content

Commit 00dba87

Browse files
committed
Added option to exclude files from minification
Closes #75 Merge remote-tracking branch 'pullrequest/exclude_minification' into develop
2 parents d6b9f46 + 1427d5c commit 00dba87

File tree

4 files changed

+90
-15
lines changed

4 files changed

+90
-15
lines changed

API.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ Regex to match against a filename/url to determine if it is a JavaScript asset.
4747

4848

4949

50+
* Visibility: **protected**
51+
52+
53+
### $no_minification_regex
54+
55+
protected string $no_minification_regex = '/.[-.]min\.(css|js)$/i'
56+
57+
Regex to match against a filename/url to determine if it should not be minified by pipeline.
58+
59+
60+
5061
* Visibility: **protected**
5162

5263

@@ -446,11 +457,11 @@ Calculate the pipeline hash.
446457

447458

448459

449-
### gatherLinks
460+
### packLinks
450461

451-
string gatherLinks(array $links)
462+
string packLinks(array $links, \Closure $minifier)
452463

453-
Download and concatenate the content of several links.
464+
Download, concatenate and minifiy the content of several links.
454465

455466

456467

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

460471
#### Arguments
461472
* $links **array**
473+
* $minifier **Closure**
462474

463475

464476

src/Laravel/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
*/
3333
//'js_regex' => '/.\.js$/i',
3434

35+
/**
36+
* Regex to match against a filename/url to determine if it should not be minified by pipeline.
37+
*
38+
* @var string
39+
*/
40+
//'no_minification_regex' => '/.[-.]min\.(css|js)$/i',
41+
3542
/**
3643
* Absolute path to the public directory of your App (WEBROOT).
3744
* Required if you enable the pipeline.

src/Manager.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class Manager
2929
*/
3030
protected $js_regex = '/.\.js$/i';
3131

32+
/**
33+
* Regex to match against a filename/url to determine if it should not be minified by pipeline.
34+
*
35+
* @var string
36+
*/
37+
protected $no_minification_regex = '/.[-.]min\.(css|js)$/i';
38+
3239
/**
3340
* Absolute path to the public directory of your App (WEBROOT).
3441
* Required if you enable the pipeline.
@@ -175,7 +182,7 @@ public function __construct(array $options = array())
175182
public function config(array $config)
176183
{
177184
// Set regex options
178-
foreach(array('asset_regex', 'css_regex', 'js_regex') as $option)
185+
foreach(array('asset_regex', 'css_regex', 'js_regex', 'no_minification_regex') as $option)
179186
if(isset($config[$option]) and (@preg_match($config[$option], null) !== false))
180187
$this->$option = $config[$option];
181188

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

468-
// Concatenate files
469-
$buffer = $this->gatherLinks($assets);
470-
471-
// Minifiy
472-
$minified = $minifier->__invoke($buffer);
475+
// Download, concatenate and minifiy files
476+
$buffer = $this->packLinks($assets, $minifier);
473477

474478
// Write minified file
475-
file_put_contents($absolute_path, $minified);
479+
file_put_contents($absolute_path, $buffer);
476480

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

484488
// Hook for pipeline event
@@ -524,16 +528,20 @@ protected function calculatePipelineHash(array $assets)
524528
}
525529

526530
/**
527-
* Download and concatenate the content of several links.
531+
* Download, concatenate and minifiy the content of several links.
528532
*
529-
* @param array $links
533+
* @param array $links
534+
* @param Closure $minifier
530535
* @return string
531536
*/
532-
protected function gatherLinks(array $links)
537+
protected function packLinks(array $links, Closure $minifier)
533538
{
534539
$buffer = '';
535540
foreach($links as $link)
536541
{
542+
$originalLink = $link;
543+
544+
// Get real link path
537545
if($this->isRemoteLink($link))
538546
{
539547
// Add current protocol to agnostic links
@@ -551,7 +559,10 @@ protected function gatherLinks(array $links)
551559
}
552560

553561
// Fetch link content
554-
$buffer .= ($this->fetch_command instanceof Closure) ? $this->fetch_command->__invoke($link) : file_get_contents($link);
562+
$content = ($this->fetch_command instanceof Closure) ? $this->fetch_command->__invoke($link) : file_get_contents($link);
563+
564+
// Minify
565+
$buffer .= (preg_match($this->no_minification_regex, $originalLink)) ? $content : $minifier->__invoke($content);
555566

556567
// Avoid JavaScript minification problems
557568
$buffer .= PHP_EOL;

tests/AssetsManagerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,51 @@ public function testDetectAndAddCollection()
162162
$this->assertStringEndsWith($asset2, array_pop($assets2));
163163
}
164164

165+
public function testRegexOptions(){
166+
167+
$files = [
168+
'.css', // Not an asset
169+
'foo.CSS',
170+
'foomin.css',
171+
'foo.min.css', // Skip from minification
172+
'foo-MIN.css', // Skip from minification
173+
174+
'.js', // Not an asset
175+
'foo.JS',
176+
'foomin.js',
177+
'foo.min.js', // Skip from minification
178+
'foo-MIN.js', // Skip from minification
179+
];
180+
181+
// Test asset detection
182+
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'asset_regex');
183+
$matching = array_filter($files, function ($file) use ($regex) {
184+
return 1 === preg_match($regex, $file);
185+
});
186+
$this->assertEquals(8, count($matching));
187+
188+
// Test CSS asset detection
189+
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'css_regex');
190+
$matching = array_filter($files, function ($file) use ($regex) {
191+
return 1 === preg_match($regex, $file);
192+
});
193+
$this->assertEquals(4, count($matching));
194+
195+
// Test JS asset detection
196+
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'js_regex');
197+
$matching = array_filter($files, function ($file) use ($regex) {
198+
return 1 === preg_match($regex, $file);
199+
});
200+
$this->assertEquals(4, count($matching));
201+
202+
// Test minification skip detection
203+
$regex = PHPUnit_Framework_Assert::readAttribute($this->manager, 'no_minification_regex');
204+
$matching = array_filter($files, function ($file) use ($regex) {
205+
return 1 === preg_match($regex, $file);
206+
});
207+
$this->assertEquals(4, count($matching));
208+
}
209+
165210
protected static function getMethod($name) {
166211
$class = new ReflectionClass('Stolz\Assets\Manager');
167212
$method = $class->getMethod($name);

0 commit comments

Comments
 (0)