rmrevin / yii2-minify-view

Yii2 View component with minification css & js
MIT License
191 stars 67 forks source link

Minification without joining files together #35

Closed OndrejVasicek closed 8 years ago

OndrejVasicek commented 8 years ago

Hi Roman, Thank you for your plugin. I’d like to ask you if you’d consider one additional feature – compressing files without joining them together. I actually thing, the concentration is really bad idea here. Just imagine even simple web. It has 20 pages. On every page there is a different combination of js/css plugins/codes.

If we don’t join them together, every file is only compressed (that’s always a god idea) but separated. It may look like disadvantage, because we created another http requests, but it’s only the first loading and to be honest, it’s usually not such a big deal. But what will happen, when we loads the second, third and etc page? Every time the page requests any from the previously generated file, It’ll be served from cache. And that’s great. The separation different pieces of code to different files, which can be loaded on different pages, is really good approach here.

But on the other hand, if we join the files together, it means for every different combination of files, there is a different output file. It means that the code is duplicated enormously. More files, more combinations, more different outputs. When we visit the pages, we have to load the new files all the time again, because they are different – made from different parts.

Me for example - I have my main js and css files, which are generated outside of Yii by gulp. They are actually combinations of many parts. So you see that doesn’t mean I’m against the joining technique. It’s great, but it has to be used carefully. My using is helpful, because my finale js and css files are the only one for the whole web. User loads only one file and can avoid loading tons of js and css request.

But in your case, a lot of different files are created. Imagine, my main JS file has 200kB. On one page, Yii adds some little 1k plugin. User has to download new 201kB file. On the other page, there is another small plugin from Yii which has 5k. User have download again 205kB. No matter he has already downloaded it before. This is now part a new combination.

:-/ So that bothers me. I understand the need Yii adds assets dynamically. It’s not possible to use everything, if they don’t know, what we are gonna use. But the joining approach, which is great for situations when you preparing a layout for example, doesn’t suits here. So what do you think about that? Am I missing something?

Wil be glad for any response.

rmrevin commented 8 years ago

Hello Ondrej (i wrote your name correctly?).

I understand that you are using a "gulp" to build the frontend. I do not understand, why would you use my extension? You can build the main js and css file with a gulp, and all dynamic dependencies will connect a standard view component. If you need some specific features of our extension, let me know, we can add additional configurations.

Thank you.

OndrejVasicek commented 8 years ago

Yep, it’s Ondřej with beautiful “ř” from our Czech alphabet :)

I build the main js and css by gulp (actually I use the Foundation 6 with their workflow updated for my Yii needs). So I can handle everything my layout needs. But I can’t manage styles and scripts which Yii generates automatically. You know, If I use captcha, there’s gonna be some jquery injected. If I use gridView on other page, the same thing will happen.

And for some really strange reason, Yii serves these files in non compressed version. So I was looking for some solution and founded yours.

Well I guess I could partly solve this by exclude my main js and css from my Asset Bundle and put it right into layout, but it doesn’t seem to be a clear solution for me. And I’d only solve the joining Yii scripts into my script.

So if you are open to some improvements (I dunno how complicated it could be), I’d appreciate an option to turn on compressing, but without concatenating files. Just serve them as they are, but compressed.

Thanks again.

rmrevin commented 8 years ago

We can make the option disabling concatenating files.

Also, as an alternative solution, you can turn yii scripts that will be used in build gulp. And in the application configuration to disable the corresponding AssetBundles.

As soon as we make a fix, we will inform you with this ticket.

You can add your own fix and send us a pull request (not to expect a solution to the problem).

Thank you.

OndrejVasicek commented 8 years ago

Also, as an alternative solution, you can turn yii scripts that will be used in build gulp. And in the application configuration to disable the corresponding AssetBundles.

I'm afraid I don't understand what you mean.

rmrevin commented 8 years ago

You can add scripts yii.js, yii.captcha.js, yii.gridView.js and etc. to gulp build. And in application config in assetManger component exclude this scripts:

app/config/main.php
return [
    // ...
    'components' => [
        // ...
        'assetManager' => [
            'class' => 'yii\web\AssetManager',
            'bundles' => [
                'yii\web\YiiAsset' => [
                    'js' => [], // disable js in yii\web\YiiAsset bundle
                ],
                'yii\captcha\CaptchaAsset' => [
                    'js' => [], // disable js in yii\captcha\CaptchaAsset bundle
                ],
                'yii\grid\GridViewAsset' => [
                    'js' => [], // disable js in yii\grid\GridViewAsset bundle
                ],
            ],
        ],
    ],
];
rmrevin commented 8 years ago

fixed in version 1.11.0 use new parameters concatCss and concatJs.

OndrejVasicek commented 8 years ago

Great work rmrevin. I'll ty in few days and let you know. Thank you a lot.

You can add scripts yii.js, yii.captcha.js, yii.gridView.js and etc. to gulp build. And in application config in assetManger component exclude this scripts: ...

I knew about that option but it’s only workaround because you never know what other part of Yii or plugin register another bundle.

OndrejVasicek commented 8 years ago

Great again. I tested it and it's working.

rmrevin commented 8 years ago

@OndrejVasicek I'm glad I could help. Good luck.