minifyclientscript

Minify and merge your CSS and JavaScript seamlessly via this ClientScript extension
22 followers

This extension allows you to seamlessly minify and merge your CSS and JS files.

Requirements

Tested on 1.1.9, should work on 1.1.*

Changelog

20120507.1438 UTC+1

  • fixed bug where the wrong resources were checked, causing the js to rebuild every request comment#8052 crasx
  • fixed bug where POS_* was ignored; thanks Renka

20120330.1039 UTC+1

  • added LOCK_EX to file_put_contents() to prevent certain access issues

20120327.1112 UTC+1

  • fixed bug on Windows(?) hosts; comment#7505 Jesse
  • added return values to registerScript and registerCss; comment#7492 thyseus
  • fixed a bug where CSS wasn't loaded correctly

Usage

Extract the files to the corresponding directories, components/* into compontents/ and extensions/* into extensions/ then configure the config file as described below, now MinifyClientScript will do the rest. :) Please comment if you have comments/suggestions/questions.

Example main.php:

return array(
  ...
  'components'=>array(
    ...
    'clientScript'=>array(
      'class'=>'application.components.MinifyClientScript',
    ),
    ...
  ),
  ...
);

More on this extension

I have written a blog about minifying and merging CSS/JS, read more about it HERE.

Total 20 comments

#8054 report it
ksangers at 2012/05/07 08:39am
Did another update

POS_* aren't ignored anymore, and the files should no longer rebuild at every request.

#8052 report it
crasx at 2012/05/06 07:26pm
Overhead problem

huge, huge problem in the component part of this:

Lines 156-157:

$compileCoreJs = YII_DEBUG || !file_exists($publishedMinifyPath . DIRECTORY_SEPARATOR . "/$filenameCore.js");
$compileJs = YII_DEBUG || !file_exists($publishedMinifyPath . DIRECTORY_SEPARATOR . "$filename.js");

Should be:

$compileCoreJs = YII_DEBUG || !file_exists($this->minifyPath. DIRECTORY_SEPARATOR . "$filenameCore.js");
$compileJs = YII_DEBUG || !file_exists($this->minifyPath. DIRECTORY_SEPARATOR . "$filename.js");

This was causing a 2 second time to first byte on my server because it generated the cached files every request.

#7976 report it
boynet at 2012/04/30 01:10pm
i get PHP notice:Undefined index: default

its says the problem in components\MinifyClientScript.php line: 132

#7538 report it
ksangers at 2012/03/28 02:02pm
RE: jQuery is not defined

I can't seem to reproduce your error, but you could check whether the runtime/MinifyClientScript directory has been published properly, just look for the core-BIGHASHHERE.js file in the assets directory.

Extending CAssetManager could be a nice way to minify the code, but it would require you to recursively search the directory to find .css- and .js-files and it would not merge the files though (which MinifyClientScript also does). You could try and use JSMin and CssCompressor to extend CAssetManager.

#7519 report it
Jesse at 2012/03/27 06:45pm
jQuery is not defined

ksangers,

Thanks for your quick reply, the 404 errors are not coming through now; however, the error "jQuery is not defined" is still an issue.

I cleaned the www/assets and www/protected/runtime/MinifyClientScript directories. Also to confirm components/MinifyClientScript.php line 132 is your updated version.

Current error is:

jQuery is not defined
jquery.yiilistview.js()

After running the application again: jquery exists in the runtime/MinifyClientScript under a hash file name. Anyone know a solution, something I'm missing?

What if you extended CAssetManager.php and replaced function publish(...) to pipe any .css or .js through minify ?

#7513 report it
ksangers at 2012/03/27 09:27am
RE: 404 and empty core files

Could you check components/MinifyClientScript.php line 132 (at the end of minifyCss())? It should be:

file_put_contents("$this->minifyPath/$filenames[$media]", CssCompressor::deflate($cssArray[$media]));

and not:

file_put_contents("$this->minifyPath/$filenames[$media]", CssCompressor::deflate($css));

Otherwise clearing the runtime and www/assets directories might help.

#7512 report it
fleuryc at 2012/03/27 08:23am
404 and empty core files

Hi!

Same as Jesse :

"NetworkError: 404 CHttpException - http://myapp.local/assets/7b768164/core-a23c688da8235c331a4a2d9edc5422a6b411e98caf8e301725933f7f5c4767b2.js"

Plus, when I take a look in the file (wich actually exists), it is empty...

Cheers!

#7511 report it
ksangers at 2012/03/27 05:18am
Update 20120327.1112UTC+1

Thanks for the comments guys! I've updated the code and put it online. Let me know what you think of this version, especially what you don't like so I can fix it! :)

#7505 report it
Jesse at 2012/03/26 09:26pm
Uncaught ReferenceError: jQuery is not defined

Hi, great extension you've made here!

I keep receiving this error:

"Uncaught ReferenceError: jQuery is not defined | jquery.yiilistview.js:114"
"NetworkError: 404 Not Found - http://localhost/WWW/assets/e94214b1%5Cdefault-d2a448605e081d92dd6f50542f3648bcdde0a938ddef420baf0df4efbe329b52.css"
"NetworkError: 404 Not Found - http://localhost/WWW/assets/e94214b1%5C45982dae651db9bcf314be0ba414a5d0d9c7abf84c4c8d5367c16c370006b112.js"

etc

Obviously the assets are not being found. Perhaps the "%5C" is the problem??

I'm wondering if anyone else had a similar issue and knows the solution?

#7492 report it
thyseus at 2012/03/26 04:21am
Nice, but a small addition

Just a small addition to this wonderful clientscript minimizer:

In order to make CClientScript chainable, please return the object:

public function registerScript() [ return parent::

public function registerCss() [ return parent::

(line 223/234/236)

#7125 report it
ksangers at 2012/02/25 06:11am
RE: Error with CSS

I have updated the file to do the callback differently, could you test this? If it still does not work I would like to know what PHP version you are using so I can fix this better.

Hope this works :)

#7123 report it
physicaal at 2012/02/24 10:49pm
Error with CSS

Your extensions give me this error :

"preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'self::cssReplaceCallback', to be a valid callback"

Where is the problem here?

#7030 report it
fleuryc at 2012/02/20 04:47am
RE: RE: RE: RE: RE: RE: 404 and empty files

Ok, thx for the update...

#7029 report it
ksangers at 2012/02/20 04:35am
RE: RE: RE: RE: RE: 404 and empty files

Yeah I noticed this, and fixed this. You got the crappy version really quick! (<5mins of me posting it)

I had the strpos(haystack, needle) wrong, I've already updated it. Sorry for the inconvenience.

#7027 report it
fleuryc at 2012/02/20 04:26am
RE: RE: RE: RE: 404 and empty files

Hi!

I found three error cases :

  1. An extension registers its assets I get this error :
// MinifyClientScript.php(253)
Could not determine asset source. Go to http://www.yiiframework.com/extension/minifyclientscript/ and tell me.

when calling this :

MinifyClientScript.php(229): MinifyClientScript->parseAssetsPath("/assets/c793e10e/yii.debugtoolbar.css")

This occurs when the Yii-Debug-Toolbar extension tries to register its own scripts...

  1. The 'is in theme' test is wrong When I disable the Yii-Debug-Toolbar extension, >I get the same error :
// MinifyClientScript.php(253)
Could not determine asset source. Go to http://www.yiiframework.com/extension/minifyclientscript/ and tell me.

when calling this :

MinifyClientScript.php(229): MinifyClientScript->parseAssetsPath("/themes/MyTheme/css/style.css")

In this case, line 248 of MinifyClientScript.php should be :

strpos($path, Yii::app()->theme->baseUrl)
// instead of strpos(Yii::app()->theme->baseUrl, $path)
  1. My CJui widgets use assets that are in a different theme

I get the same error :

// MinifyClientScript.php(253)
Could not determine asset source. Go to http://www.yiiframework.com/extension/minifyclientscript/ and tell me.

when calling this :

MinifyClientScript.php(229): MinifyClientScript->parseAssetsPath("/themes/custom-theme/jquery-ui-1.8.16.custom.css")

This occurs when calling a CJui widget. In my configuration main.php, I have :

return array (
  ...
  'components' => array (
    ...
    'widgetFactory' => array(
      'widgets' => array(
        'CJuiAccordion' => array(
          'themeUrl' => '/themes',
          'theme' => 'custom-theme',
          'cssFile'=>'jquery-ui-1.8.16.custom.css',
        ),
        ...
)

Thanks again for your help.

#7024 report it
ksangers at 2012/02/20 03:34am
RE: RE: RE: 404 and empty files

Hello fleuryc,

I think I've fixed the problem (for themed files) now, could you test it?

Thanks

#6995 report it
fleuryc at 2012/02/17 10:58am
RE: RE: 404 and empty files

Thanks for the quick answer! When I simply use the default 'clientScript' component, all of my files are found and everything is OK. The problem seems to stem from the fact that my JS and CSS files are not where the ext expects them to be.

For instance : I call :

Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/js/modernizr-2.0.6.min.js', CClientScript::POS_BEGIN);

wich gives :

// inside MinifyClientScript.MinifyClientScript 
$parsedPath = $this->parseAssetsPath($path);
var_dump($path, $parsedPath); 
// string(45) "/themes/myTheme/js/modernizr-2.0.6.min.js" 
// array(2) { 
// ["assetsUrl"]=> string(19) "/assets/myTheme" 
// ["path"]=> string(26) "/js/modernizr-2.0.6.min.js" }

and in the rendered HTML :

<script src="/assets/myTheme/js/modernizr-2.0.6.min.js" type="text/javascript"> 
// returns error 404 not found

where it should be :

<script src="/themes/myTheme/js/modernizr-2.0.6.min.js" type="text/javascript">

It's all the same for the other files I try to register. And I realise that these files are not combined and minified...

Thx for helping. Cheers!

#6993 report it
ksangers at 2012/02/17 09:44am
RE: 404 and empty files

Is it possible that you have non-existing files in your clientScript->scriptFiles or clientScript->cssFiles?

could you post them (var_dump()-ed or something) to me so I can debug it if it's a bug in MinifyClientScript.

#6992 report it
ksangers at 2012/02/17 09:42am
RE: unfortunately still some problems

3) Consider the relocation and importing done!

#6990 report it
fleuryc at 2012/02/17 09:33am
404 and empty files

Hi!

Very usefull ext! I was thinking of extending the clientScript component myself, but I'm kind of lazy, and sure other people do things better than just me.

Still, I installed and configured this ext as explained here, but some of the CSS and JS files can't be found (404 error), and when I dig into the assets, those files exist but are empty...

Any clue where that comes from?

Cheers

Leave a comment

Please to leave your comment.

Create extension