How can I minimize all EXCEPT jQuery assets in Yii?

Yii allows you to minimize and compress JS. I want to compress the entire JS application and use jQuery hosted by Google. How can I do it?

Yii lets you specify the source for jQuery http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles

But I already use the bundles key for compressed assets :

 'bundles' => require(__DIR__ . '/' . (YII_ENV_PROD ? 'assets-prod.php' : 'assets-dev.php')), 

assets-prod.php automatically generated. I tried setting up an asset package during compression using this

assets.php
 // Asset manager configuration: 'assetManager' => [ 'basePath' => '@webroot/assets', 'baseUrl' => '@web/assets', 'bundles' => [ 'yii\web\JqueryAsset' => [ 'sourcePath' => null, // do not publish the bundle 'js' => [ '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', ], ], ], ], 

But when I ran yii asset assets.php config/assets-prod.php , it did not generate jQuery files at all. This is almost what I wanted, but when I loaded the page, jQuery was completely absent. There was no link to jQuery. He created this in assets-prod.php , which seems wrong

 'yii\\web\\JqueryAsset' => [ 'sourcePath' => null, 'js' => [], 'css' => [], 'depends' => [], ], 

Ok, so I tried the asset mapping http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#asset-mapping . I put this in web.php

  'assetMap' => [ 'jquery.js' => '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', ], 

However, it will still not load jQuery. I returned assets.php and ran yii asset assets.php config/assets-prod.php , but then returned to putting jQuery in one big miniature JS file.

+6
source share
3 answers

you need to use the baseUrl property, for example:

 class GoogleAsset extends AssetBundle{ public $baseUrl = 'http://maps.googleapis.com/maps/api'; public $js = [ 'js?sensor=false&language=ru-ru&region=ru-ru' ]; } 
0
source

You can use this configuration

 'yii\\web\\JqueryAsset' => [ 'baseUrl' => '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/', 'js' => ['jquery.min.js'], 'css' => [], 'depends' => [], ], 
0
source

you don't need basURL

 <?php namespace app\assets; use yii\web\AssetBundle; class GoogleApiAsset extends AssetBundle { public $js = ['//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js']; } 
0
source

Source: https://habr.com/ru/post/987842/


All Articles