简体   繁体   English

Yii 2 JS资产缩小问题

[英]Yii 2 JS assets minification prolem

I recently installed Yii 2.0.6 and I noticed that it loads 我最近安装了Yii 2.0.6,我注意到它加载了

yii.js
yii.validation.js
yii.activeForm.js

from yiisoft/yii2/assets folder, so I wanted to overwrite these files and in common/config/main.php I added these lines of code but what it does is that it replaces only yii.js file but yii.validation.js and yii.activeForm.js keep being loaded. 来自yiisoft / yii2 / assets文件夹,所以我想覆盖这些文件,在共同的/ config / main.php中,我添加了这些代码行,但它的作用是它只替换了yii.js文件,但是yii.validation.js和yii.activeForm.js继续加载。

'assetManager' => [
            'forceCopy' => YII_DEBUG,
            'bundles' => [
                'yii\web\YiiAsset' => [
                        'js' => ['all.min.js'],
                ],
            ],
        ],

How can I replace all those files by a single one? 如何用一个文件替换所有这些文件?

You should also disable ActiveFormAsset and ValidationAsset : 您还应该禁用ActiveFormAssetValidationAsset

'bundles' => [
    'yii\web\YiiAsset' => [
        'js' => ['all.min.js'],
    ],
    'yii\widgets\ActiveFormAsset' => false,
    'yii\validators\ValidationAsset' => false,
],

Read more : Customizing asset bundles or Combining & compressing assets 了解更多: 自定义资产包组合和压缩资产

From docs : 来自docs

  1. Find all the asset bundles in your application that you plan to combine and compress. 找到您计划合并和压缩的应用程序中的所有资产包。
  2. Divide these bundles into one or a few groups. 将这些捆绑包分成一个或几个组。 Note that each bundle can only belong to a single group. 请注意,每个捆绑包只能属于一个组。
  3. Combine/compress the CSS files in each group into a single file. 将每个组中的CSS文件组合/压缩到一个文件中。 Do this similarly for the JavaScript files. 对JavaScript文件执行类似操作。
  4. Define a new asset bundle for each group: 为每个组定义新的资产包:

    • Set the css and js properties to be the combined CSS and JavaScript files, respectively. 将css和js属性分别设置为组合的CSS和JavaScript文件。

    • Customize the asset bundles in each group by setting their css and js properties to be empty, and setting their depends property to be the new asset bundle created for the group. 通过将css和js属性设置为空来自定义每个组中的资产包,并将其depends属性设置为为该组创建的新资产包。

So you need to use a tool to compress your files then you inject each to its adequate bundle : 因此,您需要使用工具来压缩文件,然后将每个文件注入其足够的包中:

'assetManager' => [
'bundles' => [
    'all' => [
        'class' => 'yii\web\AssetBundle',
        'basePath' => '@webroot/assets',
        'baseUrl' => '@web/assets',
        'css' => ['all-xyz.css'],
        'js' => ['all-xyz.js'],
    ],
    'A' => ['css' => [], 'js' => [], 'depends' => ['all']],
    'B' => ['css' => [], 'js' => [], 'depends' => ['all']],
    'C' => ['css' => [], 'js' => [], 'depends' => ['all']],
    'D' => ['css' => [], 'js' => [], 'depends' => ['all']],
],

See official docs for more info. 有关详细信息,请参阅官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM