1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-16 11:03:59 +02:00

Minify no longer tries to minify -min.js/.min.js files

2.2 used empty string as a magical value meaning do not minify, but
in the refactoring `Minify::combineMinify` forgot to interpret this
value that way and instead inherited the default compressor for the type.

This eliminates `""` as a magical value, but for BC rewrites it to
`Minify::nullMinifier` in the sources.

Fixes #499
This commit is contained in:
Steve Clay
2016-02-26 11:16:16 -05:00
parent 0b466c0892
commit 7dbd2c87e4
8 changed files with 31 additions and 10 deletions

View File

@@ -39,7 +39,7 @@ $src1 = new Minify_Source(array(
)); ));
$src2 = new Minify_Source(array( $src2 = new Minify_Source(array(
'filepath' => '//js/file2.js', 'filepath' => '//js/file2.js',
'minifier' => '', // don't compress 'minifier' => 'Minify::nullMinifier', // don't compress
)); ));
``` ```
In the above, `JmyJsMinifier()` is only called when the contents of `$src1` is needed. In the above, `JmyJsMinifier()` is only called when the contents of `$src1` is needed.

View File

@@ -168,7 +168,7 @@ class Minify {
* $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier'; * $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
* *
* // don't minify Javascript at all * // don't minify Javascript at all
* $options['minifiers'][Minify::TYPE_JS] = ''; * $options['minifiers'][Minify::TYPE_JS] = 'Minify::nullMinifier';
* </code> * </code>
* *
* 'minifierOptions' : to send options to the minifier function, specify your options * 'minifierOptions' : to send options to the minifier function, specify your options
@@ -293,7 +293,7 @@ class Minify {
$this->options['minifiers'][self::TYPE_JS] = false; $this->options['minifiers'][self::TYPE_JS] = false;
foreach ($this->sources as $key => $source) { foreach ($this->sources as $key => $source) {
if ($this->options['contentType'] === self::TYPE_JS) { if ($this->options['contentType'] === self::TYPE_JS) {
$source->setMinifier(""); $source->setMinifier('Minify::nullMinifier');
} elseif ($this->options['contentType'] === self::TYPE_CSS) { } elseif ($this->options['contentType'] === self::TYPE_CSS) {
$source->setMinifier(array('Minify_CSSmin', 'minify')); $source->setMinifier(array('Minify_CSSmin', 'minify'));
$sourceOpts = $source->getMinifierOptions(); $sourceOpts = $source->getMinifierOptions();
@@ -458,6 +458,20 @@ class Minify {
exit; exit;
} }
/**
* Default minifier for .min or -min JS files.
*
* @param string $content
* @return string
*/
public static function nullMinifier($content) {
if (isset($content[0]) && $content[0] === "\xef") {
$content = substr($content, 3);
}
$content = str_replace("\r\n", "\n", $content);
return trim($content);
}
/** /**
* Setup CSS sources for URI rewriting * Setup CSS sources for URI rewriting
*/ */

View File

@@ -190,7 +190,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
,'lastModified' => 0 ,'lastModified' => 0
// due to caching, filename is unreliable. // due to caching, filename is unreliable.
,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n" ,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
,'minifier' => '' ,'minifier' => 'Minify::nullMinifier'
))); )));
} }

View File

@@ -104,7 +104,7 @@ class Minify_Source implements Minify_SourceInterface {
$this->contentType = $spec['contentType']; $this->contentType = $spec['contentType'];
} }
if (isset($spec['minifier'])) { if (isset($spec['minifier'])) {
$this->minifier = $spec['minifier']; $this->setMinifier($spec['minifier']);
} }
if (isset($spec['minifyOptions'])) { if (isset($spec['minifyOptions'])) {
$this->minifyOptions = $spec['minifyOptions']; $this->minifyOptions = $spec['minifyOptions'];
@@ -130,8 +130,15 @@ class Minify_Source implements Minify_SourceInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setMinifier($minifier) public function setMinifier($minifier = null)
{ {
if ($minifier === '') {
error_log(__METHOD__ . " cannot accept empty string. Use 'Minify::nullMinifier' or 'trim'.");
$minifier = 'Minify::nullMinifier';
}
if ($minifier !== null && !is_callable($minifier, true)) {
throw new \InvalidArgumentException('minifier must be null or a valid callable');
}
$this->minifier = $minifier; $this->minifier = $minifier;
} }

View File

@@ -173,7 +173,7 @@ class Minify_Source_Factory {
$spec['minifyOptions']['compress'] = false; $spec['minifyOptions']['compress'] = false;
// we still want URI rewriting to work for CSS // we still want URI rewriting to work for CSS
} else { } else {
$spec['minifier'] = ''; $spec['minifier'] = 'Minify::nullMinifier';
} }
} }

View File

@@ -27,7 +27,7 @@ interface Minify_SourceInterface {
* @param callable $minifier * @param callable $minifier
* @return void * @return void
*/ */
public function setMinifier($minifier); public function setMinifier($minifier = null);
/** /**
* Get options for the minifier * Get options for the minifier

View File

@@ -55,7 +55,7 @@ if ($paths) {
$sources[] = new Minify_Source(array( $sources[] = new Minify_Source(array(
'id' => $path, 'id' => $path,
'content' => "/*** $path not found ***/\n", 'content' => "/*** $path not found ***/\n",
'minifier' => '', 'minifier' => 'Minify::nullMinifier',
)); ));
} }
} }

View File

@@ -45,7 +45,7 @@ foreach ($paths as $path) {
$sources[] = new Minify_Source(array( $sources[] = new Minify_Source(array(
'id' => $path, 'id' => $path,
'content' => "/*** $path not found ***/\n", 'content' => "/*** $path not found ***/\n",
'minifier' => '', 'minifier' => 'Minify::nullMinifier',
)); ));
} }
} }