Minifying assets

Packing CSS and JS to one file is essential in building large apps, currently the only way is to use extensions. There are a lot of extensions that try to solve this, but they are always cumbersome to use, forcing you to hardcode the filenames or folders, overwriting core ClientScript class or making you use a custom method when publishing assets like Yii::app()->clientScript->minifyAsssets(‘myfile.js’);

I think packing should be in the core. So by supplying a ‘minify’ flag to Clientscript in config, all assets that are registered should be packed to one file.

Compressing the files is another story, since there are many different libraries available, it’s better to leave that part out of the core. Create some callback where the packed JS/CSS is supplied, so the user can do anything he wants with it before it is saved.

it seems that it is good to pack CSS and JS to one file, but please let me say sth.

as we know, the browser can cache the css and js file, not to load them in ever request, right?

let say we have there page, a.html, b.html, in a.html, we will include global.css, a.css, in b.html, we will include global.css, b.css, when packing the css to one file in a.html and b.html, the browser have to load the same content of global.css, right?

so, i think it is not good to pack css or js to one file.

@Davidhhuan: unless you pack global.css, a.css and b.css into one file and use that on both a.html and b.html

But if a.css and b.css are very small, I usually don’t bother putting them in seperate files anyway. So wrapping them would not benifit me.

But to minify the js files does have my vote, I use static js files more than generated js so this could make a difference for me.

The problem isn’t the file size, it’s the amount of requests. If you use a lot of external plugins and core scripts, you can get like 20 or more assets, each of them is a request that is queued and has to be DNS resolved. This adds up quickly.

I really like the way Drupal or Magento do it, just hit a checkbox and everything is packed into one file. This should work for Yii also.

If the core allows to set a flag in the clientScript component to combine multiple entries/files then everyone has their way.

Even the compression could be pre-made by supplying the compress-flag and a method to compress a file.

It’s just a matter of selecting a compression package and overriding that one method.

You can always integrate https://github.com/kriswallsmith/assetic with Yii.

That makes more sense than the "set a flag" suggestions above. Assetic looks like it covers about the set of functions needed.

Asset handling of big projects requires design. Fully automated compiling, combining and compression produces a thoroughly sub-optimal set of downloads.

Yii’s CClientScript and CAssetManager are fine for small projects where you need to get it done quickly. The “set a flag” for combination/compression would be valuable only in those same projects.

Agreed, therefore I suggested to only combine the files, and to allow the user to easily get the combined source, so extensions could hook into it and do whatever they want with it.

I don’t see the problem of Yii taking care of combining, since the location of the page (pos, head, ready) and the order of the files can be defined already, I don’t see what additional configuration you would need for combining.

I’ve done some large projects in Magento and Drupal and automatically combining worked like a charm, so the concept would work. Of course , Yii is still a framework so you’re free to not use the automatic combining and stick to some custom build solution that fits your project needs.

Hey guys!

I’m working on extension that integrates Assetic into Yii. I’d like some feedback :)

What are pros compared to built-in asset manager?

Assetic is an asset pipeline framework. It can be used to concatenate, minify, write assets in other languages such as CoffeScript, Sass. Assets will probably fall into three categories: javascripts, stylesheets, and images.

As for built-in asset manager, I think it’s just asset publisher, and as the result we can find a lot of extensions to work with assets.

Built-in asset manager in Yii2 is able to do all that.

Give me an example how can I use different filters with built-in asset manager?

https://github.com/yiisoft/yii2/blob/master/docs/guide/assets.md#using-asset-converter

Ok, but it is Yii2, not Yii.

Well, we’re in “General Discussions for Yii 2.0” subforum.

Oops, this extension is related to Yii :)

Hope you are aware of the new Yii 2.0 version currently in development (nearing beta)? The version that is available for production release is Yii 1.1.14 (which possibly should be replaced by Yii 2.0 not far in future).

This forum is for [size="4"]Yii 2.0[/size] related discussions.

Anyway, Yii will be used for a long time.

Yep. Thanks for a useful extension.