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

9 0
6
Viewed: 39 527 times
Version: Unknown (update)
Category: How-tos
Written by: robregonm robregonm
Updated by: robregonm robregonm
Created: Jan 21, 2015
Updated: 11 years ago

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

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.