Yii 2 JS资产缩小子

Yii 2 JS assets minification prolem

我最近安装了 Yii 2.0.6,我注意到它加载了

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

来自 yiisoft/yii2/assets 文件夹,所以我想覆盖这些文件并在 common/config/main.php 我添加了这些代码行,但它的作用是仅替换 yii.js 文件,但 yii.validation.js 和 yii.activeForm.js 继续加载。

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

如何用一个文件替换所有这些文件?

来自 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. Do this similarly for the JavaScript files.
  4. Define a new asset bundle for each group:

    • Set the css and js properties to be the combined CSS and JavaScript files, respectively.

    • 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.

所以你需要使用一个工具来压缩你的文件,然后你将每个文件注入到它足够的包中:

'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']],
],

有关详细信息,请参阅 official docs

您还应该禁用 ActiveFormAssetValidationAsset :

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

阅读更多:Customizing asset bundles or Combining & compressing assets