Yii 2.0: Use minified version of JqueryAsset, BootstrapAsset (and all default assets)

5 followers

By default, Yii 2.0 chooses to use the non-minified version of Jquery and Bootstrap files (CSS and JS). However, there's a simple way to indicate Yii to use the minified version.

In your config file set this up.

'components' => [ // Check that you are inside "components" section
        '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',
                            ]
                        ]
            ],
        ],
        // ... Some other components
    ],

This way in Development environment you'll have the unminified version, but in Production Yii will use the minified file.

This works for all the assets which don't use the minified version by default.

Total 2 comments

#19384 report it
razvanphp at 2015/06/12 03:53pm
PR?

This should go into the Yii2 core. Have you considered creating a pull request?

Thanks for the tip!

#18862 report it
thomsol at 2015/01/25 10:01am
Great Tip, Thank You!!!

Your tip also lead me to solving one of my frustrations, in that most of the time I need jQuery to be loaded before the body of the page. To do so, I modified your jQuery entry as follows:

            'yii\web\JqueryAsset' => [
                'jsOptions' => ['position' => 1],
                'js' => [
                    YII_ENV_DEV ? 'jquery.js' : 'jquery.min.js'
                ]
            ],

Leave a comment

Please to leave your comment.

Write new article