我如何在Yii中缩小除jQuery之外的所有资产?

2ic8powd  于 2022-11-09  发布在  jQuery
关注(0)|答案(3)|浏览(107)

Yii可以让你缩小和压缩JS。我想压缩所有应用的JS并使用Google托管的jQuery。我该怎么做?
Yii允许您指定jQuery的源代码http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles
但是我已经在使用bundles键来压缩资产了:

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

assets-prod.php是自动生成的。我尝试在压缩过程中使用此
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',
        ],
      ],
    ],
],

个月
但是当我运行yii asset assets.php config/assets-prod.php时,它根本没有生成任何jQuery文件。这几乎是我想要的,但是当我加载页面时,jQuery完全不见了。没有对jQuery的引用。它在assets-prod.php中创建了这个,这似乎是错误的

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

然后我尝试了资产Maphttp://www.yiiframework.com/doc-2.0/guide-structure-assets.html#asset-mapping。我将其放入web.php

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

我恢复了assets.php并再次运行yii asset assets.php config/assets-prod.php,但随后它又将jQuery放到了一个大型的小型JS文件中。

sxpgvts3

sxpgvts31#

您必须使用“baseUrl”属性,如下所示:

class GoogleAsset extends AssetBundle{

    public $baseUrl = 'http://maps.googleapis.com/maps/api';

    public $js = [
        'js?sensor=false&language=ru-ru&region=ru-ru'
    ];

}
bfhwhh0e

bfhwhh0e2#

您可以使用此配置

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

8mmmxcuj3#

您不需要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'];
}

相关问题