JqueryAsset override to minify file

Hello,

I’m currently trying to force yii to use jquery.min.js for the production environment using the yii2 advanced setup.

I’ve added the following config to common/config/main.php inside components:




'assetManager' => [

            'class' => 'yii\web\AssetManager',

            'bundles' => [

                'yii\web\JqueryAsset' => [

                    'js' => [

                        YII_ENV_DEV ? 'jquery.js' : 'jquery.min.js'

                    ]

                ],

                'yii\bootstrap\BootstrapAsset' => [

                    'css' => [

                        YII_ENV_DEV ? 'css/bootstrap.css' : 'css/bootstrap.min.css',

                    ]

                ],

                'yii\bootstrap\BootstrapPluginAsset' => [

                    'js' => [

                        YII_ENV_DEV ? 'js/bootstrap.js' : 'js/bootstrap.min.js',

                    ]

                ]

            ],

        ],



Unfortunately it doesn’t seem to have any effect.

Any idea what I’m missing here?

Do var_dump(YII_ENV_DEV) to see result

It’s true, but the config doesn’t seem to be evaluated, at least the assetManager part.

Well if YII_ENV_DEV is true, it evaluate to first part of ternary operator (‘jquery.js’)

Mhm, true. But it doesn’t modify the jquery include, not matter to what I’m changing the value

Try YII_ENV_DEV==somevalue

or defined(‘YII_ENV_DEV’)

I do this in my AppAsset:

<?php


namespace app\assets;


use yii\web\AssetBundle;


class AppAsset extends AssetBundle

{

	public $sourcePath = '@app/assets/dist';

	public $css = [

    	YII_ENV_DEV ? 'css/all.css' : 'css/all.min.css'

	];

	public $js = [

    	YII_ENV_DEV ? 'js/all.js' : 'js/all.min.js',

    	YII_ENV_DEV ? 'yii/all.js' : 'js/yii.min.js'

	];

}



The main configuration section for the asset manager does not know about this :)

It works.

dose your main index.php have the following default lines




defined('YII_DEBUG') or define('YII_DEBUG', true);

defined('YII_ENV') or define('YII_ENV', 'dev');



If so, remove them.

You could also remove the if else statements from your files as they shouldn’t be changed for dev or production you should make files that override them.




//other stuff

'js'=>['jquery.min.js]



It is true that the OP needs to configure his application for production.

The ‘init’ script (IIRC, I haven’t used advanced app in a long time) should be executed whenever he wants to change dev/prod config.

That doesn’t help, even when I switch to production mode.

It also doesn’t work if I change


YII_ENV_DEV ? 'jquery.js' : 'jquery.min.js'

to


YII_ENV_DEV ? 'jquery.min.js' : 'jquery.min.js'

that’s why I thought it doesn’t get evaluated.

Where does it not get evaluated ?

If you are using a asset bundle like AppAsset, then it will override anything you’ve configured in the config.

Did you try my suggestion?

What does it look like?

Yes, but it doesn’t seem to work, it includes an all.js which doesn’t exist.

Looks like that’s that is the problem. I’m using the AppAsset.php.

And… did you modify your AppAsset.php file like I suggested?

Yes, but it doesn’t make sense to me, there is no all.js in the source path. If I don’t create it manually I receive a “The file or directory to be published does not exist” error.

I am looking at BootstrapAsset.php which the AppAsset used in the the advanced template is depending on.

It looks like this:


class BootstrapAsset extends AssetBundle

{

	public $sourcePath = '@bower/bootstrap/dist';

	public $css = [

    	'css/bootstrap.css',

	];

}



Here is the AppAsset from backend/assets:


<?php  namespace backend\assets;

  use yii\web\AssetBundle;

  /**  * Main backend application asset bundle.  */

 class AppAsset extends AssetBundle {

 	public $basePath = '@webroot';

 	public $baseUrl = '@web';

 	public $css = [

     	'css/site.css',

 	];

 	public $js = [

 	];

 	public $depends = [

     	'yii\web\YiiAsset',

     	'yii\bootstrap\BootstrapAsset',

 	]; }

 

First, you can remove the depends section.

And then, add the variable $bootstrapSourcePath with the value ‘@bower/bootstrap/dist’, add an entry for that in the $css array, and do the same for the YiiAsset - just look in ‘vendor/yiisoft/web’, I think.

Don’t forget to modify the frontend AppAsset as well. :)

Thanks, that should work. But isn’t there a more cleaner way? This doesn’t feel like the yii way.

I think if there is no other way I’ll create my own jquery asset bundle. However, it makes me wonder that there is no minify version in the core bundle.

I used this and it works as expected. You must have something custom that is not letting this evaluate or maybe you aren’t deleting your assets after changing it so you can’t see the changes. You shouldn’t have to override the default asset bundles to get this to work.




'bundles' => [

                'yii\web\JqueryAsset' => [

                    'js' => [

                        'jquery.min.js'

                    ]

                ]

            ],



I personally leave them empty and load them from CDNs for my production sites.

I finally found the problem:




        'assetManager' => [

            'bundles' => [

                '\yii\web\JqueryAsset' => [

                    'js' => [

                        YII_ENV_DEV ? 'jquery.js' : 'jquery.min.js'

                    ]

                ],

            ],

        ],



The backslash was missing.

[color="#FF0000"]\[/color]yii\web\JqueryAsset

Something that I recently discovered was the environment directory. Could you move the above to the ‘main-local.php’ of the appropriate dev/prod /config directories.

Then run the init file and select development on dev, [1] Production on the server. The first time it is run, it does a lot. the subsequent times, it only copies the environment/? directories. When run the second+ time, it will ask if you want to overwrite somefiles, it’s OK, it’s just moving the files under environment/? to the main area.

you only have to do this once, unless you overwrite the main-local config files when you update the server. The .gitignore files already know not to update the *-local.php files in the main config areas.

You can test this on your dev machine.

Edit environment/dev/config/main-local.php, to put the un-min files to be reqistered.

Edit environment/prod/config/main-local.php, to put the min versions to be reqistered.

Then run init, set to [0]dev. run your site and see what files showed up.

Then run init, set to [1]prod. run your site and see what files showed up.

Just remember to copy all the OTHER changes that you have made to the various *-local files, BEFORE you run the ‘init’ command.